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
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);
}

View File

@ -6,7 +6,7 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
void instantiateGLFWwindow();
GLFWwindow *createWindowObject();
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);
GLFWwindow *prepareForRender();

View File

@ -11,16 +11,17 @@
int drawFigure(const bool whatToDraw)
{
if(whatToDraw)
if (whatToDraw)
{
if(drawTriangle() == -1)
if (drawTriangle() == -1)
{
print("Error with drawing triangle! ");
return -1;
}
}else
}
else
{
if(drawSquare() == -1)
if (drawSquare() == -1)
{
print("Error with drawing square! ");
return -1;
@ -31,18 +32,20 @@ int drawFigure(const bool whatToDraw)
int drawSquare()
{
std::pair<unsigned int, unsigned int> shaders = compileShaders();
if(shaders.first == 0 || shaders.second == 0) return -1;
const 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);
if(shaderProgram == 0) return -1;
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
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_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER);
// 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);
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES));
@ -59,26 +62,28 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
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;
const 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);
if(shaderProgram == 0) return -1;
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if (shaderProgram == 0)
return -1;
configureVertexAttribute();
unsigned int vertexArrayObject = generateBindVAO();
const unsigned int vertexArrayObject = generateBindVAO();
copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER);
// 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);
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3);
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
glUseProgram(shaderProgram);

Binary file not shown.

View File

@ -9,21 +9,19 @@
#include "constants.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
// 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);
if(glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
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;
}
// https://stackoverflow.com/a/25680092
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
// 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
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
glEnableVertexAttribArray(0);
}
@ -98,37 +96,37 @@ void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[],
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
}
bool renderLoopInside(GLFWwindow* window, bool whatToDraw)
bool renderLoopInside(GLFWwindow *window, bool whatToDraw)
{
// input
whatToDraw = processInput(window, whatToDraw);
// We specify the color to clear the screen with
// 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);
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
glClear(GL_COLOR_BUFFER_BIT);
// input
whatToDraw = processInput(window, whatToDraw);
// We specify the color to clear the screen with
// 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);
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
glClear(GL_COLOR_BUFFER_BIT);
if(drawFigure(whatToDraw) == -1)
{
print("error with drawing!");
glfwSetWindowShouldClose(window, true);
};
if (drawFigure(whatToDraw) == -1)
{
print("error with drawing!");
glfwSetWindowShouldClose(window, true);
};
// swaps buffer containing color values of each pixel in window
// 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
glfwSwapBuffers(window);
// swaps buffer containing color values of each pixel in window
// 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
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;
// 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;
// glfwWindowShouldClose checks if GLFW was instructed to close
while(!glfwWindowShouldClose(window))
while (!glfwWindowShouldClose(window))
{
whatToDraw = renderLoopInside(window, whatToDraw);
}

View File

@ -6,7 +6,6 @@
#include "constants.hpp"
#include "misc.hpp"
unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader)
{
// link shader objects into shader program
@ -21,7 +20,8 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
// link shaders
glLinkProgram(shaderProgram);
if(!shaderSuccessful(shaderProgram, false)) return 0;
if (!shaderSuccessful(shaderProgram, false))
return 0;
// activate program
// after that every shader and rendering call will use this program object
@ -30,36 +30,37 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
// delete shaders (they are linked into shaderProgram and we do not need them anymore)
glDeleteShader(vertexShaders);
glDeleteShader(fragmentShader);
if(shaderProgram == 0) print("Shader Program Linking Failed");
if (shaderProgram == 0)
print("Shader Program Linking Failed");
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
unsigned int shaderID;
shaderID = glCreateShader(shaderType);
const unsigned int shaderID = glCreateShader(shaderType);
// 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(!shaderSuccessful(shaderID, true)) return 0;
if (!shaderSuccessful(shaderID, true))
return 0;
return shaderID;
}
std::pair<unsigned int, unsigned int> compileShaders()
{
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
if(vertexShader == 0)
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
if (vertexShader == 0)
{
print("Vertex Shader Compilation Failed");
return std::make_pair(0, 0);
}
unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
if(fragmentShader == 0)
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
if (fragmentShader == 0)
{
print("Fragment Shader Compilation Failed");
return std::make_pair(0, 0);
@ -67,14 +68,14 @@ std::pair<unsigned int, unsigned int> compileShaders()
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];
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
compilation ?
glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl;
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
std::cout << "ERROR vertex shader compilation failed \n"
<< infoLog << std::endl;
}
int shaderSuccessful(const unsigned int shader, const bool compilation)
@ -84,11 +85,10 @@ int shaderSuccessful(const unsigned int shader, const bool compilation)
int success;
// here we store info about compilation
// check if compilation was successful
compilation ?
glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
// if not display compilation log
if(!success)
if (!success)
{
shaderFailedMessage(shader, compilation);
}

View File

@ -6,11 +6,10 @@
unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader);
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 shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
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