BoxTest.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. program Box_Test;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. function startTimer(timer: integer): cuint16;
  6. begin
  7. TIMER_CR(timer)^ := 0;
  8. TIMER_DATA(0)^ := 0;
  9. TIMER_CR(timer)^ := TIMER_DIV_1 or TIMER_ENABLE;
  10. startTimer := TIMER_DATA(0)^;
  11. end;
  12. function getTimer(timer: integer): cuint16; inline;
  13. begin
  14. getTimer := TIMER_DATA(timer)^;
  15. end;
  16. //---------------------------------------------------------------------------------
  17. //draws a box...same signature as boxTest
  18. //---------------------------------------------------------------------------------
  19. procedure DrawBox(x, y, z, width, height, depth: cfloat);
  20. begin
  21. glBegin(GL_QUADS);
  22. //z face
  23. glColor3f(1,0,0);
  24. glVertex3f(x , y , z );
  25. glVertex3f(x + width, y , z );
  26. glVertex3f(x + width, y + height, z );
  27. glVertex3f(x , y + height, z );
  28. //z + depth face
  29. glColor3f(1,0,1);
  30. glVertex3f(x , y , z + depth);
  31. glVertex3f(x , y + height, z + depth);
  32. glVertex3f(x + width, y + height, z + depth);
  33. glVertex3f(x + width, y , z + depth);
  34. //x face
  35. glColor3f(1,1,0);
  36. glVertex3f(x , y , z );
  37. glVertex3f(x , y + height, z );
  38. glVertex3f(x , y + height, z + depth);
  39. glVertex3f(x , y , z + depth);
  40. //x + width face
  41. glColor3f(1,1,1);
  42. glVertex3f(x + width, y , z );
  43. glVertex3f(x + width, y , z + depth);
  44. glVertex3f(x + width, y + height, z + depth);
  45. glVertex3f(x + width, y + height, z );
  46. //y face
  47. glColor3f(0,1,0);
  48. glVertex3f(x , y , z );
  49. glVertex3f(x , y , z + depth);
  50. glVertex3f(x + width, y , z + depth);
  51. glVertex3f(x + width, y , z );
  52. //y + height face
  53. glColor3f(0,1,1);
  54. glVertex3f(x , y + height, z );
  55. glVertex3f(x + width, y + height, z );
  56. glVertex3f(x + width, y + height, z + depth);
  57. glVertex3f(x , y + height, z + depth);
  58. glEnd();
  59. end;
  60. var
  61. touchXY: touchPosition;
  62. rotX: cfloat = 0;
  63. rotY: cfloat = 0;
  64. translate: cfloat = -5;
  65. //some profiling code
  66. time: cuint16;
  67. //keep track of vertex ram usage
  68. polygon_count, vertex_count: cint;
  69. //object
  70. rx: integer = 50;
  71. ry: integer = 15;
  72. oldx: integer = 0;
  73. oldy: integer = 0;
  74. held, pressed: integer;
  75. hit: integer;
  76. i: integer;
  77. begin
  78. //put 3D on top
  79. lcdMainOnTop();
  80. //setup the sub screen for basic printing
  81. consoleDemoInit();
  82. // Setup the Main screen for 3D
  83. videoSetMode(MODE_0_3D);
  84. // initialize gl
  85. glInit();
  86. // enable antialiasing
  87. glEnable(GL_ANTIALIAS);
  88. // setup the rear plane
  89. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  90. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  91. glClearDepth($7FFF);
  92. // Set our view port to be the same size as the screen
  93. glViewport(0,0,255,191);
  94. printf(#$1b'[10;0HPress A to change culling');
  95. printf(#10#10'Press B to change Ortho vs Persp');
  96. printf(#10'Left/Right/Up/Down to rotate');
  97. printf(#10'Press L and R to zoom');
  98. printf(#10'Touch screen to rotate cube');
  99. //main loop
  100. while true do
  101. begin
  102. //process input
  103. scanKeys();
  104. touchRead(touchXY);
  105. held := keysHeld();
  106. pressed := keysDown();
  107. if( held and KEY_LEFT) <> 0 then rotY := rotY + 1;
  108. if( held and KEY_RIGHT) <> 0 then rotY := rotY - 1;
  109. if( held and KEY_UP) <> 0 then rotX := rotX + 1;
  110. if( held and KEY_DOWN) <> 0 then rotX := rotX - 1;
  111. if( held and KEY_L) <> 0 then translate := translate + 0.1;
  112. if( held and KEY_R) <> 0 then translate := translate - 0.1;
  113. //reset x and y when user touches screen
  114. if (pressed and KEY_TOUCH) <> 0 then
  115. begin
  116. oldx := touchXY.px;
  117. oldy := touchXY.py;
  118. end;
  119. //if user drags then grab the delta
  120. if (held and KEY_TOUCH) <> 0 then
  121. begin
  122. rx := rx + (touchXY.px - oldx);
  123. ry := ry + (touchXY.py - oldy);
  124. oldx := touchXY.px;
  125. oldy := touchXY.py;
  126. end;
  127. //change ortho vs perspective
  128. glMatrixMode(GL_PROJECTION);
  129. glLoadIdentity();
  130. if (keysHeld() and KEY_B) <> 0 then
  131. glOrtho(-4,4,-3,3,0.1,10)
  132. else
  133. gluPerspective(70, 256.0 / 192.0, 0.1, 10);
  134. //change cull mode
  135. if (held and KEY_A) <> 0 then
  136. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE )
  137. else
  138. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_FRONT );
  139. // Set the current matrix to be the model matrix
  140. glMatrixMode(GL_MODELVIEW);
  141. glLoadIdentity();
  142. //handle camera
  143. glRotateY(rotY);
  144. glRotateX(rotX);
  145. glTranslatef(0,0,translate);
  146. //move the cube
  147. glRotateX(ry);
  148. glRotateY(rx);
  149. DrawBox(-1,-1,-1,2,2,2);
  150. swiWaitForVBlank();
  151. printf(#$1b'[0;0HBox test cycle count');
  152. time := startTimer(0);
  153. hit := BoxTestf(-1,-1,-1,2,2,2);
  154. printf(#10'Single test (float): %i', 2*(getTimer(0) - time));
  155. time := startTimer(0);
  156. BoxTest(inttov16(-1),inttov16(-1),inttov16(-1),inttov16(2),inttov16(2),inttov16(2));
  157. printf(#10'Single test (fixed): %i', 2*(getTimer(0) - time));
  158. time := startTimer(0);
  159. for i := 0 to 63 do
  160. BoxTest(inttov16(-1),inttov16(-1),inttov16(-1),inttov16(2),inttov16(2),inttov16(2));
  161. printf(#10'64 tests avg. (fixed): %i', (getTimer(0) - time) / 32);
  162. if hit <> 0 then
  163. printf(#10'Box Test result: hit')
  164. else
  165. printf(#10'Box Test result: miss');
  166. while (GFX_STATUS^ and (1 shl 27)) <> 0 do; // wait until the geometry engine is not busy
  167. glGetInt(GL_GET_VERTEX_RAM_COUNT, vertex_count);
  168. glGetInt(GL_GET_POLYGON_RAM_COUNT, polygon_count);
  169. if (held and KEY_A)<> 0 then
  170. printf(#10#10'Ram usage: Culling none')
  171. else
  172. printf(#10#10'Ram usage: Culling back faces');
  173. printf(#10'Vertex ram: %i', vertex_count);
  174. printf(#10'Polygon ram: %i', polygon_count);
  175. // flush to the screen
  176. glFlush(0);
  177. end;
  178. end.