Advertisement
atm959

main.c

Feb 2nd, 2019
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.20 KB | None | 0 0
  1. #include <gfd.h>
  2. #include <gx2/draw.h>
  3. #include <gx2/shaders.h>
  4. #include <gx2/mem.h>
  5. #include <gx2/registers.h>
  6. #include <gx2r/draw.h>
  7. #include <gx2r/buffer.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <whb/file.h>
  11. #include <whb/proc.h>
  12. #include <whb/sdcard.h>
  13. #include <whb/gfx.h>
  14. #include <whb/log.h>
  15. #include <whb/log_udp.h>
  16. #include <math.h>
  17.  
  18. #include <cglm/cglm.h>
  19.  
  20. #include <coreinit/systeminfo.h>
  21. #include <coreinit/thread.h>
  22. #include <coreinit/time.h>
  23.  
  24. #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
  25. #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
  26.  
  27. static const float sPositionData[] = {
  28.     -0.5f,  0.5f, 0.0f, 1.0f, // top
  29.     -0.5f, -0.5f, 0.0f, 1.0f, // bottom left
  30.     0.5f, -0.5f, 0.0f, 1.0f,  // bottom right
  31.     -0.5f,  0.5f, 0.0f, 1.0f, // top
  32.     0.5f, -0.5f, 0.0f, 1.0f,  // bottom right
  33.     0.5f,  0.5f, 0.0f, 1.0f
  34. };
  35.  
  36. static const float sTexCoordData[] = {
  37.    0.0f, 1.0f,
  38.    0.0f, 0.0f,
  39.    1.0f, 0.0f,
  40.    0.0f, 1.0f,
  41.    1.0f, 0.0f,
  42.    1.0f, 1.0f
  43. };
  44.  
  45. float color[] = {
  46.    0.0f, 0.0f, 1.0f, 1.0f
  47. };
  48.  
  49. float t;
  50.  
  51. mat4 projection __attribute__ ((aligned (16)));;
  52. mat4 view __attribute__ ((aligned (16)));;
  53. mat4 model __attribute__ ((aligned (16)));;
  54. vec3 modelPos;
  55.  
  56. int main(int argc, char **argv) {
  57.    GX2RBuffer positionBuffer = { 0 };
  58.    GX2RBuffer texCoordBuffer = { 0 };
  59.    WHBGfxShaderGroup group = { 0 };
  60.    void *buffer = NULL;
  61.    char *gshFileData = NULL;
  62.    char *sdRootPath = NULL;
  63.    char path[256];
  64.    int result = 0;
  65.  
  66.    WHBLogUdpInit();
  67.    WHBProcInit();
  68.    WHBGfxInit();
  69.  
  70.    if (!WHBMountSdCard()) {
  71.       result = -1;
  72.       goto exit;
  73.    }
  74.  
  75.    sdRootPath = WHBGetSdCardMountPath();
  76.    sprintf(path, "%s/shaderStuffTEMP/pos_col_shader.gsh", sdRootPath);
  77.  
  78.    gshFileData = WHBReadWholeFile(path, NULL);
  79.    if (!gshFileData) {
  80.       result = -1;
  81.       WHBLogPrintf("WHBReadWholeFile(%s) returned NULL", path);
  82.       goto exit;
  83.    }
  84.  
  85.    if (!WHBGfxLoadGFDShaderGroup(&group, 0, gshFileData)) {
  86.       result = -1;
  87.       WHBLogPrintf("WHBGfxLoadGFDShaderGroup returned FALSE");
  88.       goto exit;
  89.    }
  90.  
  91.    WHBGfxInitShaderAttribute(&group, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
  92.    WHBGfxInitShaderAttribute(&group, "aTexCoords", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
  93.    WHBGfxInitFetchShader(&group);
  94.  
  95.    WHBFreeWholeFile(gshFileData);
  96.    gshFileData = NULL;
  97.  
  98.    // Set vertex position
  99.    positionBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  100.                           GX2R_RESOURCE_USAGE_CPU_READ |
  101.                           GX2R_RESOURCE_USAGE_CPU_WRITE |
  102.                           GX2R_RESOURCE_USAGE_GPU_READ;
  103.    positionBuffer.elemSize = 4 * 4;
  104.    positionBuffer.elemCount = 6;
  105.    GX2RCreateBuffer(&positionBuffer);
  106.    buffer = GX2RLockBufferEx(&positionBuffer, 0);
  107.    memcpy(buffer, sPositionData, positionBuffer.elemSize * positionBuffer.elemCount);
  108.    GX2RUnlockBufferEx(&positionBuffer, 0);
  109.  
  110.    // Set vertex colour
  111.    texCoordBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  112.                           GX2R_RESOURCE_USAGE_CPU_READ |
  113.                           GX2R_RESOURCE_USAGE_CPU_WRITE |
  114.                           GX2R_RESOURCE_USAGE_GPU_READ;
  115.    texCoordBuffer.elemSize = 2 * 4;
  116.    texCoordBuffer.elemCount = 6;
  117.    GX2RCreateBuffer(&texCoordBuffer);
  118.    buffer = GX2RLockBufferEx(&texCoordBuffer, 0);
  119.    memcpy(buffer, sTexCoordData, texCoordBuffer.elemSize * texCoordBuffer.elemCount);
  120.    GX2RUnlockBufferEx(&texCoordBuffer, 0);
  121.  
  122.    WHBLogPrintf("Begin rendering...");
  123.    while (WHBProcIsRunning()) {
  124.       t += 0.05f;
  125.  
  126.       glm_mat4_identity(projection);
  127.       glm_perspective(degToRad(45.0f), (float)854 / (float)480, 0.1f, 100.0f, projection);
  128.       glm_mat4_transpose(projection);
  129.  
  130.       glm_mat4_identity(view);
  131.       glm_lookat((vec3){0.0f, 0.0f, 0.0f}, (vec3){0.0f, 0.0f, 1.0f}, (vec3){0.0f, 1.0f, 0.0f}, view);
  132.       glm_mat4_transpose(view);
  133.  
  134.       glm_mat4_identity(model);
  135.       glm_translate_x(model, 50 * sin(t * 0.1f));
  136.       glm_translate_z(model, -10.0f);
  137.       glm_mat4_transpose(model);
  138.  
  139.       color[0] = (float)(1 * sin(t));
  140.       color[1] = (float)(1 * sin(t / 2));
  141.       color[2] = (float)(1 * sin(t / 4));
  142.  
  143.       // Render!
  144.       WHBGfxBeginRender();
  145.  
  146.       WHBGfxBeginRenderTV();
  147.       WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  148.       GX2SetFetchShader(&group.fetchShader);
  149.       GX2SetVertexShader(group.vertexShader);
  150.       GX2SetPixelShader(group.pixelShader);
  151.       GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
  152.       GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
  153.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)color);
  154.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
  155.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
  156.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
  157.       GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
  158.       WHBGfxFinishRenderTV();
  159.  
  160.       WHBGfxBeginRenderDRC();
  161.       WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  162.       GX2SetFetchShader(&group.fetchShader);
  163.       GX2SetVertexShader(group.vertexShader);
  164.       GX2SetPixelShader(group.pixelShader);
  165.       GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
  166.       GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
  167.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)color);
  168.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
  169.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
  170.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
  171.       GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
  172.       WHBGfxFinishRenderDRC();
  173.  
  174.       WHBGfxFinishRender();
  175.    }
  176.  
  177. exit:
  178.    WHBLogPrintf("Exiting...");
  179.    GX2RDestroyBufferEx(&positionBuffer, 0);
  180.    GX2RDestroyBufferEx(&texCoordBuffer, 0);
  181.    WHBUnmountSdCard();
  182.    WHBGfxShutdown();
  183.    WHBProcShutdown();
  184.    WHBLogUdpDeinit();
  185.    return result;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement