Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #include <string.h>
- #include <math.h>
- #include <gccore.h>
- #include <wiiuse/wpad.h>
- #include <debug.h>
- #include "Math_utils.h"
- #include "Camera.h"
- #include "MatrixStack.h"
- #include "Image.hpp"
- #include "stone_png.h"
- #define PI 3.14159265359f
- #define DEG_TO_RAD(x) ((x)*PI)/180.0f
- #define FIFO_SIZE (256 * 1024)
- GXRModeObj *screenMode;
- void *framebuffer[2] = {NULL, NULL};
- int frame = 0;
- vu8 readyForcopy = GX_FALSE;
- void copy_buffers(u32 count);
- void *fifoBuffer = NULL;
- WPADData *wiimoteData;
- joystick_t *nunchukJoystick;
- u32 wiimoteExpansion;
- float nunchuk_x = 0.0f, nunchuk_y = 0.0f;
- u32 pressed = 0, old_pressed = 0, held = 0;
- bool canFly = true;
- Mtx44 projection;
- Mtx model, world, cameraMatrix, modelView;
- float pitch = 0.0f, yaw = 0.0f;
- guVector position = {0.0f, 0.0f, 0.0f};
- Camera camera(cameraMatrix, &position, &pitch, &yaw);
- MatrixStack matrices;
- void init_screen();
- void read_controls();
- void drawCube();
- GXColor backgroundColor = {0xFF, 0xFF, 0xFF, 0xFF};
- #define CUBE_SIZE 0.5f
- float vertexData[] ATTRIBUTE_ALIGN(32) = {
- -CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
- -CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
- CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
- -CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
- -CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
- };
- float texCoordData[] ATTRIBUTE_ALIGN(32) = {
- 0.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f
- };
- int main(void) {
- WPAD_Init();
- init_screen();
- Image image((u8*)stone_png);
- GX_SetArray(GX_VA_POS, vertexData, 3*sizeof(float));
- GX_SetArray(GX_VA_TEX0, texCoordData, 2*sizeof(float));
- while (1)
- {
- read_controls();
- #define ROT_SPEED 0.075f
- #define MOV_SPEED 0.075f
- if(held & WPAD_BUTTON_RIGHT) yaw-= ROT_SPEED;
- if(held & WPAD_BUTTON_LEFT) yaw+= ROT_SPEED;
- if(held & WPAD_BUTTON_UP) pitch+= ROT_SPEED;
- if(held & WPAD_BUTTON_DOWN) pitch-= ROT_SPEED;
- if(pressed & WPAD_BUTTON_1) canFly = not canFly;
- if(wiimoteExpansion == WPAD_EXP_NUNCHUK) {
- if(held & WPAD_NUNCHUK_BUTTON_C) position.y += MOV_SPEED;
- if(held & WPAD_NUNCHUK_BUTTON_Z) position.y -= MOV_SPEED;
- if(nunchukJoystick->mag > 0.15f) {
- guVector look;
- if(canFly)
- look = camera.getLookVector();
- else
- look = camera.getForwardVector();
- guVector right = camera.getRightVector();
- guVecScale(&look, &look, -nunchuk_x/7.5f);
- guVecScale(&right, &right, nunchuk_y/7.5f);
- guVecAdd(&position, &look, &position);
- guVecAdd(&position, &right, &position);
- }
- }
- camera.updateMatrix();
- guMtxIdentity(world);
- guMtxConcat(cameraMatrix, world, modelView);
- GX_LoadPosMtxImm(modelView, GX_PNMTX0);
- GX_SetCurrentMtx(GX_PNMTX0);
- image.setGX(GX_TEXMAP0);
- guVector v = {2.0f, 0.0f, 0.0f};
- matrices.copyMatrix(cameraMatrix);
- matrices.Push();
- int i;
- for(i = 0; i < 10; i++) {
- matrices.Translate(v);
- matrices.setMatrix();
- drawCube();
- }
- matrices.Pop();
- GX_DrawDone();
- readyForcopy = GX_TRUE;
- VIDEO_WaitVSync();
- if (pressed & WPAD_BUTTON_HOME) break;
- old_pressed = pressed;
- }
- return 0;
- }
- void drawCube()
- {
- GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 36);
- GX_Position1x16(0);
- GX_TexCoord1x16(0);
- GX_Position1x16(1);
- GX_TexCoord1x16(1);
- GX_Position1x16(2);
- GX_TexCoord1x16(2);
- GX_Position1x16(1);
- GX_TexCoord1x16(1);
- GX_Position1x16(2);
- GX_TexCoord1x16(2);
- GX_Position1x16(3);
- GX_TexCoord1x16(3);
- GX_Position1x16(2);
- GX_TexCoord1x16(0);
- GX_Position1x16(3);
- GX_TexCoord1x16(1);
- GX_Position1x16(4);
- GX_TexCoord1x16(2);
- GX_Position1x16(3);
- GX_TexCoord1x16(1);
- GX_Position1x16(4);
- GX_TexCoord1x16(2);
- GX_Position1x16(5);
- GX_TexCoord1x16(3);
- GX_Position1x16(4);
- GX_TexCoord1x16(0);
- GX_Position1x16(5);
- GX_TexCoord1x16(1);
- GX_Position1x16(6);
- GX_TexCoord1x16(2);
- GX_Position1x16(5);
- GX_TexCoord1x16(1);
- GX_Position1x16(6);
- GX_TexCoord1x16(2);
- GX_Position1x16(7);
- GX_TexCoord1x16(3);
- GX_Position1x16(6);
- GX_TexCoord1x16(0);
- GX_Position1x16(7);
- GX_TexCoord1x16(1);
- GX_Position1x16(0);
- GX_TexCoord1x16(2);
- GX_Position1x16(7);
- GX_TexCoord1x16(1);
- GX_Position1x16(0);
- GX_TexCoord1x16(2);
- GX_Position1x16(1);
- GX_TexCoord1x16(3);
- GX_Position1x16(1);
- GX_TexCoord1x16(0);
- GX_Position1x16(7);
- GX_TexCoord1x16(1);
- GX_Position1x16(3);
- GX_TexCoord1x16(2);
- GX_Position1x16(7);
- GX_TexCoord1x16(1);
- GX_Position1x16(3);
- GX_TexCoord1x16(2);
- GX_Position1x16(5);
- GX_TexCoord1x16(3);
- GX_Position1x16(6);
- GX_TexCoord1x16(0);
- GX_Position1x16(0);
- GX_TexCoord1x16(1);
- GX_Position1x16(4);
- GX_TexCoord1x16(2);
- GX_Position1x16(0);
- GX_TexCoord1x16(1);
- GX_Position1x16(4);
- GX_TexCoord1x16(2);
- GX_Position1x16(2);
- GX_TexCoord1x16(3);
- GX_End();
- }
- void read_controls()
- {
- WPAD_ScanPads();
- WPAD_Probe(0, &wiimoteExpansion);
- wiimoteData = WPAD_Data(0);
- nunchukJoystick = &wiimoteData->exp.nunchuk.js;
- held = wiimoteData->btns_h;
- pressed = wiimoteData->btns_d;
- if(wiimoteExpansion == WPAD_EXP_NUNCHUK) {
- float n_angle = DEG_TO_RAD(nunchukJoystick->ang);
- nunchuk_x = cosf(n_angle) * nunchukJoystick->mag;
- nunchuk_y = sinf(n_angle) * nunchukJoystick->mag;
- }
- }
- void init_screen()
- {
- VIDEO_Init();
- screenMode = VIDEO_GetPreferredMode(NULL);
- framebuffer[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
- framebuffer[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
- VIDEO_Configure(screenMode);
- VIDEO_SetNextFramebuffer(framebuffer[0]);
- VIDEO_SetPostRetraceCallback(copy_buffers);
- VIDEO_SetBlack(FALSE);
- VIDEO_Flush();
- fifoBuffer = MEM_K0_TO_K1(memalign(32, FIFO_SIZE));
- memset(fifoBuffer, 0x0, FIFO_SIZE);
- GX_Init(fifoBuffer, FIFO_SIZE);
- GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
- GX_SetCopyClear(backgroundColor, 0x00FFFFFF);
- GX_SetViewport(0, 0, screenMode->fbWidth, screenMode->efbHeight, 0.0f, 1.0f);
- GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight);
- GX_SetScissor(0, 0, screenMode->fbWidth, screenMode->efbHeight);
- GX_SetDispCopySrc(0, 0, screenMode->fbWidth, screenMode->efbHeight);
- GX_SetDispCopyDst(screenMode->fbWidth, screenMode->xfbHeight);
- GX_SetCopyFilter(screenMode->aa, screenMode->sample_pattern, GX_TRUE, screenMode->vfilter);
- GX_SetFieldMode(screenMode->field_rendering,
- ((screenMode->viHeight == 2*screenMode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
- GX_SetCullMode(GX_CULL_NONE);
- GX_CopyDisp(framebuffer[frame], GX_TRUE);
- GX_SetDispCopyGamma(GX_GM_1_0);
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
- GX_SetVtxDesc(GX_VA_CLR0, GX_NONE);
- GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX16);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
- GX_SetNumChans(0);
- GX_SetNumTexGens(1);
- GX_SetTevOp(GX_TEVSTAGE0, GX_MODULATE);
- GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
- GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
- GX_InvVtxCache();
- GX_InvalidateTexAll();
- guPerspective(projection, 90, 1.333f, 0.1f, 1000.0f);
- GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
- }
- void copy_buffers(u32 count)
- {
- if(readyForcopy == GX_TRUE) {
- GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
- GX_SetColorUpdate(GX_TRUE);
- GX_CopyDisp(framebuffer[frame], GX_TRUE);
- frame ^= 1;
- GX_Flush();
- VIDEO_SetNextFramebuffer(framebuffer[frame]);
- VIDEO_Flush();
- readyForcopy = GX_FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement