chore: finish splitting into functions

This commit is contained in:
Krzysztof Rudnicki 2022-09-06 21:52:06 +02:00
parent 68cd8c3cf7
commit 1860ab5848
10 changed files with 63 additions and 46 deletions

2
.vscode/tasks.json vendored
View File

@ -6,7 +6,7 @@
"command": "/usr/bin/g++",
"args": [
"-g",
"-std=c++17",
"-std=c++2a",
"-I${workspaceFolder}/dependencies/include",
"-L${workspaceFolder}/dependencies/library",
"-Wall",

View File

@ -30,11 +30,11 @@ void instantiateGLFWwindow() {
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
}
GLFWwindow* createWindowObject() {
const GLFWwindow* createWindowObject() {
// First two arguments are width and height
// Third is the name of the window
// We ignore last two
GLFWwindow* window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL);
const GLFWwindow* window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL);
return window;
}

View File

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

View File

@ -38,8 +38,8 @@ int drawSquare()
if(shaderProgram == 0) return -1;
unsigned int VAO = generateBindVAO();
copyVerticesMemory<float>(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER);
copyVerticesMemory<unsigned int>(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_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);
// set vertex attribute pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

BIN
Engine/engine/match Normal file → Executable file

Binary file not shown.

View File

@ -15,7 +15,7 @@
int main()
{
// changed in glfwWindowShouldClose
GLFWwindow* window = prepareForRender();
if(window == NULL) return -1;
renderLoop(window);

View File

@ -21,24 +21,30 @@ bool processInput(GLFWwindow *window, bool whatToDraw)
return whatToDraw;
}
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);
// 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;
// https://stackoverflow.com/a/25680092
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
{
// stores vertices in gpu memory
unsigned int vertexBufferObject;
// this is open gl object so we refer it by its ID generated here and stored in vertexBufferObject variable
glGenBuffers(1, &vertexBufferObject);
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
glBindBuffer(boundBufferTarget, vertexBufferObject);
// now whenever we change GL_ARRAY_BUFFER we change bound buffer vertexBufferObject
/* we copy vertex data into buffer memory
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
GL_STATIC_DRAW: the data is set only once and used many times.
GL_DYNAMIC_DRAW: the data is changed a lot and used many times.
*/
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
return vertexBufferObject;
}
template <typename T>
// https://stackoverflow.com/a/25680092
unsigned int copyVerticesMemory(const T vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
{
// stores vertices in gpu memory
unsigned int vertexBufferObject;

View File

@ -12,7 +12,9 @@ unsigned int generateBindVAO();
void configureVertexAttribute();
bool processInput(GLFWwindow *window, bool whatToDraw);
template <class T> unsigned int copyVerticesMemory(const T vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
#endif

View File

@ -21,7 +21,7 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
// link shaders
glLinkProgram(shaderProgram);
if(!shaderProgramLinkingSuccessful(shaderProgram)) return 0;
if(!shaderSuccessful(shaderProgram, false)) return 0;
// activate program
// after that every shader and rendering call will use this program object
@ -34,6 +34,21 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
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);
// 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;
return shaderID;
}
std::pair<unsigned int, unsigned int> compileShaders()
{
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
@ -52,40 +67,32 @@ std::pair<unsigned int, unsigned int> compileShaders()
return std::make_pair(vertexShader, fragmentShader);
}
int shaderCompilationSuccessful(const unsigned int shader)
void shaderFailedMessage(const unsigned int shader, bool compilation)
{
// 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;
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;
}
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram)
int shaderSuccessful(const unsigned int shader, const bool compilation)
{
// check if compilation was successful
// int because glGetShaderiv requires int
int successfulLinking;
int success;
// here we store info about compilation
char infoLog[512];
// check if compilation was successful
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &successfulLinking);
compilation ?
glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
// if not display compilation log
if(!successfulLinking)
if(!success)
{
glGetProgramInfoLog(shaderProgram, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog);
std::cout << "ERROR shaderProgram compilation failed \n" << infoLog << std::endl;
shaderFailedMessage(shader, compilation);
}
return successfulLinking;
return success;
}
#endif

View File

@ -9,6 +9,8 @@ std::pair<unsigned int, unsigned int> compileShaders();
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);
#endif