Advertisement
atm959

main.c

Feb 5th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.57 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. #include <gx2/texture.h>
  18. #include <gx2/sampler.h>
  19. #include <malloc.h>
  20.  
  21. #include <cglm/cglm.h>
  22.  
  23. #include <coreinit/systeminfo.h>
  24. #include <coreinit/thread.h>
  25. #include <coreinit/time.h>
  26.  
  27. #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
  28. #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
  29. #define FOV 70
  30. #define NEAR_PLANE 0.1f
  31. #define FAR_PLANE 1000
  32.  
  33. static const float sPositionData[] = {
  34.    -0.5f,  0.5f, 0.0f, 1.0f, // top
  35.    -0.5f, -0.5f, 0.0f, 1.0f, // bottom left
  36.     0.5f, -0.5f, 0.0f, 1.0f,  // bottom right
  37.     -0.5f,  0.5f, 0.0f, 1.0f, // top
  38.     0.5f, -0.5f, 0.0f, 1.0f,  // bottom right
  39.     0.5f,  0.5f, 0.0f, 1.0f
  40. };
  41.  
  42. static const float sTexCoordData[] = {
  43.    0.0f, 1.0f,
  44.    0.0f, 0.0f,
  45.    1.0f, 0.0f,
  46.    0.0f, 1.0f,
  47.    1.0f, 0.0f,
  48.    1.0f, 1.0f
  49. };
  50.  
  51. float color[] = {
  52.    0.0f, 0.0f, 1.0f, 1.0f
  53. };
  54.  
  55. uint32_t texData[] = {
  56.    0xFF0000FF, 0x00FF00FF, 0x0000FFFF,
  57.    0xFFFF00FF, 0x00FFFFFF, 0xFF00FFFF,
  58.    0xFFFFFFFF, 0x808080FF, 0x8080FFFF
  59. };
  60.  
  61. float t;
  62.  
  63. mat4 projection;
  64. mat4 view;
  65. mat4 model;
  66. vec3 modelPos;
  67.  
  68. GX2Texture tex;
  69. GX2Sampler sampler;
  70.  
  71. int main(int argc, char **argv) {
  72.    GX2RBuffer positionBuffer = { 0 };
  73.    GX2RBuffer texCoordBuffer = { 0 };
  74.    WHBGfxShaderGroup group = { 0 };
  75.    void *buffer = NULL;
  76.    char *gshFileData = NULL;
  77.    char *sdRootPath = NULL;
  78.    char path[256];
  79.    int result = 0;
  80.  
  81.    WHBLogUdpInit();
  82.    WHBProcInit();
  83.    WHBGfxInit();
  84.  
  85.    if (!WHBMountSdCard()) {
  86.       result = -1;
  87.       goto exit;
  88.    }
  89.  
  90.    sdRootPath = WHBGetSdCardMountPath();
  91.    sprintf(path, "%s/shaderStuffTEMP/pos_col_shader.gsh", sdRootPath);
  92.  
  93.    gshFileData = WHBReadWholeFile(path, NULL);
  94.    if (!gshFileData) {
  95.       result = -1;
  96.       WHBLogPrintf("WHBReadWholeFile(%s) returned NULL", path);
  97.       goto exit;
  98.    }
  99.  
  100.    if (!WHBGfxLoadGFDShaderGroup(&group, 0, gshFileData)) {
  101.       result = -1;
  102.       WHBLogPrintf("WHBGfxLoadGFDShaderGroup returned FALSE");
  103.       goto exit;
  104.    }
  105.  
  106.    WHBGfxInitShaderAttribute(&group, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
  107.    WHBGfxInitShaderAttribute(&group, "aTexCoords", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
  108.    WHBGfxInitFetchShader(&group);
  109.  
  110.    WHBFreeWholeFile(gshFileData);
  111.    gshFileData = NULL;
  112.  
  113.    // Set vertex position
  114.    positionBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  115.                           GX2R_RESOURCE_USAGE_CPU_READ |
  116.                           GX2R_RESOURCE_USAGE_CPU_WRITE |
  117.                           GX2R_RESOURCE_USAGE_GPU_READ;
  118.    positionBuffer.elemSize = 4 * 4;
  119.    positionBuffer.elemCount = 6;
  120.    GX2RCreateBuffer(&positionBuffer);
  121.    buffer = GX2RLockBufferEx(&positionBuffer, 0);
  122.    memcpy(buffer, sPositionData, positionBuffer.elemSize * positionBuffer.elemCount);
  123.    GX2RUnlockBufferEx(&positionBuffer, 0);
  124.  
  125.    // Set vertex colour
  126.    texCoordBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  127.                           GX2R_RESOURCE_USAGE_CPU_READ |
  128.                           GX2R_RESOURCE_USAGE_CPU_WRITE |
  129.                           GX2R_RESOURCE_USAGE_GPU_READ;
  130.    texCoordBuffer.elemSize = 2 * 4;
  131.    texCoordBuffer.elemCount = 6;
  132.    GX2RCreateBuffer(&texCoordBuffer);
  133.    buffer = GX2RLockBufferEx(&texCoordBuffer, 0);
  134.    memcpy(buffer, sTexCoordData, texCoordBuffer.elemSize * texCoordBuffer.elemCount);
  135.    GX2RUnlockBufferEx(&texCoordBuffer, 0);
  136.  
  137.    GX2InitSampler(&sampler, GX2_TEX_CLAMP_MODE_CLAMP, GX2_TEX_XY_FILTER_MODE_LINEAR);
  138.    tex.surface.dim = GX2_SURFACE_DIM_TEXTURE_2D;
  139.    tex.surface.width = 1024;
  140.    tex.surface.height = 1024;
  141.    tex.surface.depth = 1;
  142.    tex.surface.mipLevels = 1;
  143.    tex.surface.format = GX2_SURFACE_FORMAT_UINT_R8_G8_B8_A8;
  144.    tex.surface.aa = GX2_AA_MODE1X;
  145.    tex.surface.use = GX2_SURFACE_USE_TEXTURE;
  146.    tex.surface.imageSize = 0;
  147.    tex.surface.image = NULL;
  148.    tex.surface.mipmapSize = 0;
  149.    tex.surface.mipmaps = NULL;
  150.    tex.surface.tileMode = GX2_TILE_MODE_DEFAULT;
  151.    tex.surface.swizzle = 0;
  152.    tex.surface.alignment = 0;
  153.    tex.surface.pitch = 0;
  154.    for(int i = 0; i < 13; i++) tex.surface.mipLevelOffset[i] = 0;
  155.    tex.viewFirstMip = 0;
  156.    tex.viewNumMips = 1;
  157.    tex.viewFirstSlice = 0;
  158.    tex.viewNumSlices = 1;
  159.    tex.compMap = 0x00010203;
  160.    for(int i = 0; i < 5; i++) tex.regs[i] = 0;
  161.  
  162.    GX2CalcSurfaceSizeAndAlignment(&tex.surface);
  163.    GX2InitTextureRegs(&tex);
  164.  
  165.    tex.surface.image = memalign(tex.surface.alignment, tex.surface.imageSize);
  166.    memcpy(tex.surface.image, main, tex.surface.imageSize);
  167.  
  168.    WHBLogPrintf("Begin rendering...");
  169.    while (WHBProcIsRunning()) {
  170.       t += 0.05f;
  171.  
  172.       //glm_mat4_identity(projection);
  173.       //glm_perspective(degToRad(45.0f), (float)854 / (float)480, 0.1f, 100.0f, projection);
  174.       //glm_mat4_transpose(projection);
  175.  
  176.       float aspectRatio = (float)854 / (float)480;
  177.       float y_scale = (float)((1.0f / tan(degToRad(FOV / 2.0f))));
  178.       float x_scale = y_scale / aspectRatio;
  179.       float frustum_length = FAR_PLANE - NEAR_PLANE;
  180.  
  181.       projection[0][0] = x_scale;
  182.       projection[1][1] = y_scale;
  183.       projection[2][2] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
  184.       projection[2][3] = -1;
  185.       projection[3][2] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
  186.       projection[3][3] = 0;
  187.  
  188.       view[0][0] = 1;
  189.       view[1][1] = 1;
  190.       view[2][2] = 1;
  191.       view[3][3] = 1;
  192.  
  193.       model[0][0] = 2;
  194.       model[1][1] = 2;
  195.       model[2][2] = 1;
  196.       model[3][3] = 1;
  197.       model[2][3] = -10;
  198.  
  199.       color[0] = (float)(1 * sin(t));
  200.       color[1] = (float)(1 * sin(t / 2));
  201.       color[2] = (float)(1 * sin(t / 4));
  202.  
  203.       float* vertices = (float *)GX2RLockBufferEx(&positionBuffer, 0);
  204.       vertices[0] = -0.5 + (0.125 * sin(t * 2));
  205.       vertices[12] = -0.5 + (0.125 * sin(t * 2));
  206.       vertices[20] = 0.5 + (0.125 * sin(t * 2));
  207.       vertices[1] = 0.5 + (0.125 * -cos(t * 4));
  208.       vertices[13] = 0.5 + (0.125 * -cos(t * 4));
  209.       vertices[21] = 0.5 + (0.125 * -cos(t * 4));
  210.       GX2RUnlockBufferEx(&positionBuffer, 0);
  211.  
  212.       // Render!
  213.       WHBGfxBeginRender();
  214.  
  215.       WHBGfxBeginRenderTV();
  216.       WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  217.       GX2SetFetchShader(&group.fetchShader);
  218.       GX2SetVertexShader(group.vertexShader);
  219.       GX2SetPixelShader(group.pixelShader);
  220.       GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
  221.       GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
  222.       GX2SetPixelTexture(&tex, 0);
  223.       GX2SetPixelSampler(&sampler, group.pixelShader->samplerVars[0].location);
  224.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)&color);
  225.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
  226.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
  227.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
  228.       GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
  229.       WHBGfxFinishRenderTV();
  230.  
  231.       WHBGfxBeginRenderDRC();
  232.       WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  233.       GX2SetFetchShader(&group.fetchShader);
  234.       GX2SetVertexShader(group.vertexShader);
  235.       GX2SetPixelShader(group.pixelShader);
  236.       GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
  237.       GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
  238.       GX2SetPixelTexture(&tex, 0);
  239.       GX2SetPixelSampler(&sampler, group.pixelShader->samplerVars[0].location);
  240.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)&color);
  241.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
  242.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
  243.       GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
  244.       GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
  245.       WHBGfxFinishRenderDRC();
  246.  
  247.       WHBGfxFinishRender();
  248.    }
  249.  
  250. exit:
  251.    WHBLogPrintf("Exiting...");
  252.    GX2RDestroyBufferEx(&positionBuffer, 0);
  253.    GX2RDestroyBufferEx(&texCoordBuffer, 0);
  254.    WHBUnmountSdCard();
  255.    WHBGfxShutdown();
  256.    WHBProcShutdown();
  257.    WHBLogUdpDeinit();
  258.    return result;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement