main.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (****************************************
  2. * NDS NeHe Lesson 11 *
  3. * Author: Dovoto *
  4. ****************************************)
  5. program main;
  6. {$L drunkenlogo.pcx.o}
  7. {$apptype arm9}
  8. {$define ARM9}
  9. {$mode objfpc}
  10. uses
  11. ctypes, nds9;
  12. var
  13. drunkenlogo_pcx_end: array [0..0] of u8; cvar; external;
  14. drunkenlogo_pcx: array [0..0] of u8; cvar; external;
  15. drunkenlogo_pcx_size: u32; cvar; external;
  16. points: array [0..63, 0..31, 0..2] of v16; // The Array For The Points On The Grid Of Our "Wave"
  17. wiggle_count: integer = 0; // Counter Used To Control How Fast Flag Waves
  18. xrot: GLfloat; // X Rotation ( NEW )
  19. yrot: GLfloat; // Y Rotation ( NEW )
  20. zrot: GLfloat; // Z Rotation ( NEW )
  21. hold: v16; // Temporarily Holds A Floating Point Value
  22. texture: array [0..0] of integer; // Storage For 3 Textures (only going to use 1 on the DS for this demo)
  23. function DrawGLScene(): boolean; // Here's Where We Do All The Drawing
  24. var
  25. x, y: integer;
  26. float_x, float_y, float_xb, float_yb: t16;
  27. begin
  28. glColor3b(255,255,255); // set the vertex color
  29. glLoadIdentity(); // Reset The View
  30. glTranslatef(0.0,0.0,-12.0);
  31. glRotatef(xrot,1.0,0.0,0.0);
  32. glRotatef(yrot,0.0,1.0,0.0);
  33. glRotatef(zrot,0.0,0.0,1.0);
  34. glBindTexture(GL_TEXTURE_2D, texture[0]);
  35. glBegin(GL_QUADS);
  36. for x := 0 to 30 do
  37. begin
  38. for y := 0 to 30 do
  39. begin
  40. float_x := inttot16(x) shl 2;
  41. float_y := inttot16(y) shl 2;
  42. float_xb := inttot16(x+1) shl 2;
  43. float_yb := inttot16(y+1) shl 2;
  44. glTexCoord2t16( float_x, float_y);
  45. glVertex3v16( points[x][y][0], points[x][y][1], points[x][y][2] );
  46. glTexCoord2t16( float_x, float_yb );
  47. glVertex3v16( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
  48. glTexCoord2t16( float_xb, float_yb );
  49. glVertex3v16( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
  50. glTexCoord2t16( float_xb, float_y );
  51. glVertex3v16( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
  52. end;
  53. end;
  54. glEnd();
  55. if ( wiggle_count = 2 ) then
  56. begin
  57. for y := 0 to 31 do
  58. begin
  59. hold := points[0][y][2];
  60. for x := 0 to 31 do
  61. begin
  62. points[x][y][2] := points[x+1][y][2];
  63. end;
  64. points[31][y][2]:=hold;
  65. end;
  66. wiggle_count := 0;
  67. end;
  68. inc(wiggle_count);
  69. xrot:=xrot+0.3;
  70. yrot:=yrot+0.2;
  71. zrot:=zrot+0.4;
  72. drawGLScene := TRUE; // Everything Went OK
  73. end;
  74. function LoadGLTextures(): boolean; // Load PCX files And Convert To Textures
  75. var
  76. pcx: sImage; //////////////(NEW) and different from nehe.
  77. begin
  78. //load our texture
  79. loadPCX(pu8(drunkenlogo_pcx), @pcx);
  80. image8to16(@pcx);
  81. glGenTextures(1, @texture[0]);
  82. glBindTexture(0, texture[0]);
  83. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);
  84. //imageDestroy(@pcx);
  85. result := TRUE;
  86. end;
  87. procedure InitGL();
  88. var
  89. x, y:integer;
  90. begin
  91. // Turn on everything
  92. powerON(POWER_ALL);
  93. // Setup the Main screen for 3D
  94. videoSetMode(MODE_0_3D);
  95. vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures
  96. // IRQ basic setup
  97. irqInit();
  98. irqSet(IRQ_VBLANK, nil);
  99. // initialize the geometry engine
  100. glInit();
  101. // enable textures
  102. glEnable(GL_TEXTURE_2D);
  103. // Set our viewport to be the same size as the screen
  104. glViewPort(0,0,255,191);
  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. LoadGLTextures();
  112. glMatrixMode(GL_PROJECTION);
  113. glLoadIdentity();
  114. gluPerspective(35, 256.0 / 192.0, 0.1, 100);
  115. //need to set up some material properties since DS does not have them set by default
  116. glMaterialf(GL_AMBIENT, RGB15(31,31,31));
  117. glMaterialf(GL_DIFFUSE, RGB15(31,31,31));
  118. glMaterialf(GL_SPECULAR, BIT(15) or RGB15(16,16,16));
  119. glMaterialf(GL_EMISSION, RGB15(31,31,31));
  120. //ds uses a table for shinyness..this generates a half-ass one
  121. glMaterialShinyness();
  122. //ds specific, several attributes can be set here
  123. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE );
  124. for x:=0 to 31 do
  125. begin
  126. for y:=0 to 31 do
  127. begin
  128. points[x][y][0] := (inttov16(x) div 4);
  129. points[x][y][1] := (inttov16(y) div 4);
  130. points[x][y][2] := SIN_bin[ (x * 16) and LUT_MASK];
  131. end;
  132. end;
  133. end;
  134. begin
  135. InitGL();
  136. glMatrixMode(GL_MODELVIEW);
  137. while true do
  138. begin
  139. DrawGLScene();
  140. // flush to screen
  141. glFlush(0);
  142. // wait for the screen to refresh
  143. swiWaitForVBlank();
  144. end;
  145. end.