MMBC

C3D

Jun 15th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <tice.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <graphx.h>
  10. #include <keypadc.h>
  11.  
  12. void setup();
  13. void run();
  14. void stop();
  15. void calculate();
  16. void drawEdges();
  17. void rotate2d(float* x, float* y, float rad);
  18.  
  19. const uint8_t numVerts = 8;
  20. const uint8_t numEdges = 12;
  21.  
  22. const uint16_t cx = LCD_WIDTH/2;
  23. const uint16_t cy = LCD_HEIGHT/2;
  24.  
  25. float cameraPos[3] = {0, 0, -5};
  26. float cameraRot[2] = {0, 0};
  27.  
  28. float sensitivity = 0.5;
  29.  
  30. float vertex[3][numVerts] = {{-1, -1, 1, 1, -1, -1, 1, 1},
  31.                            {-1, 1, 1, -1, -1, 1, 1, -1},
  32.                            {-1, -1, -1, -1, 1, 1, 1, 1}};
  33. float edges[2][numEdges] = {{1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8},
  34.                           {5, 6, 7, 8, 2, 3, 4, 1, 6, 7, 8, 5}};
  35.                              
  36. int coords[2][numVerts];
  37.  
  38. void main(void) {
  39.     prgm_CleanUp();
  40.    
  41.     setup();
  42.     run();
  43.     stop();
  44.    
  45.     prgm_CleanUp();
  46. }
  47.  
  48. void setup() {
  49.     gfx_Begin(gfx_8bpp);
  50.     gfx_FillScreen(gfx_black);
  51.     gfx_SetColor(gfx_white);
  52. }
  53.  
  54. void run() {
  55.     bool run = 1;
  56.     uint8_t key;
  57.     while(run) {
  58.         float x = sensitivity*sin(cameraRot[1]);
  59.         float y = sensitivity*cos(cameraRot[1]);
  60.        
  61.         key = kb_ScanGroup(kb_group_1);
  62.         switch (key) {
  63.         case kb_2nd:
  64.             cameraPos[1]+=sensitivity;
  65.             break;
  66.         case kb_Mode:
  67.             cameraPos[0]+=x;
  68.             cameraPos[2]+=y;
  69.             break;
  70.         }
  71.        
  72.         key = kb_ScanGroup(kb_group_2);
  73.         switch (key) {
  74.         case kb_Math:
  75.             cameraPos[1]-=sensitivity;
  76.             break;
  77.         case kb_Alpha:
  78.             cameraPos[0]-=y;
  79.             cameraPos[2]+=x;
  80.             break;
  81.         }
  82.        
  83.         key = kb_ScanGroup(kb_group_3);
  84.         if (key == kb_Apps) { cameraPos[0]-=x; cameraPos[2]-=y; }
  85.        
  86.         key = kb_ScanGroup(kb_group_4);
  87.         if (key == kb_Stat) { cameraPos[0]+=y; cameraPos[2]-=x; }
  88.        
  89.         key = kb_ScanGroup(kb_group_7);
  90.         switch (key) {
  91.         case kb_Up:
  92.             cameraRot[0]+=sensitivity/3;
  93.             break;
  94.         case kb_Down:
  95.             cameraRot[0]-=sensitivity/3;
  96.             break;
  97.         case kb_Left:
  98.             cameraRot[1]-=sensitivity/3;
  99.             break;
  100.         case kb_Right:
  101.             cameraRot[1]+=sensitivity/3;
  102.             break;
  103.         }
  104.        
  105.         calculate();
  106.         gfx_ZeroScreen();
  107.         drawEdges();
  108.        
  109.         key = kb_ScanGroup(kb_group_6);
  110.         if (key == kb_Clear) run = 0;
  111.         else
  112.         while(!os_GetCSC());
  113.         color++;
  114.     }
  115. }
  116.  
  117. void stop() {
  118.     gfx_End();
  119. }
  120.  
  121. void calculate() {
  122.     uint8_t iterator;
  123.     float x;
  124.     float y;
  125.     float z;
  126.     float f;
  127.     for (iterator = 0; iterator < numVerts; iterator++) {
  128.         x = vertex[0][iterator];
  129.         y = vertex[1][iterator];
  130.         z = vertex[2][iterator];
  131.        
  132.         x -= cameraPos[0];
  133.         y -= cameraPos[1];
  134.         z -= cameraPos[2];
  135.        
  136.         rotate2d(&x, &z, cameraRot[1]);
  137.         rotate2d(&y, &z, cameraRot[0]);
  138.        
  139.         f = (float)200/z;
  140.        
  141.         x = x*f;
  142.         y = y*f;
  143.        
  144.         coords[0][iterator] = (int)x+cx;
  145.         coords[1][iterator] = cy-(int)y;
  146.         zs[iterator] = z;
  147.     }
  148. }
  149.  
  150. void drawEdges() {
  151.     uint8_t iterator;
  152.     uint8_t start;
  153.     uint8_t end;
  154.     gfx_SetColor(232);
  155.     for (iterator = 0; iterator < numEdges; iterator++) {
  156.         start = edges[0][iterator]-1;
  157.         end = edges[1][iterator]-1;
  158.         gfx_Line(coords[0][start], coords[1][start], coords[0][end], coords[1][end]);
  159.     }
  160. }
  161.  
  162. void rotate2d(float* x, float* y, float rad) {
  163.     float c = cos(rad);
  164.     float s = sin(rad);
  165.     float rox = *x;
  166.     float roy = *y;
  167.     float res;
  168.    
  169.     rox = *x*c-*y*s;
  170.     roy = *y*c+*x*s;
  171.    
  172.     *x = rox;
  173.     *y = roy;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment