Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <tice.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <graphx.h>
- #include <keypadc.h>
- void setup();
- void run();
- void stop();
- void calculate();
- void drawEdges();
- void rotate2d(float* x, float* y, float rad);
- const uint8_t numVerts = 8;
- const uint8_t numEdges = 12;
- const uint16_t cx = LCD_WIDTH/2;
- const uint16_t cy = LCD_HEIGHT/2;
- float cameraPos[3] = {0, 0, -5};
- float cameraRot[2] = {0, 0};
- float sensitivity = 0.5;
- float vertex[3][numVerts] = {{-1, -1, 1, 1, -1, -1, 1, 1},
- {-1, 1, 1, -1, -1, 1, 1, -1},
- {-1, -1, -1, -1, 1, 1, 1, 1}};
- float edges[2][numEdges] = {{1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8},
- {5, 6, 7, 8, 2, 3, 4, 1, 6, 7, 8, 5}};
- int coords[2][numVerts];
- void main(void) {
- prgm_CleanUp();
- setup();
- run();
- stop();
- prgm_CleanUp();
- }
- void setup() {
- gfx_Begin(gfx_8bpp);
- gfx_FillScreen(gfx_black);
- gfx_SetColor(gfx_white);
- }
- void run() {
- bool run = 1;
- uint8_t key;
- while(run) {
- float x = sensitivity*sin(cameraRot[1]);
- float y = sensitivity*cos(cameraRot[1]);
- key = kb_ScanGroup(kb_group_1);
- switch (key) {
- case kb_2nd:
- cameraPos[1]+=sensitivity;
- break;
- case kb_Mode:
- cameraPos[0]+=x;
- cameraPos[2]+=y;
- break;
- }
- key = kb_ScanGroup(kb_group_2);
- switch (key) {
- case kb_Math:
- cameraPos[1]-=sensitivity;
- break;
- case kb_Alpha:
- cameraPos[0]-=y;
- cameraPos[2]+=x;
- break;
- }
- key = kb_ScanGroup(kb_group_3);
- if (key == kb_Apps) { cameraPos[0]-=x; cameraPos[2]-=y; }
- key = kb_ScanGroup(kb_group_4);
- if (key == kb_Stat) { cameraPos[0]+=y; cameraPos[2]-=x; }
- key = kb_ScanGroup(kb_group_7);
- switch (key) {
- case kb_Up:
- cameraRot[0]+=sensitivity/3;
- break;
- case kb_Down:
- cameraRot[0]-=sensitivity/3;
- break;
- case kb_Left:
- cameraRot[1]-=sensitivity/3;
- break;
- case kb_Right:
- cameraRot[1]+=sensitivity/3;
- break;
- }
- calculate();
- gfx_ZeroScreen();
- drawEdges();
- key = kb_ScanGroup(kb_group_6);
- if (key == kb_Clear) run = 0;
- else
- while(!os_GetCSC());
- color++;
- }
- }
- void stop() {
- gfx_End();
- }
- void calculate() {
- uint8_t iterator;
- float x;
- float y;
- float z;
- float f;
- for (iterator = 0; iterator < numVerts; iterator++) {
- x = vertex[0][iterator];
- y = vertex[1][iterator];
- z = vertex[2][iterator];
- x -= cameraPos[0];
- y -= cameraPos[1];
- z -= cameraPos[2];
- rotate2d(&x, &z, cameraRot[1]);
- rotate2d(&y, &z, cameraRot[0]);
- f = (float)200/z;
- x = x*f;
- y = y*f;
- coords[0][iterator] = (int)x+cx;
- coords[1][iterator] = cy-(int)y;
- zs[iterator] = z;
- }
- }
- void drawEdges() {
- uint8_t iterator;
- uint8_t start;
- uint8_t end;
- gfx_SetColor(232);
- for (iterator = 0; iterator < numEdges; iterator++) {
- start = edges[0][iterator]-1;
- end = edges[1][iterator]-1;
- gfx_Line(coords[0][start], coords[1][start], coords[0][end], coords[1][end]);
- }
- }
- void rotate2d(float* x, float* y, float rad) {
- float c = cos(rad);
- float s = sin(rad);
- float rox = *x;
- float roy = *y;
- float res;
- rox = *x*c-*y*s;
- roy = *y*c+*x*s;
- *x = rox;
- *y = roy;
- }
Advertisement
Add Comment
Please, Sign In to add comment