Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "common.h"
- #include "Texture.h"
- #include "Sound.h"
- #include "Obj_Loader.h"
- #define degToRad(angleInDegrees) (float)((angleInDegrees) * M_PI / 180.0)
- #define radToDeg(angleInRadians) (float)((angleInRadians) * 180.0 / M_PI)
- std::vector<float> vertexPositions;
- std::vector<float> texCoords;
- std::vector<unsigned int> indices;
- std::vector<float> vertexNormals;
- int numVert, numInd;
- objl::Loader loader;
- static const float fontVertexData[] = {
- 0.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 0.0f, 0.0f, 1.0f
- };
- glm::mat4 identity;
- glm::mat4 projection;
- glm::mat4 view;
- glm::mat4 model;
- glm::mat4 mvp;
- glm::vec3 lightPos;
- glm::vec3 lightColor;
- Texture iceBrickTex;
- Texture fontTex;
- Texture gamepadTex;
- GX2Sampler sampler;
- float t;
- int executedFrames;
- int fontWidths[256];
- int fps = 0;
- int lastTime = 0;
- time_t now;
- struct tm *date, *lastDate;
- void loadFontWidthsFromFile() {
- std::ifstream fontDataFile;
- std::stringstream fontDataStream;
- std::string fontDataString;
- fontDataFile.open("fs:/vol/external01/wiiu/apps/WiiUGame/res/Font2Data.csv");
- fontDataStream << fontDataFile.rdbuf();
- fontDataFile.close();
- fontDataString = fontDataStream.str();
- for (unsigned int i = 0; i < fontDataString.length() - 5; i++) {
- if (fontDataString.substr(i, 5) == "Char ") {
- i += 5;
- int j = 0;
- while (fontDataString.substr(i + j, 1) != " ") {
- j++;
- }
- std::string charNum = fontDataString.substr(i, j);
- i += j;
- if (fontDataString.substr(i, 12) == " Base Width,") {
- i += 12;
- j = 0;
- while (fontDataString.substr(i + j, 1) != "\n") {
- j++;
- }
- std::string widthNum = fontDataString.substr(i, j);
- i += j;
- fontWidths[std::stoi(charNum)] = std::stoi(widthNum);
- }
- }
- }
- }
- GX2RBuffer fontPositionBuffer;
- GX2RBuffer fontTexCoordBuffer;
- WHBGfxShaderGroup group;
- VPADStatus gamepad;
- VPADReadError gamepadError;
- void renderString(std::string s, int x, int y) {
- GX2RSetAttributeBuffer(&fontPositionBuffer, 0, fontPositionBuffer.elemSize, 0);
- GX2RSetAttributeBuffer(&fontTexCoordBuffer, 1, fontTexCoordBuffer.elemSize, 0);
- fontTex.mapTo(0);
- char ch;
- float u, v, u2, v2;
- GX2SetDepthOnlyControl(GX2_FALSE, GX2_TRUE, GX2_COMPARE_FUNC_LESS);
- for (unsigned int i = 0; i < s.length(); i++) {
- ch = s.at(i) - 32;
- u = (float)((ch % 32) * 32) / 1024;
- v = (float)((ch / 32) * 32) / 256;
- u2 = (float)(((ch % 32) * 32) + 32) / 1024;
- v2 = (float)(((ch / 32) * 32) + 32) / 256;
- float* texCoords = (float*)GX2RLockBufferEx(&fontTexCoordBuffer, (GX2RResourceFlags)0);
- texCoords[0] = u;
- texCoords[1] = v2;
- texCoords[2] = u2;
- texCoords[3] = v;
- texCoords[4] = u;
- texCoords[5] = v;
- texCoords[6] = u;
- texCoords[7] = v2;
- texCoords[8] = u2;
- texCoords[9] = v2;
- texCoords[10] = u2;
- texCoords[11] = v;
- GX2RUnlockBufferEx(&fontTexCoordBuffer, (GX2RResourceFlags)0);
- GX2RSetAttributeBuffer(&fontTexCoordBuffer, 1, fontTexCoordBuffer.elemSize, 0);
- model = identity;
- model = glm::translate(model, glm::vec3(x, y, 0.0f));
- model = glm::scale(model, glm::vec3(32, 32, 1));
- mvp = projection * model;
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- GX2DrawDone();
- x += fontWidths[(int)s.at(i)];
- }
- GX2SetDepthOnlyControl(GX2_TRUE, GX2_TRUE, GX2_COMPARE_FUNC_LESS);
- }
- AXVoice* musicVoice;
- unsigned int lastAudioOffset;
- char audioBuffer[8192];
- int nextAudioBuffSection = 1;
- bool loadNewData = false;
- void musicFrameCallback(){
- AXVoiceOffsets tempOffs;
- AXGetVoiceOffsets(musicVoice, &tempOffs);
- if(tempOffs.currentOffset > 0 && tempOffs.currentOffset < 4096){
- if(nextAudioBuffSection == 0){
- nextAudioBuffSection = 1;
- loadNewData = true;
- }
- } else if(tempOffs.currentOffset > 4096){
- if(nextAudioBuffSection == 1){
- nextAudioBuffSection = 0;
- loadNewData = true;
- }
- }
- if(loadNewData == true){
- AXSetVoiceOffsets(musicVoice, &tempOffs);
- for(int i = 0; i < 4096; i++){
- audioBuffer[i + (nextAudioBuffSection * 4096)] = music_raw[i + lastAudioOffset] / 2;
- }
- DCStoreRange(audioBuffer + (nextAudioBuffSection * 4096), 4096);
- lastAudioOffset += 4096;
- if(lastAudioOffset > sizeof(music_raw)){
- lastAudioOffset = 0;
- }
- loadNewData = false;
- }
- }
- float camX, camY, camZ;
- float camAngle;
- float camFacingX, camFacingZ;
- void setup3DTV(){
- projection = glm::perspective((float)degToRad(45.0f), (float)1280 / (float)720, 0.01f, 10000.0f);
- view = glm::lookAt(glm::vec3(camX, camY, camZ), glm::vec3(camX + sin(camAngle), camY, camZ + cos(camAngle)), glm::vec3(0, 1, 0));
- camFacingX = sin(camAngle);
- camFacingZ = cos(camAngle);
- model = identity;
- //model = glm::rotate(model, degToRad(90.0f), glm::vec3(1, 0, 0));
- //model = glm::scale(model, glm::vec3(1024, 1024, 1));
- mvp = projection * view;
- mvp = mvp * model;
- }
- void setup2DTV(){
- projection = glm::ortho(0.0f, 1280.0f, 720.0f, 0.0f);
- model = identity;
- model = glm::translate(model, glm::vec3(1280 / 2, 720 / 2, 0));
- model = glm::scale(model, glm::vec3(1280, 720, 1));
- mvp = projection * model;
- }
- void setup2DDRC(){
- projection = glm::ortho(0.0f, 854.0f, 480.0f, 0.0f);
- mvp = projection * model;
- }
- int main(int argc, char **argv) {
- time_t begin_time; //time at the start of the loop
- time_t current_time; //time when checking for a one-second interval
- double frames = 0; //number of times passed through the loop
- time(&begin_time); //sets begin_time
- lastAudioOffset = 8192;
- for(int i = 0; i < 8192; i++){
- audioBuffer[i] = music_raw[i] / 2;
- }
- AXVoiceOffsets offs;
- AXVoiceVeData vol = {
- .volume = 0x8000,
- };
- AXVoiceDeviceMixData drcmix = {
- .bus = {
- { .volume = 0x8000 }, //bus 0
- { .volume = 0x0000 }, //bus 1
- { .volume = 0x0000 }, //bus 2
- { .volume = 0x0000 }, //bus 3
- },
- };
- AXVoiceDeviceMixData tvmix = drcmix;
- AXInitParams initparams = {
- .renderer = AX_INIT_RENDERER_48KHZ,
- .pipeline = AX_INIT_PIPELINE_SINGLE,
- };
- AXInitWithParams(&initparams);
- AXSetDRCVSMode(0);
- musicVoice = AXAcquireVoice(31, NULL, NULL);
- AXVoiceBegin(musicVoice);
- AXSetVoiceType(musicVoice, 0);
- AXSetVoiceVe(musicVoice, &vol);
- AXSetVoiceDeviceMix(musicVoice, AX_DEVICE_TYPE_DRC, 0, &drcmix);
- AXSetVoiceDeviceMix(musicVoice, AX_DEVICE_TYPE_TV, 0, &tvmix);
- AXSetVoiceSrcRatio(musicVoice, 44100.0f / (float)AXGetInputSamplesPerSec());
- AXSetVoiceSrcType(musicVoice, AX_VOICE_SRC_TYPE_LINEAR);
- offs.dataType = AX_VOICE_FORMAT_LPCM8;
- offs.endOffset = sizeof(audioBuffer) - 1;
- offs.loopingEnabled = AX_VOICE_LOOP_ENABLED;
- offs.loopOffset = 0;
- offs.currentOffset = 0;
- offs.data = &audioBuffer;
- AXSetVoiceOffsets(musicVoice, &offs);
- AXSetVoiceState(musicVoice, AX_VOICE_STATE_PLAYING);
- AXVoiceEnd(musicVoice);
- AXRegisterAppFrameCallback(musicFrameCallback);
- GX2RBuffer positionBuffer;
- GX2RBuffer texCoordBuffer;
- GX2RBuffer normalBuffer;
- void *buffer = NULL;
- char *gshFileData = NULL;
- char *sdRootPath = NULL;
- char path[256];
- int result = 0;
- WHBLogUdpInit();
- WHBProcInit();
- WHBGfxInit();
- if (!WHBMountSdCard()) {
- result = -1;
- goto exit;
- }
- sdRootPath = WHBGetSdCardMountPath();
- sprintf(path, "%s/wiiu/apps/WiiUGame/res/shaders/terrainShader.gsh", sdRootPath);
- gshFileData = WHBReadWholeFile(path, NULL);
- if (!gshFileData) {
- result = -1;
- WHBLogPrintf("WHBReadWholeFile(%s) returned NULL", path);
- goto exit;
- }
- if (!WHBGfxLoadGFDShaderGroup(&group, 0, gshFileData)) {
- result = -1;
- WHBLogPrintf("WHBGfxLoadGFDShaderGroup returned FALSE");
- goto exit;
- }
- WHBGfxInitShaderAttribute(&group, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32);
- WHBGfxInitShaderAttribute(&group, "aTexCoords", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
- WHBGfxInitShaderAttribute(&group, "aNormals", 2, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32);
- WHBGfxInitFetchShader(&group);
- WHBFreeWholeFile(gshFileData);
- gshFileData = NULL;
- loader.LoadFile("fs:/vol/external01/wiiu/apps/WiiUGame/res/cave.obj");
- numVert = loader.LoadedVertices.size();
- numInd = loader.LoadedIndices.size();
- for(int i = 0; i < numVert; i++){
- vertexPositions.push_back(loader.LoadedVertices.at(i).Position.X);
- vertexPositions.push_back(loader.LoadedVertices.at(i).Position.Y);
- vertexPositions.push_back(loader.LoadedVertices.at(i).Position.Z);
- texCoords.push_back(loader.LoadedVertices.at(i).TextureCoordinate.X);
- texCoords.push_back(loader.LoadedVertices.at(i).TextureCoordinate.Y);
- vertexNormals.push_back(loader.LoadedVertices.at(i).Normal.X);
- vertexNormals.push_back(loader.LoadedVertices.at(i).Normal.Y);
- vertexNormals.push_back(loader.LoadedVertices.at(i).Normal.Z);
- }
- for(int i = 0; i < numInd; i++){
- indices.push_back(loader.LoadedIndices.at(i));
- }
- // Set vertex position
- positionBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ);
- positionBuffer.elemSize = 4 * 4;
- positionBuffer.elemCount = vertexPositions.size();
- GX2RCreateBuffer(&positionBuffer);
- buffer = GX2RLockBufferEx(&positionBuffer, (GX2RResourceFlags)0);
- memcpy(buffer, &vertexPositions[0], positionBuffer.elemSize * positionBuffer.elemCount);
- GX2RUnlockBufferEx(&positionBuffer, (GX2RResourceFlags)0);
- // Set vertex tex coords
- texCoordBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ);
- texCoordBuffer.elemSize = 2 * 4;
- texCoordBuffer.elemCount = texCoords.size();
- GX2RCreateBuffer(&texCoordBuffer);
- buffer = GX2RLockBufferEx(&texCoordBuffer, (GX2RResourceFlags)0);
- memcpy(buffer, &texCoords[0], texCoordBuffer.elemSize * texCoordBuffer.elemCount);
- GX2RUnlockBufferEx(&texCoordBuffer, (GX2RResourceFlags)0);
- normalBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ);
- normalBuffer.elemSize = 3 * 4;
- normalBuffer.elemCount = vertexNormals.size();
- GX2RCreateBuffer(&normalBuffer);
- buffer = GX2RLockBufferEx(&normalBuffer, (GX2RResourceFlags)0);
- memcpy(buffer, &vertexNormals[0], normalBuffer.elemSize * normalBuffer.elemCount);
- GX2RUnlockBufferEx(&normalBuffer, (GX2RResourceFlags)0);
- fontPositionBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ);
- fontPositionBuffer.elemSize = 4 * 4;
- fontPositionBuffer.elemCount = 6;
- GX2RCreateBuffer(&fontPositionBuffer);
- buffer = GX2RLockBufferEx(&fontPositionBuffer, (GX2RResourceFlags)0);
- memcpy(buffer, fontVertexData, fontPositionBuffer.elemSize * fontPositionBuffer.elemCount);
- GX2RUnlockBufferEx(&fontPositionBuffer, (GX2RResourceFlags)0);
- // Set vertex tex coords
- fontTexCoordBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ);
- fontTexCoordBuffer.elemSize = 2 * 4;
- fontTexCoordBuffer.elemCount = 6;
- GX2RCreateBuffer(&fontTexCoordBuffer);
- GX2InitSampler(&sampler, GX2_TEX_CLAMP_MODE_WRAP, GX2_TEX_XY_FILTER_MODE_POINT);
- GX2InitSamplerLOD(&sampler, 0, 13, -3.0f);
- iceBrickTex.load("fs:/vol/external01/wiiu/apps/WiiUGame/res/textures/misc/ape_escape_ice_bricks.png");
- fontTex.load("fs:/vol/external01/wiiu/apps/WiiUGame/res/textures/Splat2Font.bmp");
- gamepadTex.load("fs:/vol/external01/wiiu/apps/WiiUGame/res/textures/GamepadVector.png");
- loadFontWidthsFromFile();
- VPADInit();
- //GX2SetSwapInterval(0); //Over 60FPS???
- lightPos = glm::vec3(0.0f, 1.0f, 0.0f);
- lightColor = glm::vec3(1.0f, 0.0f, 0.0f);
- for(int i = 0; i < 3; i++){
- WHBLogPrintf("%d offset: %d", i, group.vertexShader->uniformVars[i].offset);
- }
- WHBLogPrintf("Begin rendering...");
- while (WHBProcIsRunning()) {
- t += 0.01f;
- executedFrames++;
- VPADRead(VPAD_CHAN_0, &gamepad, 1, &gamepadError);
- if(gamepad.hold & VPAD_BUTTON_A) camY += 0.5f;
- if(gamepad.hold & VPAD_BUTTON_B) camY -= 0.5f;
- if(gamepad.trigger & VPAD_BUTTON_STICK_L){
- camX = 0.0f;
- camY = 0.0f;
- camZ = 0.0f;
- }
- if(gamepad.trigger & VPAD_BUTTON_STICK_R) camAngle = 0.0f;
- camX += (gamepad.leftStick.y * 0.5f) * sin(camAngle);
- camZ += (gamepad.leftStick.y * 0.5f) * cos(camAngle);
- camAngle += gamepad.rightStick.x * 0.05f;
- frames++; //increments frames
- time(¤t_time); //sets current_time
- setup3DTV();
- // Render!
- WHBGfxBeginRender();
- WHBGfxBeginRenderTV();
- GX2SetCullOnlyControl(GX2_FRONT_FACE_CCW, GX2_FALSE, GX2_TRUE);
- GX2SetColorControl(GX2_LOGIC_OP_COPY, 0xFF, FALSE, TRUE);
- GX2SetBlendControl(GX2_RENDER_TARGET_0,
- /* RGB = [srcRGB * srcA] + [dstRGB * (1-srcA)] */
- GX2_BLEND_MODE_SRC_ALPHA, GX2_BLEND_MODE_INV_SRC_ALPHA,
- GX2_BLEND_COMBINE_MODE_ADD,
- TRUE,
- /* A = [srcA * 1] + [dstA * (1-srcA)] */
- GX2_BLEND_MODE_ONE, GX2_BLEND_MODE_INV_SRC_ALPHA,
- GX2_BLEND_COMBINE_MODE_ADD);
- WHBGfxClearColor(0.0f, 0.0f, 1.0f, 1.0f);
- GX2SetFetchShader(&group.fetchShader);
- GX2SetVertexShader(group.vertexShader);
- GX2SetPixelShader(group.pixelShader);
- GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
- GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
- GX2SetPixelSampler(&sampler, 0);
- iceBrickTex.mapTo(0);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&identity[0][0]);
- //GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- GX2DrawIndexedEx(GX2_PRIMITIVE_MODE_TRIANGLES, numInd, GX2_INDEX_TYPE_U32, &indices[0], 0, 1);
- setup2DTV();
- gamepadTex.mapTo(0);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- renderString("fps: " + std::to_string(fps), 100, 100 - 64);
- renderString("executedFrames: " + std::to_string(executedFrames), 100, 100 - 32);
- renderString("t: " + std::to_string(t), 100, 100);
- renderString("Music has been added! Now to load it from a file...", 100, 132);
- renderString("numVert: " + std::to_string(numVert), 100, 256);
- renderString("numInd: " + std::to_string(numInd), 100, 256 + 32);
- renderString("Cam Pos.: (" + std::to_string(camX) + ", " + std::to_string(camY) + ", " + std::to_string(camZ) + ")", 100, 320);
- renderString("camAngle: " + std::to_string(camAngle), 100, 352);
- renderString("camFacing: (" + std::to_string(camFacingX) + ", " + std::to_string(camFacingZ) + ")", 100, 352 + 32);
- WHBGfxFinishRenderTV();
- setup2DDRC();
- WHBGfxBeginRenderDRC();
- WHBGfxClearColor(1.0f, 0.0f, 1.0f, 1.0f);
- GX2SetFetchShader(&group.fetchShader);
- GX2SetVertexShader(group.vertexShader);
- GX2SetPixelShader(group.pixelShader);
- GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
- GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
- GX2SetPixelSampler(&sampler, 0);
- iceBrickTex.mapTo(0);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- renderString("fps: " + std::to_string(fps), 100, 100 - 64);
- renderString("executedFrames: " + std::to_string(executedFrames), 100, 100 - 32);
- renderString("t: " + std::to_string(t), 100, 100);
- renderString("lastAudioOffset: " + std::to_string(lastAudioOffset), 100, 132);
- renderString("nextAudioBuffSection: " + std::to_string(nextAudioBuffSection), 100, 164);
- WHBGfxFinishRenderDRC();
- WHBGfxFinishRender();
- if (difftime(current_time, begin_time) >= 1.0) {
- fps = frames;
- frames = 0;
- time(&begin_time);
- }
- if (difftime(current_time, begin_time) < 0.0) {
- time(&begin_time);
- }
- }
- exit:
- WHBLogPrintf("Exiting...");
- VPADShutdown();
- AXQuit();
- iceBrickTex.unload();
- fontTex.unload();
- gamepadTex.unload();
- GX2RDestroyBufferEx(&positionBuffer, (GX2RResourceFlags)0);
- GX2RDestroyBufferEx(&texCoordBuffer, (GX2RResourceFlags)0);
- GX2RDestroyBufferEx(&fontPositionBuffer, (GX2RResourceFlags)0);
- GX2RDestroyBufferEx(&fontTexCoordBuffer, (GX2RResourceFlags)0);
- WHBUnmountSdCard();
- WHBGfxShutdown();
- WHBProcShutdown();
- WHBLogUdpDeinit();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment