chore: add const

This commit is contained in:
Krzysztof Rudnicki 2022-09-07 18:54:59 +02:00
parent 0f293adb96
commit f8854f4bfa
7 changed files with 91 additions and 89 deletions

View File

@ -53,7 +53,7 @@ int initializeGLAD()
} }
// resizes viewport when user resizes window // resizes viewport when user resizes window
void framebuffer_size_callback(GLFWwindow *window, int width, int height) void framebuffer_size_callback(GLFWwindow *window, const int width, const int height)
{ {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }

View File

@ -6,7 +6,7 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
void instantiateGLFWwindow(); void instantiateGLFWwindow();
GLFWwindow *createWindowObject(); GLFWwindow *createWindowObject();
int initializeGLAD(); int initializeGLAD();
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, const int width, const int height);
void viewPort(GLFWwindow *window); void viewPort(GLFWwindow *window);
GLFWwindow *prepareForRender(); GLFWwindow *prepareForRender();

View File

@ -11,16 +11,17 @@
int drawFigure(const bool whatToDraw) int drawFigure(const bool whatToDraw)
{ {
if(whatToDraw) if (whatToDraw)
{ {
if(drawTriangle() == -1) if (drawTriangle() == -1)
{ {
print("Error with drawing triangle! "); print("Error with drawing triangle! ");
return -1; return -1;
} }
}else }
else
{ {
if(drawSquare() == -1) if (drawSquare() == -1)
{ {
print("Error with drawing square! "); print("Error with drawing square! ");
return -1; return -1;
@ -31,18 +32,20 @@ int drawFigure(const bool whatToDraw)
int drawSquare() int drawSquare()
{ {
std::pair<unsigned int, unsigned int> shaders = compileShaders(); const std::pair<unsigned int, unsigned int> shaders = compileShaders();
if(shaders.first == 0 || shaders.second == 0) return -1; if (shaders.first == 0 || shaders.second == 0)
return -1;
unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if(shaderProgram == 0) return -1; if (shaderProgram == 0)
return -1;
unsigned int VAO = generateBindVAO(); const unsigned int VAO = generateBindVAO();
copyVerticesMemory(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER); copyVerticesMemory(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER);
copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER); copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES)); doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES));
@ -55,39 +58,41 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }
int drawTriangle() int drawTriangle()
{ {
unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); const unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER);
std::pair<unsigned int, unsigned int> shaders = compileShaders();
if(shaders.first == 0 || shaders.second == 0) return -1;
unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); const std::pair<unsigned int, unsigned int> shaders = compileShaders();
if(shaderProgram == 0) return -1; if (shaders.first == 0 || shaders.second == 0)
return -1;
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if (shaderProgram == 0)
return -1;
configureVertexAttribute(); configureVertexAttribute();
unsigned int vertexArrayObject = generateBindVAO(); const unsigned int vertexArrayObject = generateBindVAO();
copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3); doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3);
return 0; return 0;
} }
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered ) void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered)
{ {
// use shader program to render an object // use shader program to render an object
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
// From left: // From left:
// primitive type we want to draw // primitive type we want to draw
// starting index of vertex array // starting index of vertex array
// how many vertices we want to draw // how many vertices we want to draw
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered); glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
} }
#endif #endif

Binary file not shown.

View File

@ -9,21 +9,19 @@
#include "constants.hpp" #include "constants.hpp"
#include "misc.hpp" #include "misc.hpp"
bool processInput(GLFWwindow *window, bool whatToDraw) bool processInput(GLFWwindow *window, const bool whatToDraw)
{ {
// glfwGetKey takes window and key as an input and checks is currently being pressed // glfwGetKey takes window and key as an input and checks is currently being pressed
// if the user pressed escape we close window // if the user pressed escape we close window
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if(glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS ) return !whatToDraw; if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
return !whatToDraw;
return whatToDraw; return whatToDraw;
} }
// https://stackoverflow.com/a/25680092 // https://stackoverflow.com/a/25680092
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget) unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
{ {
@ -75,7 +73,7 @@ void configureVertexAttribute()
// offset of where position data begins in buffer // offset of where position data begins in buffer
// see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png // see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png
// vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER // vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
} }
@ -86,7 +84,7 @@ unsigned int generateBindVAO()
// generate vao // generate vao
unsigned int vertexArrayObject; unsigned int vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject); glGenVertexArrays(1, &vertexArrayObject);
// bind vao // bind vao
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
return vertexArrayObject; return vertexArrayObject;
} }
@ -98,40 +96,40 @@ void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[],
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
} }
bool renderLoopInside(GLFWwindow* window, bool whatToDraw) bool renderLoopInside(GLFWwindow *window, bool whatToDraw)
{ {
// input // input
whatToDraw = processInput(window, whatToDraw); whatToDraw = processInput(window, whatToDraw);
// We specify the color to clear the screen with // We specify the color to clear the screen with
// RGB and alpha value // RGB and alpha value
glClearColor( constants::LEARN_OPEN_GL_COLOR.red, constants::LEARN_OPEN_GL_COLOR.green, constants::LEARN_OPEN_GL_COLOR.blue, constants::LEARN_OPEN_GL_COLOR.alpha); glClearColor(constants::LEARN_OPEN_GL_COLOR.red, constants::LEARN_OPEN_GL_COLOR.green, constants::LEARN_OPEN_GL_COLOR.blue, constants::LEARN_OPEN_GL_COLOR.alpha);
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT // There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
if(drawFigure(whatToDraw) == -1)
{
print("error with drawing!");
glfwSetWindowShouldClose(window, true);
};
// swaps buffer containing color values of each pixel in window if (drawFigure(whatToDraw) == -1)
// there is front buffer (final image) and back buffer (where all rendering commands draw to) {
// when back buffer is ready we swap it with front buffer to eliminate flickering print("error with drawing!");
glfwSwapBuffers(window); glfwSetWindowShouldClose(window, true);
};
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods) // swaps buffer containing color values of each pixel in window
glfwPollEvents(); // there is front buffer (final image) and back buffer (where all rendering commands draw to)
return whatToDraw; // when back buffer is ready we swap it with front buffer to eliminate flickering
glfwSwapBuffers(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();
return whatToDraw;
} }
void renderLoop(GLFWwindow* window) void renderLoop(GLFWwindow *window)
{ {
bool whatToDraw = true; bool whatToDraw = true;
// glfwWindowShouldClose checks if GLFW was instructed to close // glfwWindowShouldClose checks if GLFW was instructed to close
while(!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
whatToDraw = renderLoopInside(window, whatToDraw); whatToDraw = renderLoopInside(window, whatToDraw);
} }
} }
#endif #endif

View File

@ -6,13 +6,12 @@
#include "constants.hpp" #include "constants.hpp"
#include "misc.hpp" #include "misc.hpp"
unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader) unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader)
{ {
// link shader objects into shader program // link shader objects into shader program
// will store shader program id // will store shader program id
unsigned int shaderProgram; unsigned int shaderProgram;
// creates program // creates program
shaderProgram = glCreateProgram(); shaderProgram = glCreateProgram();
// attachShaders // attachShaders
@ -21,45 +20,47 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
// link shaders // link shaders
glLinkProgram(shaderProgram); glLinkProgram(shaderProgram);
if(!shaderSuccessful(shaderProgram, false)) return 0; if (!shaderSuccessful(shaderProgram, false))
return 0;
// activate program // activate program
// after that every shader and rendering call will use this program object // after that every shader and rendering call will use this program object
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
// delete shaders (they are linked into shaderProgram and we do not need them anymore) // delete shaders (they are linked into shaderProgram and we do not need them anymore)
glDeleteShader(vertexShaders); glDeleteShader(vertexShaders);
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
if(shaderProgram == 0) print("Shader Program Linking Failed"); if (shaderProgram == 0)
print("Shader Program Linking Failed");
return shaderProgram; return shaderProgram;
} }
unsigned int compileShader(const GLenum shaderType, const char * shaderSource) unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
{ {
// we create vertex shader and assign its id to shader variable // we create vertex shader and assign its id to shader variable
unsigned int shaderID; const unsigned int shaderID = glCreateShader(shaderType);
shaderID = glCreateShader(shaderType);
// attach shader source code to shader object // 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) // 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); glShaderSource(shaderID, 1, &shaderSource, NULL);
// compile shader // compile shader
glCompileShader(shaderID); glCompileShader(shaderID);
if(!shaderSuccessful(shaderID, true)) return 0; if (!shaderSuccessful(shaderID, true))
return 0;
return shaderID; return shaderID;
} }
std::pair<unsigned int, unsigned int> compileShaders() std::pair<unsigned int, unsigned int> compileShaders()
{ {
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
if(vertexShader == 0) if (vertexShader == 0)
{ {
print("Vertex Shader Compilation Failed"); print("Vertex Shader Compilation Failed");
return std::make_pair(0, 0); return std::make_pair(0, 0);
} }
unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource); const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
if(fragmentShader == 0) if (fragmentShader == 0)
{ {
print("Fragment Shader Compilation Failed"); print("Fragment Shader Compilation Failed");
return std::make_pair(0, 0); return std::make_pair(0, 0);
@ -67,28 +68,27 @@ std::pair<unsigned int, unsigned int> compileShaders()
return std::make_pair(vertexShader, fragmentShader); return std::make_pair(vertexShader, fragmentShader);
} }
void shaderFailedMessage(const unsigned int shader, bool compilation) void shaderFailedMessage(const unsigned int shader, const bool compilation)
{ {
char infoLog[512]; char infoLog[512];
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]); const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
compilation ? compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog) : glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog); std::cout << "ERROR vertex shader compilation failed \n"
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl; << infoLog << std::endl;
} }
int shaderSuccessful(const unsigned int shader, const bool compilation) int shaderSuccessful(const unsigned int shader, const bool compilation)
{ {
// check if compilation was successful // check if compilation was successful
// int because glGetShaderiv requires int // int because glGetShaderiv requires int
int success; int success;
// here we store info about compilation // here we store info about compilation
// check if compilation was successful // check if compilation was successful
compilation ? compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
glGetShaderiv(shader, GL_COMPILE_STATUS, &success) : glGetProgramiv(shader, GL_LINK_STATUS, &success);
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
// if not display compilation log // if not display compilation log
if(!success) if (!success)
{ {
shaderFailedMessage(shader, compilation); shaderFailedMessage(shader, compilation);
} }

View File

@ -6,11 +6,10 @@
unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader); unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader);
std::pair<unsigned int, unsigned int> compileShaders(); std::pair<unsigned int, unsigned int> compileShaders();
unsigned int compileShader(const GLenum shaderType, const char * shaderSource); unsigned int compileShader(const GLenum shaderType, const char *shaderSource);
int shaderCompilationSuccessful(const unsigned int shader); int shaderCompilationSuccessful(const unsigned int shader);
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram); int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
int shaderSuccessful(const unsigned int shader, const bool compilation); int shaderSuccessful(const unsigned int shader, const bool compilation);
void shaderFailedMessage(const unsigned int shader, bool compilation); void shaderFailedMessage(const unsigned int shader, const bool compilation);
#endif #endif