Advertisement
yerWizard

main.c

May 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.37 KB | None | 0 0
  1. #include <3ds.h>
  2. #include <citro3d.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include "vshader_shbin.h"
  6. #include "kitten_bin.h"
  7.  
  8. #define CLEAR_COLOR 0x68B0D8FF
  9.  
  10. #define DISPLAY_TRANSFER_FLAGS \
  11.     (GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | GX_TRANSFER_RAW_COPY(0) | \
  12.     GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | \
  13.     GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO))
  14.  
  15. typedef struct { float position[3]; float texcoord[2]; float normal[3]; } vertex;
  16.  
  17. static const vertex vertex_list[] =
  18. {
  19.     // First face (PZ)
  20.     // First triangle
  21.     { {-0.5f, -0.5f, +0.5f}, {0.0f, 0.0f}, {0.0f, 0.0f, +1.0f} },
  22.     { {+0.5f, -0.5f, +0.5f}, {1.0f, 0.0f}, {0.0f, 0.0f, +1.0f} },
  23.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, 0.0f, +1.0f} },
  24.     // Second triangle
  25.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, 0.0f, +1.0f} },
  26.     { {-0.5f, +0.5f, +0.5f}, {0.0f, 1.0f}, {0.0f, 0.0f, +1.0f} },
  27.     { {-0.5f, -0.5f, +0.5f}, {0.0f, 0.0f}, {0.0f, 0.0f, +1.0f} },
  28.  
  29.     // Second face (MZ)
  30.     // First triangle
  31.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, 0.0f, -1.0f} },
  32.     { {-0.5f, +0.5f, -0.5f}, {1.0f, 0.0f}, {0.0f, 0.0f, -1.0f} },
  33.     { {+0.5f, +0.5f, -0.5f}, {1.0f, 1.0f}, {0.0f, 0.0f, -1.0f} },
  34.     // Second triangle
  35.     { {+0.5f, +0.5f, -0.5f}, {1.0f, 1.0f}, {0.0f, 0.0f, -1.0f} },
  36.     { {+0.5f, -0.5f, -0.5f}, {0.0f, 1.0f}, {0.0f, 0.0f, -1.0f} },
  37.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, 0.0f, -1.0f} },
  38.  
  39.     // Third face (PX)
  40.     // First triangle
  41.     { {+0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {+1.0f, 0.0f, 0.0f} },
  42.     { {+0.5f, +0.5f, -0.5f}, {1.0f, 0.0f}, {+1.0f, 0.0f, 0.0f} },
  43.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {+1.0f, 0.0f, 0.0f} },
  44.     // Second triangle
  45.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {+1.0f, 0.0f, 0.0f} },
  46.     { {+0.5f, -0.5f, +0.5f}, {0.0f, 1.0f}, {+1.0f, 0.0f, 0.0f} },
  47.     { {+0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {+1.0f, 0.0f, 0.0f} },
  48.  
  49.     // Fourth face (MX)
  50.     // First triangle
  51.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} },
  52.     { {-0.5f, -0.5f, +0.5f}, {1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} },
  53.     { {-0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
  54.     // Second triangle
  55.     { {-0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
  56.     { {-0.5f, +0.5f, -0.5f}, {0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
  57.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} },
  58.  
  59.     // Fifth face (PY)
  60.     // First triangle
  61.     { {-0.5f, +0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, +1.0f, 0.0f} },
  62.     { {-0.5f, +0.5f, +0.5f}, {1.0f, 0.0f}, {0.0f, +1.0f, 0.0f} },
  63.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, +1.0f, 0.0f} },
  64.     // Second triangle
  65.     { {+0.5f, +0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, +1.0f, 0.0f} },
  66.     { {+0.5f, +0.5f, -0.5f}, {0.0f, 1.0f}, {0.0f, +1.0f, 0.0f} },
  67.     { {-0.5f, +0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, +1.0f, 0.0f} },
  68.  
  69.     // Sixth face (MY)
  70.     // First triangle
  71.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, -1.0f, 0.0f} },
  72.     { {+0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, {0.0f, -1.0f, 0.0f} },
  73.     { {+0.5f, -0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, -1.0f, 0.0f} },
  74.     // Second triangle
  75.     { {+0.5f, -0.5f, +0.5f}, {1.0f, 1.0f}, {0.0f, -1.0f, 0.0f} },
  76.     { {-0.5f, -0.5f, +0.5f}, {0.0f, 1.0f}, {0.0f, -1.0f, 0.0f} },
  77.     { {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, -1.0f, 0.0f} },
  78. };
  79.  
  80. #define vertex_list_count (sizeof(vertex_list)/sizeof(vertex_list[0]))
  81.  
  82. static DVLB_s* vshader_dvlb;
  83. static shaderProgram_s program;
  84. static int uLoc_projection, uLoc_model, uLoc_view;
  85. static int uLoc_lightVec, uLoc_lightHalfVec, uLoc_lightClr, uLoc_material;
  86. static C3D_Mtx projection;
  87. static float movx = 0.0, movz = 0.0;
  88. static float playerspeed = 0.2;
  89. static C3D_Mtx material =
  90. {
  91.     {
  92.     { { 0.0f, 0.2f, 0.2f, 0.2f } }, // Ambient
  93.     { { 0.0f, 0.4f, 0.4f, 0.4f } }, // Diffuse
  94.     { { 0.0f, 0.8f, 0.8f, 0.8f } }, // Specular
  95.     { { 1.0f, 0.0f, 0.0f, 0.0f } }, // Emission
  96.     }
  97. };
  98.  
  99. static void* vbo_data;
  100. static C3D_Tex kitten_tex;
  101. static float angleX = 0.0, angleY = 0.0;
  102.  
  103. static void sceneInit(void)
  104. {
  105.     // Load the vertex shader, create a shader program and bind it
  106.     vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size);
  107.     shaderProgramInit(&program);
  108.     shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]);
  109.     C3D_BindProgram(&program);
  110.  
  111.     // Get the location of the uniforms
  112.     uLoc_projection   = shaderInstanceGetUniformLocation(program.vertexShader, "projection");
  113.     uLoc_model        = shaderInstanceGetUniformLocation(program.vertexShader, "modelView");
  114.         uLoc_view         = shaderInstanceGetUniformLocation(program.vertexShader, "view");
  115.     uLoc_lightVec     = shaderInstanceGetUniformLocation(program.vertexShader, "lightVec");
  116.     uLoc_lightHalfVec = shaderInstanceGetUniformLocation(program.vertexShader, "lightHalfVec");
  117.     uLoc_lightClr     = shaderInstanceGetUniformLocation(program.vertexShader, "lightClr");
  118.     uLoc_material     = shaderInstanceGetUniformLocation(program.vertexShader, "material");
  119.  
  120.     // Configure attributes for use with the vertex shader
  121.     C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
  122.     AttrInfo_Init(attrInfo);
  123.     AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position
  124.     AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v1=texcoord
  125.     AttrInfo_AddLoader(attrInfo, 2, GPU_FLOAT, 3); // v2=normal
  126.  
  127.     // Compute the projection matrix
  128.     Mtx_PerspTilt(&projection, 80.0f*M_PI/180.0f, 400.0f/240.0f, 0.01f, 1000.0f, false);
  129.  
  130.     // Create the VBO (vertex buffer object)
  131.     vbo_data = linearAlloc(sizeof(vertex_list));
  132.     memcpy(vbo_data, vertex_list, sizeof(vertex_list));
  133.  
  134.     // Configure buffers
  135.     C3D_BufInfo* bufInfo = C3D_GetBufInfo();
  136.     BufInfo_Init(bufInfo);
  137.     BufInfo_Add(bufInfo, vbo_data, sizeof(vertex), 3, 0x210);
  138.  
  139.     // Load the texture and bind it to the first texture unit
  140.     C3D_TexInit(&kitten_tex, 64, 64, GPU_RGBA8);
  141.     C3D_TexUpload(&kitten_tex, kitten_bin);
  142.     C3D_TexSetFilter(&kitten_tex, GPU_LINEAR, GPU_NEAREST);
  143.     C3D_TexBind(0, &kitten_tex);
  144.  
  145.     // Configure the first fragment shading substage to blend the texture color with
  146.     // the vertex color (calculated by the vertex shader using a lighting algorithm)
  147.     // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
  148.     C3D_TexEnv* env = C3D_GetTexEnv(0);
  149.     C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0);
  150.     C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
  151.     C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
  152. }
  153.  
  154. static void sceneRender(u32 heldEvent)
  155. {
  156.  
  157.         //C3D_Mtx = glm mat
  158.         C3D_Mtx model;
  159.        
  160.     // Calculate the view matrix
  161.     C3D_Mtx view;
  162.     Mtx_Identity(&view);
  163.     Mtx_Translate(&view, 0.0, 0.0, 0.0, true);
  164.     /*Mtx_RotateX(&modelView, angleX, true);
  165.     Mtx_RotateY(&modelView, angleY, true);*/
  166.                 /*if(heldEvent & KEY_CPAD_UP ){
  167.  
  168.                         movz = movz + playerspeed;
  169.                         //printf("move up \n");
  170.  
  171.                 } else if(heldEvent & KEY_CPAD_DOWN ){
  172.  
  173.                         movz = movz - playerspeed;
  174.  
  175.                 }else if(heldEvent & KEY_CPAD_LEFT ){
  176.  
  177.                         movx = movx - playerspeed;
  178.  
  179.                 } else if(heldEvent & KEY_CPAD_RIGHT ){
  180.  
  181.                         movx = movx + playerspeed; //= 1 * playerspeed;
  182.                         //printf("move right \n");
  183.                 }*/
  184.  
  185.         //calculate model matrix
  186.         Mtx_Identity(&model);
  187.         Mtx_Translate(&model, 0.0, 0.0, 2.0f, true);
  188.  
  189.     //Rotate the cube each frame
  190.     /*angleX += M_PI / 180;
  191.     angleY += M_PI / 360;*/
  192.  
  193.     // Update the uniforms
  194.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_projection, &projection);
  195.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_model,  &model);
  196.         C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_view,  &view);
  197.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_material,   &material);
  198.     C3D_FVUnifSet(GPU_VERTEX_SHADER, uLoc_lightVec,     0.0f, 0.0f, -1.0f, 0.0f);
  199.     C3D_FVUnifSet(GPU_VERTEX_SHADER, uLoc_lightHalfVec, 0.0f, 0.0f, -1.0f, 0.0f);
  200.     C3D_FVUnifSet(GPU_VERTEX_SHADER, uLoc_lightClr,     1.0f, 1.0f,  1.0f, 1.0f);
  201.  
  202.     // Draw the VBO
  203.     C3D_DrawArrays(GPU_TRIANGLES, 0, vertex_list_count);
  204. }
  205.  
  206. static void sceneExit(void)
  207. {
  208.     // Free the texture
  209.     C3D_TexDelete(&kitten_tex);
  210.  
  211.     // Free the VBO
  212.     linearFree(vbo_data);
  213.  
  214.     // Free the shader program
  215.     shaderProgramFree(&program);
  216.     DVLB_Free(vshader_dvlb);
  217. }
  218.  
  219. int main()
  220. {
  221.     // Initialize graphics
  222.     gfxInitDefault();
  223.         consoleInit(GFX_BOTTOM, NULL);
  224.     C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
  225.  
  226.     // Initialize the render target
  227.     C3D_RenderTarget* target = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
  228.     C3D_RenderTargetSetClear(target, C3D_CLEAR_ALL, CLEAR_COLOR, 0);
  229.     C3D_RenderTargetSetOutput(target, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
  230.  
  231.     // Initialize the scene
  232.     sceneInit();
  233.  
  234.     // Main loop
  235.     while (aptMainLoop())
  236.     {
  237.         hidScanInput();
  238.  
  239.         // Respond to user input
  240.         u32 downEvent = hidKeysDown();
  241.                 u32 heldEvent = hidKeysHeld();
  242.  
  243.         if (downEvent & KEY_START){
  244.             break; // break in order to return to hbmenu
  245.                 }else if (downEvent & KEY_A){
  246.             printf("Hello world! \n");
  247.         }
  248.                
  249.                   circlePosition pos;
  250.                  //Read the CirclePad position
  251.                  hidCircleRead(&pos);
  252.                  //Print the CirclePad position
  253.                  printf("\x1b[2;0H%04d; %04d", pos.dx, pos.dy);
  254.  
  255.         // Render the scene
  256.         C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
  257.             C3D_FrameDrawOn(target);
  258.             sceneRender(heldEvent);
  259.         C3D_FrameEnd(0);
  260.     }
  261.  
  262.     // Deinitialize the scene
  263.     sceneExit();
  264.  
  265.     // Deinitialize graphics
  266.     C3D_Fini();
  267.     gfxExit();
  268.     return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement