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