main.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_FORMAT_LIGHT0);
  35. glBindTexture(GL_TEXTURE_2D, texture[0]); //no filters to swtich between
  36. glBegin(GL_QUADS);
  37. // Front Face
  38. glNormal3f( 0.0, 0.0, 1.0);
  39. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  40. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  41. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  42. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  43. // Back Face
  44. glNormal3f( 0.0, 0.0,-1.0);
  45. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  46. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  47. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  48. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  49. // Top Face
  50. glNormal3f( 0.0, 1.0, 0.0);
  51. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  52. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0);
  53. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0);
  54. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  55. // Bottom Face
  56. glNormal3f( 0.0,-1.0, 0.0);
  57. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0);
  58. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0);
  59. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  60. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  61. // Right face
  62. glNormal3f( 1.0, 0.0, 0.0);
  63. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  64. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  65. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  66. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  67. glEnd();
  68. glPolyFmt(POLY_ALPHA(15) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  69. glBegin(GL_QUADS);
  70. // Left Face
  71. glNormal3f(-1.0, 0.0, 0.0);
  72. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  73. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  74. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  75. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  76. glEnd();
  77. xrot := xrot+xspeed;
  78. yrot := yrot+yspeed;
  79. DrawGLScene := TRUE;
  80. end;
  81. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  82. var
  83. pcx: sImage; //////////////(NEW) and different from nehe.
  84. begin
  85. //load our texture
  86. loadPCX(pu8(drunkenlogo_pcx), @pcx);
  87. image8to16(@pcx);
  88. glGenTextures(1, @texture[0]);
  89. glBindTexture(0, texture[0]);
  90. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  91. imageDestroy(@pcx);
  92. result := TRUE;
  93. end;
  94. begin
  95. // Turn on everything
  96. powerON(POWER_ALL);
  97. // Setup the Main screen for 3D
  98. videoSetMode(MODE_0_3D);
  99. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  100. // IRQ basic setup
  101. irqInit();
  102. irqSet(IRQ_VBLANK, nil);
  103. // initialize the geometry engine
  104. glInit();
  105. // enable textures
  106. glEnable(GL_TEXTURE_2D);
  107. glEnable(GL_BLEND);
  108. // enable antialiasing
  109. glEnable(GL_ANTIALIAS);
  110. // setup the rear plane
  111. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  112. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  113. glClearDepth($7FFF);
  114. // Set our viewport to be the same size as the screen
  115. glViewPort(0,0,255,191);
  116. LoadGLTextures();
  117. glMatrixMode(GL_PROJECTION);
  118. glLoadIdentity();
  119. gluPerspective(35, 256.0 / 192.0, 0.1, 100);
  120. //set up a directional ligth arguments are light number (0-3), light color,
  121. //and an x,y,z vector that points in the direction of the light
  122. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  123. glColor3f(1,1,1);
  124. glMatrixMode(GL_MODELVIEW);
  125. //need to set up some material properties since DS does not have them set by default
  126. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  127. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  128. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  129. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  130. //ds uses a table for shinyness..this generates a half-ass one
  131. glMaterialShinyness();
  132. // Set the current matrix to be the model matrix
  133. glMatrixMode(GL_MODELVIEW);
  134. while true do
  135. begin
  136. //these little button functions are pretty handy
  137. scanKeys();
  138. if (keysHeld() and KEY_R) <> 0 then
  139. z := z -0.02;
  140. if (keysHeld() and KEY_L) <> 0 then
  141. z := z+0.02;
  142. if (keysHeld() and KEY_LEFT) <> 0 then
  143. xspeed := xspeed-0.01;
  144. if (keysHeld() and KEY_RIGHT) <> 0 then
  145. xspeed := xspeed+0.01;
  146. if (keysHeld() and KEY_UP) <> 0 then
  147. yspeed := yspeed+0.01;
  148. if (keysHeld() and KEY_DOWN) <> 0 then
  149. yspeed := yspeed-0.01;
  150. DrawGLScene();
  151. // flush to screen
  152. glFlush(0);
  153. // wait for the screen to refresh
  154. swiWaitForVBlank();
  155. end;
  156. end.