mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-26 10:47:20 +01:00
chore: finish splitting into functions
This commit is contained in:
parent
68cd8c3cf7
commit
1860ab5848
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
@ -6,7 +6,7 @@
|
|||||||
"command": "/usr/bin/g++",
|
"command": "/usr/bin/g++",
|
||||||
"args": [
|
"args": [
|
||||||
"-g",
|
"-g",
|
||||||
"-std=c++17",
|
"-std=c++2a",
|
||||||
"-I${workspaceFolder}/dependencies/include",
|
"-I${workspaceFolder}/dependencies/include",
|
||||||
"-L${workspaceFolder}/dependencies/library",
|
"-L${workspaceFolder}/dependencies/library",
|
||||||
"-Wall",
|
"-Wall",
|
||||||
|
|||||||
@ -30,11 +30,11 @@ void instantiateGLFWwindow() {
|
|||||||
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
|
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWwindow* createWindowObject() {
|
const GLFWwindow* createWindowObject() {
|
||||||
// First two arguments are width and height
|
// First two arguments are width and height
|
||||||
// Third is the name of the window
|
// Third is the name of the window
|
||||||
// We ignore last two
|
// 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;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
|
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
|
||||||
void instantiateGLFWwindow();
|
void instantiateGLFWwindow();
|
||||||
GLFWwindow* createWindowObject();
|
const GLFWwindow* createWindowObject();
|
||||||
int initializeGLAD();
|
int initializeGLAD();
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
void viewPort(GLFWwindow* window);
|
void viewPort(GLFWwindow* window);
|
||||||
|
|||||||
@ -38,8 +38,8 @@ int drawSquare()
|
|||||||
if(shaderProgram == 0) return -1;
|
if(shaderProgram == 0) return -1;
|
||||||
|
|
||||||
unsigned int VAO = generateBindVAO();
|
unsigned int VAO = generateBindVAO();
|
||||||
copyVerticesMemory<float>(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER);
|
copyVerticesMemory(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_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);
|
||||||
|
|||||||
BIN
Engine/engine/match
Normal file → Executable file
BIN
Engine/engine/match
Normal file → Executable file
Binary file not shown.
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
// changed in glfwWindowShouldClose
|
||||||
GLFWwindow* window = prepareForRender();
|
GLFWwindow* window = prepareForRender();
|
||||||
if(window == NULL) return -1;
|
if(window == NULL) return -1;
|
||||||
renderLoop(window);
|
renderLoop(window);
|
||||||
|
|||||||
@ -21,24 +21,30 @@ bool processInput(GLFWwindow *window, bool whatToDraw)
|
|||||||
return 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);
|
// https://stackoverflow.com/a/25680092
|
||||||
// compile shader
|
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
|
||||||
glCompileShader(shaderID);
|
{
|
||||||
if(!shaderCompilationSuccessful(shaderID)) return 0;
|
// stores vertices in gpu memory
|
||||||
return shaderID;
|
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>
|
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
|
||||||
// https://stackoverflow.com/a/25680092
|
|
||||||
unsigned int copyVerticesMemory(const T vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
|
|
||||||
{
|
{
|
||||||
// stores vertices in gpu memory
|
// stores vertices in gpu memory
|
||||||
unsigned int vertexBufferObject;
|
unsigned int vertexBufferObject;
|
||||||
|
|||||||
@ -12,7 +12,9 @@ unsigned int generateBindVAO();
|
|||||||
void configureVertexAttribute();
|
void configureVertexAttribute();
|
||||||
bool processInput(GLFWwindow *window, bool whatToDraw);
|
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
|
#endif
|
||||||
@ -21,7 +21,7 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
|
|||||||
|
|
||||||
// link shaders
|
// link shaders
|
||||||
glLinkProgram(shaderProgram);
|
glLinkProgram(shaderProgram);
|
||||||
if(!shaderProgramLinkingSuccessful(shaderProgram)) 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
|
||||||
@ -34,6 +34,21 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned
|
|||||||
return shaderProgram;
|
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()
|
std::pair<unsigned int, unsigned int> compileShaders()
|
||||||
{
|
{
|
||||||
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
|
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);
|
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];
|
char infoLog[512];
|
||||||
// check if compilation was successful
|
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &successfulCompilation);
|
compilation ?
|
||||||
// if not display compilation log
|
glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
|
||||||
if(!successfulCompilation)
|
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
|
||||||
{
|
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl;
|
||||||
glGetShaderInfoLog(shader, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog);
|
|
||||||
std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl;
|
|
||||||
}
|
|
||||||
return successfulCompilation;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram)
|
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 successfulLinking;
|
int success;
|
||||||
// here we store info about compilation
|
// here we store info about compilation
|
||||||
char infoLog[512];
|
|
||||||
// check if compilation was successful
|
// 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 not display compilation log
|
||||||
if(!successfulLinking)
|
if(!success)
|
||||||
{
|
{
|
||||||
glGetProgramInfoLog(shaderProgram, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog);
|
shaderFailedMessage(shader, compilation);
|
||||||
std::cout << "ERROR shaderProgram compilation failed \n" << infoLog << std::endl;
|
|
||||||
}
|
}
|
||||||
return successfulLinking;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -9,6 +9,8 @@ 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);
|
||||||
|
void shaderFailedMessage(const unsigned int shader, bool compilation);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user