Move texture creation to Renderer.c
This commit is contained in:
@@ -22,6 +22,13 @@ GLFWwindow* initOpenGL() {
|
|||||||
exit(-1);
|
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;
|
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* vertex_file = fopen(vertexPath, "r");
|
||||||
FILE* fragment_file = fopen(fragmentPath, "r");
|
FILE* fragment_file = fopen(fragmentPath, "r");
|
||||||
if( vertex_file == NULL || fragment_file == NULL ) {
|
if( vertex_file == NULL || fragment_file == NULL ) {
|
||||||
@@ -115,3 +122,32 @@ unsigned int loadCompileShader(char* vertexPath, char* fragmentPath) {
|
|||||||
|
|
||||||
return shaderProgram;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
GLFWwindow* initOpenGL();
|
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 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
|
#endif
|
||||||
|
|||||||
42
src/main.c
42
src/main.c
@@ -3,7 +3,6 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "stb_image.h"
|
|
||||||
#include <linmath.h>
|
#include <linmath.h>
|
||||||
|
|
||||||
#include "Renderer.h"
|
#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.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
|
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* vertex1 = "../assets/shaders/vertex3.glsl\0";
|
||||||
char* fragment1 = "../assets/shaders/fragment3.glsl\0";
|
char* fragment1 = "../assets/shaders/fragment3.glsl\0";
|
||||||
unsigned int shader1;
|
unsigned int shader1;
|
||||||
shader1 = loadCompileShader(vertex1, fragment1);
|
shader1 = createShader(vertex1, fragment1);
|
||||||
|
|
||||||
// Initialise VBO and VAO
|
// Initialise VBO and VAO
|
||||||
unsigned int VAO, VBO;
|
unsigned int VAO, VBO;
|
||||||
@@ -45,41 +45,9 @@ int main() {
|
|||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
// Set texture parametes
|
// Create Texture
|
||||||
|
|
||||||
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
|
|
||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
glGenTextures(1, &texture);
|
texture = createTexture("../assets/textures/wall.jpg");
|
||||||
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);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user