lesson06.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. (****************************************
  2. * NDS NeHe Lesson 06 *
  3. * Author: Dovoto *
  4. ****************************************)
  5. program Lesson06;
  6. {$L build/drunkenlogo.pcx.o}
  7. {$mode objfpc}
  8. uses
  9. ctypes, nds9;
  10. {$include inc/drunkenlogo.pcx.inc}
  11. var
  12. xrot: cfloat; // X Rotation ( NEW )
  13. yrot: cfloat; // Y Rotation ( NEW )
  14. zrot: cfloat; // Z Rotation ( NEW )
  15. texture: array [0..0] of integer; // Storage For One Texture ( NEW )
  16. function DrawGLScene(): boolean; // Here's Where We Do All The Drawing
  17. begin
  18. glLoadIdentity(); // Reset The View
  19. glTranslatef(0.0,0.0,-5.0);
  20. glRotatef(xrot,1.0,0.0,0.0);
  21. glRotatef(yrot,0.0,1.0,0.0);
  22. glRotatef(zrot,0.0,0.0,1.0);
  23. glBindTexture(GL_TEXTURE_2D, texture[0]);
  24. glBegin(GL_QUADS);
  25. // Front Face
  26. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  27. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  28. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  29. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
  30. // Back Face
  31. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  32. glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  33. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  34. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  35. // Top Face
  36. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  37. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0);
  38. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0);
  39. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  40. // Bottom Face
  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. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0);
  45. // Right face
  46. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  47. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
  48. glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
  49. glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
  50. // Left Face
  51. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  52. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.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. glEnd();
  56. xrot:=xrot+0.3;
  57. yrot:=yrot+0.2;
  58. zrot:=zrot+0.4;
  59. result := true;
  60. end;
  61. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  62. var
  63. pcx: sImage; //////////////(NEW) and different from nehe.
  64. begin
  65. //load our texture
  66. loadPCX(pcuint8(drunkenlogo_pcx), @pcx);
  67. image8to16(@pcx);
  68. glGenTextures(1, @texture[0]);
  69. glBindTexture(0, texture[0]);
  70. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  71. imageDestroy(@pcx);
  72. result := true;
  73. end;
  74. var
  75. keys: integer;
  76. begin
  77. // Setup the Main screen for 3D
  78. videoSetMode(MODE_0_3D);
  79. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  80. // initialize the geometry engine
  81. glInit();
  82. // enable textures
  83. glEnable(GL_TEXTURE_2D);
  84. // enable antialiasing
  85. glEnable(GL_ANTIALIAS);
  86. // setup the rear plane
  87. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  88. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  89. glClearDepth($7FFF);
  90. // Set our viewport to be the same size as the screen
  91. glViewport(0,0,255,191);
  92. LoadGLTextures();
  93. glMatrixMode(GL_PROJECTION);
  94. glLoadIdentity();
  95. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  96. // Set the current matrix to be the model matrix
  97. glMatrixMode(GL_MODELVIEW);
  98. //need to set up some material properties since DS does not have them set by default
  99. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  100. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  101. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  102. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  103. //ds uses a table for shinyness..this generates a half-ass one
  104. glMaterialShinyness();
  105. //ds specific, several attributes can be set here
  106. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_FORMAT_LIGHT0 or POLY_FORMAT_LIGHT1 or POLY_FORMAT_LIGHT2);
  107. glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
  108. glLight(1, RGB15(31,31,31) , 0, 0, floattov10(-1.0));
  109. glLight(2, RGB15(31,31,31) , 0, 0, floattov10(1.0));
  110. while true do
  111. begin
  112. glColor3f(1,1,1);
  113. DrawGLScene();
  114. // flush to screen
  115. glFlush(0);
  116. swiWaitForVBlank();
  117. scanKeys();
  118. keys := keysDown();
  119. if (keys and KEY_START) <> 0 then break;
  120. end;
  121. end.