mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-23 18:26:27 +01:00
feat: create attach link shaders
This commit is contained in:
parent
7934f833ec
commit
69f4e802a1
@ -30,6 +30,27 @@ namespace constants
|
|||||||
GLfloat alpha = 1.0f;
|
GLfloat alpha = 1.0f;
|
||||||
} LEARN_OPEN_GL_COLOR;
|
} LEARN_OPEN_GL_COLOR;
|
||||||
|
|
||||||
|
// we write vertex shader
|
||||||
|
// version of glsl (since ogl 3.3 same as ogl so we pick 330)
|
||||||
|
// in this shader we just forward input data to shader output
|
||||||
|
inline const char *vertexShaderSource { "#version 330 core\n"
|
||||||
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||||
|
"}\0" } ;
|
||||||
|
|
||||||
|
// write fragment shader
|
||||||
|
// we set the color of each pixel to be orange
|
||||||
|
inline const char *fragmentShaderSource {
|
||||||
|
"#version 330 core\n"
|
||||||
|
"out vec4 FragColor;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
||||||
|
"}\0"
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Binary file not shown.
@ -81,6 +81,60 @@ void processInput(GLFWwindow *window)
|
|||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram)
|
||||||
|
{
|
||||||
|
// check if compilation was successful
|
||||||
|
// int because glGetShaderiv requires int
|
||||||
|
int successfulLinking;
|
||||||
|
// here we store info about compilation
|
||||||
|
char infoLog[512];
|
||||||
|
// check if compilation was successful
|
||||||
|
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &successfulLinking);
|
||||||
|
// if not display compilation log
|
||||||
|
if(!successfulLinking)
|
||||||
|
{
|
||||||
|
glGetProgramInfoLog(shaderProgram, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog);
|
||||||
|
std::cout << "ERROR shaderProgram compilation failed \n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
return successfulLinking;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int shaderCompilationSuccessful(const unsigned int shader)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// check if compilation was successful
|
||||||
|
// int because glGetShaderiv requires int
|
||||||
|
int successfulCompilation;
|
||||||
|
// here we store info about compilation
|
||||||
|
char infoLog[512];
|
||||||
|
// check if compilation was successful
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &successfulCompilation);
|
||||||
|
// if not display compilation log
|
||||||
|
if(!successfulCompilation)
|
||||||
|
{
|
||||||
|
glGetShaderInfoLog(shader, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog);
|
||||||
|
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
return successfulCompilation;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int compileShader(const GLenum shaderType, const char * shaderSource)
|
||||||
|
{
|
||||||
|
// we create vertex shader and assign its id to shader variable
|
||||||
|
unsigned int shaderID;
|
||||||
|
shaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
// attach shader source code to shader object
|
||||||
|
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as NULL)
|
||||||
|
glShaderSource(shaderID, 1, &shaderSource, NULL);
|
||||||
|
// compile shader
|
||||||
|
glCompileShader(shaderID);
|
||||||
|
if(!shaderCompilationSuccessful(shaderID)) return 0;
|
||||||
|
return shaderID;
|
||||||
|
}
|
||||||
|
|
||||||
void drawTriangle()
|
void drawTriangle()
|
||||||
{
|
{
|
||||||
// we specify three vertices
|
// we specify three vertices
|
||||||
@ -108,18 +162,38 @@ void drawTriangle()
|
|||||||
*/
|
*/
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// we write vertex shader
|
|
||||||
// version of glsl (since ogl 3.3 same as ogl so we pick 330)
|
|
||||||
// in this shader we just forward input data to shader output
|
|
||||||
const char *vertexShaderSource = "#version 330 core\n"
|
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
|
||||||
"}\0";
|
|
||||||
|
|
||||||
|
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
|
||||||
|
if(vertexShader == 0) return;
|
||||||
|
|
||||||
|
unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
|
||||||
|
if(fragmentShader == 0) return;
|
||||||
|
|
||||||
|
// link shader objects into shader program
|
||||||
|
// will store shader program id
|
||||||
|
unsigned int shaderProgram;
|
||||||
|
// creates program
|
||||||
|
shaderProgram = glCreateProgram();
|
||||||
|
|
||||||
|
// attachShaders
|
||||||
|
glAttachShader(shaderProgram, vertexShader);
|
||||||
|
glAttachShader(shaderProgram, fragmentShader);
|
||||||
|
|
||||||
|
// link shaders
|
||||||
|
glLinkProgram(shaderProgram);
|
||||||
|
if(!shaderProgramLinkingSuccessful(shaderProgram)) return;
|
||||||
|
|
||||||
|
// activate program
|
||||||
|
// after that every shader and rendering call will use this program object
|
||||||
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
|
// delete shaders (they are linked into shaderProgram and we do not need them anymore)
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void renderLoop(GLFWwindow* window)
|
void renderLoop(GLFWwindow* window)
|
||||||
{
|
{
|
||||||
// glfwWindowShouldClose checks if GLFW was instructed to close
|
// glfwWindowShouldClose checks if GLFW was instructed to close
|
||||||
@ -141,6 +215,7 @@ void renderLoop(GLFWwindow* window)
|
|||||||
|
|
||||||
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
|
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
drawTriangle();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user