fork download
  1. /*
  2.  * q: 終了
  3.  * i: 初期化
  4.  */
  5.  
  6. //main.h
  7. #include <math.h>
  8. #include <GL/glut.h>
  9.  
  10. const int WINDOW_WIDTH = 512;
  11. const int WINDOW_HEIGHT = 512;
  12.  
  13. //時間刻み
  14. const float dt = 0.01f;
  15.  
  16. //壁の大きさ
  17. const float WALL_SIZE = 5.f;
  18.  
  19. //反発係数 ( 球と球、球と壁で共通 )
  20. const float coeff = .9f;
  21.  
  22. //球の数
  23. const int numSpheres = 3;
  24.  
  25. //球の速度
  26. float vx[ numSpheres ];
  27. float vy[ numSpheres ];
  28.  
  29. //球の中心の座標
  30. float px[ numSpheres ];
  31. float py[ numSpheres ];
  32.  
  33. //球の半径
  34. float radius[ numSpheres ];
  35.  
  36. //球の重さ
  37. float mass[ numSpheres ];
  38.  
  39. //球の色( diffuse )
  40. float color[ numSpheres ][ 3 ];
  41.  
  42. void setCamera( void );
  43. void setLight( void );
  44.  
  45. void initSphere( void );
  46.  
  47. void display( void );
  48. void displayAxis( float length );
  49. void displaySphere( float r,
  50. float dr, float dg, float db,
  51. float sr, float sg, float sb,
  52. float shininess );
  53. void displayWall( float size );
  54.  
  55. void keyboardFunction( unsigned char key, int x, int y );
  56.  
  57. void update( void );
  58. bool collisionDetection( const int id0, const int id1 );
  59. void calculateCollideVelocity( const int id0, const int id1 );
  60.  
  61. void calculateWall( const int id0 );
  62.  
  63. //main.cpp
  64. // #include "main.h"
  65.  
  66. void setCamera( void ) {
  67. glViewport( 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT );
  68.  
  69. glMatrixMode( GL_PROJECTION );
  70. glLoadIdentity();
  71. gluPerspective( 30.f, ( GLdouble ) WINDOW_WIDTH / ( GLdouble ) WINDOW_HEIGHT,
  72. 0.1f, 100.f );
  73.  
  74. glMatrixMode( GL_MODELVIEW );
  75. glLoadIdentity();
  76. gluLookAt( 0.f, 0.f, 20.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f );
  77. }
  78.  
  79. void setLight( void )
  80. {
  81. //点光源
  82. GLfloat light_position[ 4 ] = { 5.f, 5.f, 5.f, 1.f };
  83. GLfloat light_ambient[ 4 ] = { 0.3f, 0.3f, 0.3f, 0.f };
  84. GLfloat light_diffuse[ 4 ] = { 1.f, 1.f, 1.f, 1.f };
  85. GLfloat light_specular[ 4 ] = { 1.f, 1.f, 1.f, 1.f };
  86. glLightfv( GL_LIGHT0, GL_POSITION, light_position );
  87. glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient );
  88. glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
  89. glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular );
  90. }
  91.  
  92. //速度と位置の初期化
  93. void initSphere( void )
  94. {
  95. { // 0: Blue
  96. vx[ 0 ] = 10.f; vy[ 0 ] = 20.f;
  97. px[ 0 ] = 0.f; py[ 0 ] = 0.f;
  98. radius[ 0 ] = 1.f;
  99. mass[ 0 ] = 8.f;
  100. color[ 0 ][ 0 ] = 0.f; color[ 0 ][ 1 ] = 0.3f; color[ 0 ][ 2 ] = 1.f;
  101. }
  102. { // 1: Red
  103. vx[ 1 ] = 0.f; vy[ 1 ] = 0.f;
  104. px[ 1 ] = 3.f; py[ 1 ] = 3.f;
  105. radius[ 1 ] = 0.5f;
  106. mass[ 1 ] = 1.f;
  107. color[ 1 ][ 0 ] = 1.f; color[ 1 ][ 1 ] = 0.3f; color[ 1 ][ 2 ] = 0.3f;
  108. }
  109. { // 2: Green
  110. vx[ 2 ] = 0.f; vy[ 2 ] = 0.f;
  111. px[ 2 ] = -3.f; py[ 2 ] = -2.f;
  112. radius[ 2 ] = 2.f;
  113. mass[ 2 ] = 64.f;
  114. color[ 2 ][ 0 ] = 0.3f; color[ 2 ][ 1 ] = 1.f; color[ 2 ][ 2 ] = 0.3f;
  115. }
  116. }
  117.  
  118. void keyboardFunction( unsigned char key, int x, int y )
  119. {
  120. switch( key ) {
  121. case 'q': exit( 0 ); break;
  122. case 'i': initSphere(); break;
  123. default: break;
  124. }
  125. glutPostRedisplay();
  126. }
  127.  
  128. void displayAxis( float length )
  129. {
  130. glBegin( GL_LINES );
  131. glColor3f( 1.f, 0.f, 0.f );
  132. glVertex3f( 0.f, 0.f, 0.f );
  133. glVertex3f( length, 0.f, 0.f );
  134.  
  135. glColor3f( 0.f, 1.f, 0.f );
  136. glVertex3f( 0.f, 0.f, 0.f );
  137. glVertex3f( 0.f, length, 0.f );
  138.  
  139. glColor3f( 0.f, 0.f, 1.f );
  140. glVertex3f( 0.f, 0.f, 0.f );
  141. glVertex3f( 0.f, 0.f, length );
  142. glEnd();
  143. }
  144.  
  145. void displaySphere( float r,
  146. float dr, float dg, float db,
  147. float sr, float sg, float sb,
  148. float shininess )
  149. {
  150. float diffuse[ 4 ] = { dr, dg, db, 1.f };
  151. float specular[ 4 ] = { sr, sg, sb, 1.f };
  152. glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse );
  153. glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
  154. glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
  155. glutSolidSphere( r, 100, 100 );
  156. }
  157.  
  158. void displayWall( float size ) {
  159. glBegin( GL_LINE_LOOP );
  160. glColor3f( .9f, .9f, .9f );
  161. glVertex3f( -size, -size, 0.f ); glVertex3f( +size, -size, 0.f );
  162. glVertex3f( +size, +size, 0.f ); glVertex3f( -size, +size, 0.f );
  163. glEnd();
  164. }
  165.  
  166. void display( void )
  167. {
  168. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  169. setCamera();
  170. setLight();
  171.  
  172. displayAxis( 5.f );
  173. displayWall( WALL_SIZE );
  174.  
  175. glEnable( GL_LIGHTING );
  176. glEnable( GL_LIGHT0 );
  177.  
  178. for ( int i = 0; i < numSpheres; i++ ) {
  179. glPushMatrix();
  180. glTranslatef( px[ i ], py[ i ], 0.f );
  181. displaySphere( radius[ i ], color[ i ][ 0 ], color[ i ][ 1 ], color[ i ][ 2 ],
  182. 0.3f, 0.3f, 0.3f, 30.f );
  183. glPopMatrix();
  184. }
  185.  
  186. glDisable( GL_LIGHT0 );
  187. glDisable( GL_LIGHTING );
  188.  
  189. glutSwapBuffers();
  190. }
  191.  
  192. void update( void )
  193. {
  194. for ( int i = 0; i < numSpheres; i++ ) {
  195. px[ i ] += vx[ i ] * dt;
  196. py[ i ] += vy[ i ] * dt;
  197. }
  198. for ( int i = 0; i < numSpheres; i++ ) {
  199. for ( int j = i + 1; j < numSpheres; j++ ) {
  200. if ( collisionDetection( i, j ) ) {
  201. calculateCollideVelocity( i, j );
  202. }
  203. }
  204. calculateWall( i ); // 壁との衝突処理
  205. }
  206. glutPostRedisplay();
  207. }
  208.  
  209. bool collisionDetection( const int id0, const int id1 )
  210. {
  211. float d = ( px[ id0 ] - px[ id1 ] ) * ( px[ id0 ] - px[ id1 ] ) +
  212. ( py[ id0 ] - py[ id1 ] ) * ( py[ id0 ] - py[ id1 ] );
  213. float r = radius[ id0 ] + radius[ id1 ];
  214. return ( d <= r * r );
  215. }
  216.  
  217. void calculateCollideVelocity( const int id0, const int id1 )
  218. {
  219. float nx, ny, l, vn[ 2 ], vnn[ 2 ];
  220. nx = px[ id1 ] - px[ id0 ];
  221. ny = py[ id1 ] - py[ id0 ];
  222. l = 1.f / sqrtf( nx * nx + ny * ny );
  223. nx *= l;
  224. ny *= l;
  225.  
  226. vn[ 0 ] = vx[ id0 ] * nx + vy[ id0 ] * ny;
  227. vn[ 1 ] = vx[ id1 ] * nx + vy[ id1 ] * ny;
  228.  
  229. // 離れていく場合は衝突とみなさない
  230. if ( ( ( vn[ 1 ] * nx - vn[ 0 ] * nx ) * nx +
  231. ( vn[ 1 ] * ny - vn[ 0 ] * ny ) * ny ) > 0.f ) {
  232. return;
  233. }
  234.  
  235. vx[ id0 ] -= vn[ 0 ] * nx;
  236. vy[ id0 ] -= vn[ 0 ] * ny;
  237.  
  238. vx[ id1 ] -= vn[ 1 ] * nx;
  239. vy[ id1 ] -= vn[ 1 ] * ny;
  240.  
  241. vnn[ 0 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) *
  242. ( ( mass[ id0 ] - coeff * mass[ id1 ] ) * vn[ 0 ] +
  243. mass[ id1 ] * ( 1.f + coeff ) * vn[ 1 ] );
  244. vnn[ 1 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) *
  245. ( mass[ id0 ] * ( 1.f + coeff ) * vn[ 0 ] +
  246. ( mass[ id1 ] - mass[ id0 ] * coeff ) * vn[ 1 ] );
  247.  
  248. vx[ id0 ] += vnn[ 0 ] * nx;
  249. vy[ id0 ] += vnn[ 0 ] * ny;
  250.  
  251. vx[ id1 ] += vnn[ 1 ] * nx;
  252. vy[ id1 ] += vnn[ 1 ] * ny;
  253. }
  254.  
  255. // 壁との衝突処理
  256. void calculateWall ( const int i )
  257. {
  258. float left = -WALL_SIZE + radius[ i ];
  259. if ( px[ i ] < left && vx[ i ] < 0.f ) {
  260. px[ i ] = left;
  261. vx[ i ] *= -coeff;
  262. }
  263. float right = WALL_SIZE - radius[ i ];
  264. if ( right < px[ i ] && 0.f < vx[ i ] ) {
  265. px[ i ] = right;
  266. vx[ i ] *= -coeff;
  267. }
  268. float bottom = -WALL_SIZE + radius[ i ];
  269. if ( py[ i ] < bottom && vy[ i ] < 0.f ) {
  270. py[ i ] = bottom;
  271. vy[ i ] *= -coeff;
  272. }
  273. float top = WALL_SIZE - radius[ i ];
  274. if ( top < py[ i ] && 0.f < vy[ i ] ) {
  275. py[ i ] = top;
  276. vy[ i ] *= -coeff;
  277. }
  278. }
  279.  
  280. int main( int argc, char** argv )
  281. {
  282. glutInit( &argc, argv );
  283. glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  284. glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
  285. glutCreateWindow( "OpenGL Based Renderer" );
  286. glClearColor( 0.f, 0.f, 0.f, 0.f );
  287. glEnable( GL_DEPTH_TEST );
  288. glDepthFunc( GL_LESS );
  289.  
  290. initSphere();
  291. glutDisplayFunc( display );
  292. glutKeyboardFunc( keyboardFunction );
  293. glutIdleFunc( update );
  294.  
  295. glutMainLoop();
  296.  
  297. return 0;
  298.  
  299. }
  300.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:8:21: fatal error: GL/glut.h: No such file or directory
 #include <GL/glut.h>
                     ^
compilation terminated.
stdout
Standard output is empty