diff --git a/Makefile b/Makefile index 829792c..2cae0ff 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ LIB = -lm -lGL -lglfw -lGLEW #glew-2.2.0/lib/libGLEW.a CC = gcc -CFLAGS = -Wall -Werror -Wextra +CFLAGS = -Wall -Werror -Wextra -g3 RED = \033[31m GREEN = \033[32m diff --git a/inc/scop.h b/inc/scop.h index 342f3f2..04b851c 100644 --- a/inc/scop.h +++ b/inc/scop.h @@ -99,6 +99,8 @@ struct s_env { bool texture; bool lighting; float texture_level; + bool invert_texture; + int is_good; }; int parse(t_env *e, int argc, char **argv); diff --git a/src/env.c b/src/env.c index 3e70efe..33bfa13 100644 --- a/src/env.c +++ b/src/env.c @@ -15,6 +15,9 @@ void free_env(t_env *e) { free(e->file_name); + free(e->texture_file_name); + if (e->vbo_data) + free(e->vbo_data); free(e->object.vertices.data); free(e->object.uvs.data); free(e->object.normals.data); @@ -79,6 +82,10 @@ void init_shader_params(t_env *e) { e->texture = false; e->lighting = false; e->rotating = true; + e->is_good = 0; + e->texture_level = 0; + e->vbo_data = NULL; + e->invert_texture = false; } void init_env(t_env *e) diff --git a/src/run.c b/src/run.c index 64aebe8..7905885 100644 --- a/src/run.c +++ b/src/run.c @@ -26,14 +26,13 @@ GLuint gViewLocation; GLuint gModelLocation; GLuint gNormalLocation; GLuint texture_level_location; +GLuint invert_texture_location; GLuint lighting_location; GLuint has_uvs_location; GLuint has_normals_location; //GLuint gCountLocation; GLuint index_count; -int is_good; - GLuint gSamplerLocation; GLuint texture; @@ -95,23 +94,21 @@ static void RenderSceneCB(t_env *e) glUniform1i(has_uvs_location, e->object.uvs.ptr); glUniform1i(has_normals_location, e->object.normals.ptr); glUniform1f(texture_level_location, e->texture_level); + glUniform1i(invert_texture_location, e->invert_texture); glUniform1i(lighting_location, e->lighting); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, VBO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0); - if (is_good) { + if (e->is_good) { glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float))); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(5 * sizeof(float))); - //glBindTexture(GL_TEXTURE_2D, texture); } - //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); - //glDrawElements(GL_TRIANGLES, index_count * 3, GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLES, 0, index_count); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); @@ -127,7 +124,7 @@ static void CreateVertexBuffer(t_env *e) printf("vertices indices len %ld\n", e->object.vertices_indices.ptr); printf("uvs indices len %ld\n", e->object.uvs_indices.ptr); printf("normals indices len %ld\n", e->object.normals_indices.ptr); - is_good = 1; + e->is_good = 1; if (!(e->vbo_data = (float *)malloc(e->object.vertices_indices.ptr * 8 * sizeof(float)))) { perror("malloc"); exit(0); @@ -142,14 +139,14 @@ static void CreateVertexBuffer(t_env *e) e->vbo_data[i + 3] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2]; e->vbo_data[i + 4] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2 + 1]; } else { - is_good = 0; + e->is_good = 0; } if (e->object.normals_indices.ptr == e->object.vertices_indices.ptr && e->object.normals_indices.data[i / 8] * 3 + 2 < e->object.normals.ptr) { e->vbo_data[i + 5] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3]; e->vbo_data[i + 6] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 1]; e->vbo_data[i + 7] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 2]; } else { - is_good = 0; + e->is_good = 0; } } //printf("bruh\n"); @@ -261,6 +258,7 @@ static void CompileShaders(t_env *e) gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture"); has_uvs_location = glGetUniformLocation(e->shader_program, "has_uvs"); has_normals_location = glGetUniformLocation(e->shader_program, "has_normals"); + invert_texture_location = glGetUniformLocation(e->shader_program, "invert_texture"); texture_level_location = glGetUniformLocation(e->shader_program, "texture_level"); lighting_location = glGetUniformLocation(e->shader_program, "lighting"); // assert(gScaleLocation != 0xFFFFFFFF); @@ -286,6 +284,8 @@ void free_shaders(t_env *e) void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { t_env *e = (t_env *)glfwGetWindowUserPointer(window); + if (key == GLFW_KEY_C && (action == GLFW_PRESS)) + e->invert_texture = !e->invert_texture; if (key == GLFW_KEY_N && (action == GLFW_PRESS)) e->rotating = !e->rotating; if (key == GLFW_KEY_B && (action == GLFW_PRESS)) { @@ -330,6 +330,9 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int m e->scale.z *= 1.03; if (key == GLFW_KEY_L && (action == GLFW_PRESS || action == GLFW_REPEAT)) e->scale.z *= 0.97; + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { + glfwSetWindowShouldClose(window, GLFW_TRUE); + } (void)scancode; (void)mods; } @@ -376,16 +379,17 @@ int run(t_env *e) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int tex_width, tex_height, nrChannels; - + nrChannels = -1; unsigned char *tex_data = stbi_load(e->texture_file_name, &tex_width, &tex_height, &nrChannels, 0); - printf("number of channels : %d\n", nrChannels); if (nrChannels == 4) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); } else if (nrChannels == 3) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data); } else { printf("error: sus text file detected, aborting NOW\n"); - exit(0); + stbi_image_free(tex_data); + glfwTerminate(); + return (FAILURE); } glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(tex_data); diff --git a/src/shader.fs b/src/shader.fs index 631d6ef..af9552f 100644 --- a/src/shader.fs +++ b/src/shader.fs @@ -6,6 +6,7 @@ uniform float texture_level; uniform int lighting; uniform int has_uvs; uniform int has_normals; +uniform int invert_texture; out vec4 FragColor; flat in int vID; @@ -24,17 +25,22 @@ void main() vec3 triangle_color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time)))) * (1 - texture_level); vec3 texture_color; if (has_uvs > 0) { - texture_color = texture_level * texture(ourTexture, vec2(texCoord.x, 1.- texCoord.y)).rgb; + if (invert_texture > 0) + texture_color = texture_level * texture(ourTexture, vec2(texCoord.x, 1. - texCoord.y)).rgb; + else + texture_color = texture_level * texture(ourTexture, vec2(texCoord.x, texCoord.y)).rgb; } else { texture_color = texture_level * texture(ourTexture, position.yz).rgb; if (abs(position.x) < 0.077) { texture_color = texture_level * texture(ourTexture, position.xy).rgb; + if (position.y < -0.42 || position.y > 0.385 || (position.y < -0.253 && position.y > -0.254 && position.z < 0) || (position.z > 0.05 && position.y > -0.259 && position.y < -0.258) || (position.z > 0 && position.z < 0.5 && position.y > -0.110 && position.y < -0.100)) + texture_color = texture_level * texture(ourTexture, position.xz).rgb; } } vec3 color = triangle_color + texture_color; if (has_normals > 0 && lighting == 1) { - vec3 n = normalize(vec3(10. * cos(time), 10., 10. * sin(time)) - position); + vec3 n = normalize(vec3(5. * cos(time), 20., 5. * sin(time)) - position); float ambient = 0.2; float phong = max(0., dot(n, normalize(normal))); float specular = pow(max(dot(reflect(n, normalize(normal)), normalize(vec3(0, 0, -2) - position)), 0.), 10.);