lesson07.pp 6.0 KB

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