main.pp 5.8 KB

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