one.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * one.c
  3. *
  4. * Hey! This was the original file where freeglut development started. Just
  5. * note what I have written here at the time. And see the creation date :)
  6. *
  7. * : This is a wrapper. I still have to figure out
  8. * : how to build shared libraries under *nix :)
  9. *
  10. * Copyright (c) 1999 by Pawel W. Olszta
  11. * Written by Pawel W. Olszta, <olszta@sourceforge.net>
  12. * Creation date: czw gru 2 11:58:41 CET 1999
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <GL/freeglut.h>
  17. int g_LeaveGameMode = 0;
  18. int g_InGameMode = 0;
  19. int g_mainwin1, g_mainwin2, g_sw1, g_sw2, g_gamemodewin;
  20. /*
  21. * Call this function to have some text drawn at given coordinates
  22. */
  23. void PrintText( int nX, int nY, char* pszText )
  24. {
  25. int lines;
  26. char *p;
  27. /*
  28. * Prepare the OpenGL state
  29. */
  30. glDisable( GL_LIGHTING );
  31. glDisable( GL_DEPTH_TEST );
  32. glMatrixMode( GL_PROJECTION );
  33. glPushMatrix();
  34. glLoadIdentity();
  35. /*
  36. * Have an orthogonal projection matrix set
  37. */
  38. glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ),
  39. 0, glutGet( GLUT_WINDOW_HEIGHT ),
  40. -1, +1
  41. );
  42. /*
  43. * Now the matrix mode
  44. */
  45. glMatrixMode( GL_MODELVIEW );
  46. glPushMatrix();
  47. glLoadIdentity();
  48. /*
  49. * Now the main text
  50. */
  51. glColor3ub( 0, 0, 0 );
  52. glRasterPos2i( nX, nY );
  53. for( p=pszText, lines=0; *p; p++ )
  54. {
  55. if( *p == '\n' )
  56. {
  57. lines++;
  58. glRasterPos2i( nX, nY-(lines*18) );
  59. }
  60. glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *p );
  61. }
  62. /*
  63. * Revert to the old matrix modes
  64. */
  65. glMatrixMode( GL_PROJECTION );
  66. glPopMatrix();
  67. glMatrixMode( GL_MODELVIEW );
  68. glPopMatrix();
  69. /*
  70. * Restore the old OpenGL states
  71. */
  72. glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
  73. glEnable( GL_DEPTH_TEST );
  74. glEnable( GL_LIGHTING );
  75. }
  76. /*
  77. * This is the display routine for our sample FreeGLUT windows
  78. */
  79. void SampleDisplay( void )
  80. {
  81. int win = glutGetWindow();
  82. if (g_InGameMode && win!=g_gamemodewin)
  83. /* Don't draw other windows when in gamemode, those aren't visible
  84. * anyway. Drawing them continuously nonetheless can cause flicker trouble
  85. * on my machine. This only seems to occur only when there are child windows
  86. * among the non-visible windows
  87. */
  88. return;
  89. if (win==g_sw1)
  90. {
  91. /*
  92. * Clear the screen
  93. */
  94. glClearColor(0.7f,0.7f,0.7f,1);
  95. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  96. glutPostWindowRedisplay(g_mainwin2);
  97. }
  98. else if (win==g_sw2)
  99. {
  100. /*
  101. * Clear the screen
  102. */
  103. glClearColor(0.3f,0.3f,0.3f,1);
  104. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  105. glutPostWindowRedisplay(g_mainwin2);
  106. }
  107. else
  108. {
  109. const GLfloat time = glutGet(GLUT_ELAPSED_TIME) / 1000.f * 40;
  110. /*
  111. * Clear the screen
  112. */
  113. glClearColor( 0, 0.5, 1, 1 );
  114. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  115. /*
  116. * Have the cube rotated
  117. */
  118. glMatrixMode( GL_MODELVIEW );
  119. glPushMatrix();
  120. glRotatef( time, 0, 0, 1 );
  121. glRotatef( time, 0, 1, 0 );
  122. glRotatef( time, 1, 0, 0 );
  123. /*
  124. * And then drawn...
  125. */
  126. glColor3f( 1, 1, 0 );
  127. /* glutWireCube( 20.0 ); */
  128. glutWireTeapot( 20.0 );
  129. /* glutWireSphere( 15.0, 15, 15 ); */
  130. /* glColor3f( 0, 1, 0 ); */
  131. /* glutWireCube( 30.0 ); */
  132. /* glutSolidCone( 10, 20, 10, 2 ); */
  133. /*
  134. * Don't forget about the model-view matrix
  135. */
  136. glPopMatrix( );
  137. /*
  138. * Draw a silly text
  139. */
  140. if( g_InGameMode == 0 )
  141. PrintText( 20, 20, "Hello there cruel world!" );
  142. else
  143. PrintText( 20, 20, "Press ESC to leave the game mode!" );
  144. }
  145. /*
  146. * And swap this context's buffers
  147. */
  148. glutSwapBuffers( );
  149. glutPostWindowRedisplay(win);
  150. }
  151. /*
  152. * This is a sample idle function
  153. */
  154. void SampleIdle( void )
  155. {
  156. if( g_LeaveGameMode == 1 )
  157. {
  158. /* One could do all this just as well in SampleGameModeKeyboard... */
  159. printf("leaving gamemode...\n");
  160. glutLeaveGameMode( );
  161. g_LeaveGameMode = 0;
  162. g_InGameMode = 0;
  163. glutPostWindowRedisplay(g_mainwin1);
  164. glutPostWindowRedisplay(g_mainwin2);
  165. glutPostWindowRedisplay(g_sw1);
  166. glutPostWindowRedisplay(g_sw2);
  167. }
  168. }
  169. void SampleEntry(int state)
  170. {
  171. int window = glutGetWindow () ;
  172. printf ( "Window %d Entry Callback: %d\n", window, state ) ;
  173. }
  174. /*
  175. * The reshape function
  176. */
  177. void SampleReshape( int nWidth, int nHeight )
  178. {
  179. GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
  180. GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
  181. GLfloat fCol[ 4 ] = { 0.5f, 1.0f, 0.0f, 1.0f };
  182. /*
  183. * Update the viewport first
  184. */
  185. glViewport( 0, 0, nWidth, nHeight );
  186. /*
  187. * Then the projection matrix
  188. */
  189. glMatrixMode( GL_PROJECTION );
  190. glLoadIdentity();
  191. glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
  192. /*
  193. * Move back the camera a bit
  194. */
  195. glMatrixMode( GL_MODELVIEW );
  196. glLoadIdentity( );
  197. glTranslatef( 0.0, 0.0, -40.0f );
  198. /*
  199. * Enable some features...
  200. */
  201. glEnable( GL_CULL_FACE );
  202. glEnable( GL_DEPTH_TEST );
  203. glEnable( GL_NORMALIZE );
  204. /*
  205. * Set up some lighting
  206. */
  207. glLightfv( GL_LIGHT0, GL_POSITION, fPos );
  208. glEnable( GL_LIGHTING );
  209. glEnable( GL_LIGHT0 );
  210. /*
  211. * Set up a sample material
  212. */
  213. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
  214. }
  215. /*
  216. * A sample keyboard callback
  217. */
  218. void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
  219. {
  220. printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
  221. cChar, nMouseX, nMouseY );
  222. }
  223. /*
  224. * A sample keyboard callback (for game mode window)
  225. */
  226. void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
  227. {
  228. if( cChar == 27 )
  229. g_LeaveGameMode = 1;
  230. }
  231. /*
  232. * A sample special callback
  233. */
  234. void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
  235. {
  236. printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
  237. nSpecial, nMouseX, nMouseY );
  238. }
  239. /*
  240. * A sample menu callback
  241. */
  242. void SampleMenu( int menuID )
  243. {
  244. printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
  245. }
  246. /*
  247. * A sample menu status callback
  248. */
  249. void SampleMenuStatus( int status, int x, int y )
  250. {
  251. printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
  252. }
  253. /*
  254. * The sample's entry point
  255. */
  256. int main( int argc, char** argv )
  257. {
  258. int menuID, subMenuA, subMenuB;
  259. glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
  260. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  261. glutInitWindowPosition( 100, 100 );
  262. glutInit( &argc, argv );
  263. glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
  264. glutMenuStatusFunc( SampleMenuStatus );
  265. glutIdleFunc( SampleIdle );
  266. subMenuA = glutCreateMenu( SampleMenu );
  267. glutAddMenuEntry( "Sub menu A1 (01)", 1 );
  268. glutAddMenuEntry( "Sub menu A2 (02)", 2 );
  269. glutAddMenuEntry( "Sub menu A3 (03)", 3 );
  270. subMenuB = glutCreateMenu( SampleMenu );
  271. glutAddMenuEntry( "Sub menu B1 (04)", 4 );
  272. glutAddMenuEntry( "Sub menu B2 (05)", 5 );
  273. glutAddMenuEntry( "Sub menu B3 (06)", 6 );
  274. glutAddSubMenu( "Going to sub menu A", subMenuA );
  275. menuID = glutCreateMenu( SampleMenu );
  276. glutAddMenuEntry( "Entry one", 1 );
  277. glutAddMenuEntry( "Entry two", 2 );
  278. glutAddMenuEntry( "Entry three", 3 );
  279. glutAddMenuEntry( "Entry four", 4 );
  280. glutAddMenuEntry( "Entry five", 5 );
  281. glutAddSubMenu( "Enter sub menu A", subMenuA );
  282. glutAddSubMenu( "Enter sub menu B", subMenuB );
  283. g_mainwin1 = glutCreateWindow( "Hello world!" );
  284. glutDisplayFunc( SampleDisplay );
  285. glutReshapeFunc( SampleReshape );
  286. glutKeyboardFunc( SampleKeyboard );
  287. glutSpecialFunc( SampleSpecial );
  288. glutEntryFunc( SampleEntry );
  289. glutAttachMenu( GLUT_LEFT_BUTTON );
  290. glutInitWindowPosition( 200, 200 );
  291. g_mainwin2 = glutCreateWindow( "I am not Jan B." );
  292. glutDisplayFunc( SampleDisplay );
  293. glutReshapeFunc( SampleReshape );
  294. glutKeyboardFunc( SampleKeyboard );
  295. glutSpecialFunc( SampleSpecial );
  296. glutEntryFunc( SampleEntry );
  297. glutAttachMenu( GLUT_LEFT_BUTTON );
  298. glutSetMenu(subMenuA);
  299. glutAttachMenu( GLUT_RIGHT_BUTTON );
  300. g_sw1=glutCreateSubWindow(g_mainwin2,200,0,100,100);
  301. glutDisplayFunc( SampleDisplay );
  302. glutSetMenu(subMenuB);
  303. glutAttachMenu( GLUT_LEFT_BUTTON );
  304. g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
  305. glutDisplayFunc( SampleDisplay );
  306. glutSetMenu(menuID);
  307. glutAttachMenu( GLUT_RIGHT_BUTTON );
  308. printf( "Testing game mode string parsing, don't panic!\n" );
  309. glutGameModeString( "320x240:32@100" );
  310. glutGameModeString( "640x480:16@72" );
  311. glutGameModeString( "1024x768" );
  312. glutGameModeString( ":32@120" );
  313. glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
  314. glutGameModeString( "640x480:37@300" ); /* this one should fail */
  315. glutEnterGameMode();
  316. glutGameModeString( "800x600" ); /* this one is likely to succeed */
  317. g_gamemodewin = glutEnterGameMode();
  318. if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
  319. g_InGameMode = 1;
  320. glutDisplayFunc( SampleDisplay );
  321. glutReshapeFunc( SampleReshape );
  322. glutKeyboardFunc( SampleGameModeKeyboard );
  323. glutEntryFunc( SampleEntry );
  324. glutSetMenu(menuID);
  325. glutAttachMenu( GLUT_LEFT_BUTTON );
  326. printf( "current window is %ix%i at (%i,%i)\n",
  327. glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
  328. glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
  329. );
  330. /*
  331. * Describe pixel format
  332. */
  333. printf("The current window has %i red bits, %i green bits, %i blue bits and %i alpha bits for a total buffer size of %i bits\n",glutGet(GLUT_WINDOW_RED_SIZE),glutGet(GLUT_WINDOW_GREEN_SIZE),glutGet(GLUT_WINDOW_BLUE_SIZE),glutGet(GLUT_WINDOW_ALPHA_SIZE),glutGet(GLUT_WINDOW_BUFFER_SIZE));
  334. printf("It furthermore has %i depth bits and %i stencil bits\n",glutGet(GLUT_WINDOW_DEPTH_SIZE),glutGet(GLUT_WINDOW_STENCIL_SIZE));
  335. /*
  336. * Enter the main FreeGLUT processing loop
  337. */
  338. glutMainLoop();
  339. /*
  340. * returned from mainloop after window closed
  341. * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
  342. */
  343. printf( "glutMainLoop() termination works fine!\n" );
  344. return EXIT_SUCCESS;
  345. }
  346. /*** END OF FILE ***/