main.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 main;
  8. {$L Star.pcx.o}
  9. {$apptype arm9} //...or arm7
  10. {$define ARM9} //...or arm7, according to apptype
  11. {$mode objfpc} // required for some libc funcs implementation
  12. uses
  13. ctypes, nds9; // required by nds headers!
  14. type
  15. TStars = record // Create A Structure For Star
  16. r, g, b: integer; // Stars Color
  17. dist: cfloat; // Stars Distance From Center
  18. angle: cfloat; // Stars Current Angle
  19. end;
  20. var
  21. Star_pcx_end: array [0..0] of u8; cvar; external;
  22. Star_pcx: array [0..0] of u8; cvar; external;
  23. Star_pcx_size: u32; cvar; external;
  24. twinkle: boolean; // Twinkling Stars
  25. tp: boolean; // 'T' Key Pressed?
  26. const
  27. num = 50; // Number Of Stars To Draw
  28. var
  29. star: array [0..num-1] of TStars; // Need To Keep Track Of 'num' Stars
  30. zoom: double = -15.0; // Distance Away From Stars
  31. tilt: double = 90.0; // Tilt The View
  32. spin: double; // Spin Stars
  33. loop: integer; // General Loop Variable
  34. texture: array [0..0] of integer; // Storage For One textures
  35. // Load PCX files And Convert To Textures
  36. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  37. var
  38. pcx: sImage; //////////////(NEW) and different from nehe.
  39. begin
  40. //load our texture
  41. loadPCX(pu8(Star_pcx), @pcx);
  42. image8to16(@pcx);
  43. glGenTextures(1, @texture[0]);
  44. glBindTexture(0, texture[0]);
  45. glTexImage2D(0, 0, GL_RGBA, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  46. imageDestroy(@pcx);
  47. result := TRUE;
  48. end;
  49. function DrawGLScene(): boolean; // Here's Where We Do All The Drawing
  50. var
  51. loop: integer;
  52. begin
  53. glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture
  54. for loop := 0 to num - 1 do // Loop Through All The Stars
  55. begin
  56. glLoadIdentity(); // Reset The View Before We Draw Each Star
  57. glTranslatef(0.0, 0.0, zoom); // Zoom Into The Screen (Using The Value In 'zoom')
  58. glRotatef(tilt, 1.0, 0.0, 0.0); // Tilt The View (Using The Value In 'tilt')
  59. glRotatef(star[loop].angle, 0.0, 1.0, 0.0); // Rotate To The Current Stars Angle
  60. glTranslatef(star[loop].dist, 0.0, 0.0); // Move Forward On The X Plane
  61. glRotatef(-star[loop].angle, 0.0, 1.0, 0.0); // Cancel The Current Stars Angle
  62. glRotatef(-tilt, 1.0, 0.0, 0.0); // Cancel The Screen Tilt
  63. if (twinkle) then
  64. begin
  65. glColor3b(star[(num-loop)-1].r,star[(num-loop)-1].g,star[(num-loop)-1].b); ///different
  66. glBegin(GL_QUADS);
  67. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 0.0);
  68. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 0.0);
  69. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
  70. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
  71. glEnd();
  72. end;
  73. glRotatef(spin, 0.0, 0.0, 1.0);
  74. glColor3b(star[loop].r,star[loop].g,star[loop].b); //different
  75. glBegin(GL_QUADS);
  76. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 0.0);
  77. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 0.0);
  78. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
  79. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
  80. glEnd();
  81. spin := spin + 0.01;
  82. star[loop].angle := star[loop].angle + (loop / num);
  83. star[loop].dist := star[loop].dist - 0.01;
  84. if (star[loop].dist < 0.0) then
  85. begin
  86. star[loop].dist := star[loop].dist + 5.0;
  87. star[loop].r :=rand(256);
  88. star[loop].g :=rand(256);
  89. star[loop].b :=rand(256);
  90. end;
  91. end;
  92. result := TRUE; // Keep Going
  93. end;
  94. begin
  95. // Turn on everything
  96. powerON(POWER_ALL);
  97. // Setup the Main screen for 3D
  98. videoSetMode(MODE_0_3D);
  99. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  100. // IRQ basic setup
  101. irqInit();
  102. irqSet(IRQ_VBLANK, nil);
  103. // initialize the geometry engine
  104. glInit();
  105. // enable antialiasing
  106. glEnable(GL_ANTIALIAS);
  107. // setup the rear plane
  108. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  109. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  110. glClearDepth($7FFF);
  111. // enable textures
  112. glEnable(GL_TEXTURE_2D);
  113. // enable alpha blending
  114. glEnable(GL_BLEND);
  115. // Set our viewport to be the same size as the screen
  116. glViewport(0,0,255,191);
  117. LoadGLTextures();
  118. glMatrixMode(GL_PROJECTION);
  119. glLoadIdentity();
  120. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  121. glColor3f(1,1,1);
  122. //set up a directional ligth arguments are light number (0-3), light color,
  123. //and an x,y,z vector that points in the direction of the light
  124. glLight(0, RGB15(31,31,31) , 0, 0,floattov10(-1.0));
  125. //need to set up some material properties since DS does not have them set by default
  126. glMaterialf(GL_AMBIENT, RGB15(16,16,16));
  127. glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
  128. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(8,8,8));
  129. glMaterialf(GL_EMISSION, RGB15(16,16,16));
  130. //ds uses a table for shinyness..this generates a half-ass one
  131. glMaterialShinyness();
  132. glPolyFmt(POLY_ALPHA(15) or POLY_CULL_BACK or POLY_FORMAT_LIGHT0);
  133. glMatrixMode(GL_MODELVIEW);
  134. while true do
  135. begin
  136. DrawGLScene();
  137. // flush to screen
  138. glFlush(0);
  139. // wait for the screen to refresh
  140. swiWaitForVBlank();
  141. end;
  142. end.