asdf

Dependencies:   mbed

Fork of manworm_ticker_tv by Bayley Wang

Revision:
9:2a47b9ff8911
Child:
10:1163fb31b0a7
diff -r caeb6582cdc1 -r 2a47b9ff8911 gfx.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gfx.cpp	Sat Mar 10 05:09:55 2018 +0000
@@ -0,0 +1,287 @@
+#define N_PTS 500
+#define N_LNS 2000
+#define N_HEIGHTS 100
+#define N_SIDE 10
+
+#include "gfx.h"
+#include "mbed.h"
+#include "main.h"
+
+int n_pts = 0;
+int n_lns = 0;
+int n_frame = 0;
+int n_cubes = 0;
+
+char gfx_stat[40];
+
+point_t* pts;
+line_t* lns;
+cube_t* cubes;
+
+float heights[N_HEIGHTS];
+
+float c_i[3] = {-.1, -.1, -.1};
+float c1i[3] = {.1, -.1, -.1};
+float c2i[3] = {-.1, .1, -.1};
+float c3i[3] = {-.1, -.1, .1};
+float c_j[3] = {.1, .1, .1};
+float c1j[3] = {.1, .1, -.1};
+float c2j[3] = {-.1, .1, .1};
+float c3j[3] = {.1, -.1, .1};
+float x_rotmat[3][3] = {{1, 0, 0}, {0, cos(.02), -sin(.02)}, {0, sin(.02), cos(.02)}};
+float y_rotmat[3][3] = {{cos(.02), 0, sin(.02)}, {0, 1, 0}, { -sin(.02), 0, cos(.02)}};
+float z_rotmat[3][3] = {{cos(.02), -sin(.02), 0}, {sin(.02), cos(.02), 0}, {0, 0, 1}};
+float temp[3] = {0, 0, 0};
+float temp2[3] = {0, 0, 0};
+
+unsigned int m_z=12434,m_w=33254;
+void draw_lines();
+ 
+unsigned int rnd() {
+    m_z = 36969 * (m_z & 65535) + (m_z >>16);
+    m_w = 18000 * (m_w & 65535) + (m_w >>16);
+    return ((m_z <<16) + m_w);
+}
+
+
+
+void mat_vec_mult(float a[][3], float *b, float *c, uint8_t inv) {
+  for (int i = 0; i < 3; i++) {
+    if(inv)
+        c[i] =  (a[0][i] * b[0] + a[1][i] * b[1] + a[2][i] * b[2]);
+    else
+        c[i] =  (a[i][0] * b[0] + a[i][1] * b[1] + a[i][2] * b[2]);
+  }
+}
+
+void point_mult(float a[][3], point_t* b, float* c, uint8_t inv)
+{
+      for (int i = 0; i < 3; i++) {
+    if(inv)
+    {
+        c[i] =  (a[0][i] * b->x + a[1][i] * b->y + a[2][i] * b->z);
+    }
+    else
+    {
+        c[i] =  (a[i][0] * b->x + a[i][1] * b->y + a[i][2] * b->z);
+    }
+  }
+}
+
+
+
+void add_cube()
+{
+    
+}
+
+char* get_gfx_stat()
+{
+    n_frame++;
+    sprintf(gfx_stat,"%d/%d, %d/%d, %d",n_lns,N_LNS,n_pts,N_PTS,n_frame%1000);
+    return gfx_stat;
+}
+
+point_t* new_point()
+{
+    if(n_pts >= N_PTS - 1) return NULL;
+    return &pts[n_pts++];
+}
+
+line_t* new_linet()
+{
+    if(n_lns >= N_LNS - 1) return NULL;
+    return &lns[n_lns++];
+}
+
+void rotate_cube(uint8_t x, uint8_t y, uint8_t z,uint8_t inv);
+
+
+void new_frame(char c)
+{
+    draw_lines();
+    if(c == 'w')
+        rotate_cube(1,0,0,0);
+    if(c == 's')
+        rotate_cube(1,0,0,1);
+    if(c == 'a')
+        rotate_cube(0,1,0,0);
+    if(c == 'd')
+        rotate_cube(0,1,0,1);
+    if(c == 'q')
+        rotate_cube(0,0,1,0);
+    if(c == 'e')
+        rotate_cube(0,0,1,1);
+    else
+        rotate_cube(1,0,0,1);
+    draw_gfx_line(c_i[0],c_i[1],c1i[0],c1i[1]);
+    draw_gfx_line(c_i[0],c_i[1],c2i[0],c2i[1]);
+    draw_gfx_line(c_i[0],c_i[1],c3i[0],c3i[1]);
+    
+    draw_gfx_line(c_j[0],c_j[1],c1j[0],c1j[1]);
+    draw_gfx_line(c_j[0],c_j[1],c2j[0],c2j[1]);
+    draw_gfx_line(c_j[0],c_j[1],c3j[0],c3j[1]);
+    
+    draw_gfx_line(c3i[0],c3i[1],c3j[0],c3j[1]);
+    draw_gfx_line(c3i[0],c3i[1],c2j[0],c2j[1]);
+    
+    draw_gfx_line(c1i[0],c1i[1],c1j[0],c1j[1]);
+    draw_gfx_line(c1i[0],c1i[1],c3j[0],c3j[1]);
+    
+    draw_gfx_line(c2i[0],c2i[1],c2j[0],c2j[1]);
+    draw_gfx_line(c2i[0],c2i[1],c1j[0],c1j[1]);
+}
+
+void rotate_cube(uint8_t x, uint8_t y, uint8_t z, uint8_t inv)
+{
+      if (x) {
+    //multiply the cube point by the rotation matrix and store the array in temp
+    mat_vec_mult(x_rotmat, c_i, temp,inv);
+    //copy temp vector into point vector, updating it.
+    memcpy(c_i, temp, sizeof(c_i));
+    mat_vec_mult(x_rotmat, c1i, temp,inv);
+    memcpy(c1i, temp, sizeof(c1i));
+    mat_vec_mult(x_rotmat, c2i, temp,inv);
+    memcpy(c2i, temp, sizeof(c2i));
+    mat_vec_mult(x_rotmat, c3i, temp,inv);
+    memcpy(c3i, temp, sizeof(c3i));
+    mat_vec_mult(x_rotmat, c_j, temp,inv);
+    memcpy(c_j, temp, sizeof(c_j));
+    mat_vec_mult(x_rotmat, c1j, temp,inv);
+    memcpy(c1j, temp, sizeof(c1j));
+    mat_vec_mult(x_rotmat, c2j, temp,inv);
+    memcpy(c2j, temp, sizeof(c2j));
+    mat_vec_mult(x_rotmat, c3j, temp,inv);
+    memcpy(c3j, temp, sizeof(c3j));
+  }
+  if (y) {
+
+    mat_vec_mult(y_rotmat, c_i, temp,inv);
+    memcpy(c_i, temp, sizeof(c_i));
+    mat_vec_mult(y_rotmat, c1i, temp,inv);
+    memcpy(c1i, temp, sizeof(c1i));
+    mat_vec_mult(y_rotmat, c2i, temp,inv);
+    memcpy(c2i, temp, sizeof(c2i));
+    mat_vec_mult(y_rotmat, c3i, temp,inv);
+    memcpy(c3i, temp, sizeof(c3i));
+    mat_vec_mult(y_rotmat, c_j, temp,inv);
+    memcpy(c_j, temp, sizeof(c_j));
+    mat_vec_mult(y_rotmat, c1j, temp,inv);
+    memcpy(c1j, temp, sizeof(c1j));
+    mat_vec_mult(y_rotmat, c2j, temp,inv);
+    memcpy(c2j, temp, sizeof(c2j));
+    mat_vec_mult(y_rotmat, c3j, temp,inv);
+    memcpy(c3j, temp, sizeof(c3j));
+  }
+  //
+  if (z) {
+    mat_vec_mult(z_rotmat, c_i, temp,inv);
+    memcpy(c_i, temp, sizeof(c_i));
+    mat_vec_mult(z_rotmat, c1i, temp,inv);
+    memcpy(c1i, temp, sizeof(c1i));
+    mat_vec_mult(z_rotmat, c2i, temp,inv);
+    memcpy(c2i, temp, sizeof(c2i));
+    mat_vec_mult(z_rotmat, c3i, temp,inv);
+    memcpy(c3i, temp, sizeof(c3i));
+    mat_vec_mult(z_rotmat, c_j, temp,inv);
+    memcpy(c_j, temp, sizeof(c_j));
+    mat_vec_mult(z_rotmat, c1j, temp,inv);
+    memcpy(c1j, temp, sizeof(c1j));
+    mat_vec_mult(z_rotmat, c2j, temp,inv);
+    memcpy(c2j, temp, sizeof(c2j));
+    mat_vec_mult(z_rotmat, c3j, temp,inv);
+    memcpy(c3j, temp, sizeof(c3j));
+  }
+}
+
+void init_gfx()
+{
+    new_line();
+    draw_vincent_string("GFX START!");
+    pts = (point_t*)malloc(N_PTS * sizeof(point_t));
+    lns = (line_t*)malloc(N_LNS * sizeof(line_t)); 
+    cubes = (cube_t*)malloc(10 * sizeof(cube_t));
+    
+    for(int i = 0; i < N_HEIGHTS; i++)
+    {
+        uint32_t a = rnd();
+        heights[i] = .2f * (float)a / (float)INT_MAX;
+        new_point();
+        pts[i].z = heights[i];
+        pts[i].y = 0.001f;
+        pts[i].x = 0.001f;
+        //sprintf("%f\n",heights[i]);
+    }
+    
+    
+    for(int xi = 0; xi < N_SIDE; xi++)
+    {
+        for(int yi = 0; yi < N_SIDE; yi++)
+        {
+            // base
+            int pti = xi + N_SIDE*yi;
+            pts[pti].x = -.5f + ( (float) (xi) )/( (float) (N_SIDE) );
+            pts[pti].y = -.5f + ( (float) (yi) )/( (float) (N_SIDE) );
+            // go x+?
+            if(  (xi < (N_SIDE - 1)))
+            {
+                int ptn = (xi+1) + N_SIDE*yi;
+                line_t* lin = new_linet();
+                lin->a = pts + pti;
+                lin->b = pts + ptn;    
+            }
+            // go x-?
+            if( (xi > 0))
+            {
+                int ptn = (xi-1) + N_SIDE*yi;
+                line_t* lin = new_linet();
+                lin->a = pts + pti;
+                lin->b = pts + ptn;    
+            }      
+            
+            // go y+
+            if(  (yi < (N_SIDE - 1)))
+            {
+                int ptn = (xi) + N_SIDE*(1+yi);
+                line_t* lin = new_linet();
+                lin->a = pts + pti;
+                lin->b = pts + ptn;    
+            }
+            
+            // go y-?
+            if( (yi > 0))
+            {
+                int ptn = (xi) + N_SIDE*(yi - 1);
+                line_t* lin = new_linet();
+                lin->a = pts + pti;
+                lin->b = pts + ptn;    
+            }    
+            
+                     
+        }
+    }
+}
+
+void draw_lines()
+{
+    for(int i = 0; i < n_pts; i++)
+    {
+        point_mult(x_rotmat,pts + i,temp,0);
+        pts[i].x = temp[0];
+        pts[i].y = temp[1];
+        pts[i].z = temp[2];   
+        point_mult(y_rotmat,pts + i,temp,0);
+        pts[i].x = temp[0];
+        pts[i].y = temp[1];
+        pts[i].z = temp[2];   
+    }
+    
+    for(int i = 0; i < n_lns; i++)
+    {
+        line_t* l = lns + i;
+        //printf("i: %d, a: %d, b: %d\n",i,l->a,l->b);
+        //printf("%.3f, %.3f, %.3f, %.3f\n",l->a->x,l->a->y,l->b->x,l->b->y);
+        draw_gfx_line(l->a->x,l->a->y,l->b->x,l->b->y);
+        
+    }   
+}
\ No newline at end of file