triangle.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. program triangle;
  2. {$J+}
  3. {$macro on}
  4. {$mode objfpc}
  5. {$inline on}
  6. uses
  7. cmem, ctypes, gctypes, gccore;
  8. var
  9. framebuffer: pcuint32;
  10. screenMode: PGXRModeObj = nil;
  11. readyForCopy: cuint8;
  12. const
  13. FIFO_SIZE = (256*1024);
  14. var
  15. vertices: array[0..2, 0..2] of cint16 =((0, 15, 0),(-15, -15, 0),(15, -15, 0));
  16. colors: array[0..2, 0..3] of cuint8 = ((255, 0, 0, 255),(0, 255, 0, 255),(0, 0, 255, 255));
  17. procedure update_screen(viewMatrix: Mtx);
  18. var
  19. modelView: Mtx;
  20. begin
  21. guMtxIdentity(modelView);
  22. guMtxTransApply(modelView, modelView, 0.0, 0.0, -50.0);
  23. guMtxConcat(viewMatrix,modelView,modelView);
  24. GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  25. GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3);
  26. GX_Position1x8(0);
  27. GX_Color1x8(0);
  28. GX_Position1x8(1);
  29. GX_Color1x8(1);
  30. GX_Position1x8(2);
  31. GX_Color1x8(2);
  32. GX_End();
  33. GX_DrawDone();
  34. readyForCopy := GX_TRUE;
  35. VIDEO_WaitVSync();
  36. end;
  37. procedure copy_buffers(count: cuint32); cdecl;
  38. begin
  39. if (readyForCopy = GX_TRUE) then
  40. begin
  41. GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  42. GX_SetColorUpdate(GX_TRUE);
  43. GX_CopyDisp(frameBuffer,GX_TRUE);
  44. GX_Flush();
  45. readyForCopy := GX_FALSE;
  46. end;
  47. end;
  48. var
  49. view: Mtx;
  50. projection: Mtx44;
  51. backgroundColor: GXColor = (r:0; g:0; b:0; a:255;);
  52. fifoBuffer: pcuint32 = nil;
  53. camera, up, look: guVector;
  54. begin
  55. VIDEO_Init();
  56. WPAD_Init();
  57. screenMode := VIDEO_GetPreferredMode(nil);
  58. frameBuffer := MEM_K0_TO_K1(integer(SYS_AllocateFramebuffer(screenMode)));
  59. VIDEO_Configure(screenMode);
  60. VIDEO_SetNextFramebuffer(frameBuffer);
  61. VIDEO_SetPostRetraceCallback(@copy_buffers);
  62. VIDEO_SetBlack(FALSE);
  63. VIDEO_Flush();
  64. fifoBuffer := MEM_K0_TO_K1(integer(memalign(32,FIFO_SIZE)));
  65. memset(fifoBuffer, 0, FIFO_SIZE);
  66. GX_Init(fifoBuffer, FIFO_SIZE);
  67. GX_SetCopyClear(backgroundColor, $00ffffff);
  68. GX_SetViewport(0,0,screenMode^.fbWidth,screenMode^.efbHeight,0,1);
  69. GX_SetDispCopyYScale(f32(screenMode^.xfbHeight) / f32(screenMode^.efbHeight));
  70. GX_SetScissor(0,0,screenMode^.fbWidth,screenMode^.efbHeight);
  71. GX_SetDispCopySrc(0,0,screenMode^.fbWidth,screenMode^.efbHeight);
  72. GX_SetDispCopyDst(screenMode^.fbWidth,screenMode^.xfbHeight);
  73. GX_SetCopyFilter(screenMode^.aa, screenMode^.sample_pattern,
  74. GX_TRUE, screenMode^.vfilter);
  75. if screenMode^.viHeight = 2*screenMode^.xfbHeight then
  76. GX_SetFieldMode(screenMode^.field_rendering,GX_ENABLE)
  77. else
  78. GX_SetFieldMode(screenMode^.field_rendering,GX_DISABLE);
  79. GX_SetCullMode(GX_CULL_NONE);
  80. GX_CopyDisp(frameBuffer,GX_TRUE);
  81. GX_SetDispCopyGamma(GX_GM_1_0);
  82. camera.x := 0.0;
  83. camera.y := 0.0;
  84. camera.z := 0.0;
  85. up.x := 0.0;
  86. up.y := 1.0;
  87. up.z := 0.0;
  88. look.x := 0.0;
  89. look.y := 0.0;
  90. look.z := -1.0;
  91. guPerspective(projection, 60, 1.33, 10.0, 300.0);
  92. GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
  93. GX_ClearVtxDesc();
  94. GX_SetVtxDesc(GX_VA_POS, GX_INDEX8);
  95. GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8);
  96. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
  97. GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
  98. GX_SetArray(GX_VA_POS, @vertices, 3*sizeof(cint16));
  99. GX_SetArray(GX_VA_CLR0, @colors, 4*sizeof(cuint8));
  100. GX_SetNumChans(1);
  101. GX_SetNumTexGens(0);
  102. GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0);
  103. GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
  104. while true do
  105. begin
  106. guLookAt(view, @camera, @up, @look);
  107. GX_SetViewport(0,0,screenMode^.fbWidth,screenMode^.efbHeight,0,1);
  108. GX_InvVtxCache();
  109. GX_InvalidateTexAll();
  110. update_screen(view);
  111. WPAD_ScanPads();
  112. if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
  113. exit;
  114. end;
  115. end.