Advertisement
xerpi

wii gx index shit

Jul 15th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <gccore.h>
  7. #include <wiiuse/wpad.h>
  8. #include <debug.h>
  9. #include "Math_utils.h"
  10. #include "Camera.h"
  11. #include "MatrixStack.h"
  12. #include "Image.hpp"
  13. #include "stone_png.h"
  14.  
  15. #define PI 3.14159265359f
  16. #define DEG_TO_RAD(x) ((x)*PI)/180.0f
  17.  
  18. #define FIFO_SIZE (256 * 1024)
  19.  
  20. GXRModeObj *screenMode;
  21. void *framebuffer[2] = {NULL, NULL};
  22. int frame = 0;
  23. vu8 readyForcopy = GX_FALSE;
  24. void copy_buffers(u32 count);
  25. void *fifoBuffer = NULL;
  26.  
  27. WPADData *wiimoteData;
  28. joystick_t *nunchukJoystick;
  29. u32 wiimoteExpansion;
  30. float nunchuk_x = 0.0f, nunchuk_y = 0.0f;
  31. u32 pressed = 0, old_pressed = 0, held = 0;
  32. bool canFly = true;
  33.  
  34. Mtx44 projection;
  35. Mtx model, world, cameraMatrix, modelView;
  36. float pitch = 0.0f, yaw = 0.0f;
  37. guVector position = {0.0f, 0.0f, 0.0f};
  38. Camera camera(cameraMatrix, &position, &pitch, &yaw);
  39. MatrixStack matrices;
  40.  
  41. void init_screen();
  42. void read_controls();
  43. void drawCube();
  44. GXColor backgroundColor = {0xFF, 0xFF, 0xFF, 0xFF};
  45.  
  46.  
  47. #define CUBE_SIZE 0.5f
  48.  
  49. float vertexData[] ATTRIBUTE_ALIGN(32) = {
  50.     -CUBE_SIZE, -CUBE_SIZE,  CUBE_SIZE,
  51.     -CUBE_SIZE,  CUBE_SIZE,  CUBE_SIZE,
  52.      CUBE_SIZE, -CUBE_SIZE,  CUBE_SIZE,
  53.      CUBE_SIZE,  CUBE_SIZE,  CUBE_SIZE,
  54.      CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
  55.      CUBE_SIZE,  CUBE_SIZE, -CUBE_SIZE,
  56.     -CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
  57.     -CUBE_SIZE,  CUBE_SIZE, -CUBE_SIZE,
  58. };
  59.  
  60. float texCoordData[] ATTRIBUTE_ALIGN(32) = {
  61.      0.0f,  0.0f,
  62.      0.0f,  1.0f,
  63.      1.0f,  0.0f,
  64.      1.0f,  1.0f
  65. };
  66.  
  67.  
  68.  
  69. int main(void) {
  70.  
  71.     WPAD_Init();
  72.     init_screen();
  73.  
  74.     Image image((u8*)stone_png);
  75.  
  76.     GX_SetArray(GX_VA_POS, vertexData, 3*sizeof(float));
  77.     GX_SetArray(GX_VA_TEX0, texCoordData, 2*sizeof(float));
  78.  
  79.     while (1)
  80.     {
  81.         read_controls();
  82.  
  83.         #define ROT_SPEED 0.075f
  84.         #define MOV_SPEED 0.075f
  85.         if(held & WPAD_BUTTON_RIGHT) yaw-= ROT_SPEED;
  86.         if(held & WPAD_BUTTON_LEFT) yaw+= ROT_SPEED;
  87.         if(held & WPAD_BUTTON_UP) pitch+= ROT_SPEED;
  88.         if(held & WPAD_BUTTON_DOWN) pitch-= ROT_SPEED;
  89.         if(pressed & WPAD_BUTTON_1) canFly = not canFly;
  90.         if(wiimoteExpansion == WPAD_EXP_NUNCHUK) {
  91.             if(held & WPAD_NUNCHUK_BUTTON_C) position.y += MOV_SPEED;
  92.             if(held & WPAD_NUNCHUK_BUTTON_Z) position.y -= MOV_SPEED;
  93.             if(nunchukJoystick->mag > 0.15f) {
  94.                 guVector look;
  95.                 if(canFly)
  96.                     look = camera.getLookVector();
  97.                 else
  98.                     look = camera.getForwardVector();
  99.                 guVector right = camera.getRightVector();
  100.                 guVecScale(&look, &look, -nunchuk_x/7.5f);
  101.                 guVecScale(&right, &right, nunchuk_y/7.5f);
  102.                 guVecAdd(&position, &look, &position);
  103.                 guVecAdd(&position, &right, &position);
  104.             }
  105.         }
  106.  
  107.         camera.updateMatrix();
  108.  
  109.  
  110.         guMtxIdentity(world);
  111.         guMtxConcat(cameraMatrix, world, modelView);
  112.         GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  113.         GX_SetCurrentMtx(GX_PNMTX0);
  114.  
  115.         image.setGX(GX_TEXMAP0);
  116.  
  117.         guVector v = {2.0f, 0.0f, 0.0f};
  118.  
  119.         matrices.copyMatrix(cameraMatrix);
  120.         matrices.Push();
  121.         int i;
  122.         for(i = 0; i < 10; i++) {
  123.             matrices.Translate(v);
  124.             matrices.setMatrix();
  125.             drawCube();
  126.         }
  127.         matrices.Pop();
  128.  
  129.         GX_DrawDone();
  130.         readyForcopy = GX_TRUE;
  131.         VIDEO_WaitVSync();
  132.  
  133.         if (pressed & WPAD_BUTTON_HOME) break;
  134.         old_pressed = pressed;
  135.     }
  136.     return 0;
  137. }
  138.  
  139. void drawCube()
  140. {
  141.     GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 36);
  142.         GX_Position1x16(0);
  143.             GX_TexCoord1x16(0);
  144.         GX_Position1x16(1);
  145.             GX_TexCoord1x16(1);
  146.         GX_Position1x16(2);
  147.             GX_TexCoord1x16(2);
  148.         GX_Position1x16(1);
  149.             GX_TexCoord1x16(1);
  150.         GX_Position1x16(2);
  151.             GX_TexCoord1x16(2);
  152.         GX_Position1x16(3);
  153.             GX_TexCoord1x16(3);
  154.  
  155.         GX_Position1x16(2);
  156.             GX_TexCoord1x16(0);
  157.         GX_Position1x16(3);
  158.             GX_TexCoord1x16(1);
  159.         GX_Position1x16(4);
  160.             GX_TexCoord1x16(2);
  161.         GX_Position1x16(3);
  162.             GX_TexCoord1x16(1);
  163.         GX_Position1x16(4);
  164.             GX_TexCoord1x16(2);
  165.         GX_Position1x16(5);
  166.             GX_TexCoord1x16(3);
  167.  
  168.         GX_Position1x16(4);
  169.             GX_TexCoord1x16(0);
  170.         GX_Position1x16(5);
  171.             GX_TexCoord1x16(1);
  172.         GX_Position1x16(6);
  173.             GX_TexCoord1x16(2);
  174.         GX_Position1x16(5);
  175.             GX_TexCoord1x16(1);
  176.         GX_Position1x16(6);
  177.             GX_TexCoord1x16(2);
  178.         GX_Position1x16(7);
  179.             GX_TexCoord1x16(3);
  180.  
  181.         GX_Position1x16(6);
  182.             GX_TexCoord1x16(0);
  183.         GX_Position1x16(7);
  184.             GX_TexCoord1x16(1);
  185.         GX_Position1x16(0);
  186.             GX_TexCoord1x16(2);
  187.         GX_Position1x16(7);
  188.             GX_TexCoord1x16(1);
  189.         GX_Position1x16(0);
  190.             GX_TexCoord1x16(2);
  191.         GX_Position1x16(1);
  192.             GX_TexCoord1x16(3);
  193.  
  194.         GX_Position1x16(1);
  195.             GX_TexCoord1x16(0);
  196.         GX_Position1x16(7);
  197.             GX_TexCoord1x16(1);
  198.         GX_Position1x16(3);
  199.             GX_TexCoord1x16(2);
  200.         GX_Position1x16(7);
  201.             GX_TexCoord1x16(1);
  202.         GX_Position1x16(3);
  203.             GX_TexCoord1x16(2);
  204.         GX_Position1x16(5);
  205.             GX_TexCoord1x16(3);
  206.  
  207.         GX_Position1x16(6);
  208.             GX_TexCoord1x16(0);
  209.         GX_Position1x16(0);
  210.             GX_TexCoord1x16(1);
  211.         GX_Position1x16(4);
  212.             GX_TexCoord1x16(2);
  213.         GX_Position1x16(0);
  214.             GX_TexCoord1x16(1);
  215.         GX_Position1x16(4);
  216.             GX_TexCoord1x16(2);
  217.         GX_Position1x16(2);
  218.             GX_TexCoord1x16(3);
  219.  
  220.     GX_End();
  221. }
  222.  
  223. void read_controls()
  224. {
  225.     WPAD_ScanPads();
  226.     WPAD_Probe(0, &wiimoteExpansion);
  227.     wiimoteData = WPAD_Data(0);
  228.     nunchukJoystick = &wiimoteData->exp.nunchuk.js;
  229.     held = wiimoteData->btns_h;
  230.     pressed = wiimoteData->btns_d;
  231.     if(wiimoteExpansion == WPAD_EXP_NUNCHUK) {
  232.         float n_angle = DEG_TO_RAD(nunchukJoystick->ang);
  233.         nunchuk_x =  cosf(n_angle) * nunchukJoystick->mag;
  234.         nunchuk_y =  sinf(n_angle) * nunchukJoystick->mag;
  235.     }
  236. }
  237.  
  238.  
  239. void init_screen()
  240. {
  241.     VIDEO_Init();
  242.  
  243.     screenMode = VIDEO_GetPreferredMode(NULL);
  244.  
  245.     framebuffer[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
  246.     framebuffer[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
  247.  
  248.     VIDEO_Configure(screenMode);
  249.     VIDEO_SetNextFramebuffer(framebuffer[0]);
  250.     VIDEO_SetPostRetraceCallback(copy_buffers);
  251.     VIDEO_SetBlack(FALSE);
  252.     VIDEO_Flush();
  253.  
  254.     fifoBuffer = MEM_K0_TO_K1(memalign(32, FIFO_SIZE));
  255.     memset(fifoBuffer, 0x0, FIFO_SIZE);
  256.  
  257.     GX_Init(fifoBuffer, FIFO_SIZE);
  258.     GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
  259.     GX_SetCopyClear(backgroundColor, 0x00FFFFFF);
  260.  
  261.     GX_SetViewport(0, 0, screenMode->fbWidth, screenMode->efbHeight, 0.0f, 1.0f);
  262.     GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight);
  263.     GX_SetScissor(0, 0, screenMode->fbWidth, screenMode->efbHeight);
  264.     GX_SetDispCopySrc(0, 0, screenMode->fbWidth, screenMode->efbHeight);
  265.     GX_SetDispCopyDst(screenMode->fbWidth, screenMode->xfbHeight);
  266.     GX_SetCopyFilter(screenMode->aa, screenMode->sample_pattern, GX_TRUE, screenMode->vfilter);
  267.  
  268.     GX_SetFieldMode(screenMode->field_rendering,
  269.                     ((screenMode->viHeight == 2*screenMode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
  270.  
  271.  
  272.     GX_SetCullMode(GX_CULL_NONE);
  273.     GX_CopyDisp(framebuffer[frame], GX_TRUE);
  274.     GX_SetDispCopyGamma(GX_GM_1_0);
  275.  
  276.     GX_ClearVtxDesc();
  277.  
  278.     GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
  279.     GX_SetVtxDesc(GX_VA_CLR0, GX_NONE);
  280.     GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX16);
  281.  
  282.  
  283.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  284.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
  285.  
  286.     GX_SetNumChans(0);
  287.     GX_SetNumTexGens(1);
  288.  
  289.     GX_SetTevOp(GX_TEVSTAGE0, GX_MODULATE);
  290.     GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
  291.     GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
  292.  
  293.  
  294.     GX_InvVtxCache();
  295.     GX_InvalidateTexAll();
  296.  
  297.  
  298.     guPerspective(projection, 90, 1.333f, 0.1f, 1000.0f);
  299.     GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
  300.  
  301. }
  302.  
  303. void copy_buffers(u32 count)
  304. {
  305.     if(readyForcopy == GX_TRUE) {
  306.         GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  307.         GX_SetColorUpdate(GX_TRUE);
  308.         GX_CopyDisp(framebuffer[frame], GX_TRUE);
  309.         frame ^= 1;
  310.         GX_Flush();
  311.         VIDEO_SetNextFramebuffer(framebuffer[frame]);
  312.         VIDEO_Flush();
  313.         readyForcopy = GX_FALSE;
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement