Posted By

autofasurer on 10/27/10


Tagged

opengl


Versions (?)

Elastic Spheres


 / Published in: C++
 

URL: www.autofasurer.net/wp

Little test to control separate objects in OpenGL using pushMatrix() and popMatrix()

  1. #include <GLUT/glut.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. //initialising variables
  6. float x_positie = 0.0, x_doel = 0.0, x_doel2, x_kracht = 0.02, x_versnelling = -0.001, weerstand = 0.99, doeldist = 0.0;
  7. float y_positie = 0.0, y_doel = 0.0, y_doel2, y_kracht = 0.02, y_versnelling = -0.001, rot = 0.0;
  8. float z_positie = 0.0, z_doel = 0.0, z_doel2, z_kracht = 0.02, z_versnelling = -0.001, t = 0.0;
  9. //initialising states
  10. void init()
  11. {
  12. glClearColor(0.0,0.0,0.0,1.0);
  13. glEnable(GL_BLEND);
  14. glBlendFunc(GL_ONE, GL_ONE);
  15. }
  16.  
  17. //reshaping the viewport and projection
  18. void reshape(int width, int height)
  19. {
  20. glViewport(0, 0, 1024, 768);
  21. glMatrixMode(GL_PROJECTION);
  22. glLoadIdentity();
  23. gluPerspective(60.,(GLfloat) width/(GLfloat) height,0.1, 800.0);
  24. glMatrixMode(GL_MODELVIEW);
  25. gluLookAt(0.0,0.0,200.0, 0.0, 0.0, 0.0, 0,1,0);
  26.  
  27. }
  28.  
  29. //reading the keyboard input
  30. void key(unsigned char key, int x, int y){
  31. switch (key){
  32. case 32:
  33. x_doel2 = (rand()%160-80);
  34. y_doel2 = (rand()%160-80);
  35. z_doel2 = (rand()%160-80);
  36. doeldist = sqrt (pow((x_doel-x_doel2),2.0) + pow((y_doel-y_doel2),2.0) + pow((z_doel-z_doel2),2.0));
  37. break;
  38. }
  39. }
  40.  
  41. //drawing and displaying
  42. void display()
  43. {
  44. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  45. //glRotatef(0.5,0,1,0);
  46.  
  47. glPushMatrix();
  48. x_positie += x_kracht ;
  49. y_positie += y_kracht ;
  50. z_positie += z_kracht ;
  51. x_versnelling = (x_doel2 - x_positie)*0.0003;
  52. y_versnelling = (y_doel2 - y_positie)*0.0003;
  53. z_versnelling = (z_doel2 - z_positie)*0.0003;
  54. x_kracht += x_versnelling;
  55. y_kracht += y_versnelling;
  56. z_kracht += z_versnelling;
  57. x_kracht *= weerstand;
  58. y_kracht *= weerstand;
  59. z_kracht *= weerstand;
  60.  
  61. glBegin(GL_LINES);
  62. glVertex3f(x_doel,y_doel,z_doel);
  63. glVertex3f(x_positie,y_positie,z_positie);
  64. glEnd();
  65.  
  66. glTranslatef(x_positie, y_positie, z_positie);
  67. glColor4f(0.6, 0.0, 0.0, 1.0);
  68. glutSolidSphere(2.0,9,9);
  69. glPopMatrix();
  70.  
  71. glPushMatrix();
  72. glTranslatef(x_positie+sin(t)*9,y_positie+cos(t)*9,z_positie);
  73. glutSolidSphere(2.0,9,9);
  74. if (t > 6.2829){
  75. t = 0;
  76. }
  77. t += 0.02;
  78. glPopMatrix();
  79.  
  80. glPushMatrix();
  81. glTranslatef(x_doel, y_doel, z_doel);
  82. glColor4f(0.3,0.3,0.3,0.3);
  83. glutSolidSphere(5.0,15,15);
  84. glPopMatrix();
  85. glutSwapBuffers();
  86. }
  87.  
  88. //main loop with initialising of window and glut, assigning functions
  89. int main(int argc, char** argv)
  90. {
  91. srand(time(NULL));
  92. glutInit(&argc, argv);
  93. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  94. glutGameModeString("1024x768:32");
  95. glutEnterGameMode();
  96. init();
  97. glutDisplayFunc(display);
  98. glutIdleFunc(display);
  99. glutReshapeFunc(reshape);
  100. glutKeyboardFunc(key);
  101. glutMainLoop();
  102. return 0;
  103. }

Report this snippet  

You need to login to post a comment.