From cc2801dcc8105b22cf67c288aeedaf10b7043470 Mon Sep 17 00:00:00 2001 From: MrFloor Date: Sun, 30 Jan 2022 11:36:44 +0100 Subject: [PATCH] Move texture creation to Renderer.c --- src/Renderer.c | 38 +++++++++++++++++++++++++++++++++++++- src/Renderer.h | 5 ++++- src/main.c | 42 +++++------------------------------------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/Renderer.c b/src/Renderer.c index f0a5f64..9eed8ce 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -22,6 +22,13 @@ GLFWwindow* initOpenGL() { exit(-1); } + + // Set Global Texture Parameters + float borderColor[] = {1.0f, 1.0f, 0.0f, 1.0f}; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + return window; } @@ -76,7 +83,7 @@ unsigned int compileShaderProgram(const char** vertexSrc, const char** fragmentS } -unsigned int loadCompileShader(char* vertexPath, char* fragmentPath) { +unsigned int createShader(char* vertexPath, char* fragmentPath) { FILE* vertex_file = fopen(vertexPath, "r"); FILE* fragment_file = fopen(fragmentPath, "r"); if( vertex_file == NULL || fragment_file == NULL ) { @@ -115,3 +122,32 @@ unsigned int loadCompileShader(char* vertexPath, char* fragmentPath) { return shaderProgram; } + +unsigned int createTexture(char* path){ + // Generate Texture + unsigned int texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + // Set Texure Parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Load texture image + int width, height, channels; + unsigned char *data = stbi_load(path, &width, &height, &channels, 0); + + if (data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + printf("Failed to load texture %s", path); + } + stbi_image_free(data); + + return texture; +} + + diff --git a/src/Renderer.h b/src/Renderer.h index da85d8e..2c512e2 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -8,6 +8,7 @@ #include #include #include +#include GLFWwindow* initOpenGL(); @@ -15,6 +16,8 @@ void WindowGotResized(GLFWwindow* window, int width, int height); unsigned int compileShaderProgram(const char** vertexSrc, const char** fragmentSrc); -unsigned int loadCompileShader(char* vertexPath, char* fragmentPath); +unsigned int createShader(char* vertexPath, char* fragmentPath); + +unsigned int createTexture(char* path); #endif diff --git a/src/main.c b/src/main.c index d2d3a2b..cdbc367 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,6 @@ #include #include -#include "stb_image.h" #include #include "Renderer.h" @@ -22,11 +21,12 @@ int main() { 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 1.0f }; - // Compile Shader + + // CreateShader char* vertex1 = "../assets/shaders/vertex3.glsl\0"; char* fragment1 = "../assets/shaders/fragment3.glsl\0"; unsigned int shader1; - shader1 = loadCompileShader(vertex1, fragment1); + shader1 = createShader(vertex1, fragment1); // Initialise VBO and VAO unsigned int VAO, VBO; @@ -45,41 +45,9 @@ int main() { glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); - // Set texture parametes - - float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f }; - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - - - // Generate Texture + // Create Texture unsigned int texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - - //Set Texture Parametes - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - // Load Texture image - int width, height, channels; - unsigned char *data = stbi_load("../assets/textures/wall.jpg", &width, &height, &channels, 0); - - if(data) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } else { - printf("failed to load texture\n"); - } - - stbi_image_free(data); - - + texture = createTexture("../assets/textures/wall.jpg"); while(!glfwWindowShouldClose(window)) {