embedded_gbfs.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. program embedded_gbfs;
  2. {$L build/data.gbfs.o}
  3. uses
  4. ctypes, nds9, gbfs;
  5. var
  6. data_gbfs: GBFS_FILE; cvar; external;
  7. const
  8. piover180: cfloat = 0.0174532925;
  9. var
  10. heading: cfloat;
  11. xpos: cfloat;
  12. zpos: cfloat;
  13. yrot: cfloat; // Y Rotation
  14. walkbias: cfloat = 0.0;
  15. walkbiasangle: cfloat = 0.0;
  16. lookupdown: cfloat = 0.0;
  17. texture: array [0..2] of integer; // Storage For 3 Textures (only going to use 1 on the DS for this demo)
  18. type
  19. tagVERTEX = record
  20. x, y, z: cfloat;
  21. u, v: cfloat;
  22. end;
  23. VERTEX = tagVERTEX;
  24. tagTRIANGLE = record
  25. vertex: array [0..2] of VERTEX;
  26. end;
  27. TRIANGLE = tagTRIANGLE;
  28. TTriangle = TRIANGLE;
  29. PTriangle = ^TRIANGLE;
  30. tagSECTOR = record
  31. numtriangles: integer;
  32. triangle: PTRIANGLE;
  33. end;
  34. SECTOR = tagSECTOR;
  35. var
  36. sector1: SECTOR; // Our Model Goes Here:
  37. function DrawGLScene(): boolean;
  38. var
  39. x_m, y_m, z_m: cfloat;
  40. u_m, v_m: cfloat;
  41. xtrans, ztrans, ytrans: cfloat;
  42. sceneroty: cfloat;
  43. numtriangles: integer;
  44. loop_m: integer;
  45. begin
  46. // Reset The View
  47. xtrans := -xpos;
  48. ztrans := -zpos;
  49. ytrans := -walkbias - 0.25;
  50. sceneroty := 360.0 - yrot;
  51. glLoadIdentity();
  52. glRotatef(lookupdown,1.0,0,0);
  53. glRotatef(sceneroty,0,1.0,0);
  54. glTranslatef(xtrans, ytrans, ztrans);
  55. glBindTexture(GL_TEXTURE_2D, texture[0]);
  56. numtriangles := sector1.numtriangles;
  57. // Process Each Triangle
  58. for loop_m := 0 to numtriangles - 1 do
  59. begin
  60. glBegin(GL_TRIANGLES);
  61. glNormal3f( 0.0, 0.0, 1.0);
  62. x_m := sector1.triangle[loop_m].vertex[0].x;
  63. y_m := sector1.triangle[loop_m].vertex[0].y;
  64. z_m := sector1.triangle[loop_m].vertex[0].z;
  65. u_m := sector1.triangle[loop_m].vertex[0].u;
  66. v_m := sector1.triangle[loop_m].vertex[0].v;
  67. glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  68. x_m := sector1.triangle[loop_m].vertex[1].x;
  69. y_m := sector1.triangle[loop_m].vertex[1].y;
  70. z_m := sector1.triangle[loop_m].vertex[1].z;
  71. u_m := sector1.triangle[loop_m].vertex[1].u;
  72. v_m := sector1.triangle[loop_m].vertex[1].v;
  73. glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  74. x_m := sector1.triangle[loop_m].vertex[2].x;
  75. y_m := sector1.triangle[loop_m].vertex[2].y;
  76. z_m := sector1.triangle[loop_m].vertex[2].z;
  77. u_m := sector1.triangle[loop_m].vertex[2].u;
  78. v_m := sector1.triangle[loop_m].vertex[2].v;
  79. glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  80. glEnd();
  81. end;
  82. DrawGLScene := true; // Everything Went OK
  83. end;
  84. function tsin(angle: cfloat): cfloat;
  85. var
  86. s: cint32;
  87. begin
  88. s := sinLerp(trunc((angle * DEGREES_IN_CIRCLE) / 360.0));
  89. tsin := f32tofloat(s);
  90. end;
  91. function tcos(angle: cfloat): cfloat;
  92. var
  93. c: cint32;
  94. begin
  95. c := cosLerp(trunc((angle * DEGREES_IN_CIRCLE) / 360.0));
  96. tcos := f32tofloat(c);
  97. end;
  98. var
  99. Myfile: pchar;
  100. procedure myGetStr(buff: pchar; size: integer);
  101. begin
  102. buff^ := Myfile^;
  103. inc(MyFile);
  104. while (buff^ <> #10) and (buff^ <> #13) do
  105. begin
  106. inc(buff);
  107. buff^ := Myfile^;
  108. inc(MyFile);
  109. end;
  110. buff[0] := #10;
  111. buff[1] := #0;
  112. end;
  113. procedure readstr(str: pchar);
  114. begin
  115. repeat
  116. myGetStr(str, 255);
  117. until ((str[0] <> '/') and (str[0] <> #10));
  118. end;
  119. //---------------------------------------------------------------------------------
  120. procedure SetupWorld();
  121. var
  122. x, y, z, u, v: cfloat;
  123. numtriangles: integer;
  124. oneline: array [0..254] of char;
  125. loop, vert: integer;
  126. begin
  127. Myfile := gbfs_get_obj(@data_gbfs, 'World.txt', nil);
  128. readstr(oneline);
  129. sscanf(oneline, 'NUMPOLYS %d'#10, @numtriangles);
  130. GetMem(sector1.triangle, numtriangles * sizeof(TRIANGLE));
  131. sector1.numtriangles := numtriangles;
  132. for loop := 0 to numtriangles - 1 do
  133. begin
  134. for vert := 0 to 2 do
  135. begin
  136. readstr(oneline);
  137. sscanf(oneline, '%f %f %f %f %f', @x, @y, @z, @u, @v);
  138. sector1.triangle[loop].vertex[vert].x := x;
  139. sector1.triangle[loop].vertex[vert].y := y;
  140. sector1.triangle[loop].vertex[vert].z := z;
  141. sector1.triangle[loop].vertex[vert].u := u;
  142. sector1.triangle[loop].vertex[vert].v := v;
  143. end;
  144. end;
  145. end;
  146. // Load PCX files And Convert To Textures
  147. function LoadGLTextures(): boolean;
  148. var
  149. pcx: sImage;
  150. pcx_file: pchar;
  151. begin
  152. //load our texture
  153. pcx_file := gbfs_get_obj(@data_gbfs, 'Mud.pcx', nil);
  154. loadPCX(pcuint8(pcx_file), @pcx);
  155. image8to16(@pcx);
  156. glGenTextures(1, @texture[0]);
  157. glBindTexture(0, texture[0]);
  158. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD or GL_TEXTURE_WRAP_S or GL_TEXTURE_WRAP_T, pcx.image.data8);
  159. imageDestroy(@pcx);
  160. LoadGLTextures := true;
  161. end;
  162. begin
  163. // Setup the Main screen for 3D
  164. videoSetMode(MODE_0_3D);
  165. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  166. // Reset the screen and setup the view
  167. glInit();
  168. // Set our viewport to be the same size as the screen
  169. glViewport(0,0,255,191);
  170. // enable textures
  171. glEnable(GL_TEXTURE_2D);
  172. //setup the projection matrix
  173. glMatrixMode(GL_PROJECTION);
  174. glLoadIdentity();
  175. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  176. //setup a light
  177. glLight(0, RGB15(31,31,31), 0, floattov10(-1.0), 0);
  178. //need to set up some material properties since DS does not have them set by default
  179. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  180. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  181. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  182. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  183. //ds uses a table for shinyness..this generates a half-ass one
  184. glMaterialShinyness();
  185. //ds specific, several attributes can be set here
  186. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_FORMAT_LIGHT0);
  187. // Set the current matrix to be the model matrix
  188. glMatrixMode(GL_MODELVIEW);
  189. // Specify the Clear Color and Depth
  190. glClearColor(0,0,0,31);
  191. glClearDepth($7FFF);
  192. // specify the color for vertices
  193. glColor3f(1,1,1);
  194. LoadGLTextures();
  195. SetupWorld();
  196. while true do
  197. begin
  198. //these little button functions are pretty handy
  199. scanKeys();
  200. if (keysHeld() and KEY_A) <> 0 then lookupdown := lookupdown - 1.0;
  201. if (keysHeld() and KEY_B) <> 0 then lookupdown := lookupdown + 1.0;
  202. if (keysHeld() and KEY_LEFT) <> 0 then
  203. begin
  204. heading := heading + 1.0;
  205. yrot := heading;
  206. end;
  207. if (keysHeld() and KEY_RIGHT) <> 0 then
  208. begin
  209. heading := heading - 1.0;
  210. yrot := heading;
  211. end;
  212. if (keysHeld() and KEY_DOWN) <> 0 then
  213. begin
  214. xpos := xpos + (tsin(heading)) * 0.05;
  215. zpos := zpos + (tcos(heading)) * 0.05;
  216. if (walkbiasangle >= 359.0) then
  217. walkbiasangle := 0.0
  218. else
  219. walkbiasangle:= walkbiasangle+10;
  220. walkbias := tsin(walkbiasangle) / 20.0;
  221. end;
  222. if (keysHeld() and KEY_UP) <> 0 then
  223. begin
  224. xpos := xpos - (tsin(heading)) * 0.05;
  225. zpos := zpos - (tcos(heading)) * 0.05;
  226. if (walkbiasangle <= 1.0) then
  227. walkbiasangle := 359.0
  228. else
  229. walkbiasangle := walkbiasangle- 10;
  230. walkbias := tsin(walkbiasangle) / 20.0;
  231. end;
  232. //Push our original Matrix onto the stack (save state)
  233. glPushMatrix();
  234. DrawGLScene();
  235. // Pop our Matrix from the stack (restore state)
  236. glPopMatrix(1);
  237. // flush to screen
  238. glFlush(0);
  239. end;
  240. end.