lesson4.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. program lesson4;
  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. view: Mtx;
  15. perspective: Mtx44;
  16. model, modelview: Mtx;
  17. rtri: cfloat = 0.0;
  18. rquad: cfloat = 0.0;
  19. fb: cuint32 = 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. w, h: f32;
  26. Yaxis: guVector = (x: 0; y: 1; z: 0;);
  27. Xaxis: guVector = (x: 1; y: 0; z: 0;);
  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. VIDEO_Configure(rmode);
  37. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  38. VIDEO_SetBlack(FALSE);
  39. VIDEO_Flush();
  40. VIDEO_WaitVSync();
  41. if (rmode^.viTVMode and VI_NON_INTERLACE) <> 0 then
  42. VIDEO_WaitVSync();
  43. // setup the fifo and then init the flipper
  44. gp_fifo := memalign(32, DEFAULT_FIFO_SIZE);
  45. memset(gp_fifo, 0, DEFAULT_FIFO_SIZE);
  46. GX_Init(gp_fifo, DEFAULT_FIFO_SIZE);
  47. // clears the bg to color and clears the z buffer
  48. GX_SetCopyClear(background, $00ffffff);
  49. // other gx setup
  50. GX_SetViewport(0, 0, rmode^.fbWidth, rmode^.efbHeight, 0, 1);
  51. yscale := GX_GetYScaleFactor(rmode^.efbHeight, rmode^.xfbHeight);
  52. xfbHeight := GX_SetDispCopyYScale(yscale);
  53. GX_SetScissor(0, 0, rmode^.fbWidth, rmode^.efbHeight);
  54. GX_SetDispCopySrc(0, 0, rmode^.fbWidth, rmode^.efbHeight);
  55. GX_SetDispCopyDst(rmode^.fbWidth, xfbHeight);
  56. GX_SetCopyFilter(rmode^.aa, rmode^.sample_pattern, GX_TRUE, rmode^.vfilter);
  57. if rmode^.viHeight = 2*rmode^.xfbHeight then
  58. GX_SetFieldMode(rmode^.field_rendering, GX_ENABLE)
  59. else
  60. GX_SetFieldMode(rmode^.field_rendering, GX_DISABLE);
  61. GX_SetCullMode(GX_CULL_NONE);
  62. GX_CopyDisp(frameBuffer[fb], GX_TRUE);
  63. GX_SetDispCopyGamma(GX_GM_1_0);
  64. // setup the vertex descriptor
  65. // tells the flipper to expect direct data
  66. GX_ClearVtxDesc();
  67. GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  68. GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  69. // setup the vertex attribute table
  70. // describes the data
  71. // args: vat location 0-7, type of data, data format, size, scale
  72. // so for ex. in the first call we are sending position data with
  73. // 3 values X,Y,Z of size F32. scale sets the number of fractional
  74. // bits for non float data.
  75. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  76. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
  77. GX_SetNumChans(1);
  78. GX_SetNumTexGens(0);
  79. GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0);
  80. GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
  81. // setup our camera at the origin
  82. // looking down the -z axis with y up
  83. guLookAt(view, @cam, @up, @look);
  84. // setup our projection matrix
  85. // this creates a perspective matrix with a view angle of 90,
  86. // and aspect ratio based on the display resolution
  87. w := rmode^.viWidth;
  88. h := rmode^.viHeight;
  89. guPerspective(perspective, 45, f32(w / h), 0.1, 300.0);
  90. GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
  91. while true do
  92. begin
  93. WPAD_ScanPads();
  94. if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
  95. exit;
  96. // do this before drawing
  97. GX_SetViewport(0, 0, rmode^.fbWidth, rmode^.efbHeight, 0, 1);
  98. guMtxIdentity(model);
  99. guMtxRotAxisDeg(model, @Yaxis, rtri);
  100. guMtxTransApply(model, model, -1.5, 0.0, -6.0);
  101. guMtxConcat(view, model, modelview);
  102. // load the modelview matrix into matrix memory
  103. GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  104. GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3);
  105. GX_Position3f32( 0.0, 1.0, 0.0); // Top
  106. GX_Color3f32( 1.0, 0.0, 0.0); // Set The Color To Red
  107. GX_Position3f32(-1.0,-1.0, 0.0); // Bottom Left
  108. GX_Color3f32( 0.0, 1.0, 0.0); // Set The Color To Green
  109. GX_Position3f32( 1.0,-1.0, 0.0); // Bottom Right
  110. GX_Color3f32( 0.0, 0.0, 1.0); // Set The Color To Blue
  111. GX_End();
  112. guMtxIdentity(model);
  113. guMtxRotAxisDeg(model, @Xaxis, rquad);
  114. guMtxTransApply(model, model, 1.5, 0.0, -6.0);
  115. guMtxConcat(view, model, modelview);
  116. // load the modelview matrix into matrix memory
  117. GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  118. GX_Begin(GX_QUADS, GX_VTXFMT0, 4); // Draw A Quad
  119. GX_Position3f32(-1.0, 1.0, 0.0); // Top Left
  120. GX_Color3f32( 0.5, 0.5, 1.0); // Set The Color To Blue
  121. GX_Position3f32( 1.0, 1.0, 0.0); // Top Right
  122. GX_Color3f32( 0.5, 0.5, 1.0); // Set The Color To Blue
  123. GX_Position3f32( 1.0,-1.0, 0.0); // Bottom Right
  124. GX_Color3f32( 0.5, 0.5, 1.0); // Set The Color To Blue
  125. GX_Position3f32(-1.0,-1.0, 0.0); // Bottom Left
  126. GX_Color3f32( 0.5, 0.5, 1.0); // Set The Color To Blue
  127. GX_End(); // Done Drawing The Quad
  128. // do this stuff after drawing
  129. GX_DrawDone();
  130. fb := fb xor 1; // flip framebuffer
  131. GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  132. GX_SetColorUpdate(GX_TRUE);
  133. GX_CopyDisp(frameBuffer[fb], GX_TRUE);
  134. VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  135. VIDEO_Flush();
  136. VIDEO_WaitVSync();
  137. rtri := rtri + 0.2; // Increase The Rotation Variable For The Triangle ( NEW )
  138. rquad := rquad - 0.15; // Decrease The Rotation Variable For The Quad ( NEW )
  139. end;
  140. end.