Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <os.h>
- #include <keys.h>
- #include <SDL/SDL_config.h>
- #include <SDL/SDL.h>
- #include <math.h>
- #define BACKFACE_CULL 1
- const Uint16 RGB332to565lookupTable[256] = {
- 0x0000, 0x000a, 0x0015, 0x001f, 0x0120, 0x012a, 0x0135, 0x013f,
- 0x0240, 0x024a, 0x0255, 0x025f, 0x0360, 0x036a, 0x0375, 0x037f,
- 0x0480, 0x048a, 0x0495, 0x049f, 0x05a0, 0x05aa, 0x05b5, 0x05bf,
- 0x06c0, 0x06ca, 0x06d5, 0x06df, 0x07e0, 0x07ea, 0x07f5, 0x07ff,
- 0x2000, 0x200a, 0x2015, 0x201f, 0x2120, 0x212a, 0x2135, 0x213f,
- 0x2240, 0x224a, 0x2255, 0x225f, 0x2360, 0x236a, 0x2375, 0x237f,
- 0x2480, 0x248a, 0x2495, 0x249f, 0x25a0, 0x25aa, 0x25b5, 0x25bf,
- 0x26c0, 0x26ca, 0x26d5, 0x26df, 0x27e0, 0x27ea, 0x27f5, 0x27ff,
- 0x4800, 0x480a, 0x4815, 0x481f, 0x4920, 0x492a, 0x4935, 0x493f,
- 0x4a40, 0x4a4a, 0x4a55, 0x4a5f, 0x4b60, 0x4b6a, 0x4b75, 0x4b7f,
- 0x4c80, 0x4c8a, 0x4c95, 0x4c9f, 0x4da0, 0x4daa, 0x4db5, 0x4dbf,
- 0x4ec0, 0x4eca, 0x4ed5, 0x4edf, 0x4fe0, 0x4fea, 0x4ff5, 0x4fff,
- 0x6800, 0x680a, 0x6815, 0x681f, 0x6920, 0x692a, 0x6935, 0x693f,
- 0x6a40, 0x6a4a, 0x6a55, 0x6a5f, 0x6b60, 0x6b6a, 0x6b75, 0x6b7f,
- 0x6c80, 0x6c8a, 0x6c95, 0x6c9f, 0x6da0, 0x6daa, 0x6db5, 0x6dbf,
- 0x6ec0, 0x6eca, 0x6ed5, 0x6edf, 0x6fe0, 0x6fea, 0x6ff5, 0x6fff,
- 0x9000, 0x900a, 0x9015, 0x901f, 0x9120, 0x912a, 0x9135, 0x913f,
- 0x9240, 0x924a, 0x9255, 0x925f, 0x9360, 0x936a, 0x9375, 0x937f,
- 0x9480, 0x948a, 0x9495, 0x949f, 0x95a0, 0x95aa, 0x95b5, 0x95bf,
- 0x96c0, 0x96ca, 0x96d5, 0x96df, 0x97e0, 0x97ea, 0x97f5, 0x97ff,
- 0xb000, 0xb00a, 0xb015, 0xb01f, 0xb120, 0xb12a, 0xb135, 0xb13f,
- 0xb240, 0xb24a, 0xb255, 0xb25f, 0xb360, 0xb36a, 0xb375, 0xb37f,
- 0xb480, 0xb48a, 0xb495, 0xb49f, 0xb5a0, 0xb5aa, 0xb5b5, 0xb5bf,
- 0xb6c0, 0xb6ca, 0xb6d5, 0xb6df, 0xb7e0, 0xb7ea, 0xb7f5, 0xb7ff,
- 0xd800, 0xd80a, 0xd815, 0xd81f, 0xd920, 0xd92a, 0xd935, 0xd93f,
- 0xda40, 0xda4a, 0xda55, 0xda5f, 0xdb60, 0xdb6a, 0xdb75, 0xdb7f,
- 0xdc80, 0xdc8a, 0xdc95, 0xdc9f, 0xdda0, 0xddaa, 0xddb5, 0xddbf,
- 0xdec0, 0xdeca, 0xded5, 0xdedf, 0xdfe0, 0xdfea, 0xdff5, 0xdfff,
- 0xf800, 0xf80a, 0xf815, 0xf81f, 0xf920, 0xf92a, 0xf935, 0xf93f,
- 0xfa40, 0xfa4a, 0xfa55, 0xfa5f, 0xfb60, 0xfb6a, 0xfb75, 0xfb7f,
- 0xfc80, 0xfc8a, 0xfc95, 0xfc9f, 0xfda0, 0xfdaa, 0xfdb5, 0xfdbf,
- 0xfec0, 0xfeca, 0xfed5, 0xfedf, 0xffe0, 0xffea, 0xfff5, 0xffff};
- #define Vertice Vec2i
- typedef struct {
- float x, y, z, vr, hr;
- } Camera;
- typedef struct {
- float x, y, z;
- } Vec3f;
- typedef struct {
- int x, y;
- } Vec2i;
- typedef struct {
- int x, y, z;
- } Vec3i;
- int numVerts = 8;
- Vec3f verts[] = {
- {-1, -1, -1},
- {-1, 1, -1},
- { 1, 1, -1},
- { 1, -1, -1},
- {-1, -1, 1},
- {-1, 1, 1},
- { 1, 1, 1},
- { 1, -1, 1}
- };
- int numFaces = 12;
- Vec3i faces[] = {
- {1, 2, 3},
- {1, 3, 4},
- {1, 4, 8},
- {1, 8, 5},
- {1, 5, 6},
- {1, 6, 2},
- {7, 3, 2},
- {7, 2, 6},
- {7, 6, 5},
- {7, 5, 8},
- {7, 8, 4},
- {7, 4, 3}
- };
- Vec3f vertsColor[] = {
- {255 , 0, 255},
- {255-1*21.5, 21.25, 255},
- {255-2*21.5, 2*21.25, 255},
- {255-3*21.5, 3*21.25, 255},
- {255-4*21.5, 4*21.25, 255},
- {255-5*21.5, 5*21.25, 255},
- {255-6*21.5, 6*21.25, 255},
- {255-7*21.5, 7*21.25, 255}
- };
- const Vec3f pxv = {1,0,0};
- const Vec3f nxv = {-1,0,0};
- const Vec3f pyv = {0,1,0};
- const Vec3f nyv = {0,-1,0};
- const Vec3f pzv = {0,0,1};
- const Vec3f nzv = {0,0,-1};
- Uint16 MapRGB565(int r, int g, int b) {
- float fr = (float)r;
- float fg = (float)g;
- float fb = (float)b;
- fr /= 255;
- fg /= 255;
- fb /= 255;
- int ir = (int)(fr*31*2048);
- int ig = (int)(fg*63*32);
- int ib = (int)(fb*31);
- return (Uint16)(ir+ig+ib);
- }
- void setPixel(SDL_Surface *screen, int x, int y, Uint16 color) {
- volatile unsigned char *ptr = screen->pixels;
- ptr += x*2+y*640;
- if (x > 0 && x < 320 && y > 0 && y < 240)
- *(volatile unsigned short*)ptr = color;
- }
- void drawLine(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint16 color) {
- int dx = abs(x1-x0), cx = x0 < x1 ? 1 : -1;
- int dy = -abs(y1-y0), cy = y0 < y1 ? 1 : -1;
- int err = dx+dy, e2;
- for(;;) {
- setPixel(screen, x0, y0, color);
- if (x0==x1 && y0==y1) break;
- e2 = 2*err;
- if (e2 >= dy) { err += dy; x0 += cx; }
- if (e2 <= dx) { err += dx; y0 += cy; }
- }
- }
- void HLine(SDL_Surface *screen, int x0, int x1, int y, Uint16 color) {
- setPixel(screen, x0, y, color);
- if (x0 == x1) return;
- int cx = x0 < x1 ? 1 : -1;
- for (int x = x0+cx; x != x1; x+=cx) setPixel(screen, x, y, color);
- setPixel(screen, x1, y, color);
- }
- void stdTriangleFlatBottom(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Uint16 color) {
- float invslope1 = (float)(v2.x-v1.x) / (float)(v2.y-v1.y);
- float invslope2 = (float)(v3.x-v1.x) / (float)(v3.y-v1.y);
- float curx1 = v1.x;
- float curx2 = v1.x;
- for (int scanLineY = v1.y; scanLineY <= v2.y; scanLineY++) {
- HLine(screen, (int)curx1, (int)curx2, scanLineY, color);
- curx1 += invslope1;
- curx2 += invslope2;
- }
- }
- void stdTriangleFlatTop(SDL_Surface *screen, Vertice t1, Vertice t2, Vertice t3, Uint16 color) {
- float invslope1 = (float)(t3.x-t1.x) / (float)(t3.y-t1.y);
- float invslope2 = (float)(t3.x-t2.x) / (float)(t3.y-t2.y);
- float curx1 = t3.x;
- float curx2 = t3.x;
- for (int scanLineY = t3.y; scanLineY >= t1.y; scanLineY--) {
- HLine(screen, (int)curx1, (int)curx2, scanLineY, color);
- curx1 -= invslope1;
- curx2 -= invslope2;
- }
- }
- void stdTriangle(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Uint16 color) {
- Vertice t1 = {0, 0}, t2 = t1, t3 = t1;
- if (v1.y < v2.y && v1.y < v3.y) {
- t1 = v1;
- t2 = (v2.y < v3.y ? v2 : v3);
- t3 = (v2.y < v3.y ? v3 : v2);
- }
- else if (v2.y < v3.y) {
- t1 = v2;
- t2 = (v1.y < v3.y ? v1 : v3);
- t3 = (v1.y < v3.y ? v3 : v1);
- }
- else {
- t1 = v3;
- t2 = (v1.y < v2.y ? v1 : v2);
- t3 = (v1.y < v2.y ? v2 : v1);
- }
- if (t2.y == t3.y) {
- stdTriangleFlatBottom(screen, t1, t2, t3, color);
- }
- else if (t1.y == t2.y) {
- stdTriangleFlatTop(screen, t1, t2, t3, color);
- }
- else {
- Vertice t4 = {(int)(t1.x+((float)(t2.y-t1.y)/(float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y};
- stdTriangleFlatBottom(screen, t1, t2, t4, color);
- stdTriangleFlatTop(screen, t2, t4, t3, color);
- }
- }
- void IHLine(SDL_Surface *screen, int x0, int x1, int y, Vec3f c0, Vec3f c1) {
- setPixel(screen, x0, y, (Uint16)SDL_MapRGB(screen->format, c0.x, c0.y, c0.z));
- if (x0 == x1) return;
- int cx = x0 < x1 ? 1 : -1;
- for (int x = x0+cx; x != x1; x+=cx) {
- float w1 = 1/((float)abs(x-x0));
- float w2 = 1/((float)abs(x-x1));
- float r = (w1*c0.x+w2*c1.x)/(w1+w2);
- float g = (w1*c0.y+w2*c1.y)/(w1+w2);
- float b = (w1*c0.z+w2*c1.z)/(w1+w2);
- setPixel(screen, x, y, (Uint16)SDL_MapRGB(screen->format, (int)r, (int)g, (int)b));
- }
- setPixel(screen, x1, y, (Uint16)SDL_MapRGB(screen->format, c1.x, c1.y, c1.z));
- }
- void stdITriangleFlatBottom(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Vec3f c1, Vec3f c2, Vec3f c3) {
- float invslope1 = (float)(v2.x-v1.x) / (float)(v2.y-v1.y);
- float invslope2 = (float)(v3.x-v1.x) / (float)(v3.y-v1.y);
- float curx1 = v1.x;
- float curx2 = v1.x;
- for (int scanLineY = v1.y; scanLineY <= v2.y; scanLineY++) {
- float we1 = 1/((float)abs(scanLineY-v1.y));
- float we2 = 1/((float)abs(scanLineY-v2.y));
- 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)};
- 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)};
- IHLine(screen, (int)curx1, (int)curx2, scanLineY, ce1, ce2);
- curx1 += invslope1;
- curx2 += invslope2;
- }
- IHLine(screen, v2.x, v3.x, v2.y, c2, c3);
- }
- void stdITriangleFlatTop(SDL_Surface *screen, Vertice t1, Vertice t2, Vertice t3, Vec3f c1, Vec3f c2, Vec3f c3) {
- float invslope1 = (float)(t3.x-t1.x) / (float)(t3.y-t1.y);
- float invslope2 = (float)(t3.x-t2.x) / (float)(t3.y-t2.y);
- float curx1 = t3.x;
- float curx2 = t3.x;
- for (int scanLineY = t3.y; scanLineY > t1.y; scanLineY--) {
- float we1 = 1/((float)abs(scanLineY-t3.y));
- float we2 = 1/((float)abs(scanLineY-t1.y));
- 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)};
- 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)};
- IHLine(screen, (int)curx1, (int)curx2, scanLineY, ce1, ce2);
- curx1 -= invslope1;
- curx2 -= invslope2;
- }
- IHLine(screen, t1.x, t2.x, t1.y, c1, c2);
- }
- void stdITriangle(SDL_Surface *screen, Vertice v1, Vertice v2, Vertice v3, Vec3f c1, Vec3f c2, Vec3f c3) {
- Vertice t1 = {0, 0}, t2 = t1, t3 = t1;
- Vec3f h1 = c1, h2 = c2, h3 = c3;
- if (v1.y < v2.y && v1.y < v3.y) {
- t1 = v1; h1 = c1;
- t2 = (v2.y < v3.y ? v2 : v3); h2 = (v2.y < v3.y ? c2 : c3);
- t3 = (v2.y < v3.y ? v3 : v2); h3 = (v2.y < v3.y ? c3 : c2);
- }
- else if (v2.y < v3.y) {
- t1 = v2; h1 = c2;
- t2 = (v1.y < v3.y ? v1 : v3); h2 = (v1.y < v3.y ? c1 : c3);
- t3 = (v1.y < v3.y ? v3 : v1); h3 = (v1.y < v3.y ? c3 : c1);
- }
- else {
- t1 = v3; h1 = c3;
- t2 = (v1.y < v2.y ? v1 : v2); h2 = (v1.y < v2.y ? c1 : c2);
- t3 = (v1.y < v2.y ? v2 : v1); h3 = (v1.y < v2.y ? c2 : c1);
- }
- if (t2.y == t3.y) {
- stdITriangleFlatBottom(screen, t1, t2, t3, h1, h2, h3);
- }
- else if (t1.y == t2.y) {
- stdITriangleFlatTop(screen, t1, t2, t3, h1, h2, h3);
- }
- else {
- Vertice t4 = {(int)(t1.x+((float)(t2.y-t1.y)/(float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y};
- float w1 = 1/sqrt((t4.x-t1.x)*(t4.x-t1.x)+(t4.y-t1.y)*(t4.y-t1.y));
- float w2 = 1/sqrt((t4.x-t3.x)*(t4.x-t3.x)+(t4.y-t3.y)*(t4.y-t3.y));
- 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)};
- stdITriangleFlatBottom(screen, t1, t2, t4, h1, h2, h4);
- //stdTriangleFlatBottom(screen, t1, t2, t4, (Uint16)SDL_MapRGB(screen->format, h1.x, h2.y, h4.z));
- stdITriangleFlatTop(screen, t2, t4, t3, h2, h4, h3);
- //stdTriangleFlatTop(screen, t2, t4, t3, (Uint16)SDL_MapRGB(screen->format, h2.x, h4.y, h3.z));
- }
- }
- float absolute(Vec3f v) {
- return sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
- }
- void rotate2d(float *x, float *y, float angle) {
- float c = cos(angle);
- float s = sin(angle);
- float rox = *x;
- float roy = *y;
- *x = rox*c-roy*s;
- *y = roy*c+rox*s;
- }
- void normalize(Vec3f* v) {
- float abss = absolute(*v);
- v->x /= abss;
- v->y /= abss;
- v->z /= abss;
- }
- float dot(Vec3f u, Vec3f v) {
- return u.x*v.x+u.y*v.y+u.z*v.z;
- }
- float lightdot(Vec3f u, Vec3f l) {
- return u.x*-l.x+u.y*-l.y+u.z*-l.z;
- }
- void calculate(Camera cam, Vec2i* scrc, float* z_values) {
- for (int i = 0; i < numVerts; i++) {
- float x = verts[i].x;
- float y = verts[i].y;
- float z = verts[i].z;
- x -= cam.x;
- y -= cam.y;
- z -= cam.z;
- rotate2d(&x, &z, cam.hr);
- rotate2d(&y, &z, cam.vr);
- float f = 160/z;
- x *= f;
- y *= f;
- Vec2i on_scr = {160+(int)x, 120-(int)y};
- scrc[i] = on_scr;
- z_values[i] = z;
- }
- }
- void drawVerts(SDL_Surface *screen, Vec2i* scrc) {
- for (int i = 0; i < numVerts; i++) {
- setPixel(screen, scrc[i].x, scrc[i].y, RGB332to565lookupTable[(int)((float)i/numVerts*50)+200]);
- }
- }
- /*
- void drawEdges(SDL_Surface *screen, Vec2i* scrc) {
- for (int i = 0; i < numEdges; i++) {
- int v1x = scrc[edges[i].x].x;
- int v1y = scrc[edges[i].x].y;
- int v2x = scrc[edges[i].y].x;
- int v2y = scrc[edges[i].y].y;
- drawLine(screen, v1x, v1y, v2x, v2y, 0b1111111111111111);
- }
- }
- */
- void computeVcolors(Vec3f* vcolors, float* z_values) {
- /*for (int i = 0; i < numVerts; i++) {
- vcolors[i].x = 255-10*z_values[i];
- vcolors[i].y = 0;
- vcolors[i].z = 0;
- }*/
- for (int i = 0; i < numVerts; i++) {
- float v = 255*(float)i/numVerts;
- vcolors[i].x = v;
- vcolors[i].y = 255-v;
- vcolors[i].z = 255;
- }
- };
- void depthsort(float* z_values, int* faces_index) {
- float tris_depth[numFaces];
- for (int i = 0; i < numFaces; i++) {
- tris_depth[i] = (z_values[faces[i].x-1]+z_values[faces[i].y-1]+z_values[faces[i].z-1])/3.0f;
- }
- float maxdepth;
- int faceindex;
- for (int i = 0; i < numFaces; i++) {
- maxdepth = tris_depth[i];
- faceindex = i;
- for (int j = i+1; j < numFaces; j++) {
- float depth = tris_depth[j];
- if (depth > maxdepth) {
- maxdepth = depth;
- faceindex = j;
- }
- }
- float tmp = tris_depth[i];
- tris_depth[i] = tris_depth[faceindex];
- tris_depth[faceindex] = tmp;
- int tmm = faces_index[i];
- faces_index[i] = faces_index[faceindex];
- faces_index[faceindex] = tmm;
- }
- }
- void computeNormals(Vec3f* normals) {
- for (int i = 0; i < numFaces; i++) {
- float x1 = verts[faces[i].x-1].x;
- float y1 = verts[faces[i].x-1].y;
- float z1 = verts[faces[i].x-1].z;
- float x2 = verts[faces[i].y-1].x;
- float y2 = verts[faces[i].y-1].y;
- float z2 = verts[faces[i].y-1].z;
- float x3 = verts[faces[i].z-1].x;
- float y3 = verts[faces[i].z-1].y;
- float z3 = verts[faces[i].z-1].z;
- Vec3f normal;
- normal.x = (y2-y1)*(z3-z1) - (z2-z1)*(y3-y1);
- normal.y = (z2-z1)*(x3-x1) - (x2-x1)*(z3-z1);
- normal.z = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1);
- normal.x /= absolute(normal);
- normal.y /= absolute(normal);
- normal.z /= absolute(normal);
- normals[i] = normal;
- }
- }
- void backfaceCull(Vec3f* normals, Camera cam, bool* unculledFaces) {
- for (int i = 0; i < numFaces; i++) {
- 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};
- float abs = absolute(triscam);
- triscam.x /= abs;
- triscam.y /= abs;
- triscam.z /= abs;
- float scal = dot(normals[i], triscam);
- if (scal < 0) unculledFaces[i] = 0;
- else unculledFaces[i] = 1;
- }
- }
- void lightScene(Vec3f* normals, Vec3f light, int ambiantLight, float* lightPerFaces) {
- float al = (float)ambiantLight;
- al/=255;
- for (int i = 0; i < numFaces; i++) {
- float lightt = 0.2*dot(normals[i], light);
- if (lightt < 0) lightt = 0;
- lightt+=al;
- if (lightt > 1) lightt = 1;
- lightPerFaces[i] = lightt;
- }
- }
- void drawFaces(SDL_Surface *screen, Vec2i* scrc, float* z_values, Vec3f* normals, Camera cam, Vec3f light, int ambiantLight, Vec3f* vcolors) {
- int faces_index[numFaces];
- for (int i = 0; i < numFaces; i++) {
- faces_index[i] = i;
- }
- depthsort(z_values, faces_index);
- bool unculledFaces[numFaces];
- backfaceCull(normals, cam, unculledFaces);
- float lightPerFaces[numFaces];
- //lightScene(normals, light, ambiantLight, lightPerFaces);
- for (int i = 0; i < numFaces; i++) {
- if (unculledFaces[faces_index[i]] || !(BACKFACE_CULL)) {
- int findex = faces_index[i];
- Vec2i v1 = scrc[faces[findex].x-1];
- Vec2i v2 = scrc[faces[findex].y-1];
- Vec2i v3 = scrc[faces[findex].z-1];
- /*float ll = lightPerFaces[findex];
- float ffi = (float)findex;
- float r = (ffi/12)*255;
- float g = 255-r;
- float b = 255.0f*ll;*/
- //Uint16 color = (Uint16)SDL_MapRGB(screen->format, 255-(21*findex), 21*findex, 255);
- /*Vec3f c1 = {255-(21*findex), 21*findex, 255};
- Vec3f c2 = {21*findex, 255-(21*findex), 255};
- Vec3f c3 = {255, 255-(21*findex), 21*findex};*/
- Vec3f c1 = vcolors[faces[findex].x-1];
- Vec3f c2 = vcolors[faces[findex].y-1];
- Vec3f c3 = vcolors[faces[findex].z-1];
- //Uint16 color = MapRGB565(148, 148, 148);
- //color += 32-2*faces_index[i];
- stdITriangle(screen, v1, v2, v3, c1, c2, c3);
- //stdTriangle(screen, v1, v2, v3, color);
- }
- }
- }
- int main(void) {
- SDL_Surface *screen;
- SDL_Init(SDL_INIT_VIDEO);
- screen = SDL_SetVideoMode(320, 240, has_colors ? 16 : 8, SDL_SWSURFACE);
- Camera cam = {0, 0, -5, 0, 0};
- Vec2i scrCoords[numVerts];
- float z_values[numVerts];
- Vec3f normals[numFaces];
- computeNormals(normals);
- bool g_running = 1;
- float camradius = 5;
- Vec3f light = {0, 0, -4};
- float hlr = 0;
- float vlr = 0;
- normalize(&light);
- int ambiantLight = 150;
- //SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
- //wait_key_pressed();
- while (g_running) {
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
- calculate(cam, scrCoords, z_values);
- //drawVerts(screen, scrCoords);
- //drawEdges(screen, scrCoords);
- drawFaces(screen, scrCoords, z_values, normals, cam, light, ambiantLight, vertsColor);
- /*Vertice v1 = {0, 0};
- Vertice v2 = {20, 0};
- Vertice v3 = {0, 20};
- Vec3f c1 = {255, 0, 0};
- Vec3f c2 = {0, 255, 0};
- Vec3f c3 = {0, 0, 255};
- stdITriangle(screen, v1, v2, v3, c1, c2, c3);*/
- SDL_Flip(screen);
- cam.hr+=0.1;
- cam.vr+=0.05;
- //hlr+=0.01;
- //vlr+=0.005;
- //float xy = cos(cam.hr);
- //float xx = sin(cam.hr);
- /*if (isKeyPressed(KEY_NSPIRE_8)) cam.vr+=0.01;
- if (isKeyPressed(KEY_NSPIRE_2)) cam.vr-=0.01;
- if (isKeyPressed(KEY_NSPIRE_6)) cam.hr+=0.01;
- if (isKeyPressed(KEY_NSPIRE_4)) cam.hr-=0.01;
- if (isKeyPressed(KEY_NSPIRE_PLUS)) { cam.y += 0.5; }
- if (isKeyPressed(KEY_NSPIRE_MINUS)) { cam.y -= 0.5; }
- if (isKeyPressed(KEY_NSPIRE_UP)) { cam.x += 0.5*xx; cam.z +=0.5*xy; }
- if (isKeyPressed(KEY_NSPIRE_DOWN)) { cam.x -= 0.5*xx; cam.z -=0.5*xy; }
- if (isKeyPressed(KEY_NSPIRE_RIGHT)) { cam.x += 0.5*xy; cam.z -=0.5*xx; }
- if (isKeyPressed(KEY_NSPIRE_LEFT)) { cam.x -= 0.5*xy; cam.z +=0.5*xx; }*/
- cam.x = -camradius*sin(cam.hr)*cos(cam.vr);
- cam.z = -camradius*cos(cam.hr)*cos(cam.vr);
- cam.y = -camradius*sin(cam.vr);
- //light.x = -sin(hlr)*cos(vlr);
- //light.z = -cos(hlr)*cos(vlr);
- //light.y = -sin(vlr);
- if (isKeyPressed(KEY_NSPIRE_ESC)) g_running = 0;
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment