void GLUTKeyboard(unsigned char key, int x, int y) { // Process keyboard button event switch (key) { case 'P': case 'p': show_points = !show_points; break; case 'K': case 'k': show_kdtree = !show_kdtree; break; case 27: // ESCAPE GLUTStop(); break; } // Remember mouse position GLUTmouse[0] = x; GLUTmouse[1] = GLUTwindow_height - y; // Remember modifiers GLUTmodifiers = glutGetModifiers(); // Redraw glutPostRedisplay(); }
void GLUTRedraw(void) { // Set projection transformation glMatrixMode(GL_PROJECTION); glLoadIdentity(); double mesh_radius = mesh->Radius(); gluPerspective(180.0*camera_yfov/M_PI, (GLdouble) GLUTwindow_width /(GLdouble) GLUTwindow_height, 0.01 * mesh_radius, 100 * mesh_radius); // Set camera transformation R3Vector& t = camera_towards; R3Vector& u = camera_up; R3Vector r = camera_up % camera_towards; GLdouble camera_matrix[16] = { r[0], u[0], t[0], 0, r[1], u[1], t[1], 0, r[2], u[2], t[2], 0, 0, 0, 0, 1 }; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixd(camera_matrix); glTranslated(-camera_eye[0], -camera_eye[1], -camera_eye[2]); // Clear window glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set lights static GLfloat light0_position[] = { 3.0, 4.0, 5.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light0_position); static GLfloat light1_position[] = { -3.0, -2.0, -3.0, 0.0 }; glLightfv(GL_LIGHT1, GL_POSITION, light1_position); // Draw faces if (show_faces) { glEnable(GL_LIGHTING); // glEnable(GL_TEXTURE_2D); //ABIUSX // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //ABIUSX static GLfloat diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; static GLfloat specular[] = { 0.2, 0.2, 0.2, 1.0 }; static GLfloat shininess[] = { 64 }; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); for (int i = 0; i < mesh->NFaces(); i++) { R3MeshFace *face = mesh->Face(i); if (face->isLeaf) //ABIUSX glBindTexture(GL_TEXTURE_2D, leaf); //ABIUSX else //ABIUSX glBindTexture(GL_TEXTURE_2D, tree); //ABIUSX glBegin(GL_POLYGON); const R3Vector& normal = face->plane.Normal(); glNormal3d(normal[0], normal[1], normal[2]); for (unsigned int j = 0; j < face->vertices.size(); j++) { R3MeshVertex *vertex = face->vertices[j]; const R3Point& p = vertex->position; // printf("%f %f\n",vertex->texcoords.X(),vertex->texcoords.Y()); glTexCoord2f(vertex->texcoords.X(),vertex->texcoords.Y()); //ABIUSX // glTexCoord2f(p[0],p[1]); //ABIUSX glVertex3f(p[0], p[1], p[2]); } glEnd(); } } // Draw edges if (show_edges) { glDisable(GL_LIGHTING); glColor3d(0.3, 0.3, 0.3); glLineWidth(3); for (int i = 0; i < mesh->NFaces(); i++) { glBegin(GL_LINE_LOOP); R3MeshFace *face = mesh->Face(i); for (unsigned int j = 0; j < face->vertices.size(); j++) { R3MeshVertex *vertex = face->vertices[j]; const R3Point& p = vertex->position; glVertex3f(p[0], p[1], p[2]); } glEnd(); } } // Draw vertices if (show_vertices) { glDisable(GL_LIGHTING); glColor3d(0, 0, 0); glPointSize(5); glBegin(GL_POINTS); for (int i = 0; i < mesh->NVertices(); i++) { R3MeshVertex *vertex = mesh->Vertex(i); const R3Point& p = vertex->position; glVertex3f(p[0], p[1], p[2]); } glEnd(); } // Draw vertex IDs if (show_ids) { char buffer[256]; glDisable(GL_LIGHTING); glColor3d(0, 0, 0); for (int i = 0; i < mesh->NVertices(); i++) { R3MeshVertex *vertex = mesh->Vertex(i); sprintf(buffer, "%d", vertex->id); GLUTDrawText(vertex->position, buffer); } } // Draw normals if (show_normals) { glDisable(GL_LIGHTING); glColor3d(0, 0, 0); glLineWidth(3); glBegin(GL_LINES); for (int i = 0; i < mesh->NVertices(); i++) { R3MeshVertex *vertex = mesh->Vertex(i); double length = vertex->AverageEdgeLength(); const R3Point& p = vertex->position; R3Vector v = length * vertex->normal; glVertex3f(p[0], p[1], p[2]); glVertex3f(p[0] + v[0], p[1] + v[1], p[2] + v[2]); } glEnd(); } // Draw curvatures if (show_curvatures) { glDisable(GL_LIGHTING); glPointSize(10); glBegin(GL_POINTS); for (int i = 0; i < mesh->NVertices(); i++) { R3MeshVertex *vertex = mesh->Vertex(i); const R3Point& p = vertex->position; double curvature = vertex->curvature; double magnitude = curvature * mesh->Radius(); if (curvature < 0) glColor3d(-magnitude, 0, 0); else glColor3d(0, 0, magnitude); glVertex3f(p[0], p[1], p[2]); } glEnd(); } // Draw bounding box if (show_bbox) { const R3Box& bbox = mesh->bbox; glDisable(GL_LIGHTING); glColor3d(1, 0, 0); glLineWidth(3); glBegin(GL_LINE_LOOP); glVertex3d(bbox[0][0], bbox[0][1], bbox[0][2]); glVertex3d(bbox[0][0], bbox[0][1], bbox[1][2]); glVertex3d(bbox[0][0], bbox[1][1], bbox[1][2]); glVertex3d(bbox[0][0], bbox[1][1], bbox[0][2]); glVertex3d(bbox[0][0], bbox[0][1], bbox[0][2]); glVertex3d(bbox[1][0], bbox[0][1], bbox[0][2]); glVertex3d(bbox[1][0], bbox[0][1], bbox[1][2]); glVertex3d(bbox[1][0], bbox[1][1], bbox[1][2]); glVertex3d(bbox[1][0], bbox[1][1], bbox[0][2]); glVertex3d(bbox[1][0], bbox[0][1], bbox[0][2]); glVertex3d(bbox[1][0], bbox[0][1], bbox[1][2]); glVertex3d(bbox[0][0], bbox[0][1], bbox[1][2]); glVertex3d(bbox[0][0], bbox[1][1], bbox[1][2]); glVertex3d(bbox[1][0], bbox[1][1], bbox[1][2]); glVertex3d(bbox[1][0], bbox[1][1], bbox[0][2]); glVertex3d(bbox[0][0], bbox[1][1], bbox[0][2]); glEnd(); } // Draw pick position if (show_pick && pick_active) { // Draw pick position glEnable(GL_LIGHTING); static GLfloat diffuse[] = { 1, 0, 0, 1.0 }; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse); double radius = 0.01 * mesh->Radius(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslated(pick_position[0], pick_position[1], pick_position[2]); static GLUquadricObj *sphere = gluNewQuadric(); gluQuadricNormals(sphere, (GLenum) GLU_SMOOTH); gluQuadricDrawStyle(sphere, (GLenum) GLU_FILL); gluSphere(sphere, radius, 8, 8); glPopMatrix(); // Draw pick face if (pick_face) { glDisable(GL_LIGHTING); glColor3f(1, 1, 0); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(-2, -2); glBegin(GL_POLYGON); for (unsigned int j = 0; j < pick_face->vertices.size(); j++) { R3MeshVertex *vertex = pick_face->vertices[j]; const R3Point& p = vertex->position; glVertex3f(p[0], p[1], p[2]); } glEnd(); glDisable(GL_POLYGON_OFFSET_FILL); } } // Write image if (save_image) { char image_name[256]; static int image_number = 1; for (;;) { sprintf(image_name, "image%d.jpg", image_number++); FILE *fp = fopen(image_name, "r"); if (!fp) break; else fclose(fp); } GLUTSaveImage(image_name); printf("Saved %s\n", image_name); save_image = 0; } // Quit here so that can save image before exit if (quit) { if (output_image_name) GLUTSaveImage(output_image_name); if (output_mesh_name) mesh->Write(output_mesh_name); GLUTStop(); } // Swap buffers glutSwapBuffers(); }
void GLUTRedraw(void) { // Initialize OpenGL drawing modes glEnable(GL_LIGHTING); glDisable(GL_BLEND); glBlendFunc(GL_ONE, GL_ZERO); glDepthMask(true); // Clear window R3Rgb background = scene->background; glClearColor(background[0], background[1], background[2], background[3]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Load camera LoadCamera(&camera); // Load scene lights LoadLights(scene); // Draw scene camera DrawCamera(scene); // Draw scene lights DrawLights(scene); // Draw particles DrawParticles(scene); // Draw particle sources DrawParticleSources(scene); // Draw particle sinks DrawParticleSinks(scene); // Draw particle springs DrawParticleSprings(scene); // Draw scene surfaces if (show_faces) { glEnable(GL_LIGHTING); DrawScene(scene); } // Draw scene edges if (show_edges) { glDisable(GL_LIGHTING); glColor3d(1 - background[0], 1 - background[1], 1 - background[2]); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); DrawScene(scene); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } // Save image if (save_image) { char image_name[256]; static int image_number = 1; for (;;) { sprintf(image_name, "image%d.jpg", image_number++); FILE *fp = fopen(image_name, "r"); if (!fp) break; else fclose(fp); } GLUTSaveImage(image_name); printf("Saved %s\n", image_name); save_image = 0; } // Save video if (save_video) { char frame_name[512]; static int next_frame = 0; static int num_frames_recorded = 0; for (;;) { sprintf(frame_name, "%sframe%04d.jpg", video_prefix, next_frame++); FILE *fp = fopen(frame_name, "r"); if (!fp) break; else fclose(fp); } GLUTSaveImage(frame_name); if (next_frame % 100 == 1) { printf("Saved %s\n", frame_name); } if (num_frames_to_record == ++num_frames_recorded) { save_video = 0; printf("Recorded %d frames, stopping as instructed.\n", num_frames_recorded); quit = 1; } } // Quit here so that can save image before exit if (quit) { if (output_image_name) GLUTSaveImage(output_image_name); GLUTStop(); } // Swap buffers glutSwapBuffers(); }
void GLUTKeyboard(unsigned char key, int x, int y) { // Process keyboard button event switch (key) { case '~': { // Dump screen shot to file iX.jpg static char buffer[64]; static int image_count = 1; sprintf(buffer, "i%d.jpg", image_count++); screenshot_image_name = buffer; break; } case 'B': case 'b': show_bboxes = !show_bboxes; break; case 'C': case 'c': show_camera = !show_camera; break; case 'L': case 'l': show_lights = !show_lights; break; case 'R': case 'r': show_rays = !show_rays; break; case '1': case '!': show_global_samples = !show_global_samples; break; case '2': case '@': show_caustic_samples = !show_caustic_samples; break; case 'P': case 'p': show_photons = !show_photons; break; case 'S': case 's': show_shapes = !show_shapes; break; case 'T': case 't': show_frame_rate = !show_frame_rate; break; case ' ': viewer->SetCamera(scene->Camera()); break; case '-': case '_': viewer->ScaleWorld(center, 0.8); break; case '+': case '=': viewer->ScaleWorld(center, 1.25); break; case 27: // ESCAPE GLUTStop(); break; } // Remember mouse position GLUTmouse[0] = x; GLUTmouse[1] = GLUTwindow_height - y; // Remember modifiers GLUTmodifiers = glutGetModifiers(); // Redraw glutPostRedisplay(); }