PalettedCube.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. program PalettedCube;
  2. {$L build/texture.bin.o}
  3. {$L build/texture1_RGB16_pal.bin.o}
  4. {$L build/texture1_RGB16_tex.bin.o}
  5. {$L build/texture2_RGB16_pal.bin.o}
  6. {$L build/texture2_RGB16_tex.bin.o}
  7. {$L build/texture3_RGB16_pal.bin.o}
  8. {$L build/texture3_RGB16_tex.bin.o}
  9. {$L build/texture4_RGB16_pal.bin.o}
  10. {$L build/texture4_RGB16_tex.bin.o}
  11. {$L build/texture5_RGB16_pal.bin.o}
  12. {$L build/texture5_RGB16_tex.bin.o}
  13. {$L build/texture6_RGB4_pal.bin.o}
  14. {$L build/texture6_RGB4_tex.bin.o}
  15. {$L build/texture7_RGB4_pal.bin.o}
  16. {$L build/texture7_RGB4_tex.bin.o}
  17. {$L build/texture8_RGB32_A3_pal.bin.o}
  18. {$L build/texture8_RGB32_A3_tex.bin.o}
  19. {$L build/texture9_RGB32_A3_pal.bin.o}
  20. {$L build/texture9_RGB32_A3_tex.bin.o}
  21. {$L build/texture10_COMP_tex.bin.o}
  22. {$L build/texture10_COMP_texExt.bin.o}
  23. {$L build/texture10_COMP_pal.bin.o}
  24. {$mode objfpc}
  25. uses
  26. ctypes, nds9;
  27. // most of the following textures were generated from online samples available at
  28. // http://www.marlinstudios.com/samples/sampvtf.htm and http://www.3dtotal.com/textures_v15/
  29. {$include inc/texture.bin.inc}
  30. {$include inc/texture1_RGB16_pal.bin.inc}
  31. {$include inc/texture1_RGB16_tex.bin.inc}
  32. {$include inc/texture2_RGB16_pal.bin.inc}
  33. {$include inc/texture2_RGB16_tex.bin.inc}
  34. {$include inc/texture3_RGB16_pal.bin.inc}
  35. {$include inc/texture3_RGB16_tex.bin.inc}
  36. {$include inc/texture4_RGB16_pal.bin.inc}
  37. {$include inc/texture4_RGB16_tex.bin.inc}
  38. {$include inc/texture5_RGB16_pal.bin.inc}
  39. {$include inc/texture5_RGB16_tex.bin.inc}
  40. {$include inc/texture6_RGB4_pal.bin.inc}
  41. {$include inc/texture6_RGB4_tex.bin.inc}
  42. {$include inc/texture7_RGB4_pal.bin.inc}
  43. {$include inc/texture7_RGB4_tex.bin.inc}
  44. {$include inc/texture8_RGB32_A3_pal.bin.inc}
  45. {$include inc/texture8_RGB32_A3_tex.bin.inc}
  46. {$include inc/texture9_RGB32_A3_pal.bin.inc}
  47. {$include inc/texture9_RGB32_A3_tex.bin.inc}
  48. {$include inc/texture10_COMP_tex.bin.inc}
  49. {$include inc/texture10_COMP_texExt.bin.inc}
  50. {$include inc/texture10_COMP_pal.bin.inc}
  51. var
  52. //verticies for the cube
  53. CubeVectors: array [0..23] of v16;
  54. //polys
  55. CubeFaces: array [0..23] of cuint8;
  56. //texture coordinates
  57. uv: array [0..3] of cuint32;
  58. normals: array [0..5] of cuint32;
  59. procedure Initialize();
  60. begin
  61. //verticies for the cube
  62. CubeVectors[0] := floattov16(-0.5); CubeVectors[1] := floattov16(-0.5); CubeVectors[2] := floattov16(0.5);
  63. CubeVectors[3] := floattov16(0.5); CubeVectors[4] := floattov16(-0.5); CubeVectors[5] := floattov16(0.5);
  64. CubeVectors[6] := floattov16(0.5); CubeVectors[7] := floattov16(-0.5); CubeVectors[8] := floattov16(-0.5);
  65. CubeVectors[9] := floattov16(-0.5); CubeVectors[10] := floattov16(-0.5); CubeVectors[11] := floattov16(-0.5);
  66. CubeVectors[12] := floattov16(-0.5); CubeVectors[13] := floattov16(0.5); CubeVectors[14] := floattov16(0.5);
  67. CubeVectors[15] := floattov16(0.5); CubeVectors[16] := floattov16(0.5); CubeVectors[17] := floattov16(0.5);
  68. CubeVectors[18] := floattov16(0.5); CubeVectors[19] := floattov16(0.5); CubeVectors[20] := floattov16(-0.5);
  69. CubeVectors[21] := floattov16(-0.5); CubeVectors[22] := floattov16(0.5); CubeVectors[23] := floattov16(-0.5);
  70. //polys
  71. CubeFaces[0] := 3; CubeFaces[1] := 2; CubeFaces[2] := 1; CubeFaces[3] := 0;
  72. CubeFaces[4] := 0; CubeFaces[5] := 1; CubeFaces[6] := 5; CubeFaces[7] := 4;
  73. CubeFaces[8] := 1; CubeFaces[9] := 2; CubeFaces[10] := 6; CubeFaces[11] := 5;
  74. CubeFaces[12] := 2; CubeFaces[13] := 3; CubeFaces[14] := 7; CubeFaces[15] := 6;
  75. CubeFaces[16] := 3; CubeFaces[17] := 0; CubeFaces[18] := 4; CubeFaces[19] := 7;
  76. CubeFaces[20] := 5; CubeFaces[21] := 6; CubeFaces[22] := 7; CubeFaces[23] := 4;
  77. //texture coordinates
  78. uv[0] := TEXTURE_PACK(inttot16(128), 0);
  79. uv[1] := TEXTURE_PACK(inttot16(128), inttot16(128));
  80. uv[2] := TEXTURE_PACK(0, inttot16(128));
  81. uv[3] := TEXTURE_PACK(0, 0);
  82. normals[0] := NORMAL_PACK(0, floattov10(-0.97), 0);
  83. normals[1] := NORMAL_PACK(0, 0, floattov10(0.97));
  84. normals[2] := NORMAL_PACK(floattov10(0.97), 0, 0);
  85. normals[3] := NORMAL_PACK(0, 0, floattov10(-0.97));
  86. normals[4] := NORMAL_PACK(floattov10(-0.97), 0, 0);
  87. normals[5] := NORMAL_PACK(0, floattov10(0.97), 0);
  88. end;
  89. //draw a cube face at the specified color
  90. procedure drawQuad(poly: integer);
  91. var
  92. f1, f2, f3, f4: cuint32;
  93. begin
  94. f1 := CubeFaces[poly * 4] ;
  95. f2 := CubeFaces[poly * 4 + 1] ;
  96. f3 := CubeFaces[poly * 4 + 2] ;
  97. f4 := CubeFaces[poly * 4 + 3] ;
  98. glNormal(normals[poly]);
  99. GFX_TEX_COORD^ := (uv[0]);
  100. glVertex3v16(CubeVectors[f1*3], CubeVectors[f1*3 + 1], CubeVectors[f1*3 + 2]);
  101. GFX_TEX_COORD^ := (uv[1]);
  102. glVertex3v16(CubeVectors[f2*3], CubeVectors[f2*3 + 1], CubeVectors[f2*3 + 2]);
  103. GFX_TEX_COORD^ := (uv[2]);
  104. glVertex3v16(CubeVectors[f3*3], CubeVectors[f3*3 + 1], CubeVectors[f3*3 + 2]);
  105. GFX_TEX_COORD^ := (uv[3]);
  106. glVertex3v16(CubeVectors[f4*3], CubeVectors[f4*3 + 1], CubeVectors[f4*3 + 2]);
  107. end;
  108. var
  109. textureIDS: array [0..10] of cint;
  110. i, j: integer;
  111. rotateX: cfloat = 0.0;
  112. rotateY: cfloat = 0.0;
  113. keyspressed: cuint16;
  114. keys: cuint16;
  115. nTexture: integer;
  116. compTexture: pcuint8;
  117. begin
  118. Initialize();
  119. lcdMainOnTop();
  120. //set mode 0, enable BG0 and set it to 3D
  121. videoSetMode(MODE_0_3D);
  122. //Because of letting the user manipulate which video banks the program will use,
  123. // I chose to manually set the console data into Bank I, as the demo default uses Bank C.
  124. videoSetModeSub(MODE_0_2D);
  125. vramSetBankI(VRAM_I_SUB_BG_0x06208000);
  126. consoleInit(nil, 0, BgType_Text4bpp, BgSize_T_256x256, 23, 2, false, true);
  127. // initialize gl
  128. glInit();
  129. //enable textures
  130. glEnable(GL_TEXTURE_2D);
  131. // enable antialiasing
  132. glEnable(GL_ANTIALIAS);
  133. // setup the rear plane
  134. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  135. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  136. glClearDepth($7FFF);
  137. //this should work the same as the normal gl call
  138. glViewport(0,0,255,191);
  139. //ds uses a table for shinyness..this generates a half-ass one
  140. glMaterialShinyness();
  141. // setup other material properties
  142. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  143. glMaterialf(GL_DIFFUSE, RGB15(20,20,20));
  144. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  145. glMaterialf(GL_EMISSION, RGB15(5,5,5));
  146. // setup the lighting
  147. glLight(0, RGB15(31,31,31) , 0, floattov10(-0.5), floattov10(-0.85));
  148. //You may comment/uncomment what you like, as the integration of nglVideo into libnds works
  149. // by examining the state of the banks, and deciding where to put textures/texpalettes based on that.
  150. //There are some exceptions to get certain stuff working though...
  151. // At least one main bank (A-D) must be allocated to textures to load/use them obviously, as well as
  152. // sub banks (E-G) for texture palettes
  153. // Compressed textures require bank B allocated, as well as bank A or C (or both) to be loadable/usable
  154. // 4 color palettes (not 4-bit) require either bank E, or bank F/G as slot0/1
  155. //vramSetBankA(VRAM_A_TEXTURE);
  156. vramSetBankB(VRAM_B_TEXTURE);
  157. vramSetBankC(VRAM_C_TEXTURE);
  158. //vramSetBankD(VRAM_D_TEXTURE);
  159. //vramSetBankE(VRAM_E_TEX_PALETTE);
  160. vramSetBankF(VRAM_F_TEX_PALETTE_SLOT0);
  161. vramSetBankG(VRAM_G_TEX_PALETTE_SLOT5);
  162. glGenTextures(11, textureIDS);
  163. // inital full 16 bit colour texture
  164. glBindTexture(0, textureIDS[0]);
  165. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture_bin));
  166. // Load a 16 colour texture
  167. glBindTexture(0, textureIDS[1]);
  168. glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture1_RGB16_tex_bin));
  169. glColorTableEXT(0, 0, 16, 0, 0, pcuint16(@texture1_RGB16_pal_bin));
  170. // Just to show that this works, let's go and delete that very first texture that was loaded
  171. glDeleteTextures(1, @textureIDS[0]);
  172. // Load some more 16 color textures
  173. glBindTexture(0, textureIDS[2]);
  174. glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture2_RGB16_tex_bin));
  175. glColorTableEXT( 0, 0, 16, 0, 0, pcuint16(@texture2_RGB16_pal_bin));
  176. glBindTexture(0, textureIDS[3]);
  177. glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture3_RGB16_tex_bin));
  178. glColorTableEXT( 0, 0, 16, 0, 0, pcuint16(@texture3_RGB16_pal_bin));
  179. // Now, re-generate the first texture, who's VRAM position won't be the same as before in the end
  180. glGenTextures(1, @textureIDS[0]);
  181. glBindTexture(0, textureIDS[4]);
  182. glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture4_RGB16_tex_bin));
  183. glColorTableEXT( 0, 0, 16, 0, 0, pcuint16(@texture4_RGB16_pal_bin));
  184. glBindTexture(0, textureIDS[5]);
  185. glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture5_RGB16_tex_bin));
  186. glColorTableEXT(0, 0, 16, 0, 0, pcuint16(@texture5_RGB16_pal_bin));
  187. // Load some 4 colour textures
  188. glBindTexture(0, textureIDS[6]);
  189. glTexImage2D(0, 0, GL_RGB4, TEXTURE_SIZE_128, TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture6_RGB4_tex_bin));
  190. glColorTableEXT(0, 0, 4, 0, 0, pcuint16(@texture6_RGB4_pal_bin));
  191. glBindTexture(0, textureIDS[7]);
  192. glTexImage2D(0, 0, GL_RGB4, TEXTURE_SIZE_128, TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture7_RGB4_tex_bin));
  193. glColorTableEXT(0, 0, 4, 0, 0, pcuint16(@texture7_RGB4_pal_bin));
  194. // Load some 32 colour textures, 8 levels of alpha
  195. glBindTexture(0, textureIDS[8]);
  196. glTexImage2D(0, 0, GL_RGB32_A3, TEXTURE_SIZE_128, TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture8_RGB32_A3_tex_bin));
  197. glColorTableEXT(0, 0, 32, 0, 0, pcuint16(@texture8_RGB32_A3_pal_bin));
  198. glBindTexture(0, textureIDS[9]);
  199. glTexImage2D(0, 0, GL_RGB32_A3, TEXTURE_SIZE_128, TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture9_RGB32_A3_tex_bin));
  200. glColorTableEXT(0, 0, 32, 0, 0, pcuint16(@texture9_RGB32_A3_pal_bin));
  201. // Load a 4x4 texel compressed texture
  202. // The tiles and header need to be combined together in that order
  203. // If this data is already pre-combined together, then you can just send it into the nglTexImage2D function
  204. compTexture := pcuint8(malloc(texture10_COMP_tex_bin_size + texture10_COMP_texExt_bin_size));
  205. swiCopy(@texture10_COMP_tex_bin, compTexture, (texture10_COMP_tex_bin_size shr 2) or COPY_MODE_WORD);
  206. swiCopy(@texture10_COMP_texExt_bin, compTexture + texture10_COMP_tex_bin_size, (texture10_COMP_texExt_bin_size shr 2) or COPY_MODE_WORD);
  207. glBindTexture(0, textureIDS[10]);
  208. glTexImage2D(0, 0, GL_COMPRESSED, TEXTURE_SIZE_128, TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@compTexture));
  209. glColorTableEXT(0, 0, texture10_COMP_pal_bin_size shr 1, 0, 0, pcuint16(@texture10_COMP_pal_bin));
  210. // Now, let's reload the full 16 bit color texture was had it's name deleted and regenerated
  211. glBindTexture(0, textureIDS[0]);
  212. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture_bin));
  213. iprintf(#$1b'[4;8HPaletted Cube');
  214. iprintf(#$1b'[6;2HRight/Left shoulder to switch');
  215. glMatrixMode(GL_PROJECTION);
  216. glLoadIdentity();
  217. gluPerspective(70, 256.0 / 192.0, 0.1, 40);
  218. gluLookAt( 0.0, 0.0, 2.0, //camera possition
  219. 0.0, 0.0, 0.0, //look at
  220. 0.0, 1.0, 0.0); //up
  221. //not a real gl function and will likely change
  222. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_FORMAT_LIGHT0 or POLY_ID(1));
  223. glColor3f(1,1,1);
  224. glMatrixMode(GL_MODELVIEW);
  225. nTexture := 0;
  226. while true do
  227. begin
  228. glPushMatrix();
  229. glRotateX(rotateX);
  230. glRotateY(rotateY);
  231. scanKeys();
  232. keys := keysHeld();
  233. if((keys and KEY_UP)) <> 0 then rotateX := rotateX +3;
  234. if((keys and KEY_DOWN)) <> 0 then rotateX := rotateX -3;
  235. if((keys and KEY_LEFT)) <> 0 then rotateY := rotateY +3;
  236. if((keys and KEY_RIGHT)) <> 0 then rotateY := rotateY -3;
  237. keysPressed := keysDown();
  238. if (keysPressed and KEY_R) = 0 then
  239. begin
  240. inc(nTexture);
  241. if( nTexture = 11 ) then
  242. nTexture := 0;
  243. end;
  244. if (keysPressed and KEY_L) = 0 then
  245. begin
  246. dec(nTexture);
  247. if( nTexture = -1 ) then
  248. nTexture := 10;
  249. end;
  250. glBindTexture(0, textureIDS[nTexture]);
  251. //draw the obj
  252. glBegin(GL_QUAD);
  253. for i := 0 to 5 do
  254. drawQuad(i);
  255. glEnd();
  256. glPopMatrix(1);
  257. glFlush(0);
  258. swiWaitForVBlank();
  259. if (keysPressed and KEY_START) <> 0 then break;
  260. end;
  261. end.