lesson7.pp 9.1 KB

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