void handle_middle_drag(double x, double y) { static const float MAX_PHI = 89.0f*M_PI/180.0f; cam_theta -= (x-mouse_x) / 1000.0f; if(cam_theta > 2.0f*M_PI) cam_theta = 2.0f*M_PI - cam_theta; cam_phi -= (y-mouse_y)/1000.0f; cam_phi = gnd::clamp(cam_phi, (float)M_PI/36.0f, MAX_PHI); cam_pos = point_on_sphere(cam_radius, cam_theta, cam_phi); glm::mat4 mview{glm::lookAt(cam_pos, glm::vec3{0,0,0}, glm::vec3{0,1,0})}; glUniformMatrix4fv(glGetUniformLocation(shader, "u_view"), 1, GL_FALSE, &mview[0][0]); }
const Sphere Sphere::Transform(const Matrix44& transform_matrix) const { Sphere result; Vector3 point_on_sphere(1.0f, 1.0f, 1.0f); point_on_sphere.Normalise(); point_on_sphere *= radius_; point_on_sphere = point_on_sphere.TransformNoTranslation(transform_matrix); result.set_radius(point_on_sphere.Length()); result.set_position(position_.Transform(transform_matrix)); return result; }
int main() { gnd::debug_allocator<gnd::stack_allocator> alloc{"def", 1024}; Foo * f1 = GND_NEW(Foo,alloc); GND_DELETE(f1,alloc); window.create(1280, 720, "Plant"); window.on_left_drag.connect([](double x, double y){ handle_middle_drag(x, y); return false; }); window.on_cursor_move.connect([](double x, double y){ mouse_x = x; mouse_y = y; return false; }); glClearColor(0.82,0.86,0.91,1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); VAO grid = make_grid(100); shader = gnd::load_shader("../data/basic3d.vs", "../data/basic3d.fs"); glUseProgram(shader); cam_pos = point_on_sphere(cam_radius, cam_theta, cam_phi); glm::mat4 mmodel{1.0f}; glm::mat4 mview{glm::lookAt(cam_pos, glm::vec3{0,0,0}, glm::vec3{0,1,0})}; glm::mat4 mproj{glm::perspective(0.785398f, 1280.0f/720.0f, 0.1f, 100.0f)}; glUniformMatrix4fv(glGetUniformLocation(shader, "u_projection"), 1, GL_FALSE, &mproj[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader, "u_view"), 1, GL_FALSE, &mview[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader, "u_model"), 1, GL_FALSE, &mmodel[0][0]); std::vector<IndexedVAO> model = load_model("../data/model.blend"); double last_measure = glfwGetTime(); double time_now = last_measure; size_t frames_rendered = 0; char title_buf[64]; glfwSwapInterval(0); glProvokingVertex(GL_FIRST_VERTEX_CONVENTION); glfwGetCursorPos(window.handle(), &mouse_x, &mouse_y); while(!window.should_close()) { time_now = glfwGetTime(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(grid.handle); glDrawArrays(grid.mode, 0, grid.count); draw_indexed_vaos(&model.front(), &model.back()+1); window.swap_buffers(); glfwPollEvents(); ++frames_rendered; if(time_now - last_measure >= 1.0) { snprintf(title_buf, 64, "%.2lf FPS", (double)frames_rendered/(time_now-last_measure)); window.set_title(title_buf); last_measure = time_now; frames_rendered = 0; } } return 0; }