lesson09.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. (****************************************
  2. * NDS NeHe Lesson 09 *
  3. * Author: dovoto
  4. * DS does not appear to support
  5. the features needed for this demo
  6. ****************************************)
  7. program Lesson09;
  8. {$L build/star.pcx.o}
  9. {$mode objfpc}
  10. uses
  11. ctypes, nds9;
  12. type
  13. TStars = record // Create A Structure For Star
  14. r, g, b: cint; // Stars Color
  15. dist: cfloat; // Stars Distance From Center
  16. angle: cfloat; // Stars Current Angle
  17. end;
  18. {$include inc/star.pcx.inc}
  19. var
  20. twinkle: boolean; // Twinkling Stars
  21. tp: boolean; // 'T' Key Pressed?
  22. const
  23. num = 50; // Number Of Stars To Draw
  24. var
  25. star: array [0..num-1] of TStars; // Need To Keep Track Of 'num' Stars
  26. zoom: cfloat = -15.0; // Distance Away From Stars
  27. tilt: cfloat = 90.0; // Tilt The View
  28. spin: cfloat; // Spin Stars
  29. loop: integer; // General Loop Variable
  30. texture: array [0..0] of integer; // Storage For One textures
  31. // Load PCX files And Convert To Textures
  32. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  33. var
  34. pcx: sImage; //////////////(NEW) and different from nehe.
  35. begin
  36. //load our texture
  37. loadPCX(pcuint8(Star_pcx), @pcx);
  38. image8to16(@pcx);
  39. glGenTextures(1, @texture[0]);
  40. glBindTexture(0, texture[0]);
  41. glTexImage2D(0, 0, GL_RGBA, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  42. imageDestroy(@pcx);
  43. result := true;
  44. end;
  45. function DrawGLScene(): boolean; // Here's Where We Do All The Drawing
  46. var
  47. loop: integer;
  48. begin
  49. glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture
  50. for loop := 0 to num - 1 do // Loop Through All The Stars
  51. begin
  52. glLoadIdentity(); // Reset The View Before We Draw Each Star
  53. glTranslatef(0.0, 0.0, zoom); // Zoom Into The Screen (Using The Value In 'zoom')
  54. glRotatef(tilt, 1.0, 0.0, 0.0); // Tilt The View (Using The Value In 'tilt')
  55. glRotatef(star[loop].angle, 0.0, 1.0, 0.0); // Rotate To The Current Stars Angle
  56. glTranslatef(star[loop].dist, 0.0, 0.0); // Move Forward On The X Plane
  57. glRotatef(-star[loop].angle, 0.0, 1.0, 0.0); // Cancel The Current Stars Angle
  58. glRotatef(-tilt, 1.0, 0.0, 0.0); // Cancel The Screen Tilt
  59. if (twinkle) then
  60. begin
  61. glColor3b(star[(num-loop)-1].r,star[(num-loop)-1].g,star[(num-loop)-1].b); ///different
  62. glBegin(GL_QUADS);
  63. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 0.0);
  64. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 0.0);
  65. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
  66. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
  67. glEnd();
  68. end;
  69. glRotatef(spin, 0.0, 0.0, 1.0);
  70. glColor3b(star[loop].r,star[loop].g,star[loop].b); //different
  71. glBegin(GL_QUADS);
  72. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 0.0);
  73. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 0.0);
  74. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
  75. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
  76. glEnd();
  77. spin := spin + 0.01;
  78. star[loop].angle := star[loop].angle + cfloat(loop) / num;
  79. star[loop].dist := star[loop].dist - 0.01;
  80. if (star[loop].dist < 0.0) then
  81. begin
  82. star[loop].dist := star[loop].dist + 5.0;
  83. star[loop].r := random(256);
  84. star[loop].g := random(256);
  85. star[loop].b := random(256);
  86. end;
  87. end;
  88. result := true; // Keep Going
  89. end;
  90. begin
  91. Randomize;
  92. // Setup the Main screen for 3D
  93. videoSetMode(MODE_0_3D);
  94. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  95. // initialize the geometry engine
  96. glInit();
  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. // enable textures
  104. glEnable(GL_TEXTURE_2D);
  105. // enable alpha blending
  106. glEnable(GL_BLEND);
  107. // Set our viewport to be the same size as the screen
  108. glViewport(0,0,255,191);
  109. LoadGLTextures();
  110. glMatrixMode(GL_PROJECTION);
  111. glLoadIdentity();
  112. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  113. glColor3f(1,1,1);
  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, 0, floattov10(-1.0));
  117. //need to set up some material properties since DS does not have them set by default
  118. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  119. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  120. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  121. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  122. //ds uses a table for shinyness..this generates a half-ass one
  123. glMaterialShinyness();
  124. glPolyFmt(POLY_ALPHA(15) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  125. glMatrixMode(GL_MODELVIEW);
  126. while true do
  127. begin
  128. DrawGLScene();
  129. // flush to screen
  130. glFlush(0);
  131. // wait for the screen to refresh
  132. swiWaitForVBlank();
  133. end;
  134. end.