The goal of this assignment is to create a display like the one in the image attached. The shapes are animated; see the attached video for a demonstration.
How To:
1. "06-animation-many" in "m04-transformation" is the base
2. Reduce loops by removing z loop
3. Create 6 parts in the loop each time it executes
4. Transforms
- Translate, based on center and using radius
- Rotation axes
- cylN and cylW the x axis (cylinders north and west of the central cube)
- cylE and cylS the y axis
- The torus the z axis
- Rotation center
- cylS, cylW, and torus rotate around the object center
- cylN rotates around the top of the object
- cylE rotates around the center of the cube
5. Use different shape objects for the four cylinders, so they can have different color schemes
- Check the documentation for shape for details
m04-transformation/01-static-transform/static-transform.cpp m04-transformation/01-static-transform/static-transform.cpp // #include
#include "GLM/glm.hpp" #include "GLM/ext.hpp" #include #include #include #include #include "cs4722/x11.h" #include "cs4722/shape.h" #include "cs4722/utility_glm.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint number_of_vertices; static GLuint program; static GLuint u_transform; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl" ); glUseProgram(program); u_transform = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto* vBuf = new std::vector(); auto* cBuf = new std::vector(); // create a shape // cs4722::shape *b = new cs4722::cylinder(.05, 20); // cs4722::shape* b = new cs4722::cylinder(); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::sphere(4, 5); // cs4722::shape* b = new cs4722::block(); number_of_vertices = b->get_size(); auto* vertices = b->positions(); auto* colors = b->colors(); // enum Buffer_IDs { bPosition, bColor, NumBuffers }; const int number_of_buffers = 2; GLuint buffers[number_of_buffers]; glCreateBuffers(number_of_buffers, buffers); const auto b_position = glGetAttribLocation(program, "bPosition"); const auto b_color = glGetAttribLocation(program, "bColor"); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferStorage(GL_ARRAY_BUFFER, 4 * 4 * number_of_vertices, vertices->data(), 0); glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(b_position); glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glBufferStorage(GL_ARRAY_BUFFER, 1 * 4 * number_of_vertices, colors->data(), 0); glVertexAttribPointer(b_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(b_color); delete vertices; delete colors; } //---------------------------------------------------------------------------- // // display // void display(void) { // compute transformation matrix glm::mat4 tr = glm::mat4(1.0f); // identity tr = glm::translate(tr, glm::vec3(.5f, -.5f, 0.0f)); // tr = tr * translate // tr = glm::rotate(tr, -M_PI / 4, glm::vec3(0, 0, 1)); // tr = glm::rotate(tr, -M_PI / 4, glm::vec3(0, 1, 0)); tr = glm::rotate(tr, -F_PI / 4, glm::vec3(1, 0, 0)); // tr = tr * translate * rotate tr = glm::scale(tr, glm::vec3(.2f, .5f, .5f)); // tr = tr * translate * rotate * scale // //std::cout <>< std::endl; gluniformmatrix4fv(u_transform, 1, gl_false, glm::value_ptr(tr));="" ="" gldrawarrays(gl_triangles, 0, number_of_vertices);="" ="" }="" int="" main(int argc, char** argv)="" {="" glfwinit();="" auto* window =" cs4722::setup_window("Static Transform", .7);" gl3winit();="" init();="" while (!glfwwindowshouldclose(window))="" {="" glclearbufferfv(gl_color, 0, cs4722::x11::gray50.as_float());="" glclear(gl_depth_buffer_bit);="" display();="" glfwswapbuffers(window);="" glfwpollevents();="" }="" glfwdestroywindow(window);="" glfwterminate();="" }="" m04-transformation/01a-static-transform-use-transform/static-transform-use-transform.cpp="" m04-transformation/01a-static-transform-use-transform/static-transform-use-transform.cpp=""> std::endl;> #include "GLM/glm.hpp" #include "GLM/ext.hpp" #include #include #include #include #include #include "cs4722/x11.h" #include "cs4722/shape.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint number_of_vertices; static GLuint program; static GLuint u_transform; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl" ,"fragment_shader-m04.glsl" ); glUseProgram(program); u_transform = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto* vBuf = new std::vector(); auto* cBuf = new std::vector(); // create a shape // cs4722::shape *b = new cs4722::cylinder(.05, 20); // cs4722::shape* b = new cs4722::cylinder(); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::sphere(4, 5); // cs4722::shape* b = new cs4722::block(); number_of_vertices = b->get_size(); auto* vertices = b->positions(); auto* colors = b->colors(); // enum Buffer_IDs { bPosition, bColor, NumBuffers }; const int number_of_buffers = 2; GLuint buffers[number_of_buffers]; glCreateBuffers(number_of_buffers, buffers); const auto b_position = glGetAttribLocation(program, "bPosition"); const auto b_color = glGetAttribLocation(program, "bColor"); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferStorage(GL_ARRAY_BUFFER, 4 * 4 * number_of_vertices, vertices->data(), 0); glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(b_position); glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glBufferStorage(GL_ARRAY_BUFFER, 1 * 4 * number_of_vertices, colors->data(), 0); glVertexAttribPointer(b_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(b_color); delete vertices; delete colors; } //---------------------------------------------------------------------------- // // display // void display(void) { auto* tr = new cs4722::transform(); tr->translate = glm::vec3(.5f, -.5f, 0.0f); tr->rotation_angle = -M_PI / 4; tr->rotation_axis = glm::vec3(0,0,1); tr->scale = glm::vec3(.2f, .5f, .5f); auto tr_matrix = tr->matrix(); glUniformMatrix4fv(u_transform, 1, GL_FALSE, glm::value_ptr(tr_matrix)); glDrawArrays(GL_TRIANGLES, 0, number_of_vertices); } int main(int argc, char** argv) { glfwInit(); auto * window = cs4722::setup_window("Static Transorm Use Transform", .7); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/02-use-part-and-attributes/use_part.cpp m04-transformation/02-use-part-and-attributes/use_part.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include #include #include "cs4722/x11.h" #include "cs4722/part.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static GLuint u_transform; static cs4722::part obj; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); u_transform = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto* vBuf = new std::vector(); auto* cBuf = new std::vector(); // create a shape //cs4722::shape *b = new cs4722::cylinder(.75, 20); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::block(); // obj = new cs4722::object(); obj.the_shape = b; // obj->translate = (glm::vec3(.5, -.5, 0)); obj.world_transform.rotation_axis = (glm::vec3(1, 1, 1)); obj.world_transform.rotation_angle = (M_PI / 4); obj.world_transform.rotation_center = (glm::vec3(0, 0, .7)); obj.world_transform.scale = (glm::vec3(.5f, .7f, .5f)); obj.the_shape->buffer_size = obj.the_shape->get_size(); obj.the_shape->buffer_start = 0; auto number_of_vertices = b->get_size(); auto* vertices = obj.the_shape->positions(); auto* colors = obj.the_shape->colors(); const int number_of_buffers = 2; GLuint buffers[number_of_buffers]; glCreateBuffers(number_of_buffers, buffers); const auto b_position = glGetAttribLocation(program, "bPosition"); const auto b_color = glGetAttribLocation(program, "bColor"); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferStorage(GL_ARRAY_BUFFER, 4 * 4 * number_of_vertices, vertices->data(), 0); glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(b_position); glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glBufferStorage(GL_ARRAY_BUFFER, 1 * 4 * number_of_vertices, colors->data(), 0); glVertexAttribPointer(b_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(b_color); delete vertices; delete colors; } //---------------------------------------------------------------------------- // // display // void display() { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); auto tr = obj.world_transform.matrix(); glUniformMatrix4fv(u_transform, 1, GL_FALSE, glm::value_ptr(tr)); glDrawArrays(GL_TRIANGLES, obj.the_shape->buffer_start, obj.the_shape->buffer_size); } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Use Part Class", .7); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/03-multiple-parts/multiple_parts.cpp m04-transformation/03-multiple-parts/multiple_parts.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include #include #include "cs4722/x11.h" #include "cs4722/shape.h" #include "cs4722/part.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static std::list part_list; static GLuint transform_loc; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); transform_loc = glGetUniformLocation(program, "transform"); // create a shape //cs4722::shape *b = new cs4722::cylinder(.75, 20); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::block(); auto* obj = new cs4722::part(); obj->the_shape = (b); obj->world_transform.translate = (glm::vec3(.5, -.5, 0)); obj->world_transform.rotation_axis = (glm::vec3(0, 1, 1)); obj->world_transform.rotation_angle = (M_PI / 3); obj->world_transform.scale = (glm::vec3(.4f, .6f, .4f)); part_list.push_back(obj); cs4722::shape* b1 = new cs4722::block(); auto* obj1 = new cs4722::part(); obj1->the_shape = (b1); obj1->world_transform.translate = (glm::vec3(-.5, -.5, 0)); obj1->world_transform.rotation_axis = (glm::vec3(1, 1, 0)); obj1->world_transform.rotation_angle = (M_PI / 4); obj1->world_transform.scale = (glm::vec3(.4f, .6f, .4f)); part_list.push_back(obj1); auto* v_buf = new std::vector(); auto* c_buf = new std::vector(); for (auto current_object : part_list) { auto* current_shape = current_object->the_shape; current_shape->buffer_size = current_shape->get_size(); current_shape->buffer_start = v_buf->size(); auto* vertices = current_shape->positions(); v_buf->insert(v_buf->end(), vertices->begin(), vertices->end()); delete vertices; auto* colors = current_shape->colors(); c_buf->insert(c_buf->end(), colors->begin(), colors->end()); delete colors; } const int number_of_buffers = 2; GLuint buffers[number_of_buffers]; glCreateBuffers(number_of_buffers, buffers); const auto b_position = glGetAttribLocation(program, "bPosition"); const auto b_color = glGetAttribLocation(program, "bColor"); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferStorage(GL_ARRAY_BUFFER, 4 * 4 * v_buf->size(), v_buf->data(), 0); glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(b_position); glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glBufferStorage(GL_ARRAY_BUFFER, 1 * 4 * c_buf->size(), c_buf->data(), 0); glVertexAttribPointer(b_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(b_color); delete v_buf; delete c_buf; } //---------------------------------------------------------------------------- // // display // void display() { //std::cout <><>< std::endl; ="" for (auto obj : part_list) {="" gluniformmatrix4fv(transform_loc, 1, gl_false, glm::value_ptr(obj-="">world_transform.matrix())); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Multiple Parts", .7); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/04-shared-shapes/shared_shapes.cpp m04-transformation/04-shared-shapes/shared_shapes.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include "cs4722/part.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static std::vector part_list; static GLuint transform_loc; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); transform_loc = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto *sphere = new cs4722::sphere(); auto number = 15 ; auto d = 4.0f / (2 * number + 1); auto radius = d / 4; auto base = -1 + 3 * radius; for(auto x = 0; x < number; ++x) {="" for (auto y =""> number; ++x)>< number; ++y) {="" for (auto z =""> number; ++y)>< number; ++z) {="" auto* obj =" new cs4722::part();" obj-="">the_shape = sphere; obj->world_transform.translate = glm::vec3(base + x * d, base + y * d, base + z * d); obj->world_transform.scale = glm::vec3(radius, radius, radius); part_list.push_back(obj); } } } auto* v_buf = new std::vector(); auto* c_buf = new std::vector(); for (auto current_object : part_list) { auto* current_shape = current_object->the_shape; if (current_shape->buffer_size == 0) { // test: skip shape if already processed current_shape->buffer_size = current_shape->get_size(); current_shape->buffer_start = v_buf->size(); auto* vertices = current_shape->positions(); v_buf->insert(v_buf->end(), vertices->begin(), vertices->end()); delete vertices; auto* colors = current_shape->colors(); c_buf->insert(c_buf->end(), colors->begin(), colors->end()); delete colors; } } const int number_of_buffers = 2; GLuint buffers[number_of_buffers]; glCreateBuffers(number_of_buffers, buffers); const auto b_position = glGetAttribLocation(program, "bPosition"); const auto b_color = glGetAttribLocation(program, "bColor"); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferStorage(GL_ARRAY_BUFFER, 4 * 4 * v_buf->size(), v_buf->data(), 0); glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(b_position); glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glBufferStorage(GL_ARRAY_BUFFER, 1 * 4 * c_buf->size(), c_buf->data(), 0); glVertexAttribPointer(b_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(b_color); std::cout <>< v_buf->size() < std::endl; // 2673000 no sharing number =" 15" // 792 sharing number =" 15" ="" delete v_buf;="" delete c_buf;="" ="" }="" ----------------------------------------------------------------------------="" display="" void="" display()="" {="" for (auto obj : part_list) {="" gluniformmatrix4fv(transform_loc, 1, gl_false, glm::value_ptr(obj-="">world_transform.matrix())); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Shared Shapes", .9); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/04A-buffer-utilities/shared_shapes_buffer_utilities.cpp m04-transformation/04A-buffer-utilities/shared_shapes_buffer_utilities.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include "cs4722/part.h" #include "cs4722/buffer_utilities.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static std::vector part_list; static GLuint transform_loc; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); transform_loc = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto *sphere = new cs4722::sphere(); auto number = 15 ; auto d = 4.0f / (2 * number + 1); auto radius = d / 4; auto base = -1 + 3 * radius; for(auto x = 0; x < number; ++x) {="" for (auto y =""> number; ++x)>< number; ++y) {="" for (auto z =""> number; ++y)>< number; ++z) {="" auto* obj =" new cs4722::part();" obj-="">the_shape = sphere; obj->world_transform.translate = glm::vec3(base + x * d, base + y * d, base + z * d); obj->world_transform.scale = glm::vec3(radius, radius, radius); part_list.push_back(obj); } } } cs4722::init_buffers(program, part_list, "bPosition", "bColor"); } //---------------------------------------------------------------------------- // // display // void display() { for (auto obj : part_list) { glUniformMatrix4fv(transform_loc, 1, GL_FALSE, glm::value_ptr(obj->world_transform.matrix())); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Shared Shapes, Buffer Utility", .7); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/05-animation/animation.cpp m04-transformation/05-animation/animation.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include "cs4722/part.h" #include "cs4722/buffer_utilities.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static std::vector part_list; static GLuint program; static GLuint transform_loc; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); transform_loc = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); // create a shape //cs4722::shape *b = new cs4722::cylinder(.75, 20); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::block(); auto* obj = new cs4722::part_rotating(); obj->the_shape = b; obj->world_transform.translate = glm::vec3(.5, -.5, 0); obj->animation_transform.rotation_axis = glm::vec3(1, 1, 0); obj->rotation_rate = M_PI / 3; obj->world_transform.scale = glm::vec3(.4f, .6f, .4f); part_list.push_back(obj); cs4722::shape* b1 = new cs4722::block(); auto* obj1 = new cs4722::part_rotating(); obj1->the_shape = b1; obj1->world_transform.translate = glm::vec3(-.5, 0, 0); obj1->animation_transform.rotation_axis = glm::vec3(1, 0, 1); obj1->rotation_rate = M_PI / 3; // obj1->animation_transform.rotation_center = glm::vec3(0, .3f, 0); obj1->world_transform.scale = glm::vec3(.2f, .6f, .2f); part_list.push_back(obj1); cs4722::init_buffers(program, part_list, "bPosition", "bColor"); } //---------------------------------------------------------------------------- // // display // void display() { //std::cout <><>< std::endl; glclearbufferfv(gl_color, 0, cs4722::x11::gray50.as_float());="" glclear(gl_depth_buffer_bit);="" static auto last_time =" 0.0;" printf("last time %.5f\n", last_time);="" auto time =" glfwGetTime();" auto delta_time =" time - last_time;" last_time =" time;" for (auto obj : part_list) {="" obj-="">animate(time, delta_time); auto model_matrix = obj->animation_transform.matrix() * obj->world_transform.matrix(); glUniformMatrix4fv(transform_loc, 1, GL_FALSE, glm::value_ptr(model_matrix)); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Animation", .9); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/05A-rotate-about-center/rotate-about-center.cpp m04-transformation/05A-rotate-about-center/rotate-about-center.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include "cs4722/buffer_utilities.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static GLuint transform_loc; static std::vector part_list; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); transform_loc = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); // create a shape //cs4722::shape *b = new cs4722::cylinder(.75, 20); cs4722::shape* b = new cs4722::sphere(15,50); // cs4722::shape* b = new cs4722::block(); auto* obj = new cs4722::part_rotating(); obj->the_shape = b; obj->world_transform.translate = glm::vec3(.5, -.5, 0); obj->rotation_rate = M_PI / 3; obj->world_transform.scale = glm::vec3(.4f, .6f, .4f); obj->animation_transform.rotation_axis = glm::vec3(1, 1, 0); // rotate about the object center obj->animation_transform.rotation_center = obj->world_transform.matrix() * glm::vec4(0,0,0,1); part_list.push_back(obj); cs4722::shape* b1 = new cs4722::block(); auto* obj1 = new cs4722::part_rotating(); obj1->the_shape = b1; obj1->world_transform.translate = glm::vec3(-.5, 0, 0); obj1->rotation_rate = M_PI / 3; // obj1->animation_transform.rotation_center = glm::vec3(0, .3f, 0); obj1->world_transform.scale = glm::vec3(.2f, .6f, .2f); obj1->animation_transform.rotation_axis = glm::vec3(1, 0, 1); // rotate about one end of the object obj1->animation_transform.rotation_center = obj1->world_transform.matrix() * glm::vec4(0,.5,0,1); part_list.push_back(obj1); cs4722::init_buffers(program, part_list, "bPosition", "bColor"); } void display() { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); static auto last_time = 0.0; auto time = glfwGetTime(); auto delta_time = time - last_time; last_time = time; for (auto obj : part_list) { obj->animate(time, delta_time); auto model_matrix = obj->animation_transform.matrix() * obj->world_transform.matrix(); glUniformMatrix4fv(transform_loc, 1, GL_FALSE, glm::value_ptr(model_matrix)); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Rotate About Center", .9); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/06-animation-many/animation_many.cpp m04-transformation/06-animation-many/animation_many.cpp #include "GLM/gtc/type_ptr.hpp" #include #include #include "cs4722/buffer_utilities.h" #include "cs4722/load_shaders.h" #include "cs4722/window.h" static GLuint program; static GLuint transform_loc; static std::vector part_list; void init(void) { program = cs4722::load_shaders("vertex_shader-m04.glsl","fragment_shader-m04.glsl"); glUseProgram(program); transform_loc = glGetUniformLocation(program, "transform"); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); auto* shape = new cs4722::block(); auto number = 5; auto d = 4.0f / (2 * number + 1); auto radius = d / 4; auto base = -1 + 3 * radius; for (auto x = 0; x < number; ++x) {="" for (auto y =""> number; ++x)>< number; ++y) {="" for (auto z =""> number; ++y)>< number; ++z) {="" auto* obj =" new cs4722::part_rotating();" obj-="">the_shape = shape; obj->world_transform.translate = glm::vec3(base + x * d, base + y * d, base + z * d); obj->world_transform.scale = glm::vec3(radius, radius, radius); obj->animation_transform.rotation_axis = glm::vec3(3*x+1, 3*y+1, 3*z+1); obj->animation_transform.rotation_center = obj->world_transform.matrix() * glm::vec4(0,0,0,1); //obj->set_rotation_rate(glm::linearRand(-M_PI/2, M_PI / 2)); obj->rotation_rate = M_PI / 3; part_list.push_back(obj); } } } cs4722::init_buffers(program, part_list, "bPosition", "bColor"); } void display() { static auto last_time = 0.0; auto time = glfwGetTime(); auto delta_time = time - last_time; last_time = time; // uncomment the following few lines to see the frame rate computed two different ways // static double arate = 0.0; // auto x = (100 * arate + 1 / delta_time) / 101; // arate = x; // printf("part count: %d rate: %.3f arate: %.3f\n", part_list.size(), 1/delta_time, arate); for (auto obj : part_list) { obj->animate(time, delta_time); auto model_matrix = obj->animation_transform.matrix() * obj->world_transform.matrix(); glUniformMatrix4fv(transform_loc, 1, GL_FALSE, glm::value_ptr(model_matrix)); glDrawArrays(GL_TRIANGLES, obj->the_shape->buffer_start, obj->the_shape->buffer_size); } } int main(int argc, char** argv) { glfwInit(); auto *window = cs4722::setup_window("Animation Many", .9); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { glClearBufferfv(GL_COLOR, 0, cs4722::x11::gray50.as_float()); glClear(GL_DEPTH_BUFFER_BIT); display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } m04-transformation/CMakeLists.txt cmake_minimum_required(VERSION 3.17) project(m04-transformation) set(CMAKE_CXX_STANDARD 14) add_compile_definitions(_GLFW_WIN32) link_libraries(cs4722 glfw3 opengl32 glu32) configure_file(fragment_shader-m04.glsl .) configure_file(vertex_shader-m04.glsl .) add_executable(01-static-transform 01-static-transform/static-transform.cpp) add_executable(01A-static-transform-use-transform 01A-static-transform-use-transform/static-transform-use-transform.cpp) add_executable(02-use-part-and-attributes 02-use-part-and-attributes/use_part.cpp) add_executable(03-multiple-parts 03-multiple-parts/multiple_parts.cpp) add_executable(04-shared-shapes 04-shared-shapes/shared_shapes.cpp) add_executable(04A-buffer-utilities 04A-buffer-utilities/shared_shapes_buffer_utilities.cpp) add_executable(05-animation 05-animation/animation.cpp) add_executable(05A-rotate-about-center 05A-rotate-about-center/rotate-about-center.cpp) add_executable(06-animation-many 06-animation-many/animation_many.cpp) m04-transformation/fragment_shader-m04.glsl #version 450 core out vec4 fColor; in vec4 vColor; void main() { fColor = vColor; // fColor = vec4(0,1,0,1); } m04-transformation/vertex_shader-m04.glsl #version 450 core in vec4 bPosition; in vec4 bColor; uniform mat4 transform; out vec4 vColor; void main() { vColor = bColor; gl_PointSize = 25.0; gl_Position = transform * bPosition; // gl_Position = bPosition; } number; ++z)> std::endl;> number; ++z)> std::endl;> v_buf-> number; ++z)> std::endl;>