example.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <igl/get_seconds.h>
  2. #include <igl/material_colors.h>
  3. #include <igl/png/render_to_png.h>
  4. #ifdef __APPLE__
  5. # include <GLUT/glut.h>
  6. #else
  7. # include <GL/glut.h>
  8. #endif
  9. #include <cstdlib>
  10. #include <cstdio>
  11. #include <cmath>
  12. #include <iostream>
  13. #include <iomanip>
  14. #include <sstream>
  15. #include <algorithm>
  16. // This example displays one of the following shapes
  17. typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS=2, SHAPE_CONE=3, SHAPE_SPHERE=4 } Shape;
  18. #define NUM_SHAPES 4
  19. Shape g_CurrentShape = SHAPE_TEAPOT;
  20. int width,height;
  21. double alpha = 0.8;
  22. int capture_count = 0;
  23. bool capture_on_next = false;
  24. const float light_pos[4] = {-0.1,-0.1,1.0,0.0};
  25. // Callback function called by GLUT to render screen
  26. void Display(void)
  27. {
  28. using namespace igl;
  29. using namespace std;
  30. float v[4]; // will be used to set light paramters
  31. // Clear frame buffer
  32. glClearColor(1, 1, 1, 0);
  33. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  34. glEnable(GL_DEPTH_TEST);
  35. glDisable(GL_CULL_FACE);
  36. glEnable(GL_NORMALIZE);
  37. glEnable(GL_LIGHTING);
  38. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  39. // Set light
  40. glEnable(GL_LIGHTING);
  41. glEnable(GL_LIGHT0);
  42. v[0] = v[1] = v[2] = 0.4f; v[3] = 1.0f;
  43. glLightfv(GL_LIGHT0, GL_AMBIENT, v);
  44. v[0] = v[1] = v[2] = 0.8f; v[3] = 1.0f;
  45. glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
  46. glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  47. // Set material
  48. glDisable(GL_COLOR_MATERIAL);
  49. float mat_ambient[4], mat_diffuse[4], mat_specular[4], mat_shininess=128;
  50. copy(CYAN_AMBIENT,CYAN_AMBIENT+4,mat_ambient);
  51. copy(CYAN_DIFFUSE,CYAN_DIFFUSE+4,mat_diffuse);
  52. copy(CYAN_SPECULAR,CYAN_SPECULAR+4,mat_specular);
  53. mat_ambient[3] = alpha;
  54. mat_diffuse[3] = alpha;
  55. mat_specular[3] = alpha;
  56. glMaterialfv(GL_BACK, GL_AMBIENT, mat_ambient);
  57. glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse);
  58. glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
  59. glMaterialf( GL_BACK, GL_SHININESS, mat_shininess);
  60. copy(GOLD_AMBIENT,GOLD_AMBIENT+4,mat_ambient);
  61. copy(GOLD_DIFFUSE,GOLD_DIFFUSE+4,mat_diffuse);
  62. copy(GOLD_SPECULAR,GOLD_SPECULAR+4,mat_specular);
  63. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  64. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  65. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  66. glMaterialf( GL_FRONT, GL_SHININESS, mat_shininess);
  67. if(g_CurrentShape==SHAPE_TEAPOT)
  68. {
  69. glFrontFace(GL_CW);
  70. }else
  71. {
  72. glFrontFace(GL_CCW);
  73. }
  74. // Rotate and draw shape
  75. glPushMatrix();
  76. glRotated(30,1,0,0);
  77. glRotated(360*(fmod(get_seconds(),10.0)/10.0),0,1,0);
  78. glScaled(1.5,1.5,1.5);
  79. glCallList(g_CurrentShape);
  80. glPopMatrix();
  81. if(capture_on_next)
  82. {
  83. stringstream padnum;
  84. padnum << "render_to_png-example-" << setw(4) << setfill('0') << capture_count++ << ".png";
  85. igl::png::render_to_png(padnum.str(),width,height);
  86. capture_on_next = false;
  87. }
  88. // Present frame buffer
  89. glutSwapBuffers();
  90. // Recall Display at next frame
  91. glutPostRedisplay();
  92. }
  93. // Callback function called by GLUT when window size changes
  94. void Reshape(int width, int height)
  95. {
  96. // Set OpenGL viewport and camera
  97. glViewport(0, 0, width, height);
  98. ::width = width;
  99. ::height = height;
  100. glMatrixMode(GL_PROJECTION);
  101. glLoadIdentity();
  102. gluPerspective(40, (double)width/height, 1, 10);
  103. glMatrixMode(GL_MODELVIEW);
  104. glLoadIdentity();
  105. gluLookAt(0,0,5, 0,0,0, 0,1,0);
  106. glTranslatef(0, 0.6f, -1);
  107. }
  108. void key(unsigned char key, int mouse_x, int mouse_y)
  109. {
  110. using namespace std;
  111. switch(key)
  112. {
  113. case 's':
  114. g_CurrentShape = (Shape)((g_CurrentShape)%NUM_SHAPES+1);
  115. cout<<"g_CurrentShape: "<<g_CurrentShape<<endl;
  116. break;
  117. case char(27):
  118. exit(0);
  119. case ' ':
  120. capture_on_next = true;
  121. break;
  122. default:
  123. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  124. }
  125. }
  126. // Main
  127. int main(int argc, char *argv[])
  128. {
  129. // Initialize GLUT
  130. glutInit(&argc, argv);
  131. glutInitDisplayString( "rgba depth double samples>=8 ");
  132. glutInitWindowSize(640, 480);
  133. glutCreateWindow("render_to_png example (press space to render)");
  134. glutCreateMenu(NULL);
  135. // Set GLUT callbacks
  136. glutDisplayFunc(Display);
  137. glutReshapeFunc(Reshape);
  138. glutKeyboardFunc(key);
  139. // Create some 3D objects (stored in display lists)
  140. glNewList(SHAPE_TEAPOT, GL_COMPILE);
  141. glutSolidTeapot(1.0);
  142. glEndList();
  143. glNewList(SHAPE_TORUS, GL_COMPILE);
  144. glutSolidTorus(0.3, 1.0, 16, 32);
  145. glEndList();
  146. glNewList(SHAPE_CONE, GL_COMPILE);
  147. glutSolidCone(1.0, 1.5, 64, 4);
  148. glEndList();
  149. glNewList(SHAPE_SPHERE, GL_COMPILE);
  150. glutSolidSphere(1.0, 50, 40);
  151. glEndList();
  152. // Call the GLUT main loop
  153. glutMainLoop();
  154. return 0;
  155. }