lesson08.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. (****************************************
  2. * NDS NeHe Lesson 08 *
  3. * Author: Ethos *
  4. ****************************************)
  5. program Lesson08;
  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. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_FORMAT_LIGHT0);
  31. glBindTexture(GL_TEXTURE_2D, texture[0]); //no filters to swtich between
  32. glBegin(GL_QUADS);
  33. // Front Face
  34. glNormal3f( 0.0, 0.0, 1.0);
  35. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  36. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  37. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  38. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  39. // Back Face
  40. glNormal3f( 0.0, 0.0,-1.0);
  41. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  42. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  43. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  44. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  45. // Top Face
  46. glNormal3f( 0.0, 1.0, 0.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. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0);
  50. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  51. // Bottom Face
  52. glNormal3f( 0.0,-1.0, 0.0);
  53. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0);
  54. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0);
  55. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  56. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  57. // Right face
  58. glNormal3f( 1.0, 0.0, 0.0);
  59. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  60. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  61. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  62. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  63. glEnd();
  64. glPolyFmt(POLY_ALPHA(15) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  65. glBegin(GL_QUADS);
  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. result := 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(pcuint8(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. var
  91. pressed: integer;
  92. held: integer;
  93. begin
  94. // Setup the Main screen for 3D
  95. videoSetMode(MODE_0_3D);
  96. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  97. // initialize the geometry engine
  98. glInit();
  99. // enable textures
  100. glEnable(GL_TEXTURE_2D);
  101. glEnable(GL_BLEND);
  102. // enable antialiasing
  103. glEnable(GL_ANTIALIAS);
  104. // setup the rear plane
  105. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  106. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  107. glClearDepth($7FFF);
  108. // Set our viewport to be the same size as the screen
  109. glViewPort(0,0,255,191);
  110. LoadGLTextures();
  111. glMatrixMode(GL_PROJECTION);
  112. glLoadIdentity();
  113. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  114. //set up a directional ligth arguments are light number (0-3), light color,
  115. //and an x,y,z vector that points in the direction of the light
  116. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  117. glColor3f(1,1,1);
  118. glMatrixMode(GL_MODELVIEW);
  119. //need to set up some material properties since DS does not have them set by default
  120. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  121. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  122. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  123. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  124. //ds uses a table for shinyness..this generates a half-ass one
  125. glMaterialShinyness();
  126. // Set the current matrix to be the model matrix
  127. glMatrixMode(GL_MODELVIEW);
  128. while true do
  129. begin
  130. //these little button functions are pretty handy
  131. scanKeys();
  132. held := keysHeld();
  133. pressed := keysDown();
  134. if (held and KEY_R) <> 0 then z := z - 0.02;
  135. if (held and KEY_L) <> 0 then z := z + 0.02;
  136. if (held and KEY_LEFT) <> 0 then xspeed := xspeed - 0.01;
  137. if (held and KEY_RIGHT) <> 0 then xspeed := xspeed + 0.01;
  138. if (held and KEY_UP) <> 0 then yspeed := yspeed + 0.01;
  139. if (held and KEY_DOWN) <> 0 then yspeed := yspeed - 0.01;
  140. DrawGLScene();
  141. // flush to screen
  142. glFlush(0);
  143. // wait for the screen to refresh
  144. swiWaitForVBlank();
  145. if (pressed and KEY_START) then break;
  146. end;
  147. end.