lesson5.pp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. program lesson5;
  2. {$J+}
  3. {$macro on}
  4. {$mode objfpc}
  5. uses
  6. cmem, ctypes, gccore;
  7. const
  8. DEFAULT_FIFO_SIZE = (256 * 1024);
  9. var
  10. frameBuffer: array [0..1] of pcuint32 = (nil, nil);
  11. rmode: PGXRModeObj = nil;
  12. yscale: f32;
  13. xfbHeight: cuint32;
  14. fb: cuint32 = 0;
  15. view: Mtx;
  16. perspective: Mtx44;
  17. model, modelview: Mtx;
  18. rtri: cfloat = 0.0;
  19. rquad: cfloat = 0.0;
  20. background: GXColor = (r:0; g:0; b:0; a:$ff;);
  21. gp_fifo: pointer = nil;
  22. cam: guVector = (x: 0.0; y: 0.0; z: 0.0;);
  23. up: guVector = (x: 0.0; y: 1.0; z: 0.0;);
  24. look: guVector = (x: 0.0; y: 0.0; z:-1.0;);
  25. triAxis: guVector = (x: 0; y: 1; z: 0;);
  26. cubeAxis: guVector = (x: 1; y: 1; z: 1;);
  27. w, h: f32;
  28. begin
  29. // init the vi.
  30. VIDEO_Init();
  31. WPAD_Init();
  32. rmode := VIDEO_GetPreferredMode(nil);
  33. // allocate 2 framebuffers for double buffering
  34. // frameBuffer[0] := MEM_K0_TO_K1(integer(SYS_AllocateFramebuffer(rmode)));
  35. // frameBuffer[1] := MEM_K0_TO_K1(integer(SYS_AllocateFramebuffer(rmode)));
  36. frameBuffer[0] := SYS_AllocateFramebuffer(rmode);
  37. frameBuffer[1] := SYS_AllocateFramebuffer(rmode);
  38. VIDEO_Configure(rmode);
  39. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  40. VIDEO_SetBlack(FALSE);
  41. VIDEO_Flush();
  42. VIDEO_WaitVSync();
  43. if (rmode^.viTVMode and VI_NON_INTERLACE) <> 0 then
  44. VIDEO_WaitVSync();
  45. // setup the fifo and then init the flipper
  46. gp_fifo := memalign(32,DEFAULT_FIFO_SIZE);
  47. memset(gp_fifo,0,DEFAULT_FIFO_SIZE);
  48. GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
  49. // clears the bg to color and clears the z buffer
  50. GX_SetCopyClear(background, $00ffffff);
  51. // other gx setup
  52. GX_SetViewport(0,0,rmode^.fbWidth,rmode^.efbHeight,0,1);
  53. yscale := GX_GetYScaleFactor(rmode^.efbHeight,rmode^.xfbHeight);
  54. xfbHeight := GX_SetDispCopyYScale(yscale);
  55. GX_SetScissor(0,0,rmode^.fbWidth,rmode^.efbHeight);
  56. GX_SetDispCopySrc(0,0,rmode^.fbWidth,rmode^.efbHeight);
  57. GX_SetDispCopyDst(rmode^.fbWidth,xfbHeight);
  58. GX_SetCopyFilter(rmode^.aa,rmode^.sample_pattern,GX_TRUE,rmode^.vfilter);
  59. if rmode^.viHeight = 2*rmode^.xfbHeight then
  60. GX_SetFieldMode(rmode^.field_rendering, GX_ENABLE)
  61. else
  62. GX_SetFieldMode(rmode^.field_rendering, GX_DISABLE);
  63. GX_SetCullMode(GX_CULL_NONE);
  64. GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  65. GX_SetDispCopyGamma(GX_GM_1_0);
  66. // setup the vertex descriptor
  67. // tells the flipper to expect direct data
  68. GX_ClearVtxDesc();
  69. GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  70. GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  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_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  78. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
  79. GX_SetNumChans(1);
  80. GX_SetNumTexGens(0);
  81. GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0);
  82. GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
  83. // setup our camera at the origin
  84. // looking down the -z axis with y up
  85. guLookAt(view, @cam, @up, @look);
  86. // setup our projection matrix
  87. // this creates a perspective matrix with a view angle of 90,
  88. // and aspect ratio based on the display resolution
  89. w := rmode^.viWidth;
  90. h := rmode^.viHeight;
  91. guPerspective(perspective, 45, f32(w/h), 0.1, 300.0);
  92. GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
  93. while true do
  94. begin
  95. WPAD_ScanPads();
  96. if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
  97. exit;
  98. // do this before drawing
  99. GX_SetViewport(0,0,rmode^.fbWidth,rmode^.efbHeight,0,1);
  100. guMtxIdentity(model);
  101. guMtxRotAxisDeg(model, @triAxis, rtri);
  102. guMtxTransApply(model, model, -1.5,0.0,-6.0);
  103. guMtxConcat(view,model,modelview);
  104. // load the modelview matrix into matrix memory
  105. GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  106. GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 12); // Draw A Pyramid
  107. GX_Position3f32( 0.0, 1.0, 0.0); // Top of Triangle (front)
  108. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  109. GX_Position3f32(-1.0,-1.0, 1.0); // Left of Triangle (front)
  110. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  111. GX_Position3f32( 1.0,-1.0, 1.0); // Right of Triangle (front)
  112. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  113. GX_Position3f32( 0.0, 1.0, 0.0); // Top of Triangle (Right)
  114. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  115. GX_Position3f32( 1.0,-1.0, 1.0); // Left of Triangle (Right)
  116. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  117. GX_Position3f32( 1.0,-1.0,-1.0); // Right of Triangle (Right)
  118. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  119. GX_Position3f32( 0.0, 1.0, 0.0); // Top of Triangle (Back)
  120. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  121. GX_Position3f32(-1.0,-1.0,-1.0); // Left of Triangle (Back)
  122. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  123. GX_Position3f32( 1.0,-1.0,-1.0); // Right of Triangle (Back)
  124. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  125. GX_Position3f32( 0.0, 1.0, 0.0); // Top of Triangle (Left)
  126. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  127. GX_Position3f32(-1.0,-1.0,-1.0); // Left of Triangle (Left)
  128. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  129. GX_Position3f32(-1.0,-1.0, 1.0); // Right of Triangle (Left)
  130. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  131. GX_End();
  132. guMtxIdentity(model);
  133. guMtxRotAxisDeg(model, @cubeAxis, rquad);
  134. guMtxTransApply(model, model, 1.5,0.0,-7.0);
  135. guMtxConcat(view,model,modelview);
  136. // load the modelview matrix into matrix memory
  137. GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  138. GX_Begin(GX_QUADS, GX_VTXFMT0, 24); // Draw a Cube
  139. GX_Position3f32( 1.0, 1.0,-1.0); // Top Left of the quad (top)
  140. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  141. GX_Position3f32(-1.0, 1.0,-1.0); // Top Right of the quad (top)
  142. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  143. GX_Position3f32(-1.0, 1.0, 1.0); // Bottom Right of the quad (top)
  144. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  145. GX_Position3f32( 1.0, 1.0, 1.0); // Bottom Left of the quad (top)
  146. GX_Color3f32(0.0,1.0,0.0); // Set The Color To Green
  147. GX_Position3f32( 1.0,-1.0, 1.0); // Top Left of the quad (bottom)
  148. GX_Color3f32(1.0,0.5,0.0); // Set The Color To Orange
  149. GX_Position3f32(-1.0,-1.0, 1.0); // Top Right of the quad (bottom)
  150. GX_Color3f32(1.0,0.5,0.0); // Set The Color To Orange
  151. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Right of the quad (bottom)
  152. GX_Color3f32(1.0,0.5,0.0); // Set The Color To Orange
  153. GX_Position3f32( 1.0,-1.0,-1.0); // Bottom Left of the quad (bottom)
  154. GX_Color3f32(1.0,0.5,0.0); // Set The Color To Orange
  155. GX_Position3f32( 1.0, 1.0, 1.0); // Top Right Of The Quad (Front)
  156. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  157. GX_Position3f32(-1.0, 1.0, 1.0); // Top Left Of The Quad (Front)
  158. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  159. GX_Position3f32(-1.0,-1.0, 1.0); // Bottom Left Of The Quad (Front)
  160. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  161. GX_Position3f32( 1.0,-1.0, 1.0); // Bottom Right Of The Quad (Front)
  162. GX_Color3f32(1.0,0.0,0.0); // Set The Color To Red
  163. GX_Position3f32( 1.0,-1.0,-1.0); // Bottom Left Of The Quad (Back)
  164. GX_Color3f32(1.0,1.0,0.0); // Set The Color To Yellow
  165. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Right Of The Quad (Back)
  166. GX_Color3f32(1.0,1.0,0.0); // Set The Color To Yellow
  167. GX_Position3f32(-1.0, 1.0,-1.0); // Top Right Of The Quad (Back)
  168. GX_Color3f32(1.0,1.0,0.0); // Set The Color To Yellow
  169. GX_Position3f32( 1.0, 1.0,-1.0); // Top Left Of The Quad (Back)
  170. GX_Color3f32(1.0,1.0,0.0); // Set The Color To Yellow
  171. GX_Position3f32(-1.0, 1.0, 1.0); // Top Right Of The Quad (Left)
  172. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  173. GX_Position3f32(-1.0, 1.0,-1.0); // Top Left Of The Quad (Left)
  174. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  175. GX_Position3f32(-1.0,-1.0,-1.0); // Bottom Left Of The Quad (Left)
  176. GX_Color3f32(0.0,0.0,1.0); // Set The Color To Blue
  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_Position3f32( 1.0, 1.0,-1.0); // Top Right Of The Quad (Right)
  180. GX_Color3f32(1.0,0.0,1.0); // Set The Color To Violet
  181. GX_Position3f32( 1.0, 1.0, 1.0); // Top Left Of The Quad (Right)
  182. GX_Color3f32(1.0,0.0,1.0); // Set The Color To Violet
  183. GX_Position3f32( 1.0,-1.0, 1.0); // Bottom Left Of The Quad (Right)
  184. GX_Color3f32(1.0,0.0,1.0); // Set The Color To Violet
  185. GX_Position3f32( 1.0,-1.0,-1.0); // Bottom Right Of The Quad (Right)
  186. GX_Color3f32(1.0,0.0,1.0); // Set The Color To Violet
  187. GX_End(); // Done Drawing The Quad
  188. // do this stuff after drawing
  189. GX_DrawDone();
  190. fb := fb xor 1; // flip framebuffer
  191. GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  192. GX_SetColorUpdate(GX_TRUE);
  193. GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  194. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  195. VIDEO_Flush();
  196. VIDEO_WaitVSync();
  197. rtri := rtri + 0.2; // Increase The Rotation Variable For The Triangle ( NEW )
  198. rquad := rquad - 0.15; // Decrease The Rotation Variable For The Quad ( NEW )
  199. end;
  200. end.