main.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. (****************************************
  2. * NDS NeHe Lesson 04 *
  3. * Author: Ethos *
  4. ****************************************)
  5. program main;
  6. {$L drunkenlogo.pcx.o}
  7. {$apptype arm9} //...or arm7
  8. {$define ARM9} //...or arm7, according to apptype
  9. {$mode objfpc} // required for some libc funcs implementation
  10. uses
  11. ctypes, nds9; // required by nds headers!
  12. var
  13. drunkenlogo_pcx_end: array [0..0] of u8; cvar; external;
  14. drunkenlogo_pcx: array [0..0] of u8; cvar; external;
  15. drunkenlogo_pcx_size: u32; cvar; external;
  16. light: boolean; // Lighting ON/OFF ( NEW )
  17. lp: boolean; // L Pressed? ( NEW )
  18. xrot: GLfloat; // X Rotation
  19. yrot: GLfloat; // Y Rotation
  20. xspeed: GLfloat; // X Rotation Speed
  21. yspeed: GLfloat; // Y Rotation Speed
  22. z: GLfloat = -5.0; // Depth Into The Screen
  23. texture: array [0..2] of integer; // Storage For 3 Textures (only going to use 1 on the DS for this demo)
  24. const
  25. LightAmbient: array [0..3] of GLfloat = ( 0.5, 0.5, 0.5, 1.0 );
  26. LightDiffuse: array [0..3] of GLfloat = ( 1.0, 1.0, 1.0, 1.0 );
  27. LightPosition: array [0..3] of GLfloat = ( 0.0, 0.0, 2.0, 1.0 );
  28. function DrawGLScene(): boolean; // Here's Where We Do All The Drawing
  29. begin
  30. glLoadIdentity(); // Reset The View
  31. glTranslatef(0.0,0.0,z);
  32. glRotatef(xrot,1.0,0.0,0.0);
  33. glRotatef(yrot,0.0,1.0,0.0);
  34. glBindTexture(GL_TEXTURE_2D, texture[0]); //no filters to swtich between
  35. glBegin(GL_QUADS);
  36. // Front Face
  37. glNormal3f( 0.0, 0.0, 1.0);
  38. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  39. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  40. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  41. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  42. // Back Face
  43. glNormal3f( 0.0, 0.0,-1.0);
  44. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  45. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  46. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  47. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  48. // Top Face
  49. glNormal3f( 0.0, 1.0, 0.0);
  50. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  51. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0);
  52. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0);
  53. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  54. // Bottom Face
  55. glNormal3f( 0.0,-1.0, 0.0);
  56. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0);
  57. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0);
  58. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  59. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  60. // Right face
  61. glNormal3f( 1.0, 0.0, 0.0);
  62. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  63. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  64. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  65. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  66. // Left Face
  67. glNormal3f(-1.0, 0.0, 0.0);
  68. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  69. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  70. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  71. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  72. glEnd();
  73. xrot:=xrot+xspeed;
  74. yrot:=yrot+yspeed;
  75. DrawGLScene := TRUE;
  76. end;
  77. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  78. var
  79. pcx: sImage; //////////////(NEW) and different from nehe.
  80. begin
  81. //load our texture
  82. loadPCX(pu8(drunkenlogo_pcx), @pcx);
  83. image8to16(@pcx);
  84. glGenTextures(1, @texture[0]);
  85. glBindTexture(0, texture[0]);
  86. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  87. imageDestroy(@pcx);
  88. result := TRUE;
  89. end;
  90. begin
  91. // Turn on everything
  92. powerON(POWER_ALL);
  93. // Setup the Main screen for 3D
  94. videoSetMode(MODE_0_3D);
  95. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  96. // IRQ basic setup
  97. irqInit();
  98. irqSet(IRQ_VBLANK, nil);
  99. // initialize the geometry engine
  100. glInit();
  101. // enable textures
  102. glEnable(GL_TEXTURE_2D);
  103. // enable antialiasing
  104. glEnable(GL_ANTIALIAS);
  105. // setup the rear plane
  106. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  107. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  108. glClearDepth($7FFF);
  109. // Set our viewport to be the same size as the screen
  110. glViewPort(0,0,255,191);
  111. LoadGLTextures();
  112. glMatrixMode(GL_PROJECTION);
  113. glLoadIdentity();
  114. gluPerspective(35, 256.0 / 192.0, 0.1, 100);
  115. //set up a directional light arguments are light number (0-3), light color,
  116. //and an x,y,z vector that points in the direction of the light, the direction must be normalized
  117. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  118. //need to set up some material properties since DS does not have them set by default
  119. glMaterialf(GL_AMBIENT, RGB15(8,8,8));
  120. glMaterialf(GL_DIFFUSE, RGB15(8,8,8));
  121. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  122. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  123. //ds uses a table for shinyness..this generates a half-ass one
  124. glMaterialShinyness();
  125. // Set the current matrix to be the model matrix
  126. glMatrixMode(GL_MODELVIEW);
  127. while true do
  128. begin
  129. //these little button functions are pretty handy
  130. scanKeys();
  131. if ((keysDown() and KEY_A)) <> 0 then
  132. light := not light;
  133. if (keysHeld() and KEY_R) <> 0 then
  134. z := z -0.02;
  135. if (keysHeld() and KEY_L) <> 0 then
  136. z := z+0.02;
  137. if (keysHeld() and KEY_LEFT) <> 0 then
  138. xspeed := xspeed-0.01;
  139. if (keysHeld() and KEY_RIGHT) <> 0 then
  140. xspeed := xspeed+0.01;
  141. if (keysHeld() and KEY_UP) <> 0 then
  142. yspeed := yspeed+0.01;
  143. if (keysHeld() and KEY_DOWN) <> 0 then
  144. yspeed := yspeed-0.01;
  145. glColor3f(1,1,1);
  146. if (not light) then
  147. begin
  148. //ds specific, several attributes can be set here including turning on our light
  149. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK);
  150. end else
  151. begin
  152. //ds specific, several attributes can be set here including turning on our light
  153. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  154. end;
  155. DrawGLScene();
  156. // flush to screen
  157. glFlush(0);
  158. // wait for the screen to refresh
  159. swiWaitForVBlank();
  160. end;
  161. end.