diff options
| author | Aaditya Dhruv <[email protected]> | 2026-01-25 19:05:38 -0600 |
|---|---|---|
| committer | Aaditya Dhruv <[email protected]> | 2026-01-25 19:05:38 -0600 |
| commit | 4d9627422f94c165102baa8d9166049321c25b10 (patch) | |
| tree | c5005850d2076cfe2a538c4c806074bc86d27e5e /src/block.c | |
| parent | 955ffb0d4a0235532fdb9b808745bcf571735122 (diff) | |
Better rotating cube
Diffstat (limited to 'src/block.c')
| -rw-r--r-- | src/block.c | 76 |
1 files changed, 37 insertions, 39 deletions
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; |
