t
This commit is contained in:
parent
adf5e6a761
commit
3147e6e9a5
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
CC = clang #Set compiler
|
CC = clang #Set compiler
|
||||||
CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags
|
CFLAGS = -pg -g -Wall -O3 -pg #set compiler flags
|
||||||
LDFLAGS = -pg -g #set linker flags
|
LDFLAGS = -pg -g #set linker flags
|
||||||
LDLIBS = -lraylib -lm #set libraries to use
|
LDLIBS = -lraylib -lm #set libraries to use
|
||||||
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files
|
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files
|
||||||
|
|
91
c3d.c
91
c3d.c
|
@ -118,36 +118,32 @@ void Tri2DSortByY(Tri2D *t) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, int scanline) {
|
||||||
|
|
||||||
void DrawScanline(Zee * zee, Tri2D * trianglepointer, double start, double end, int scanline) {
|
Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline});
|
||||||
|
float f1depth = DepthAtBary(trianglepointer, f1baryatpoint);
|
||||||
|
|
||||||
Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline});
|
Vector3 f1baryatpoint2 =
|
||||||
float f1depth = DepthAtBary(trianglepointer, f1baryatpoint);
|
Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline});
|
||||||
|
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
|
||||||
|
float dslope = f1depth2 - f1depth;
|
||||||
|
|
||||||
Vector3 f1baryatpoint2 = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline});
|
for (int i = Max(start, 0); i < Min(end, RENDERWIDTH); i++) {
|
||||||
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
|
/* zee[IndexOfZBuff(i, scanline)].triangle = tp; */
|
||||||
float dslope = f1depth2 - f1depth;
|
|
||||||
|
|
||||||
for (int i = Max(start, 0); i < Min(end, RENDERWIDTH); i++) {
|
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
|
||||||
/* zee[IndexOfZBuff(i, scanline)].triangle = tp; */
|
/* float depth = DepthAtBary(tp, baryatpoint); */
|
||||||
|
|
||||||
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
|
float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(start, 0))));
|
||||||
/* float depth = DepthAtBary(tp, baryatpoint); */
|
|
||||||
|
|
||||||
|
if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) {
|
||||||
|
zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color;
|
||||||
float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(start, 0))));
|
zee[IndexOfZBuff(i, scanline)].depth = aproxdepth;
|
||||||
|
zee[IndexOfZBuff(i, scanline)].tri = trianglepointer;
|
||||||
|
}
|
||||||
if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) {
|
}
|
||||||
|
|
||||||
zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color;
|
|
||||||
zee[IndexOfZBuff(i, scanline)].depth = aproxdepth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Draws triangle with a flat top. Note A and B must be the top points with C
|
// Draws triangle with a flat top. Note A and B must be the top points with C
|
||||||
// being the bottom "spike"
|
// being the bottom "spike"
|
||||||
void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
|
void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
|
||||||
|
@ -168,14 +164,13 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
|
||||||
double curx1 = t->c.x;
|
double curx1 = t->c.x;
|
||||||
double curx2 = t->c.x;
|
double curx2 = t->c.x;
|
||||||
|
|
||||||
int scanbottom = t->c.y;
|
int scanbottom = t->c.y;
|
||||||
int scantop = Max(t->a.y, 0);
|
int scantop = Max(t->a.y, 0);
|
||||||
|
|
||||||
for (int scanline = scanbottom; scanline >= scantop; scanline--) {
|
for (int scanline = scanbottom; scanline >= scantop; scanline--) {
|
||||||
if (0 <= scanline && scanline < RENDERHEIGHT) {
|
if (0 <= scanline && scanline < RENDERHEIGHT) {
|
||||||
|
|
||||||
DrawScanline(zee, tp, curx1, curx2, scanline);
|
DrawScanline(zee, tp, curx1, curx2, scanline);
|
||||||
|
|
||||||
}
|
}
|
||||||
curx1 -= atocslopeinv; // subtract because we are working backwards (reason
|
curx1 -= atocslopeinv; // subtract because we are working backwards (reason
|
||||||
// why we start with point c in slope equtn)
|
// why we start with point c in slope equtn)
|
||||||
|
@ -183,7 +178,6 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PrintTri2D(Tri2D t) {
|
void PrintTri2D(Tri2D t) {
|
||||||
printf("{(TRI2D) A: (%f, %f), B: (%f, %f), C:(%f,%f) }\n ", t.a.x, t.a.y, t.b.x, t.b.y, t.c.x,
|
printf("{(TRI2D) A: (%f, %f), B: (%f, %f), C:(%f,%f) }\n ", t.a.x, t.a.y, t.b.x, t.b.y, t.c.x,
|
||||||
t.c.y);
|
t.c.y);
|
||||||
|
@ -209,15 +203,17 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
|
||||||
double curx1 = t->a.x;
|
double curx1 = t->a.x;
|
||||||
double curx2 = t->a.x;
|
double curx2 = t->a.x;
|
||||||
|
|
||||||
int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise everything will be offset
|
int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise
|
||||||
int scanbottom = Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen
|
// everything will be offset
|
||||||
|
int scanbottom =
|
||||||
|
Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen
|
||||||
|
|
||||||
for (int scanline = scantop; scanline < scanbottom;
|
for (int scanline = scantop; scanline < scanbottom;
|
||||||
scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance
|
scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance
|
||||||
// for y, not just x to get depth
|
// for y, not just x to get depth
|
||||||
if (0 <= scanline && scanline < RENDERHEIGHT) {
|
if (0 <= scanline && scanline < RENDERHEIGHT) {
|
||||||
DrawScanline(zee, tp, curx1, curx2, scanline);
|
DrawScanline(zee, tp, curx1, curx2, scanline);
|
||||||
}
|
}
|
||||||
curx1 += atobslopeinv;
|
curx1 += atobslopeinv;
|
||||||
curx2 += atocslopeinv;
|
curx2 += atocslopeinv;
|
||||||
}
|
}
|
||||||
|
@ -258,7 +254,6 @@ void DrawTriZuff(Zee *zbuf, Tri2D *t) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CtrlLocalCam(LocalCam *cam, float time) {
|
void CtrlLocalCam(LocalCam *cam, float time) {
|
||||||
cam->velocity.x = 0;
|
cam->velocity.x = 0;
|
||||||
cam->velocity.y = 0;
|
cam->velocity.y = 0;
|
||||||
|
@ -469,11 +464,11 @@ int main() {
|
||||||
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN};
|
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN};
|
||||||
|
|
||||||
bool run3d = true;
|
bool run3d = true;
|
||||||
Object3D t = ReadObjectFromFile("mario.obj");
|
Object3D t = ReadObjectFromFile("teapot.obj");
|
||||||
for (int i = 0; i < t.triangles->length; i++){
|
for (int i = 0; i < t.triangles->length; i++) {
|
||||||
//printf("t: %f\n", t.triangles->arr[i].a.x);
|
// printf("t: %f\n", t.triangles->arr[i].a.x);
|
||||||
TriArrayAppend(&tarr, t.triangles->arr[i]);
|
TriArrayAppend(&tarr, t.triangles->arr[i]);
|
||||||
}
|
}
|
||||||
while (!WindowShouldClose() && run3d) {
|
while (!WindowShouldClose() && run3d) {
|
||||||
float frametime = GetFrameTime();
|
float frametime = GetFrameTime();
|
||||||
CtrlLocalCam(&camera, frametime);
|
CtrlLocalCam(&camera, frametime);
|
||||||
|
@ -505,6 +500,12 @@ int main() {
|
||||||
DrawTriZuff(ZBuff, &Tri2Darr.arr[i]);
|
DrawTriZuff(ZBuff, &Tri2Darr.arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IsKeyDown(KEY_H)) {
|
||||||
|
if (ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2 )].tri != 0) {
|
||||||
|
ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2)].tri->color = RED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FillTopFlatZbuffer(ZBuff, &funners);
|
// FillTopFlatZbuffer(ZBuff, &funners);
|
||||||
// FillBottomFlatZbuffer(ZBuff,&funners2);
|
// FillBottomFlatZbuffer(ZBuff,&funners2);
|
||||||
|
|
||||||
|
@ -517,21 +518,11 @@ int main() {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int y = 0; y < RENDERHEIGHT; y++) {
|
for (int y = 0; y < RENDERHEIGHT; y++) {
|
||||||
for (int x = 0; x < RENDERWIDTH; x++) {
|
for (int x = 0; x < RENDERWIDTH; x++) {
|
||||||
// int index = (x+y*RENDERWIDTH);
|
|
||||||
/* Color * c = &ZBuff[x][y].triangle->color;
|
|
||||||
display[index] = c->r;
|
|
||||||
display[index+1] = c->g;
|
|
||||||
display[index+2] = c->b;
|
|
||||||
display[index+3] = c->a;
|
|
||||||
*/
|
|
||||||
|
|
||||||
//if (ZBuff[IndexOfZBuff(x, y)].triangle != 0 ) { // memset sets this to 0
|
|
||||||
// DrawPixel(x,y,ZBuff[x][y].triangle->color);
|
|
||||||
|
|
||||||
display[index] = ZBuff[IndexOfZBuff(x, y)].c;
|
if (ZBuff[IndexOfZBuff(x, y)].tri != 0) {
|
||||||
// Zee test = ZBuff[IndexOfZBuff(x,y)];
|
display[index] = ZBuff[IndexOfZBuff(x, y)].tri->color;
|
||||||
// display[index] = test.triangle->color;
|
}
|
||||||
//}
|
|
||||||
index = index + 1;
|
index = index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ typedef struct Tri2D Tri2D;
|
||||||
|
|
||||||
struct Zee {
|
struct Zee {
|
||||||
int depth;
|
int depth;
|
||||||
|
Tri2D * tri;
|
||||||
Color c;
|
Color c;
|
||||||
};
|
};
|
||||||
typedef struct Zee Zee;
|
typedef struct Zee Zee;
|
||||||
|
|
Loading…
Reference in a new issue