Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gfd.h>
- #include <gx2/draw.h>
- #include <gx2/shaders.h>
- #include <gx2/mem.h>
- #include <gx2/registers.h>
- #include <gx2r/draw.h>
- #include <gx2r/buffer.h>
- #include <string.h>
- #include <stdio.h>
- #include <whb/file.h>
- #include <whb/proc.h>
- #include <whb/sdcard.h>
- #include <whb/gfx.h>
- #include <whb/log.h>
- #include <whb/log_udp.h>
- #include <math.h>
- #include <cglm/cglm.h>
- #include <coreinit/systeminfo.h>
- #include <coreinit/thread.h>
- #include <coreinit/time.h>
- #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
- #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
- static const float sPositionData[] = {
- -0.5f, 0.5f, 0.0f, 1.0f, // top
- -0.5f, -0.5f, 0.0f, 1.0f, // bottom left
- 0.5f, -0.5f, 0.0f, 1.0f, // bottom right
- -0.5f, 0.5f, 0.0f, 1.0f, // top
- 0.5f, -0.5f, 0.0f, 1.0f, // bottom right
- 0.5f, 0.5f, 0.0f, 1.0f
- };
- static const float sTexCoordData[] = {
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f
- };
- float color[] = {
- 0.0f, 0.0f, 1.0f, 1.0f
- };
- float t;
- mat4 projection __attribute__ ((aligned (16)));;
- mat4 view __attribute__ ((aligned (16)));;
- mat4 model __attribute__ ((aligned (16)));;
- vec3 modelPos;
- int main(int argc, char **argv) {
- GX2RBuffer positionBuffer = { 0 };
- GX2RBuffer texCoordBuffer = { 0 };
- WHBGfxShaderGroup group = { 0 };
- 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/shaderStuffTEMP/pos_col_shader.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_32);
- WHBGfxInitShaderAttribute(&group, "aTexCoords", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
- WHBGfxInitFetchShader(&group);
- WHBFreeWholeFile(gshFileData);
- gshFileData = NULL;
- // Set vertex position
- positionBuffer.flags = 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 = 6;
- GX2RCreateBuffer(&positionBuffer);
- buffer = GX2RLockBufferEx(&positionBuffer, 0);
- memcpy(buffer, sPositionData, positionBuffer.elemSize * positionBuffer.elemCount);
- GX2RUnlockBufferEx(&positionBuffer, 0);
- // Set vertex colour
- texCoordBuffer.flags = 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 = 6;
- GX2RCreateBuffer(&texCoordBuffer);
- buffer = GX2RLockBufferEx(&texCoordBuffer, 0);
- memcpy(buffer, sTexCoordData, texCoordBuffer.elemSize * texCoordBuffer.elemCount);
- GX2RUnlockBufferEx(&texCoordBuffer, 0);
- WHBLogPrintf("Begin rendering...");
- while (WHBProcIsRunning()) {
- t += 0.05f;
- glm_mat4_identity(projection);
- glm_perspective(degToRad(45.0f), (float)854 / (float)480, 0.1f, 100.0f, projection);
- glm_mat4_transpose(projection);
- glm_mat4_identity(view);
- glm_lookat((vec3){0.0f, 0.0f, 0.0f}, (vec3){0.0f, 0.0f, 1.0f}, (vec3){0.0f, 1.0f, 0.0f}, view);
- glm_mat4_transpose(view);
- glm_mat4_identity(model);
- glm_translate_x(model, 50 * sin(t * 0.1f));
- glm_translate_z(model, -10.0f);
- glm_mat4_transpose(model);
- color[0] = (float)(1 * sin(t));
- color[1] = (float)(1 * sin(t / 2));
- color[2] = (float)(1 * sin(t / 4));
- // Render!
- WHBGfxBeginRender();
- WHBGfxBeginRenderTV();
- WHBGfxClearColor(0.5f, 0.5f, 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);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)color);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- WHBGfxFinishRenderTV();
- WHBGfxBeginRenderDRC();
- WHBGfxClearColor(0.5f, 0.5f, 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);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)color);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- WHBGfxFinishRenderDRC();
- WHBGfxFinishRender();
- }
- exit:
- WHBLogPrintf("Exiting...");
- GX2RDestroyBufferEx(&positionBuffer, 0);
- GX2RDestroyBufferEx(&texCoordBuffer, 0);
- WHBUnmountSdCard();
- WHBGfxShutdown();
- WHBProcShutdown();
- WHBLogUdpDeinit();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement