example.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "BeachBall.h"
  2. #include <igl/quat_to_mat.h>
  3. #include <igl/opengl/report_gl_error.h>
  4. #include <igl/trackball.h>
  5. #include <igl/canonical_quaternions.h>
  6. #include <igl/PI.h>
  7. #include <GLUT/GLUT.h>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <cmath>
  12. double back[3] = {85.0/255.0,85.0/255.0,85.0/255.0};
  13. double width,height;
  14. double scene_rot[4] = {0,0,0,1};
  15. double * BeachBall::scene_rot = NULL;
  16. double down_scene_rot[4] = {0,0,0,1};
  17. int down_mouse_x=-1,down_mouse_y=-1;
  18. bool trackball_on = false;
  19. const float light_pos[4] = {-0.2, -0.1, 1,0};
  20. std::vector<BeachBall> balls;
  21. float plane[4] = {0,0,1,1.5};
  22. int display_count = 0;
  23. void push_scene()
  24. {
  25. using namespace igl;
  26. glMatrixMode(GL_PROJECTION);
  27. glLoadIdentity();
  28. //gluOrtho2D(0,width,0,height);
  29. gluPerspective(15,(double)width/(double)height,1e-2,300);
  30. glMatrixMode(GL_MODELVIEW);
  31. glLoadIdentity();
  32. glPushMatrix();
  33. gluLookAt(0,0,20,0,0,0,0,1,0);
  34. double mat[4*4];
  35. quat_to_mat(scene_rot,mat);
  36. glMultMatrixd(mat);
  37. }
  38. void lights()
  39. {
  40. using namespace std;
  41. using namespace igl;
  42. glEnable(GL_LIGHTING);
  43. float ones[4] = {1.0,1.0,1.0,1.0};
  44. glLightfv(GL_LIGHT0,GL_AMBIENT,ones);
  45. glLightfv(GL_LIGHT0,GL_DIFFUSE,ones);
  46. glLightfv(GL_LIGHT0,GL_SPECULAR,ones);
  47. glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
  48. glEnable(GL_LIGHT0);
  49. }
  50. void pop_scene()
  51. {
  52. glPopMatrix();
  53. }
  54. void display()
  55. {
  56. using namespace std;
  57. using namespace igl;
  58. glEnable(GL_BLEND);
  59. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  60. if(true || display_count==0 || display_count==200)
  61. {
  62. glClearColor(back[0],back[1],back[2],0);
  63. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  64. glEnable(GL_DEPTH_TEST);
  65. lights();
  66. push_scene();
  67. //// Normal list
  68. //for(int i = 0;i<(int)balls.size();i++)
  69. //{
  70. // balls[i].draw();
  71. //}
  72. // single root
  73. balls[0].draw();
  74. balls[0].push();
  75. for(int i = 1;i<(int)balls.size();i++)
  76. {
  77. balls[i].radius = 0.5;
  78. balls[i].draw();
  79. }
  80. balls[0].pop();
  81. // Draw floor
  82. glEnable(GL_COLOR_MATERIAL);
  83. glEnable(GL_POLYGON_OFFSET_FILL);
  84. glPolygonOffset(10.0,1);
  85. float size = 10;
  86. glBegin(GL_QUADS);
  87. glNormal3f(0,0,1);
  88. glColor4f(0,0.2,0.8,0.5);
  89. glVertex4f(-size,-size,-plane[3],1);
  90. glColor4f(0,0,1,0.5);
  91. glVertex4f( size,-size,-plane[3],1);
  92. glColor4f(0.0,0.1,0.5,0.5);
  93. glVertex4f( size, size,-plane[3],1);
  94. //glColor4f(back[0],back[1],back[2],1);
  95. glColor4f(0.0,0.1,0.5,0.5);
  96. glVertex4f(-size, size,-plane[3],1);
  97. glEnd();
  98. glDisable(GL_POLYGON_OFFSET_FILL);
  99. pop_scene();
  100. }
  101. //// Animation
  102. //for(int i = 0;i<(int)balls.size();i++)
  103. //{
  104. // balls[i].r[0] += cos(double(i*display_count)/1000.0) + sin(double(display_count)/1000.0);
  105. // balls[i].r[1] += sin(double(2*i*display_count)/1000.0) + cos(double(2*display_count)/1000.0);
  106. // balls[i].r[2] += cos(double(3*i*display_count)/1000.0) + sin(double(3*display_count)/1000.0);
  107. // balls[i].r[3] += sin(double(4*i*display_count)/1000.0) + cos(double(4*display_count)/1000.0);
  108. // double len =
  109. // sqrt(balls[i].r[0]*balls[i].r[0]+
  110. // balls[i].r[1]*balls[i].r[1]+
  111. // balls[i].r[2]*balls[i].r[2]+
  112. // balls[i].r[3]*balls[i].r[3]);
  113. // for(int j = 0;j<4;j++)
  114. // {
  115. // balls[i].r[j] /= len;
  116. // }
  117. //}
  118. igl::opengl::report_gl_error();
  119. glutSwapBuffers();
  120. glutPostRedisplay();
  121. display_count++;
  122. }
  123. void reshape(int width, int height)
  124. {
  125. using namespace std;
  126. ::width = width;
  127. ::height = height;
  128. glMatrixMode(GL_PROJECTION);
  129. glLoadIdentity();
  130. glViewport(0,0,width,height);
  131. }
  132. void key(unsigned char key, int /*mouse_x*/, int /*mouse_y*/)
  133. {
  134. using namespace std;
  135. switch(key)
  136. {
  137. case char(27):
  138. exit(0);
  139. default:
  140. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  141. }
  142. }
  143. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  144. {
  145. mouse_y = height-mouse_y;
  146. using namespace std;
  147. push_scene();
  148. if(glutState==1)
  149. {
  150. for(int i = 0;i<(int)balls.size();i++)
  151. {
  152. balls[i].up(mouse_x,mouse_y);
  153. balls[i].hover(mouse_x,mouse_y);
  154. }
  155. trackball_on = false;
  156. }else if(glutState==0)
  157. {
  158. // down
  159. bool any = false;
  160. for(int i = 0;!any && i<(int)balls.size();i++)
  161. {
  162. if(glutButton==0)
  163. {
  164. any |= balls[i].down(mouse_x,mouse_y);
  165. }else if(glutButton==2)
  166. {
  167. any |= balls[i].right_down(mouse_x,mouse_y);
  168. }
  169. }
  170. trackball_on = !any;
  171. copy(scene_rot,scene_rot+4,down_scene_rot);
  172. down_mouse_x = mouse_x;
  173. down_mouse_y = mouse_y;
  174. }
  175. pop_scene();
  176. }
  177. void mouse_drag(int mouse_x, int mouse_y)
  178. {
  179. mouse_y = height-mouse_y;
  180. using namespace std;
  181. using namespace igl;
  182. if(trackball_on)
  183. {
  184. trackball<double>(
  185. width,
  186. height,
  187. 2,
  188. down_scene_rot,
  189. down_mouse_x,
  190. height-down_mouse_y,
  191. mouse_x,
  192. height-mouse_y,
  193. scene_rot);
  194. }
  195. push_scene();
  196. for(int i = 0;i<(int)balls.size();i++)
  197. {
  198. balls[i].drag(mouse_x,mouse_y);
  199. }
  200. pop_scene();
  201. }
  202. void mouse_move(int mouse_x, int mouse_y)
  203. {
  204. mouse_y = height-mouse_y;
  205. push_scene();
  206. bool any = false;
  207. for(int i = 0;!any && i<(int)balls.size();i++)
  208. {
  209. any |= balls[i].hover(mouse_x,mouse_y);
  210. }
  211. pop_scene();
  212. }
  213. int main(int argc,char * argv[])
  214. {
  215. using namespace igl;
  216. using namespace std;
  217. glutInit(&argc,argv);
  218. glutInitDisplayString( "rgba depth double samples>=8 ");
  219. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH),glutGet(GLUT_SCREEN_HEIGHT)/2);
  220. glutCreateWindow(__FILE__);
  221. glutDisplayFunc(display);
  222. glutReshapeFunc(reshape);
  223. glutKeyboardFunc(key);
  224. glutMouseFunc(mouse);
  225. glutMotionFunc(mouse_drag);
  226. glutPassiveMotionFunc(mouse_move);
  227. //copy(XZ_PLANE_QUAT_D,XZ_PLANE_QUAT_D+4,scene_rot);
  228. balls.push_back(BeachBall());
  229. const int children = 10;
  230. for(int i = 1;i<=children;i++)
  231. {
  232. balls.push_back(BeachBall());
  233. balls[i].t[0] = 2.5*sin(double(i)*2.0*PI/double(children));
  234. balls[i].t[1] = 2.5*cos(double(i)*2.0*PI/double(children));
  235. }
  236. BeachBall::scene_rot = scene_rot;
  237. glutMainLoop();
  238. return 0;
  239. }