TexturedCube.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. program TexturedCube;
  2. {$L build/texture.bin.o}
  3. {$mode objfpc}
  4. uses
  5. ctypes, nds9;
  6. //texture_bin.h is created automagicaly from the texture.bin placed in arm9/resources
  7. //texture.bin is a raw 128x128 16 bit image. I will release a tool for texture conversion
  8. //later
  9. {$include inc/texture.bin.inc}
  10. var
  11. CubeVectors: array [0..23] of v16;
  12. CubeFaces: array [0..23] of u8;
  13. uv: array [0..3] of u32;
  14. normals: array [0..5] of u32;
  15. procedure Initialize();
  16. begin
  17. //verticies for the cube
  18. CubeVectors[0] := floattov16(-0.5); CubeVectors[1] := floattov16(-0.5); CubeVectors[2] := floattov16(0.5);
  19. CubeVectors[3] := floattov16(0.5); CubeVectors[4] := floattov16(-0.5); CubeVectors[5] := floattov16(0.5);
  20. CubeVectors[6] := floattov16(0.5); CubeVectors[7] := floattov16(-0.5); CubeVectors[8] := floattov16(-0.5);
  21. CubeVectors[9] := floattov16(-0.5); CubeVectors[10] := floattov16(-0.5); CubeVectors[11] := floattov16(-0.5);
  22. CubeVectors[12] := floattov16(-0.5); CubeVectors[13] := floattov16(0.5); CubeVectors[14] := floattov16(0.5);
  23. CubeVectors[15] := floattov16(0.5); CubeVectors[16] := floattov16(0.5); CubeVectors[17] := floattov16(0.5);
  24. CubeVectors[18] := floattov16(0.5); CubeVectors[19] := floattov16(0.5); CubeVectors[20] := floattov16(-0.5);
  25. CubeVectors[21] := floattov16(-0.5); CubeVectors[22] := floattov16(0.5); CubeVectors[23] := floattov16(-0.5);
  26. //polys
  27. CubeFaces[0] := 3; CubeFaces[1] := 2; CubeFaces[2] := 1; CubeFaces[3] := 0;
  28. CubeFaces[4] := 0; CubeFaces[5] := 1; CubeFaces[6] := 5; CubeFaces[7] := 4;
  29. CubeFaces[8] := 1; CubeFaces[9] := 2; CubeFaces[10] := 6; CubeFaces[11] := 5;
  30. CubeFaces[12] := 2; CubeFaces[13] := 3; CubeFaces[14] := 7; CubeFaces[15] := 6;
  31. CubeFaces[16] := 3; CubeFaces[17] := 0; CubeFaces[18] := 4; CubeFaces[19] := 7;
  32. CubeFaces[20] := 5; CubeFaces[21] := 6; CubeFaces[22] := 7; CubeFaces[23] := 4;
  33. //texture coordinates
  34. uv[0] := TEXTURE_PACK(inttot16(128), 0);
  35. uv[1] := TEXTURE_PACK(inttot16(128),inttot16(128));
  36. uv[2] := TEXTURE_PACK(0, inttot16(128));
  37. uv[3] := TEXTURE_PACK(0,0);
  38. normals[0] := NORMAL_PACK(0,floattov10(-0.97),0);
  39. normals[1] := NORMAL_PACK(0,0,floattov10(0.97));
  40. normals[2] := NORMAL_PACK(floattov10(0.97),0,0);
  41. normals[3] := NORMAL_PACK(0,0,floattov10(-0.97));
  42. normals[4] := NORMAL_PACK(floattov10(-0.97),0,0);
  43. normals[5] := NORMAL_PACK(0,floattov10(0.97),0);
  44. end;
  45. //draw a cube face at the specified color
  46. procedure drawQuad(poly: integer);
  47. var
  48. f1, f2, f3, f4: u32;
  49. begin
  50. f1 := CubeFaces[poly * 4] ;
  51. f2 := CubeFaces[poly * 4 + 1] ;
  52. f3 := CubeFaces[poly * 4 + 2] ;
  53. f4 := CubeFaces[poly * 4 + 3] ;
  54. glNormal(normals[poly]);
  55. GFX_TEX_COORD^ := (uv[0]);
  56. glVertex3v16(CubeVectors[f1*3], CubeVectors[f1*3 + 1], CubeVectors[f1*3 + 2] );
  57. GFX_TEX_COORD^ := (uv[1]);
  58. glVertex3v16(CubeVectors[f2*3], CubeVectors[f2*3 + 1], CubeVectors[f2*3 + 2] );
  59. GFX_TEX_COORD^ := (uv[2]);
  60. glVertex3v16(CubeVectors[f3*3], CubeVectors[f3*3 + 1], CubeVectors[f3*3 + 2] );
  61. GFX_TEX_COORD^ := (uv[3]);
  62. glVertex3v16(CubeVectors[f4*3], CubeVectors[f4*3 + 1], CubeVectors[f4*3 + 2] );
  63. end;
  64. var
  65. textureID: integer;
  66. i: integer;
  67. rotateX: cfloat = 0.0;
  68. rotateY: cfloat = 0.0;
  69. keys: cuint16;
  70. begin
  71. Initialize();
  72. //set mode 0, enable BG0 and set it to 3D
  73. videoSetMode(MODE_0_3D);
  74. // initialize gl
  75. glInit();
  76. //enable textures
  77. glEnable(GL_TEXTURE_2D);
  78. //this should work the same as the normal gl call
  79. glViewport(0,0,255,191);
  80. // enable antialiasing
  81. glEnable(GL_ANTIALIAS);
  82. // setup the rear plane
  83. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  84. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  85. glClearDepth($7FFF);
  86. vramSetBankA(VRAM_A_TEXTURE);
  87. glGenTextures(1, @textureID);
  88. glBindTexture(0, textureID);
  89. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcuint8(@texture_bin));
  90. //any floating point gl call is being converted to fixed prior to being implemented
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. gluPerspective(70, 256.0 / 192.0, 0.1, 40);
  94. gluLookAt( 0.0, 0.0, 1.0, //camera possition
  95. 0.0, 0.0, 0.0, //look at
  96. 0.0, 1.0, 0.0); //up
  97. while true do
  98. begin
  99. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  100. glLight(1, RGB15(31,0,31), 0, floattov10(1) - 1, 0);
  101. glLight(2, RGB15(0,31,0) , floattov10(-1.0), 0, 0);
  102. glLight(3, RGB15(0,0,31) , floattov10(1.0) - 1, 0, 0);
  103. glPushMatrix();
  104. //move it away from the camera
  105. glTranslate3f32(0, 0, floattof32(-1));
  106. glRotateX(rotateX);
  107. glRotateY(rotateY);
  108. glMatrixMode(GL_TEXTURE);
  109. glLoadIdentity();
  110. glMatrixMode(GL_MODELVIEW);
  111. glMaterialf(GL_AMBIENT, RGB15(8,8,8));
  112. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  113. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  114. glMaterialf(GL_EMISSION, RGB15(5,5,5));
  115. //ds uses a table for shinyness..this generates a half-ass one
  116. glMaterialShinyness();
  117. //not a real gl function and will likely change
  118. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0 or POLY_FORMAT_LIGHT1 or
  119. POLY_FORMAT_LIGHT2 or POLY_FORMAT_LIGHT3 ) ;
  120. scanKeys();
  121. keys := keysHeld();
  122. if((keys and KEY_UP)) <> 0 then rotateX := rotateX +3;
  123. if((keys and KEY_DOWN)) <> 0 then rotateX := rotateX -3;
  124. if((keys and KEY_LEFT)) <> 0 then rotateY := rotateY +3;
  125. if((keys and KEY_RIGHT)) <> 0 then rotateY := rotateY -3;
  126. glBindTexture(0, textureID);
  127. //draw the obj
  128. glBegin(GL_QUAD);
  129. for i := 0 to 5 do
  130. drawQuad(i);
  131. glEnd();
  132. glPopMatrix(1);
  133. glFlush(0);
  134. swiWaitForVBlank();
  135. end;
  136. end.