touchLook.pp 7.4 KB

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