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

@ -18,7 +18,8 @@ int drawFigure(const bool whatToDraw)
print("Error with drawing triangle! ");
return -1;
}
}else
}
else
{
if (drawSquare() == -1)
{
@ -31,13 +32,15 @@ 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);
@ -59,16 +62,18 @@ 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

Binary file not shown.

View File

@ -9,7 +9,7 @@
#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
@ -17,13 +17,11 @@ bool processInput(GLFWwindow *window, bool whatToDraw)
glfwSetWindowShouldClose(window, true);
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)
{

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,35 +30,36 @@ 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)
{
// 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);
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);
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
if (fragmentShader == 0)
{
print("Fragment Shader Compilation Failed");
@ -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)
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl;
std::cout << "ERROR vertex shader compilation failed \n"
<< infoLog << std::endl;
}
int shaderSuccessful(const unsigned int shader, const bool compilation)
@ -84,8 +85,7 @@ 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)
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
// if not display compilation log
if (!success)

View File

@ -10,7 +10,6 @@ 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