diff --git a/inc/scop.h b/inc/scop.h index bdc8710..ad3cae0 100644 --- a/inc/scop.h +++ b/inc/scop.h @@ -78,6 +78,7 @@ struct s_env { t_cam camera; t_window window; t_obj object; + double *vbo_data; GLuint shader_program; GLuint vertex_shader; GLuint fragment_shader; diff --git a/resources/racer.jpg b/resources/racer.jpg new file mode 100644 index 0000000..27759b8 Binary files /dev/null and b/resources/racer.jpg differ diff --git a/src/run.c b/src/run.c index 3f71b7a..6be6145 100644 --- a/src/run.c +++ b/src/run.c @@ -18,13 +18,14 @@ #include "stb_image.h" GLuint VBO; -GLuint IBO; +//GLuint IBO; GLuint gScaleLocation; -GLuint gScaleIntLocation; +GLuint gTimeLocation; GLuint gProjLocation; GLuint gViewLocation; GLuint gModelLocation; -GLuint gCountLocation; +GLuint has_texture_location; +//GLuint gCountLocation; GLuint index_count; GLuint gSamplerLocation; @@ -38,9 +39,9 @@ static void RenderSceneCB(t_env *e) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); - static float Scale = 0.0f; + static float time = 0.0f; - Scale += 0.01f; + time += 0.01f; t_vec3 eye; t_vec3 up; @@ -63,25 +64,28 @@ static void RenderSceneCB(t_env *e) t_mat4 model = mat4_scale( mat4_rotatexyz( mat4_translate(mat4_identity(), e->translate), e->rotate), e->scale); - glUniform1f(gScaleIntLocation, Scale); + glUniform1f(gTimeLocation, time); glUniformMatrix4fv(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data); glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data); glUniformMatrix4fv(gModelLocation, 1, GL_FALSE, (GLfloat *)model.data); - glUniform1i(gCountLocation, index_count); + //glUniform1i(gCountLocation, index_count); glActiveTexture(GL_TEXTURE0); glUniform1i(gSamplerLocation, 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0); + glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), 0); - //layout text - //glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, 0, 0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), (void *)(3 * sizeof(double))); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), (void *)(5 * sizeof(double))); //glBindTexture(GL_TEXTURE_2D, texture); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); + //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); - glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0); + //glDrawElements(GL_TRIANGLES, index_count * 3, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, index_count); glDisableVertexAttribArray(0); } @@ -95,12 +99,30 @@ 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); + + e->vbo_data = (double *)malloc(e->object.vertices_indices.ptr * 8 * sizeof(double)); + for (size_t i = 0; i < e->object.vertices_indices.ptr * 8; i += 8) { + e->vbo_data[i] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3] * 0.3; + e->vbo_data[i + 1] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 1] * 0.3; + e->vbo_data[i + 2] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 2] * 0.3; + 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]; + 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]; + } + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, e->object.vertices_indices.ptr * 8 * sizeof(double), e->vbo_data, GL_STATIC_DRAW); + printf("Buffer created with success !\n"); +/* glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, e->object.vertices.ptr * sizeof(double), e->object.vertices.data, GL_STATIC_DRAW); glGenBuffers(1, &IBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->object.vertices_indices.ptr * sizeof(int), e->object.vertices_indices.data, GL_STATIC_DRAW); + */ } static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType, GLuint *shader) @@ -190,13 +212,13 @@ static void CompileShaders(t_env *e) glUseProgram(e->shader_program); // gScaleLocation = glGetUniformLocation(e->shader_program, "gScale"); - gScaleIntLocation = glGetUniformLocation(e->shader_program, "time"); + gTimeLocation = glGetUniformLocation(e->shader_program, "time"); gProjLocation = glGetUniformLocation(e->shader_program, "proj"); gViewLocation = glGetUniformLocation(e->shader_program, "view"); gModelLocation = glGetUniformLocation(e->shader_program, "model"); gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture"); // assert(gScaleLocation != 0xFFFFFFFF); - assert(gScaleIntLocation != 0xFFFFFFFF); + assert(gTimeLocation != 0xFFFFFFFF); assert(gProjLocation != 0xFFFFFFFF); assert(gViewLocation != 0xFFFFFFFF); assert(gModelLocation != 0xFFFFFFFF); @@ -243,17 +265,17 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int m if (key == GLFW_KEY_H && (action == GLFW_PRESS || action == GLFW_REPEAT)) e->rotate.z -= 0.03; if (key == GLFW_KEY_U && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.x += 0.03; + e->scale.x *= 1.03; if (key == GLFW_KEY_J && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.x -= 0.03; + e->scale.x *= 0.97; if (key == GLFW_KEY_I && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.y += 0.03; + e->scale.y *= 1.03; if (key == GLFW_KEY_K && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.y -= 0.03; + e->scale.y *= 0.97; if (key == GLFW_KEY_O && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.z += 0.03; + e->scale.z *= 1.03; if (key == GLFW_KEY_L && (action == GLFW_PRESS || action == GLFW_REPEAT)) - e->scale.z -= 0.03; + e->scale.z *= 0.97; (void)scancode; (void)mods; } @@ -300,7 +322,7 @@ int run(t_env *e) int tex_width, tex_height, nrChannels; - unsigned char *tex_data = stbi_load("resources/fox.jpg", &tex_width, &tex_height, &nrChannels, 0); + unsigned char *tex_data = stbi_load("resources/racer.jpg", &tex_width, &tex_height, &nrChannels, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data); glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(tex_data); diff --git a/src/shader.fs b/src/shader.fs index cb20089..87a0c97 100644 --- a/src/shader.fs +++ b/src/shader.fs @@ -5,6 +5,9 @@ uniform sampler2D ourTexture; out vec4 FragColor; flat in int vID; +in vec2 texCoord; +in vec3 position; +in vec3 normal; float rand(float n) @@ -15,8 +18,11 @@ float rand(float n) void main() { vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time)))); - FragColor = vec4(color, 1.); - //FragColor = vec4(color * 0.001 + texture(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0); - //FragColor = vec4(color * 0.1 + texture(ourTexture, gl_FragCoord.xy).rgb, 1.0); - //FragColor = vec4(color * 0.01 + texture2D(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0); + vec3 n = normalize(vec3(50. * cos(time), 50., 50. * sin(time)) - position); + //vec3 n = normalize(vec3(0., 50., 0.) - position); + float phong = 0.2 + 0.8 * max(0., dot(n, normalize(normal))); + float specular = pow(max(dot(reflect(n, normalize(normal)), normalize(vec3(0, 0, -9) - position)), 0.), 10.); + FragColor = vec4(color * 0.001 + texture(ourTexture, texCoord).rgb * phong + vec3(1.) * specular, 1.); + //FragColor = vec4(color * 0.001 + vec3(0.5) * phong + vec3(1.) * specular, 1.); + //FragColor = vec4(color * 0.001 + normal, 1.); } diff --git a/src/shader.vs b/src/shader.vs index d119ba3..01ec4ce 100644 --- a/src/shader.vs +++ b/src/shader.vs @@ -1,6 +1,8 @@ #version 330 layout (location = 0) in vec3 Position; +layout (location = 1) in vec2 Uvs; +layout (location = 2) in vec3 Normal; uniform float gScale; uniform mat4 view; @@ -8,10 +10,20 @@ uniform mat4 proj; uniform mat4 model; flat out int vID; +out vec2 texCoord; +out vec3 position; +out vec3 normal; void main() { - gl_Position = proj * view * model * vec4(Position, 1.0); + vec4 test = proj * view * model * vec4(Position, 1.0); + gl_Position = test; // gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0); vID = gl_VertexID; + texCoord = Uvs; + + mat4 normalMat = transpose(inverse(view * model)); + + position = Position; + normal = (normalMat * vec4(Normal, 1.)).xyz; }