lesson6.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. program lesson6;
  2. {$J+}
  3. {$macro on}
  4. {$mode objfpc}
  5. uses
  6. cmem, ctypes, gccore;
  7. {$include inc/NeHe.tpl.inc}
  8. {$link build/NeHe.tpl.o}
  9. { from NeHe.h} const nehe = 0;
  10. const
  11. DEFAULT_FIFO_SIZE = (256 * 1024);
  12. var
  13. frameBuffer: array [0..1] of pcuint32 = (nil, nil);
  14. rmode: PGXRModeObj = nil;
  15. yscale: f32;
  16. xfbHeight: cuint32;
  17. fb: cuint32 = 0;
  18. rquad: f32 = 0.0;
  19. view, model, modelview: Mtx;
  20. perspective: Mtx44;
  21. first_frame: cuint32 = 1;
  22. texture: GXTexObj;
  23. gpfifo: pointer = nil;
  24. background: GXColor = (r:0; g:0; b:0; a:$ff;);
  25. cam : guVector = (x: 0.0; y: 0.0; z: 0.0;);
  26. up : guVector = (x: 0.0; y: 1.0; z: 0.0;);
  27. look : guVector = (x: 0.0; y: 0.0; z:-1.0;);
  28. cubeAxis: guVector = (x: 1.0; y: 1.0; z: 1.0;);
  29. neheTPL: TPLFile;
  30. w, h: f32;
  31. begin
  32. VIDEO_Init();
  33. WPAD_Init();
  34. rmode := VIDEO_GetPreferredMode(nil);
  35. // allocate 2 framebuffers for double buffering
  36. frameBuffer[0] := SYS_AllocateFramebuffer(rmode);
  37. frameBuffer[1] := SYS_AllocateFramebuffer(rmode);
  38. VIDEO_Configure(rmode);
  39. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  40. VIDEO_Flush();
  41. VIDEO_WaitVSync();
  42. if (rmode^.viTVMode and VI_NON_INTERLACE) <> 0 then
  43. VIDEO_WaitVSync();
  44. // allocate the fifo buffer
  45. gpfifo := memalign(32, DEFAULT_FIFO_SIZE);
  46. memset(gpfifo, 0, DEFAULT_FIFO_SIZE);
  47. fb := fb xor 1;
  48. // init the flipper
  49. GX_Init(gpfifo, DEFAULT_FIFO_SIZE);
  50. // clears the bg to color and clears the z buffer
  51. GX_SetCopyClear(background, $00ffffff);
  52. // other gx setup
  53. GX_SetViewport(0, 0, rmode^.fbWidth, rmode^.efbHeight, 0, 1);
  54. yscale := GX_GetYScaleFactor(rmode^.efbHeight, rmode^.xfbHeight);
  55. xfbHeight := GX_SetDispCopyYScale(yscale);
  56. GX_SetScissor(0, 0, rmode^.fbWidth, rmode^.efbHeight);
  57. GX_SetDispCopySrc(0, 0, rmode^.fbWidth, rmode^.efbHeight);
  58. GX_SetDispCopyDst(rmode^.fbWidth, xfbHeight);
  59. GX_SetCopyFilter(rmode^.aa, rmode^.sample_pattern, GX_TRUE, rmode^.vfilter);
  60. if rmode^.viHeight = 2 * rmode^.xfbHeight then
  61. GX_SetFieldMode(rmode^.field_rendering, GX_ENABLE)
  62. else
  63. GX_SetFieldMode(rmode^.field_rendering, GX_DISABLE);
  64. if (rmode^.aa) <> 0 then
  65. GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR)
  66. else
  67. GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
  68. GX_SetCullMode(GX_CULL_NONE);
  69. GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  70. GX_SetDispCopyGamma(GX_GM_1_0);
  71. // setup the vertex attribute table
  72. // describes the data
  73. // args: vat location 0-7, type of data, data format, size, scale
  74. // so for ex. in the first call we are sending position data with
  75. // 3 values X,Y,Z of size F32. scale sets the number of fractional
  76. // bits for non float data.
  77. GX_ClearVtxDesc();
  78. GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  79. GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  80. GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
  81. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  82. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
  83. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
  84. // set number of rasterized color channels
  85. GX_SetNumChans(1);
  86. //set number of textures to generate
  87. GX_SetNumTexGens(1);
  88. // setup texture coordinate generation
  89. // args: texcoord slot 0-7, matrix type, source to generate texture coordinates from, matrix to use
  90. GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
  91. GX_InvVtxCache();
  92. GX_InvalidateTexAll();
  93. TPL_OpenTPLFromMemory(@neheTPL, @NeHe_tpl[0], NeHe_tpl_size);
  94. TPL_GetTexture(@neheTPL, nehe, @texture);
  95. // setup our camera at the origin
  96. // looking down the -z axis with y up
  97. guLookAt(view, @cam, @up, @look);
  98. // setup our projection matrix
  99. // this creates a perspective matrix with a view angle of 90,
  100. // and aspect ratio based on the display resolution
  101. w := rmode^.viWidth;
  102. h := rmode^.viHeight;
  103. guPerspective(perspective, 45, f32(w / h), 0.1, 300.0);
  104. GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
  105. while true do
  106. begin
  107. WPAD_ScanPads();
  108. if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
  109. exit;
  110. GX_SetTevOp(GX_TEVSTAGE0,GX_REPLACE);
  111. GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
  112. GX_LoadTexObj(@texture, GX_TEXMAP0);
  113. guMtxIdentity(model);
  114. guMtxRotAxisDeg(model, @cubeAxis, rquad);
  115. guMtxTransApply(model, model, 1.5, 0.0, -7.0);
  116. guMtxConcat(view, model, modelview);
  117. // load the modelview matrix into matrix memory
  118. GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  119. GX_Begin(GX_QUADS, GX_VTXFMT0, 24); // Draw a Cube
  120. GX_Position3f32(-1.0, 1.0,-1.0); // Top Left of the quad (top)
  121. GX_Color3f32( 0.0, 1.0, 0.0); // Set The Color To Green
  122. GX_TexCoord2f32( 0.0, 0.0);
  123. GX_Position3f32(-1.0, 1.0, 1.0); // Top Right of the quad (top)
  124. GX_Color3f32( 0.0, 1.0, 0.0); // Set The Color To Green
  125. GX_TexCoord2f32( 1.0, 0.0);
  126. GX_Position3f32(-1.0,-1.0, 1.0); // Bottom Right of the quad (top)
  127. GX_Color3f32( 0.0, 1.0, 0.0); // Set The Color To Green
  128. GX_TexCoord2f32( 1.0, 1.0);
  129. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Left of the quad (top)
  130. GX_Color3f32( 0.0, 1.0, 0.0); // Set The Color To Green
  131. GX_TexCoord2f32( 0.0, 1.0);
  132. GX_Position3f32( 1.0, 1.0,-1.0); // Top Left of the quad (bottom)
  133. GX_Color3f32( 1.0, 0.5, 0.0); // Set The Color To Orange
  134. GX_TexCoord2f32( 0.0, 0.0);
  135. GX_Position3f32( 1.0,-1.0,-1.0); // Top Right of the quad (bottom)
  136. GX_Color3f32( 1.0, 0.5, 0.0); // Set The Color To Orange
  137. GX_TexCoord2f32( 1.0, 0.0);
  138. GX_Position3f32( 1.0,-1.0, 1.0); // Bottom Right of the quad (bottom)
  139. GX_Color3f32( 1.0, 0.5, 0.0); // Set The Color To Orange
  140. GX_TexCoord2f32( 1.0, 1.0);
  141. GX_Position3f32( 1.0, 1.0, 1.0); // Bottom Left of the quad (bottom)
  142. GX_Color3f32( 1.0, 0.5, 0.0); // Set The Color To Orange
  143. GX_TexCoord2f32( 0.0, 1.0);
  144. GX_Position3f32(-1.0,-1.0, 1.0); // Top Right Of The Quad (Front)
  145. GX_Color3f32( 1.0, 0.0, 0.0); // Set The Color To Red
  146. GX_TexCoord2f32( 0.0, 0.0);
  147. GX_Position3f32( 1.0,-1.0, 1.0); // Top Left Of The Quad (Front)
  148. GX_Color3f32( 1.0, 0.0, 0.0); // Set The Color To Red
  149. GX_TexCoord2f32( 1.0, 0.0);
  150. GX_Position3f32( 1.0,-1.0,-1.0); // Bottom Left Of The Quad (Front)
  151. GX_Color3f32( 1.0, 0.0, 0.0); // Set The Color To Red
  152. GX_TexCoord2f32( 1.0, 1.0);
  153. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Right Of The Quad (Front)
  154. GX_Color3f32( 1.0, 0.0, 0.0); // Set The Color To Red
  155. GX_TexCoord2f32( 0.0, 1.0);
  156. GX_Position3f32(-1.0, 1.0, 1.0); // Bottom Left Of The Quad (Back)
  157. GX_Color3f32( 1.0, 1.0, 0.0); // Set The Color To Yellow
  158. GX_TexCoord2f32( 0.0, 0.0);
  159. GX_Position3f32(-1.0, 1.0,-1.0); // Bottom Right Of The Quad (Back)
  160. GX_Color3f32( 1.0, 1.0, 0.0); // Set The Color To Yellow
  161. GX_TexCoord2f32( 1.0, 0.0);
  162. GX_Position3f32( 1.0, 1.0,-1.0); // Top Right Of The Quad (Back)
  163. GX_Color3f32( 1.0, 1.0, 0.0); // Set The Color To Yellow
  164. GX_TexCoord2f32( 1.0, 1.0);
  165. GX_Position3f32( 1.0, 1.0, 1.0); // Top Left Of The Quad (Back)
  166. GX_Color3f32( 1.0, 1.0, 0.0); // Set The Color To Yellow
  167. GX_TexCoord2f32( 0.0, 1.0);
  168. GX_Position3f32( 1.0,-1.0,-1.0); // Top Right Of The Quad (Left)
  169. GX_Color3f32( 0.0, 0.0, 1.0); // Set The Color To Blue
  170. GX_TexCoord2f32( 0.0, 0.0);
  171. GX_Position3f32( 1.0, 1.0,-1.0); // Top Left Of The Quad (Left)
  172. GX_Color3f32( 0.0, 0.0, 1.0); // Set The Color To Blue
  173. GX_TexCoord2f32( 1.0, 0.0);
  174. GX_Position3f32(-1.0, 1.0,-1.0); // Bottom Left Of The Quad (Left)
  175. GX_Color3f32( 0.0, 0.0, 1.0); // Set The Color To Blue
  176. GX_TexCoord2f32( 1.0, 1.0);
  177. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Right Of The Quad (Left)
  178. GX_Color3f32( 0.0, 0.0, 1.0); // Set The Color To Blue
  179. GX_TexCoord2f32( 0.0, 1.0);
  180. GX_Position3f32( 1.0,-1.0, 1.0); // Top Right Of The Quad (Right)
  181. GX_Color3f32( 1.0, 0.0, 1.0); // Set The Color To Violet
  182. GX_TexCoord2f32( 0.0, 0.0);
  183. GX_Position3f32(-1.0,-1.0, 1.0); // Top Left Of The Quad (Right)
  184. GX_Color3f32( 1.0, 0.0, 1.0); // Set The Color To Violet
  185. GX_TexCoord2f32( 1.0, 0.0);
  186. GX_Position3f32(-1.0, 1.0, 1.0); // Bottom Left Of The Quad (Right)
  187. GX_Color3f32( 1.0, 0.0, 1.0); // Set The Color To Violet
  188. GX_TexCoord2f32( 1.0, 1.0);
  189. GX_Position3f32( 1.0, 1.0, 1.0); // Bottom Right Of The Quad (Right)
  190. GX_Color3f32( 1.0, 0.0, 1.0); // Set The Color To Violet
  191. GX_TexCoord2f32( 0.0, 1.0);
  192. GX_End(); // Done Drawing The Quad
  193. GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  194. GX_SetColorUpdate(GX_TRUE);
  195. GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  196. GX_DrawDone();
  197. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  198. if (first_frame) <> 0 then
  199. begin
  200. first_frame := 0;
  201. VIDEO_SetBlack(FALSE);
  202. end;
  203. VIDEO_Flush();
  204. VIDEO_WaitVSync();
  205. fb := fb xor 1;
  206. rquad := rquad - 0.15; // Decrease The Rotation Variable For The Quad ( NEW )
  207. end;
  208. end.