MMBC

Vertex3D demo

Sep 21st, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.20 KB | None | 0 0
  1. #include <os.h>
  2. #include <keys.h>
  3. #include <SDL/SDL_config.h>
  4. #include <SDL/SDL.h>
  5. #include <math.h>
  6.  
  7. #define BACKFACE_CULL 1
  8.  
  9. const Uint16 RGB332to565lookupTable[256] = {
  10.     0x0000, 0x000a, 0x0015, 0x001f, 0x0120, 0x012a, 0x0135, 0x013f,
  11.     0x0240, 0x024a, 0x0255, 0x025f, 0x0360, 0x036a, 0x0375, 0x037f,
  12.     0x0480, 0x048a, 0x0495, 0x049f, 0x05a0, 0x05aa, 0x05b5, 0x05bf,
  13.     0x06c0, 0x06ca, 0x06d5, 0x06df, 0x07e0, 0x07ea, 0x07f5, 0x07ff,
  14.     0x2000, 0x200a, 0x2015, 0x201f, 0x2120, 0x212a, 0x2135, 0x213f,
  15.     0x2240, 0x224a, 0x2255, 0x225f, 0x2360, 0x236a, 0x2375, 0x237f,
  16.     0x2480, 0x248a, 0x2495, 0x249f, 0x25a0, 0x25aa, 0x25b5, 0x25bf,
  17.     0x26c0, 0x26ca, 0x26d5, 0x26df, 0x27e0, 0x27ea, 0x27f5, 0x27ff,
  18.     0x4800, 0x480a, 0x4815, 0x481f, 0x4920, 0x492a, 0x4935, 0x493f,
  19.     0x4a40, 0x4a4a, 0x4a55, 0x4a5f, 0x4b60, 0x4b6a, 0x4b75, 0x4b7f,
  20.     0x4c80, 0x4c8a, 0x4c95, 0x4c9f, 0x4da0, 0x4daa, 0x4db5, 0x4dbf,
  21.     0x4ec0, 0x4eca, 0x4ed5, 0x4edf, 0x4fe0, 0x4fea, 0x4ff5, 0x4fff,
  22.     0x6800, 0x680a, 0x6815, 0x681f, 0x6920, 0x692a, 0x6935, 0x693f,
  23.     0x6a40, 0x6a4a, 0x6a55, 0x6a5f, 0x6b60, 0x6b6a, 0x6b75, 0x6b7f,
  24.     0x6c80, 0x6c8a, 0x6c95, 0x6c9f, 0x6da0, 0x6daa, 0x6db5, 0x6dbf,
  25.     0x6ec0, 0x6eca, 0x6ed5, 0x6edf, 0x6fe0, 0x6fea, 0x6ff5, 0x6fff,
  26.     0x9000, 0x900a, 0x9015, 0x901f, 0x9120, 0x912a, 0x9135, 0x913f,
  27.     0x9240, 0x924a, 0x9255, 0x925f, 0x9360, 0x936a, 0x9375, 0x937f,
  28.     0x9480, 0x948a, 0x9495, 0x949f, 0x95a0, 0x95aa, 0x95b5, 0x95bf,
  29.     0x96c0, 0x96ca, 0x96d5, 0x96df, 0x97e0, 0x97ea, 0x97f5, 0x97ff,
  30.     0xb000, 0xb00a, 0xb015, 0xb01f, 0xb120, 0xb12a, 0xb135, 0xb13f,
  31.     0xb240, 0xb24a, 0xb255, 0xb25f, 0xb360, 0xb36a, 0xb375, 0xb37f,
  32.     0xb480, 0xb48a, 0xb495, 0xb49f, 0xb5a0, 0xb5aa, 0xb5b5, 0xb5bf,
  33.     0xb6c0, 0xb6ca, 0xb6d5, 0xb6df, 0xb7e0, 0xb7ea, 0xb7f5, 0xb7ff,
  34.     0xd800, 0xd80a, 0xd815, 0xd81f, 0xd920, 0xd92a, 0xd935, 0xd93f,
  35.     0xda40, 0xda4a, 0xda55, 0xda5f, 0xdb60, 0xdb6a, 0xdb75, 0xdb7f,
  36.     0xdc80, 0xdc8a, 0xdc95, 0xdc9f, 0xdda0, 0xddaa, 0xddb5, 0xddbf,
  37.     0xdec0, 0xdeca, 0xded5, 0xdedf, 0xdfe0, 0xdfea, 0xdff5, 0xdfff,
  38.     0xf800, 0xf80a, 0xf815, 0xf81f, 0xf920, 0xf92a, 0xf935, 0xf93f,
  39.     0xfa40, 0xfa4a, 0xfa55, 0xfa5f, 0xfb60, 0xfb6a, 0xfb75, 0xfb7f,
  40.     0xfc80, 0xfc8a, 0xfc95, 0xfc9f, 0xfda0, 0xfdaa, 0xfdb5, 0xfdbf,
  41.     0xfec0, 0xfeca, 0xfed5, 0xfedf, 0xffe0, 0xffea, 0xfff5, 0xffff};
  42.  
  43. #define Vertice Vec2i
  44.  
  45. typedef struct {
  46.     float x, y, z, vr, hr;
  47. } Camera;
  48.  
  49. typedef struct {
  50.     float x, y, z;
  51. } Vec3f;
  52.  
  53. typedef struct {
  54.     int x, y;
  55. } Vec2i;
  56.  
  57. typedef struct {
  58.     int x, y, z;
  59. } Vec3i;
  60.  
  61. int numVerts = 8;
  62. Vec3f verts[] = {
  63.     {-1, -1, -1},
  64.     {-1,  1, -1},
  65.     { 1,  1, -1},
  66.     { 1, -1, -1},
  67.     {-1, -1,  1},
  68.     {-1,  1,  1},
  69.     { 1,  1,  1},
  70.     { 1, -1,  1}
  71. };
  72.  
  73. int numFaces = 12;
  74. Vec3i faces[] = {
  75.     {1, 2, 3},
  76.     {1, 3, 4},
  77.     {1, 4, 8},
  78.     {1, 8, 5},
  79.     {1, 5, 6},
  80.     {1, 6, 2},
  81.     {7, 3, 2},
  82.     {7, 2, 6},
  83.     {7, 6, 5},
  84.     {7, 5, 8},
  85.     {7, 8, 4},
  86.     {7, 4, 3}
  87. };
  88. Vec3f vertsColor[] = {
  89.     {255       ,       0, 255},
  90.     {255-1*21.5,   21.25, 255},
  91.     {255-2*21.5, 2*21.25, 255},
  92.     {255-3*21.5, 3*21.25, 255},
  93.     {255-4*21.5, 4*21.25, 255},
  94.     {255-5*21.5, 5*21.25, 255},
  95.     {255-6*21.5, 6*21.25, 255},
  96.     {255-7*21.5, 7*21.25, 255}
  97. };
  98.  
  99. const Vec3f pxv = {1,0,0};
  100. const Vec3f nxv = {-1,0,0};
  101. const Vec3f pyv = {0,1,0};
  102. const Vec3f nyv = {0,-1,0};
  103. const Vec3f pzv = {0,0,1};
  104. const Vec3f nzv = {0,0,-1};
  105.                  
  106. Uint16 MapRGB565(int r, int g, int b) {
  107.     float fr = (float)r;
  108.     float fg = (float)g;
  109.     float fb = (float)b;
  110.     fr /= 255;
  111.     fg /= 255;
  112.     fb /= 255;
  113.     int ir = (int)(fr*31*2048);
  114.     int ig = (int)(fg*63*32);
  115.     int ib = (int)(fb*31);
  116.     return (Uint16)(ir+ig+ib);
  117. }
  118.  
  119. void setPixel(SDL_Surface *screen, int x, int y, Uint16 color) {
  120.     volatile unsigned char *ptr = screen->pixels;
  121.     ptr += x*2+y*640;
  122.     if (x > 0 && x < 320 && y > 0 && y < 240)
  123.         *(volatile unsigned short*)ptr = color;
  124. }
  125.  
  126. void drawLine(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint16 color) {
  127.     int dx =  abs(x1-x0), cx = x0 < x1 ? 1 : -1;
  128.     int dy = -abs(y1-y0), cy = y0 < y1 ? 1 : -1;
  129.     int err = dx+dy, e2;
  130.     for(;;) {
  131.         setPixel(screen, x0, y0, color);
  132.         if (x0==x1 && y0==y1) break;
  133.         e2 = 2*err;
  134.         if (e2 >= dy) { err += dy; x0 += cx; }
  135.         if (e2 <= dx) { err += dx; y0 += cy; }
  136.     }
  137. }
  138.  
  139. void HLine(SDL_Surface *screen, int x0, int x1, int y, Uint16 color) {
  140.     setPixel(screen, x0, y, color);
  141.     if (x0 == x1) return;
  142.     int cx = x0 < x1 ? 1 : -1;
  143.     for (int x = x0+cx; x != x1; x+=cx) setPixel(screen, x, y, color);
  144.     setPixel(screen, x1, y, color);
  145. }
  146.  
  147. void stdTriangleFlatBottom(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Uint16 color) {
  148.     float invslope1 = (float)(v2.x-v1.x) / (float)(v2.y-v1.y);
  149.     float invslope2 = (float)(v3.x-v1.x) / (float)(v3.y-v1.y);
  150.  
  151.     float curx1 = v1.x;
  152.     float curx2 = v1.x;
  153.  
  154.     for (int scanLineY = v1.y; scanLineY <= v2.y; scanLineY++) {
  155.         HLine(screen, (int)curx1, (int)curx2, scanLineY, color);
  156.         curx1 += invslope1;
  157.         curx2 += invslope2;
  158.     }
  159. }
  160.  
  161. void stdTriangleFlatTop(SDL_Surface *screen, Vertice t1, Vertice t2, Vertice t3, Uint16 color) {
  162.     float invslope1 = (float)(t3.x-t1.x) / (float)(t3.y-t1.y);
  163.     float invslope2 = (float)(t3.x-t2.x) / (float)(t3.y-t2.y);
  164.  
  165.     float curx1 = t3.x;
  166.     float curx2 = t3.x;
  167.  
  168.     for (int scanLineY = t3.y; scanLineY >= t1.y; scanLineY--) {
  169.         HLine(screen, (int)curx1, (int)curx2, scanLineY, color);
  170.         curx1 -= invslope1;
  171.         curx2 -= invslope2;
  172.     }
  173. }
  174.  
  175. void stdTriangle(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Uint16 color) {
  176.     Vertice t1 = {0, 0}, t2 = t1, t3 = t1;
  177.     if (v1.y < v2.y && v1.y < v3.y) {
  178.         t1 = v1;
  179.         t2 = (v2.y < v3.y ? v2 : v3);
  180.         t3 = (v2.y < v3.y ? v3 : v2);
  181.     }
  182.     else if (v2.y < v3.y) {
  183.         t1 = v2;
  184.         t2 = (v1.y < v3.y ? v1 : v3);
  185.         t3 = (v1.y < v3.y ? v3 : v1);
  186.     }
  187.     else {
  188.         t1 = v3;
  189.         t2 = (v1.y < v2.y ? v1 : v2);
  190.         t3 = (v1.y < v2.y ? v2 : v1);
  191.     }
  192.  
  193.     if (t2.y == t3.y) {
  194.         stdTriangleFlatBottom(screen, t1, t2, t3, color);
  195.     }
  196.     else if (t1.y == t2.y) {
  197.         stdTriangleFlatTop(screen, t1, t2, t3, color);
  198.     }
  199.     else {
  200.         Vertice t4 = {(int)(t1.x+((float)(t2.y-t1.y)/(float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y};
  201.         stdTriangleFlatBottom(screen, t1, t2, t4, color);
  202.         stdTriangleFlatTop(screen, t2, t4, t3, color);
  203.     }
  204. }
  205.  
  206. void IHLine(SDL_Surface *screen, int x0, int x1, int y, Vec3f c0, Vec3f c1) {
  207.     setPixel(screen, x0, y, (Uint16)SDL_MapRGB(screen->format, c0.x, c0.y, c0.z));
  208.     if (x0 == x1) return;
  209.     int cx = x0 < x1 ? 1 : -1;
  210.     for (int x = x0+cx; x != x1; x+=cx) {
  211.         float w1 = 1/((float)abs(x-x0));
  212.         float w2 = 1/((float)abs(x-x1));
  213.         float r = (w1*c0.x+w2*c1.x)/(w1+w2);
  214.         float g = (w1*c0.y+w2*c1.y)/(w1+w2);
  215.         float b = (w1*c0.z+w2*c1.z)/(w1+w2);
  216.         setPixel(screen, x, y, (Uint16)SDL_MapRGB(screen->format, (int)r, (int)g, (int)b));
  217.     }
  218.     setPixel(screen, x1, y, (Uint16)SDL_MapRGB(screen->format, c1.x, c1.y, c1.z));
  219. }  
  220.  
  221. void stdITriangleFlatBottom(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Vec3f c1, Vec3f c2, Vec3f c3) {
  222.     float invslope1 = (float)(v2.x-v1.x) / (float)(v2.y-v1.y);
  223.     float invslope2 = (float)(v3.x-v1.x) / (float)(v3.y-v1.y);
  224.  
  225.     float curx1 = v1.x;
  226.     float curx2 = v1.x;
  227.  
  228.     for (int scanLineY = v1.y; scanLineY <= v2.y; scanLineY++) {
  229.         float we1 = 1/((float)abs(scanLineY-v1.y));
  230.         float we2 = 1/((float)abs(scanLineY-v2.y));
  231.         Vec3f ce1 = {(we1*c1.x+we2*c2.x)/(we1+we2), (we1*c1.y+we2*c2.y)/(we1+we2), (we1*c1.z+we2*c2.z)/(we1+we2)};
  232.         Vec3f ce2 = {(we1*c1.x+we2*c3.x)/(we1+we2), (we1*c1.y+we2*c3.y)/(we1+we2), (we1*c1.z+we2*c3.z)/(we1+we2)};
  233.         IHLine(screen, (int)curx1, (int)curx2, scanLineY, ce1, ce2);
  234.         curx1 += invslope1;
  235.         curx2 += invslope2;
  236.     }
  237.     IHLine(screen, v2.x, v3.x, v2.y, c2, c3);
  238. }
  239.  
  240. void stdITriangleFlatTop(SDL_Surface *screen, Vertice t1, Vertice t2, Vertice t3, Vec3f c1, Vec3f c2, Vec3f c3) {
  241.     float invslope1 = (float)(t3.x-t1.x) / (float)(t3.y-t1.y);
  242.     float invslope2 = (float)(t3.x-t2.x) / (float)(t3.y-t2.y);
  243.  
  244.     float curx1 = t3.x;
  245.     float curx2 = t3.x;
  246.  
  247.     for (int scanLineY = t3.y; scanLineY > t1.y; scanLineY--) {
  248.         float we1 = 1/((float)abs(scanLineY-t3.y));
  249.         float we2 = 1/((float)abs(scanLineY-t1.y));
  250.         Vec3f ce1 = {(we1*c3.x+we2*c1.x)/(we1+we2), (we1*c3.y+we2*c1.y)/(we1+we2), (we1*c3.z+we2*c1.z)/(we1+we2)};
  251.         Vec3f ce2 = {(we1*c3.x+we2*c2.x)/(we1+we2), (we1*c3.y+we2*c2.y)/(we1+we2), (we1*c3.z+we2*c2.z)/(we1+we2)};
  252.         IHLine(screen, (int)curx1, (int)curx2, scanLineY, ce1, ce2);
  253.         curx1 -= invslope1;
  254.         curx2 -= invslope2;
  255.     }
  256.     IHLine(screen, t1.x, t2.x, t1.y, c1, c2);
  257. }
  258.  
  259. void stdITriangle(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Vec3f c1, Vec3f c2, Vec3f c3) {
  260.     Vertice t1 = {0, 0}, t2 = t1, t3 = t1;
  261.     Vec3f h1 = c1, h2 = c2, h3 = c3;
  262.     if (v1.y < v2.y && v1.y < v3.y) {
  263.         t1 = v1; h1 = c1;
  264.         t2 = (v2.y < v3.y ? v2 : v3); h2 = (v2.y < v3.y ? c2 : c3);
  265.         t3 = (v2.y < v3.y ? v3 : v2); h3 = (v2.y < v3.y ? c3 : c2);
  266.     }
  267.     else if (v2.y < v3.y) {
  268.         t1 = v2; h1 = c2;
  269.         t2 = (v1.y < v3.y ? v1 : v3); h2 = (v1.y < v3.y ? c1 : c3);
  270.         t3 = (v1.y < v3.y ? v3 : v1); h3 = (v1.y < v3.y ? c3 : c1);
  271.     }
  272.     else {
  273.         t1 = v3; h1 = c3;
  274.         t2 = (v1.y < v2.y ? v1 : v2); h2 = (v1.y < v2.y ? c1 : c2);
  275.         t3 = (v1.y < v2.y ? v2 : v1); h3 = (v1.y < v2.y ? c2 : c1);
  276.     }
  277.  
  278.     if (t2.y == t3.y) {
  279.         stdITriangleFlatBottom(screen, t1, t2, t3, h1, h2, h3);
  280.     }
  281.     else if (t1.y == t2.y) {
  282.         stdITriangleFlatTop(screen, t1, t2, t3, h1, h2, h3);
  283.     }
  284.     else {
  285.         Vertice t4 = {(int)(t1.x+((float)(t2.y-t1.y)/(float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y};
  286.         float w1 = 1/sqrt((t4.x-t1.x)*(t4.x-t1.x)+(t4.y-t1.y)*(t4.y-t1.y));
  287.         float w2 = 1/sqrt((t4.x-t3.x)*(t4.x-t3.x)+(t4.y-t3.y)*(t4.y-t3.y));
  288.         Vec3f h4 = {(w1*h1.x+w2*h3.x)/(w1+w2), (w1*h1.y+w2*h3.y)/(w1+w2), (w1*h1.z+w2*h3.z)/(w1+w2)};
  289.         stdITriangleFlatBottom(screen, t1, t2, t4, h1, h2, h4);
  290.         //stdTriangleFlatBottom(screen, t1, t2, t4, (Uint16)SDL_MapRGB(screen->format, h1.x, h2.y, h4.z));
  291.         stdITriangleFlatTop(screen, t2, t4, t3, h2, h4, h3);
  292.         //stdTriangleFlatTop(screen, t2, t4, t3, (Uint16)SDL_MapRGB(screen->format, h2.x, h4.y, h3.z));
  293.     }
  294. }
  295.  
  296. float absolute(Vec3f v) {
  297.     return sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
  298. }
  299.  
  300. void rotate2d(float *x, float *y, float angle) {
  301.     float c = cos(angle);
  302.     float s = sin(angle);
  303.     float rox = *x;
  304.     float roy = *y;
  305.     *x = rox*c-roy*s;
  306.     *y = roy*c+rox*s;
  307. }
  308.  
  309. void normalize(Vec3f* v) {
  310.     float abss = absolute(*v);
  311.     v->x /= abss;
  312.     v->y /= abss;
  313.     v->z /= abss;
  314. }
  315.  
  316. float dot(Vec3f u, Vec3f v) {
  317.     return u.x*v.x+u.y*v.y+u.z*v.z;
  318. }
  319.  
  320. float lightdot(Vec3f u, Vec3f l) {
  321.     return u.x*-l.x+u.y*-l.y+u.z*-l.z;
  322. }
  323.  
  324. void calculate(Camera cam, Vec2i* scrc, float* z_values) {
  325.     for (int i = 0; i < numVerts; i++) {
  326.         float x = verts[i].x;
  327.         float y = verts[i].y;
  328.         float z = verts[i].z;
  329.  
  330.         x -= cam.x;
  331.         y -= cam.y;
  332.         z -= cam.z;
  333.  
  334.         rotate2d(&x, &z, cam.hr);
  335.         rotate2d(&y, &z, cam.vr);
  336.  
  337.         float f = 160/z;
  338.         x *= f;
  339.         y *= f;
  340.         Vec2i on_scr = {160+(int)x, 120-(int)y};
  341.         scrc[i] = on_scr;
  342.         z_values[i] = z;
  343.     }
  344. }
  345.  
  346. void drawVerts(SDL_Surface *screen, Vec2i* scrc) {
  347.     for (int i = 0; i < numVerts; i++) {
  348.         setPixel(screen, scrc[i].x, scrc[i].y, RGB332to565lookupTable[(int)((float)i/numVerts*50)+200]);
  349.     }
  350. }
  351.  
  352. /*
  353. void drawEdges(SDL_Surface *screen, Vec2i* scrc) {
  354.     for (int i = 0; i < numEdges; i++) {
  355.         int v1x = scrc[edges[i].x].x;
  356.         int v1y = scrc[edges[i].x].y;
  357.         int v2x = scrc[edges[i].y].x;
  358.         int v2y = scrc[edges[i].y].y;
  359.         drawLine(screen, v1x, v1y, v2x, v2y, 0b1111111111111111);
  360.     }
  361. }
  362. */
  363.  
  364. void computeVcolors(Vec3f* vcolors, float* z_values) {
  365.     /*for (int i = 0; i < numVerts; i++) {
  366.         vcolors[i].x = 255-10*z_values[i];
  367.         vcolors[i].y = 0;
  368.         vcolors[i].z = 0;
  369.     }*/
  370.     for (int i = 0; i < numVerts; i++) {
  371.         float v = 255*(float)i/numVerts;
  372.         vcolors[i].x = v;
  373.         vcolors[i].y = 255-v;
  374.         vcolors[i].z = 255;
  375.     }
  376. };
  377.  
  378. void depthsort(float* z_values, int* faces_index) {
  379.     float tris_depth[numFaces];
  380.     for (int i = 0; i < numFaces; i++) {
  381.         tris_depth[i] = (z_values[faces[i].x-1]+z_values[faces[i].y-1]+z_values[faces[i].z-1])/3.0f;
  382.     }
  383.     float maxdepth;
  384.     int faceindex;
  385.     for (int i = 0; i < numFaces; i++) {
  386.         maxdepth = tris_depth[i];
  387.         faceindex = i;
  388.         for (int j = i+1; j < numFaces; j++) {
  389.             float depth = tris_depth[j];
  390.             if (depth > maxdepth) {
  391.                 maxdepth = depth;
  392.                 faceindex = j;
  393.             }
  394.         }
  395.         float tmp = tris_depth[i];
  396.         tris_depth[i] = tris_depth[faceindex];
  397.         tris_depth[faceindex] = tmp;
  398.         int tmm = faces_index[i];
  399.         faces_index[i] = faces_index[faceindex];
  400.         faces_index[faceindex] = tmm;
  401.     }
  402. }
  403.  
  404. void computeNormals(Vec3f* normals) {
  405.     for (int i = 0; i < numFaces; i++) {
  406.         float x1 = verts[faces[i].x-1].x;
  407.         float y1 = verts[faces[i].x-1].y;
  408.         float z1 = verts[faces[i].x-1].z;
  409.         float x2 = verts[faces[i].y-1].x;
  410.         float y2 = verts[faces[i].y-1].y;
  411.         float z2 = verts[faces[i].y-1].z;
  412.         float x3 = verts[faces[i].z-1].x;
  413.         float y3 = verts[faces[i].z-1].y;
  414.         float z3 = verts[faces[i].z-1].z;
  415.         Vec3f normal;
  416.         normal.x = (y2-y1)*(z3-z1) - (z2-z1)*(y3-y1);
  417.         normal.y = (z2-z1)*(x3-x1) - (x2-x1)*(z3-z1);
  418.         normal.z = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1);
  419.         normal.x /= absolute(normal);
  420.         normal.y /= absolute(normal);
  421.         normal.z /= absolute(normal);
  422.         normals[i] = normal;
  423.     }
  424. }
  425.  
  426. void backfaceCull(Vec3f* normals, Camera cam, bool* unculledFaces) {
  427.     for (int i = 0; i < numFaces; i++) {
  428.         Vec3f triscam = {cam.x - verts[faces[i].x-1].x, cam.y - verts[faces[i].x-1].y, cam.z - verts[faces[i].x-1].z};
  429.         float abs = absolute(triscam);
  430.         triscam.x /= abs;
  431.         triscam.y /= abs;
  432.         triscam.z /= abs;
  433.         float scal = dot(normals[i], triscam);
  434.         if (scal < 0) unculledFaces[i] = 0;
  435.         else unculledFaces[i] = 1;
  436.     }
  437. }
  438.  
  439. void lightScene(Vec3f* normals, Vec3f light, int ambiantLight, float* lightPerFaces) {
  440.     float al = (float)ambiantLight;
  441.     al/=255;
  442.     for (int i = 0; i < numFaces; i++) {
  443.         float lightt = 0.2*dot(normals[i], light);
  444.         if (lightt < 0) lightt = 0;
  445.         lightt+=al;
  446.         if (lightt > 1) lightt = 1;
  447.         lightPerFaces[i] = lightt;
  448.     }
  449. }
  450.  
  451. void drawFaces(SDL_Surface *screen, Vec2i* scrc, float* z_values, Vec3f* normals, Camera cam, Vec3f light, int ambiantLight, Vec3f* vcolors) {
  452.     int faces_index[numFaces];
  453.     for (int i = 0; i < numFaces; i++) {
  454.         faces_index[i] = i;
  455.     }
  456.     depthsort(z_values, faces_index);
  457.     bool unculledFaces[numFaces];
  458.     backfaceCull(normals, cam, unculledFaces);
  459.     float lightPerFaces[numFaces];
  460.     //lightScene(normals, light, ambiantLight, lightPerFaces);
  461.     for (int i = 0; i < numFaces; i++) {
  462.         if (unculledFaces[faces_index[i]] || !(BACKFACE_CULL)) {
  463.             int findex = faces_index[i];
  464.             Vec2i v1 = scrc[faces[findex].x-1];
  465.             Vec2i v2 = scrc[faces[findex].y-1];
  466.             Vec2i v3 = scrc[faces[findex].z-1];
  467.             /*float ll = lightPerFaces[findex];
  468.             float ffi = (float)findex;
  469.             float r = (ffi/12)*255;
  470.             float g = 255-r;
  471.             float b = 255.0f*ll;*/
  472.             //Uint16 color = (Uint16)SDL_MapRGB(screen->format, 255-(21*findex), 21*findex, 255);
  473.             /*Vec3f c1 = {255-(21*findex), 21*findex, 255};
  474.             Vec3f c2 = {21*findex, 255-(21*findex), 255};
  475.             Vec3f c3 = {255, 255-(21*findex), 21*findex};*/
  476.             Vec3f c1 = vcolors[faces[findex].x-1];
  477.             Vec3f c2 = vcolors[faces[findex].y-1];
  478.             Vec3f c3 = vcolors[faces[findex].z-1];
  479.             //Uint16 color = MapRGB565(148, 148, 148);
  480.             //color += 32-2*faces_index[i];
  481.             stdITriangle(screen, v1, v2, v3, c1, c2, c3);
  482.             //stdTriangle(screen, v1, v2, v3, color);
  483.         }
  484.     }
  485. }
  486.  
  487. int main(void) {
  488.     SDL_Surface *screen;
  489.     SDL_Init(SDL_INIT_VIDEO);
  490.     screen = SDL_SetVideoMode(320, 240, has_colors ? 16 : 8, SDL_SWSURFACE);
  491.     Camera cam = {0, 0, -5, 0, 0};
  492.     Vec2i scrCoords[numVerts];
  493.     float z_values[numVerts];
  494.     Vec3f normals[numFaces];
  495.     computeNormals(normals);
  496.     bool g_running = 1;
  497.     float camradius = 5;
  498.     Vec3f light = {0, 0, -4};
  499.     float hlr = 0;
  500.     float vlr = 0;
  501.     normalize(&light);
  502.     int ambiantLight = 150;
  503.     //SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  504.     //wait_key_pressed();
  505.     while (g_running) {
  506.         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  507.         calculate(cam, scrCoords, z_values);
  508.         //drawVerts(screen, scrCoords);
  509.         //drawEdges(screen, scrCoords);
  510.         drawFaces(screen, scrCoords, z_values, normals, cam, light, ambiantLight, vertsColor);
  511.         /*Vertice v1 = {0, 0};
  512.         Vertice v2 = {20, 0};
  513.         Vertice v3 = {0, 20};
  514.         Vec3f c1 = {255, 0, 0};
  515.         Vec3f c2 = {0, 255, 0};
  516.         Vec3f c3 = {0, 0, 255};
  517.         stdITriangle(screen, v1, v2, v3, c1, c2, c3);*/
  518.         SDL_Flip(screen);
  519.         cam.hr+=0.1;
  520.         cam.vr+=0.05;
  521.         //hlr+=0.01;
  522.         //vlr+=0.005;
  523.         //float xy = cos(cam.hr);
  524.         //float xx = sin(cam.hr);
  525.         /*if (isKeyPressed(KEY_NSPIRE_8)) cam.vr+=0.01;
  526.         if (isKeyPressed(KEY_NSPIRE_2)) cam.vr-=0.01;
  527.         if (isKeyPressed(KEY_NSPIRE_6)) cam.hr+=0.01;
  528.         if (isKeyPressed(KEY_NSPIRE_4)) cam.hr-=0.01;
  529.         if (isKeyPressed(KEY_NSPIRE_PLUS)) { cam.y += 0.5; }
  530.         if (isKeyPressed(KEY_NSPIRE_MINUS)) { cam.y -= 0.5; }
  531.         if (isKeyPressed(KEY_NSPIRE_UP)) { cam.x += 0.5*xx; cam.z +=0.5*xy; }
  532.         if (isKeyPressed(KEY_NSPIRE_DOWN)) { cam.x -= 0.5*xx; cam.z -=0.5*xy; }
  533.         if (isKeyPressed(KEY_NSPIRE_RIGHT)) { cam.x += 0.5*xy; cam.z -=0.5*xx; }
  534.         if (isKeyPressed(KEY_NSPIRE_LEFT)) { cam.x -= 0.5*xy; cam.z +=0.5*xx; }*/
  535.         cam.x = -camradius*sin(cam.hr)*cos(cam.vr);
  536.         cam.z = -camradius*cos(cam.hr)*cos(cam.vr);
  537.         cam.y = -camradius*sin(cam.vr);
  538.         //light.x = -sin(hlr)*cos(vlr);
  539.         //light.z = -cos(hlr)*cos(vlr);
  540.         //light.y = -sin(vlr);
  541.         if (isKeyPressed(KEY_NSPIRE_ESC)) g_running = 0;
  542.     }
  543.     SDL_Quit();
  544.     return 0;
  545. }
Advertisement
Add Comment
Please, Sign In to add comment