Nove repo opengl-learning
This commit is contained in:
34
lessons/1. Getting started/4. Shaders/Makefile
Normal file
34
lessons/1. Getting started/4. Shaders/Makefile
Normal file
@@ -0,0 +1,34 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
87
lessons/1. Getting started/4. Shaders/src/Shader.cpp
Normal file
87
lessons/1. Getting started/4. Shaders/src/Shader.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
20
lessons/1. Getting started/4. Shaders/src/Shader.h
Normal file
20
lessons/1. Getting started/4. Shaders/src/Shader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
107
lessons/1. Getting started/4. Shaders/src/main.cpp
Normal file
107
lessons/1. Getting started/4. Shaders/src/main.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include "Shader.h"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("./../src/shaders/vertexShader.vert", "./../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
myShader.Use();
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec4 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = outColor;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.xyz, 1.0);
|
||||
outColor = vec4(aColor, 1.0f);
|
||||
}
|
||||
Reference in New Issue
Block a user