lesson07.pp 5.6 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. begin
  87. // Setup the Main screen for 3D
  88. videoSetMode(MODE_0_3D);
  89. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  90. // initialize the geometry engine
  91. glInit();
  92. // enable textures
  93. glEnable(GL_TEXTURE_2D);
  94. // enable antialiasing
  95. glEnable(GL_ANTIALIAS);
  96. // setup the rear plane
  97. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  98. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  99. glClearDepth($7FFF);
  100. // Set our viewport to be the same size as the screen
  101. glViewPort(0,0,255,191);
  102. LoadGLTextures();
  103. glMatrixMode(GL_PROJECTION);
  104. glLoadIdentity();
  105. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  106. //set up a directional light arguments are light number (0-3), light color,
  107. //and an x,y,z vector that points in the direction of the light, the direction must be normalized
  108. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  109. //need to set up some material properties since DS does not have them set by default
  110. glMaterialf(GL_AMBIENT, RGB15(8,8,8));
  111. glMaterialf(GL_DIFFUSE, RGB15(8,8,8));
  112. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  113. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  114. //ds uses a table for shinyness..this generates a half-ass one
  115. glMaterialShinyness();
  116. // Set the current matrix to be the model matrix
  117. glMatrixMode(GL_MODELVIEW);
  118. while true do
  119. begin
  120. //these little button functions are pretty handy
  121. scanKeys();
  122. if ((keysDown() and KEY_A)) <> 0 then
  123. light := not light;
  124. if (keysHeld() and KEY_R) <> 0 then
  125. z := z -0.02;
  126. if (keysHeld() and KEY_L) <> 0 then
  127. z := z+0.02;
  128. if (keysHeld() and KEY_LEFT) <> 0 then
  129. xspeed := xspeed-0.01;
  130. if (keysHeld() and KEY_RIGHT) <> 0 then
  131. xspeed := xspeed+0.01;
  132. if (keysHeld() and KEY_UP) <> 0 then
  133. yspeed := yspeed+0.01;
  134. if (keysHeld() and KEY_DOWN) <> 0 then
  135. yspeed := yspeed-0.01;
  136. glColor3f(1,1,1);
  137. if (not light) then
  138. //ds specific, several attributes can be set here including turning on our light
  139. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK)
  140. else
  141. //ds specific, several attributes can be set here including turning on our light
  142. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  143. DrawGLScene();
  144. // flush to screen
  145. glFlush(0);
  146. // wait for the screen to refresh
  147. swiWaitForVBlank();
  148. end;
  149. end.