diff options
| -rw-r--r-- | shaders/fragment.glsl | 4 | ||||
| -rw-r--r-- | shaders/vertex.glsl | 5 | ||||
| -rw-r--r-- | src/block.c | 76 | ||||
| -rw-r--r-- | src/block.h | 3 | ||||
| -rw-r--r-- | src/engine.c | 2 | ||||
| -rw-r--r-- | src/shader.c | 4 |
6 files changed, 45 insertions, 49 deletions
diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 2b1879f..da55675 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,6 +1,6 @@ #version 410 core out vec4 frag_colour; -flat in vec3 color; +uniform vec3 face_colors[6]; void main() { - frag_colour = vec4( color, 1.0 ); + frag_colour = vec4( face_colors[gl_PrimitiveID/2], 1.0 ); }; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 7d99479..9ae73dc 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,12 +1,9 @@ #version 410 core layout(location=0) in vec3 pos; -layout(location=1) in vec3 col; -flat out vec3 color; uniform mat4 model; uniform mat4 view; uniform mat4 perspective; void main() { - gl_Position = perspective * model * vec4( pos, 1.0 ); - color = col; + gl_Position = perspective*view*model*vec4(pos, 1.0); }; diff --git a/src/block.c b/src/block.c index 3679892..8b7ba20 100644 --- a/src/block.c +++ b/src/block.c @@ -20,27 +20,15 @@ int block_init(vec3 pos, struct block* blk) { // ========== Constants of a block ================ // Local world coordinates float vertices[] = { - 0.5f, 0.5f, 0.0f, // top-right - -0.5f, 0.5f, 0.0f, // top-left - -0.5f, -0.5f, 0.0f, // bottom-left - 0.5f, -0.5f, 0.0f, // bottom-right - - 0.5f, 0.5f, -0.5f, // top-right (back plane) - -0.5f, 0.5f, -0.5f, // top-left (back plane) - -0.5f, -0.5f, -0.5f, // bottom-left (back plane) - 0.5f, -0.5f, -0.5f, // bottom-right (back plane) - }; - float colors[] = { - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - - 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, - 1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, // top-right + -0.5f, 0.5f, 0.5f, // top-left + -0.5f, -0.5f, 0.5f, // bottom-left + 0.5f, -0.5f, 0.5f, // bottom-right + 0.5f, 0.5f, -0.5f, // top-right (back plane) + -0.5f, 0.5f, -0.5f, // top-left (back plane) + -0.5f, -0.5f, -0.5f, // bottom-left (back plane) + 0.5f, -0.5f, -0.5f, // bottom-right (back plane) }; int vertex_order[] = { 1, 2, 3, 3, 0, 1, // Front @@ -52,22 +40,18 @@ int block_init(vec3 pos, struct block* blk) { }; // ================ OpenGL work ================ - create_vbo(&blk->_vbo1, (void*)vertices, sizeof(float) * ARRAY_SIZE(vertices)); - create_vbo(&blk->_vbo2, (void*)colors, sizeof(float) * ARRAY_SIZE(colors)); + 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 and color + // Enable 2 attribs - position glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); // set vao_buffer to pos buffer obj - glBindBuffer(GL_ARRAY_BUFFER, blk->_vbo1); + glBindBuffer(GL_ARRAY_BUFFER, blk->_vbo); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); - // set vao_buffer to color buffer obj - glBindBuffer(GL_ARRAY_BUFFER, blk->_vbo2); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); + // Set EBO to the vertex_order glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, blk->_ebo); return 0; @@ -79,30 +63,44 @@ void block_update(struct block* blk) { // RTS matrix - rotate, translate, scale glm_mat4_identity(blk->model); float angle = glm_rad(blk->angle); + vec3 rot_axis = { 1.0f, 1.0f, 0.0f }; vec3 axis_y = { 0.0f, 1.0f, 0.0f }; - vec3 pivot = { 0.0f, 0.0f, -0.25f }; + vec3 pivot = { 0.0f, 0.0f, 0.0f }; vec3 translation = { 0.0f, 0, -2.0f }; - glm_translate(blk->model, translation); - glm_rotate_at(blk->model, pivot, angle, axis_y); - glm_mat4_print(blk->model, stderr); + glm_translate(blk->model, blk->coords); + glm_rotate_at(blk->model, pivot, angle, rot_axis); // View matrix (camera) - vec3 camera = { 0.0f, 0.0f, -2.0f }; - vec3 camera_direction = { 0.0f, 0.0f, 1.0f }; + vec3 camera = { 0.0f, 0.0f, 2.0f }; + vec3 camera_direction = { 0.0f, 0.0f, -1.0f }; glm_look(camera, camera_direction, axis_y, blk->view); - // Projection (perspective) matrix - glm_perspective(glm_rad(45.0f), 800.0f / 600.0f, 0.1f, -10.0f, blk->perspective); - blk->angle = fmodf(blk->angle + 0.01f, 360.0f); + blk->angle = fmodf(blk->angle + 0.001f, 360.0f); } // Register block vbos and ebos to context int block_draw(struct block* blk, struct shader* shader) { glBindVertexArray(blk->_vao); set_uniform_mat4("model", shader, blk->model); -// set_uniform_mat4("view", shader, blk->view); - set_uniform_mat4("perspective", shader, blk->perspective); + set_uniform_mat4("view", shader, blk->view); + set_uniform_mat4("perspective", shader, blk->perspective); + GLuint loc = glGetUniformLocation(shader->program, "face_colors"); + if (loc == -1) { + fprintf(stderr, "Invalid var %s for get_uniform_mat4: Does not exist\n", "face_colors"); + exit(1); + return -1; + } + float colors[] = { + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + 1.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 1.0f, + 1.0f, 0.0f, 1.0f, + + }; + glUniform3fv(loc, 6, (void*)colors); glDrawElements(GL_TRIANGLES, blk->_vertex_count, GL_UNSIGNED_INT, 0); block_update(blk); return 0; diff --git a/src/block.h b/src/block.h index 00668f6..faabe58 100644 --- a/src/block.h +++ b/src/block.h @@ -7,8 +7,7 @@ struct block { vec3 coords; int type; GLuint _vao; - GLuint _vbo1; - GLuint _vbo2; + GLuint _vbo; GLuint _ebo; int _vertex_count; mat4 model; diff --git a/src/engine.c b/src/engine.c index 9e007ca..e5cda7e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -26,7 +26,7 @@ int engine_init(struct engine *engine) { // Setup Objects to draw struct block* blk = malloc(sizeof(struct block)); memset(blk, 0, sizeof(struct block)); - vec3 pos = { 0, 0, 0 }; + vec3 pos = { 0, 0, -1.0f }; if (block_init(pos, blk) != 0) { free(window); free(shader); diff --git a/src/shader.c b/src/shader.c index 339b76f..9c36791 100644 --- a/src/shader.c +++ b/src/shader.c @@ -35,7 +35,9 @@ GLuint compile_shader(GLuint type, const char* code) { GLint out; glGetShaderiv(shader, GL_COMPILE_STATUS, &out); if (out == GL_FALSE) { - fprintf(stderr, "Failed to compile shader: %s", code); + GLchar buf[100]; + glGetShaderInfoLog(shader, 100, NULL, buf); + fprintf(stderr, "Failed to compile shader: %s\nLogs:\n%s\n", code, buf); exit(1); } return shader; |
