diff options
| -rw-r--r-- | shaders/fragment.glsl | 11 | ||||
| -rw-r--r-- | shaders/light.glsl | 5 | ||||
| -rw-r--r-- | shaders/vertex.glsl | 6 | ||||
| -rw-r--r-- | src/block.c | 75 | ||||
| -rw-r--r-- | src/engine.c | 2 | ||||
| -rw-r--r-- | src/input.c | 1 |
6 files changed, 88 insertions, 12 deletions
diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index da55675..e3bd9db 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,6 +1,15 @@ #version 410 core out vec4 frag_colour; uniform vec3 face_colors[6]; +uniform vec3 light_color; +in vec3 normal; +in vec3 frag_pos; void main() { - frag_colour = vec4( face_colors[gl_PrimitiveID/2], 1.0 ); + vec3 ambient_color = vec3(0.1); + vec3 norm = normalize(normal); + vec3 light_dir = normalize(vec3(1.0f, 1.0f, 1.0f)); + float diff = max(dot(norm, light_dir), 0.0); + vec3 diffuse = diff * vec3(1.0); + + frag_colour = vec4((ambient_color + diffuse) * face_colors[gl_PrimitiveID/2], 1.0); }; diff --git a/shaders/light.glsl b/shaders/light.glsl new file mode 100644 index 0000000..a68fb43 --- /dev/null +++ b/shaders/light.glsl @@ -0,0 +1,5 @@ +#version 410 core +out vec4 frag_colour; +void main() { + frag_colour = vec4(1.0); +}; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 9ae73dc..2af2c7e 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,9 +1,15 @@ #version 410 core layout(location=0) in vec3 pos; +layout(location=1) in vec3 v_normal; uniform mat4 model; uniform mat4 view; uniform mat4 perspective; +out vec3 normal; +out vec3 frag_pos; void main() { gl_Position = perspective*view*model*vec4(pos, 1.0); + normal = v_normal; + frag_pos = vec3(model*vec4(pos, 1.0)); + }; diff --git a/src/block.c b/src/block.c index 544be0a..80a62d2 100644 --- a/src/block.c +++ b/src/block.c @@ -25,36 +25,93 @@ int block_init(vec3 pos, struct block* blk) { // Local world coordinates float vertices[] = { 1.0f, 1.0f, 0.0f, // top-right + 0.0f, 0.0f, 1.0f, // Front 0.0f, 1.0f, 0.0f, // top-left + 0.0f, 0.0f, 1.0f, // Front 0.0f, 0.0f, 0.0f, // bottom-left + 0.0f, 0.0f, 1.0f, // Front 1.0f, 0.0f, 0.0f, // bottom-right + 0.0f, 0.0f, 1.0f, // Front 1.0f, 1.0f, -1.0f, // top-right (back plane) + 0.0f, 0.0f, -1.0f, // Back 0.0f, 1.0f, -1.0f, // top-left (back plane) + 0.0f, 0.0f, -1.0f, // Back 0.0f, 0.0f, -1.0f, // bottom-left (back plane) + 0.0f, 0.0f, -1.0f, // Back 1.0f, 0.0f, -1.0f, // bottom-right (back plane) + 0.0f, 0.0f, -1.0f, // Back + + 1.0f, 1.0f, -1.0f, // top-right (back plane) + 1.0f, 0.0f, 0.0f, // Right + 1.0f, 1.0f, 0.0f, // top-right + 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, 0.0f, // bottom-right + 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, -1.0f, // bottom-right (back plane) + 1.0f, 0.0f, 0.0f, // Right + + 0.0f, 1.0f, 0.0f, // top-left + -1.0f, 0.0f, 0.0f, // Left + 0.0f, 1.0f, -1.0f, // top-left (back plane) + -1.0f, 0.0f, 0.0f, // Left + 0.0f, 0.0f, -1.0f, // bottom-left (back plane) + -1.0f, 0.0f, 0.0f, // Left + 0.0f, 0.0f, 0.0f, // bottom-left + -1.0f, 0.0f, 0.0f, // Left + + 1.0f, 1.0f, -1.0f, // top-right (back plane) + 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, -1.0f, // top-left (back plane) + 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, 0.0f, // top-left + 0.0f, 1.0f, 0.0f, // Top + 1.0f, 1.0f, 0.0f, // top-right + 0.0f, 1.0f, 0.0f, // Top + + 1.0f, 0.0f, -1.0f, // bottom-right (back plane) + 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, 0.0f, -1.0f, // bottom-left (back plane) + 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, 0.0f, 0.0f, // bottom-left + 0.0f, -1.0f, 0.0f, // Bottom + 1.0f, 0.0f, 0.0f, // bottom-right + 0.0f, -1.0f, 0.0f, // Bottom }; + // int vertex_order[] = { + // 1, 2, 3, 3, 0, 1, // Front + // 5, 6, 7, 7, 4, 5, // Back + // 9, 10, 11, 11, 8, 9, // Right + // 13, 14, 15, 15, 12, 13, // Left + // 17, 18, 19, 19, 16, 17, // Top + // 21, 22, 23, 23, 20, 21, // Bottom + // + // }; int vertex_order[] = { - 1, 2, 3, 3, 0, 1, // Front - 5, 6, 7, 7, 4, 5, // Back - 0, 3, 7, 7, 4, 0, // Right - 1, 2, 6, 6, 5, 1, // Left - 5, 1, 0, 0, 4, 5, // Top - 6, 2, 3, 3, 7, 6, // Bottom + 1, 2, 3, 3, 0, 1, // Front + 5, 6, 7, 7, 4, 5, // Back + 9, 10, 11, 11, 8, 9, // Right + 13, 14, 15, 15, 12, 13, // Left + 17, 18, 19, 19, 16, 17, // Top + 21, 22, 23, 23, 20, 21, // Bottom }; // ================ OpenGL work ================ create_vbo(&blk->_vbo, (void*)vertices, sizeof(float) * ARRAY_SIZE(vertices)); create_ebo(&blk->_ebo, (void*)vertex_order, sizeof(int) * ARRAY_SIZE(vertex_order)); + blk->_vertex_count = ARRAY_SIZE(vertex_order); glGenVertexArrays(1, &blk->_vao); glBindVertexArray(blk->_vao); - // Enable 2 attribs - position + // Enable 2 attribs - position and normals glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); // set vao_buffer to pos buffer obj glBindBuffer(GL_ARRAY_BUFFER, blk->_vbo); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0); + // set vao_buffer to normals buffer obj + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*)(3*sizeof(float))); // Set EBO to the vertex_order glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, blk->_ebo); //NOTE: This is important, otherwise with multiple block_init calls, it @@ -76,7 +133,7 @@ void block_update(struct block* blk) { float angle = glm_rad(blk->angle); vec3 scale = { 0.90f, 0.90f, 0.90f }; glm_translate(blk->model, blk->coords); - glm_scale(blk->model, scale); + // glm_scale(blk->model, scale); // glm_rotate_at(blk->model, pivot, angle, rot_axis); // View matrix (camera) //blk->angle = fmodf(blk->angle + 0.005f, 360.0f); diff --git a/src/engine.c b/src/engine.c index 23c6aa2..ed2b6c2 100644 --- a/src/engine.c +++ b/src/engine.c @@ -49,7 +49,7 @@ int engine_init(struct engine *engine) { // Setup camera camera_init(&engine->camera); - vec3 camera_pos = { 0.0f, 5.0f, -0.0f }; + vec3 camera_pos = { 0.0f, 5.0f, 0.0f }; camera_set_position(engine->camera, camera_pos); // Final step - Start the game diff --git a/src/input.c b/src/input.c index be240be..b4dde18 100644 --- a/src/input.c +++ b/src/input.c @@ -49,7 +49,6 @@ void input_handle(struct engine *engine) { int y; SDL_GetRelativeMouseState(&x, &y); if (x != 0 || y != 0) { - fprintf(stderr, "X: %d, Y %d\n", x, y); vec2 offset = { x, y }; camera_rotate(engine->camera, offset); } |
