Compare commits
3 Commits
d962951880
...
7091c822f6
Author | SHA1 | Date |
---|---|---|
|
7091c822f6 | |
|
9eb127bdcd | |
|
7ddf2c5067 |
BIN
obj/base.o
BIN
obj/base.o
Binary file not shown.
BIN
obj/display.o
BIN
obj/display.o
Binary file not shown.
BIN
obj/entities.o
BIN
obj/entities.o
Binary file not shown.
BIN
obj/generation.o
BIN
obj/generation.o
Binary file not shown.
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/move.o
BIN
obj/move.o
Binary file not shown.
BIN
obj/triangles.o
BIN
obj/triangles.o
Binary file not shown.
48
src/base.c
48
src/base.c
|
@ -95,14 +95,50 @@ int convex_seg(int x1, int x2, double theta) {
|
|||
return (int)(((1.0f - theta) * x1 + theta * x2));
|
||||
}
|
||||
|
||||
double convex_pt(double a, double b, double theta) {
|
||||
return (a+(b-a)*theta) ;
|
||||
}
|
||||
|
||||
double convex_tri(double a, double tha, double b, double thb, double c, double thc) {
|
||||
return (a*tha + b*thb + c*thc);
|
||||
}
|
||||
|
||||
pt_2d convex_pt2d(pt_2d A, pt_2d B, double theta) {
|
||||
pt_2d res;
|
||||
res.x = convex_pt(A.x, B.x, theta);
|
||||
res.y = convex_pt(A.y, B.y, theta);
|
||||
res.z = convex_pt(A.z, B.z, theta);
|
||||
return res;
|
||||
}
|
||||
|
||||
pt_2d convex_pt2d_tri(pt_2d A, double tha, pt_2d B, double thb, pt_2d C, double thc) {
|
||||
pt_2d res;
|
||||
res.x = convex_tri(A.x, tha, B.x, thb, C.x, thc);
|
||||
res.y = convex_tri(A.y, tha, B.y, thb, C.y, thc);
|
||||
res.z = convex_tri(A.z, tha, B.z, thb, C.z, thc);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool is_an_integer(char c) {
|
||||
return ((int)c >= 48 && (int)c <= 57);
|
||||
}
|
||||
|
||||
pt_2d vect_diff(pt_2d p1, pt_2d p2) {
|
||||
pt_2d res;
|
||||
res.x = p2.x - p1.x ;
|
||||
res.y = p2.y - p1.y ;
|
||||
res.z = p2.z - p1.z ;
|
||||
return res;
|
||||
}
|
||||
|
||||
double dot2D(pt_2d p1, pt_2d p2) {
|
||||
return p1.x * p2.x + p1.y * p2.y ;
|
||||
}
|
||||
|
||||
double dot3D(pt_2d p1, pt_2d p2) {
|
||||
return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z ;
|
||||
}
|
||||
|
||||
double to_double(int n) {
|
||||
return (double)n ;
|
||||
}
|
||||
|
@ -188,14 +224,18 @@ teleporter create_teleporter(
|
|||
|
||||
// ------------------------------------------------------------------------------------------------ //
|
||||
|
||||
double convex_pt(double a, double b, double theta) {
|
||||
return (a+(b-a)*theta) ;
|
||||
}
|
||||
|
||||
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
|
||||
return sqrt((x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0)+(z1 - z0)*(z1 - z0)) ;
|
||||
}
|
||||
|
||||
double proj_distance_to_camera(double x, double y, double z) {
|
||||
return sqrt(x*x + y*y + z*z);
|
||||
}
|
||||
|
||||
double proj_pt_distance_to_camera(pt_2d p) {
|
||||
return proj_distance_to_camera(p.x, p.y, p.z);
|
||||
}
|
||||
|
||||
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez) {
|
||||
double theta = -(
|
||||
((ex - sx) * (sx - x) + (ey - sy) * (sy - y) + (ez - sz) * (sz - z)) /
|
||||
|
|
|
@ -12,6 +12,8 @@ double maxd(double a, double b);
|
|||
double absf(double n);
|
||||
int convex_seg(int x1, int x2, double theta);
|
||||
double convex_tri(double a, double tha, double b, double thb, double c, double thc);
|
||||
pt_2d convex_pt2d(pt_2d A, pt_2d B, double theta);
|
||||
pt_2d convex_pt2d_tri(pt_2d A, double tha, pt_2d B, double thb, pt_2d C, double thc);
|
||||
bool is_an_integer(char c);
|
||||
double to_double(int n);
|
||||
int to_int(double n);
|
||||
|
@ -23,6 +25,10 @@ bool pt_equal_2D(pt_2d p1, pt_2d p2);
|
|||
bool pt_equal_3D_eps(pt_2d p1, pt_2d p2, double epsilon);
|
||||
bool pt_equal_2D_eps(pt_2d p1, pt_2d p2, double epsilon, bool debug0);
|
||||
|
||||
pt_2d vect_diff(pt_2d p1, pt_2d p2);
|
||||
double dot2D(pt_2d p1, pt_2d p2);
|
||||
double dot3D(pt_2d p1, pt_2d p2);
|
||||
|
||||
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
|
||||
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
|
||||
void free_cube(cube c);
|
||||
|
@ -33,6 +39,8 @@ teleporter create_teleporter(
|
|||
|
||||
double convex_pt(double a, double b, double theta);
|
||||
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
|
||||
double proj_distance_to_camera(double x, double y, double z);
|
||||
double proj_pt_distance_to_camera(pt_2d p);
|
||||
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);
|
||||
|
||||
double convex_pt(double a, double b, double theta);
|
||||
|
|
235
src/display.c
235
src/display.c
|
@ -20,16 +20,17 @@
|
|||
#include "generation.h"
|
||||
#include "display.h"
|
||||
|
||||
int flashlight = 20 ;
|
||||
int flashlight = 0 ;
|
||||
|
||||
int MAX_SIZE = 8192 ;
|
||||
|
||||
int* drawOrder;
|
||||
|
||||
double draw_constant ;
|
||||
|
||||
int* cubeDrawOrder;
|
||||
bool* seen ;
|
||||
|
||||
pt_2d** triangles_to_render ;
|
||||
pt_2d** triangles_og_coords ;
|
||||
//double* triangles_areas ;
|
||||
int triangles_i ;
|
||||
|
||||
int* reds ;
|
||||
|
@ -42,21 +43,18 @@ int visited_i ;
|
|||
|
||||
void init_draworder() {
|
||||
drawOrder = malloc(sizeof(int)*6) ;
|
||||
cubeDrawOrder = malloc(sizeof(int)*2048);
|
||||
seen = malloc(sizeof(bool)*2048);
|
||||
for(int k = 0; k < 2048; k++) {
|
||||
cubeDrawOrder[k] = 0;
|
||||
seen[k] = false;
|
||||
}
|
||||
draw_constant = 0.4 ;
|
||||
triangles_to_render = malloc(sizeof(pt_2d*)*8192) ;
|
||||
reds = malloc(sizeof(int)*8192) ;
|
||||
greens = malloc(sizeof(int)*8192) ;
|
||||
blues = malloc(sizeof(int)*8192) ;
|
||||
triangles_order = malloc(sizeof(int)*8192) ;
|
||||
visited_tri = malloc(sizeof(bool)*8192) ;
|
||||
for(int k = 0; k < 8192; k++) {
|
||||
triangles_to_render = malloc(sizeof(pt_2d*)*MAX_SIZE) ;
|
||||
triangles_og_coords = malloc(sizeof(pt_2d*)*MAX_SIZE) ;
|
||||
reds = malloc(sizeof(int)*MAX_SIZE) ;
|
||||
greens = malloc(sizeof(int)*MAX_SIZE) ;
|
||||
blues = malloc(sizeof(int)*MAX_SIZE) ;
|
||||
triangles_order = malloc(sizeof(int)*MAX_SIZE) ;
|
||||
//triangles_areas = malloc(sizeof(double)*MAX_SIZE) ;
|
||||
visited_tri = malloc(sizeof(bool)*MAX_SIZE) ;
|
||||
for(int k = 0; k < MAX_SIZE; k++) {
|
||||
triangles_to_render[k] = malloc(sizeof(pt_2d)*3);
|
||||
triangles_og_coords[k] = malloc(sizeof(pt_2d)*3);
|
||||
triangles_order[k] = k;
|
||||
}
|
||||
triangles_i = 0;
|
||||
|
@ -488,9 +486,10 @@ void renderTriangleNoProject(
|
|||
|
||||
double near = -1.0 ;
|
||||
double far = 1.0 ;
|
||||
double getZbuffer(double z) {
|
||||
double getZbuffer(double x, double y, double z) {
|
||||
return sqrt(x*x + y*y + z*z);
|
||||
//return z;
|
||||
return (2.0*(z - near)/(far - near) -1.0);
|
||||
//return (2.0*(z - near)/(far - near) -1.0);
|
||||
//return ((far + near)/(far - near) + (1.0/z)*((-2.0*far*near)/(far - near)));
|
||||
}
|
||||
|
||||
|
@ -502,6 +501,30 @@ pt_2d to_pt_2d(double x0, double y0, double z0) {
|
|||
return res;
|
||||
}
|
||||
|
||||
bool inside_fov(double px, double py) {
|
||||
return true ;
|
||||
}
|
||||
|
||||
pt_2d bounding_box_botleft(int k) {
|
||||
pt_2d res;
|
||||
res.x = mind(triangles_to_render[k][0].x, mind(triangles_to_render[k][1].x, triangles_to_render[k][2].x));
|
||||
res.y = mind(triangles_to_render[k][0].y, mind(triangles_to_render[k][1].y, triangles_to_render[k][2].y));
|
||||
return res;
|
||||
}
|
||||
|
||||
pt_2d bounding_box_topright(int k) {
|
||||
pt_2d res;
|
||||
res.x = maxd(triangles_to_render[k][0].x, maxd(triangles_to_render[k][1].x, triangles_to_render[k][2].x));
|
||||
res.y = maxd(triangles_to_render[k][0].y, maxd(triangles_to_render[k][1].y, triangles_to_render[k][2].y));
|
||||
return res;
|
||||
}
|
||||
|
||||
bool bbox_inside_cam(int k) {
|
||||
pt_2d btl = bounding_box_botleft(k);
|
||||
pt_2d tpr = bounding_box_topright(k);
|
||||
return !(tpr.x <= 0.0 || btl.x >= 1500.0 || tpr.y <= 0.0 || btl.y >= 1000.0);
|
||||
}
|
||||
|
||||
void addTriangle(
|
||||
double x0, double y0, double z0,
|
||||
double x1, double y1, double z1,
|
||||
|
@ -515,84 +538,99 @@ void addTriangle(
|
|||
double fpx1; double fpy1; double fpz1;
|
||||
double mpx0; double mpy0; double mpz0;
|
||||
double mpx1; double mpy1; double mpz1;
|
||||
double mx0, my0, mz0, mx1, my1, mz1;
|
||||
project_to_camera(x0, y0, z0, &px0, &py0, &pz0);
|
||||
project_to_camera(x1, y1, z1, &px1, &py1, &pz1);
|
||||
project_to_camera(x2, y2, z2, &px2, &py2, &pz2);
|
||||
if(inside_fov(px0, py0) || inside_fov(px1, py1) || inside_fov(px2, py2)) {
|
||||
if(pz0 >= draw_constant && pz1 >= draw_constant && pz2 >= draw_constant) {
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(pz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(pz1));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(pz2));
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2));
|
||||
if(bbox_inside_cam(triangles_i)) {
|
||||
triangles_og_coords[triangles_i][0] = to_pt_2d(px0, py0, pz0);
|
||||
triangles_og_coords[triangles_i][1] = to_pt_2d(px1, py1, pz1);
|
||||
triangles_og_coords[triangles_i][2] = to_pt_2d(px2, py2, pz2);
|
||||
//triangles_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]);
|
||||
reds[triangles_i] = red ;
|
||||
greens[triangles_i] = green ;
|
||||
blues[triangles_i] = blue ;
|
||||
triangles_i += 1;
|
||||
}
|
||||
} else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 2) {
|
||||
if(pz0 < draw_constant) {
|
||||
// pz1 >= draw_constant and pz2 >+ draw_constant
|
||||
fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ;
|
||||
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
|
||||
mx0 = convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0));
|
||||
my0 = convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0));
|
||||
mz0 = convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0));
|
||||
mx1 = convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0));
|
||||
my1 = convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0));
|
||||
mz1 = convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0));
|
||||
// 1-0 segment
|
||||
project_to_camera(
|
||||
convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 0-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ;
|
||||
} else if(pz1 < draw_constant) {
|
||||
// pz0 >= draw_constant and pz2 >+ draw_constant
|
||||
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
|
||||
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
|
||||
mx0 = convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1));
|
||||
my0 = convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1));
|
||||
mz0 = convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1));
|
||||
mx1 = convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1));
|
||||
my1 = convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1));
|
||||
mz1 = convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1));
|
||||
// 0-1 segment
|
||||
project_to_camera(
|
||||
convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 1-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ;
|
||||
} else /*if(pz2 < draw_constant)*/ {
|
||||
// pz1 >= draw_constant and pz0 >+ draw_constant
|
||||
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
|
||||
fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ;
|
||||
mx0 = convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2));
|
||||
my0 = convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2));
|
||||
mz0 = convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2));
|
||||
mx1 = convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2));
|
||||
my1 = convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2));
|
||||
mz1 = convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2));
|
||||
// 0-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 1-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ;
|
||||
}
|
||||
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, getZbuffer(fpz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpz0));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpz1));
|
||||
triangles_to_render[triangles_i+1][0] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpz0));
|
||||
triangles_to_render[triangles_i+1][1] = to_pt_2d(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, getZbuffer(mpz1));
|
||||
triangles_to_render[triangles_i+1][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpz1));
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, getZbuffer(fpx0, fpy0, fpz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1));
|
||||
if(bbox_inside_cam(triangles_i)) {
|
||||
triangles_og_coords[triangles_i][0] = to_pt_2d(fpx0, fpy0, fpz0);
|
||||
triangles_og_coords[triangles_i][1] = to_pt_2d(mpx0, mpy0, mpz0);
|
||||
triangles_og_coords[triangles_i][2] = to_pt_2d(fpx1, fpy1, fpz1);
|
||||
reds[triangles_i] = red ;
|
||||
greens[triangles_i] = green ;
|
||||
blues[triangles_i] = blue ;
|
||||
reds[triangles_i+1] = red ;
|
||||
greens[triangles_i+1] = green ;
|
||||
blues[triangles_i+1] = blue ;
|
||||
triangles_i += 2 ;
|
||||
triangles_i += 1;
|
||||
}
|
||||
//triangles_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]);
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, getZbuffer(mpx1, mpy1, mpz1));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1));
|
||||
if(bbox_inside_cam(triangles_i)) {
|
||||
triangles_og_coords[triangles_i][0] = to_pt_2d(mpx0, mpy0, mpz0);
|
||||
triangles_og_coords[triangles_i][1] = to_pt_2d(mpx1, mpy1, mpz1);
|
||||
triangles_og_coords[triangles_i][2] = to_pt_2d(fpx1, fpy1, fpz1);
|
||||
reds[triangles_i] = red ;
|
||||
greens[triangles_i] = green ;
|
||||
blues[triangles_i] = blue ;
|
||||
triangles_i += 1;
|
||||
}
|
||||
//triangles_areas[triangles_i+1] = area_of_triangle(triangles_to_render[triangles_i+1]);
|
||||
} else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 1) {
|
||||
if(pz0 >= draw_constant) {
|
||||
project_to_camera(
|
||||
|
@ -629,16 +667,23 @@ void addTriangle(
|
|||
&px1, &py1, &pz1);
|
||||
}
|
||||
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(pz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(pz1));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(pz2));
|
||||
triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0));
|
||||
triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1));
|
||||
triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2));
|
||||
if(bbox_inside_cam(triangles_i)) {
|
||||
triangles_og_coords[triangles_i][0] = to_pt_2d(px0, py0, pz0);
|
||||
triangles_og_coords[triangles_i][1] = to_pt_2d(px1, py1, pz1);
|
||||
triangles_og_coords[triangles_i][2] = to_pt_2d(px2, py2, pz2);
|
||||
//triangles_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]);
|
||||
reds[triangles_i] = red ;
|
||||
greens[triangles_i] = green ;
|
||||
blues[triangles_i] = blue ;
|
||||
triangles_i += 1;
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void addTriangleRotated(
|
||||
|
@ -663,7 +708,6 @@ void add_single(cube_0 c) { // x
|
|||
int leng = surfaceDrawOrder(camx, camy, camz, c);
|
||||
for(int sf0 = 0; sf0 < leng; sf0++) {
|
||||
int sf = drawOrder[sf0];
|
||||
//printf("%d\n", sf);
|
||||
if(sf == 0 || sf == 1) {
|
||||
addTriangleRotated(
|
||||
c.x + c.w*(sf==0), c.y , c.z,
|
||||
|
@ -734,14 +778,43 @@ void renderTriangleFull(SDL_Renderer* renderer, int k) {
|
|||
);
|
||||
}
|
||||
|
||||
double deltaz ;
|
||||
void visiting(int k, bool vflag) {
|
||||
void remove_hidden(SDL_Renderer* renderer) {
|
||||
printf("%d --> ", triangles_i);
|
||||
for(int k = 0; k < triangles_i; k++) {
|
||||
int halt = 1 ;
|
||||
bool fst = false ;
|
||||
bool snd = false ;
|
||||
bool thd = false ;
|
||||
for(int l = 0; l < halt*triangles_i; l++) {
|
||||
if(l != k) {
|
||||
fst = fst || (is_hidden(renderer, triangles_to_render[k][0], triangles_og_coords[k][0], triangles_to_render[l], triangles_og_coords[l]));
|
||||
snd = snd || (is_hidden(renderer, triangles_to_render[k][1], triangles_og_coords[k][1], triangles_to_render[l], triangles_og_coords[l]));
|
||||
thd = thd || (is_hidden(renderer, triangles_to_render[k][2], triangles_og_coords[k][2], triangles_to_render[l], triangles_og_coords[l]));
|
||||
if(fst && snd && thd) {
|
||||
triangles_to_render[k][0] = triangles_to_render[triangles_i-1][0] ;
|
||||
triangles_to_render[k][1] = triangles_to_render[triangles_i-1][1] ;
|
||||
triangles_to_render[k][2] = triangles_to_render[triangles_i-1][2] ;
|
||||
reds[k] = reds[triangles_i-1];
|
||||
greens[k] = greens[triangles_i-1];
|
||||
blues[k] = blues[triangles_i-1];
|
||||
//triangles_areas[k] = triangles_areas[triangles_i-1];
|
||||
triangles_i -= 1;
|
||||
k -= 1;
|
||||
halt = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", triangles_i);
|
||||
}
|
||||
|
||||
void visit(int k, bool vflag) {
|
||||
if(visited_tri[k] != vflag) {
|
||||
visited_tri[k] = vflag;
|
||||
for(int k2 = 0; k2 < triangles_i; k2++) {
|
||||
if(k2 != k && visited_tri[k2] != vflag) {
|
||||
if(delta_get(triangles_to_render[k], triangles_to_render[k2], &deltaz) && deltaz > 0.01) {
|
||||
visiting(k2, vflag);
|
||||
visited_tri[k] = vflag ;
|
||||
for(int l = 0; l < triangles_i; l++) {
|
||||
if(l != k && visited_tri[l] != vflag) {
|
||||
if(is_in_front(triangles_to_render[k], triangles_og_coords[k], triangles_to_render[l], triangles_og_coords[l])) {
|
||||
visit(l, vflag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -753,7 +826,7 @@ void visiting(int k, bool vflag) {
|
|||
void topological_sort() {
|
||||
bool vflag = !visited_tri[0];
|
||||
for(int k = 0; k < triangles_i; k++) {
|
||||
visiting(k, vflag);
|
||||
visit(k, vflag);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -764,17 +837,29 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
|
|||
add_triangles_tp(current_room->tps, current_room->tps_size);
|
||||
add_triangles_ent(current_room->ents, current_room->ent_len);
|
||||
topological_sort();
|
||||
//remove_hidden(renderer);
|
||||
//topological_sort();
|
||||
for(int k = 0; k < triangles_i; k++) {
|
||||
renderTriangleFull(renderer, triangles_order[k]);
|
||||
}
|
||||
/*for(int k = 0; k < triangles_i; k++) {
|
||||
drawNumberToRenderer(renderer, digits,
|
||||
(int)(0.33*(triangles_to_render[k][0].z+triangles_to_render[k][1].z+triangles_to_render[k][2].z)),
|
||||
(int)(3.3*(proj_pt_distance_to_camera(triangles_og_coords[k][0])+proj_pt_distance_to_camera(triangles_og_coords[k][1])+proj_pt_distance_to_camera(triangles_og_coords[k][2]))),
|
||||
(int)(0.33*(triangles_to_render[k][0].x+triangles_to_render[k][1].x+triangles_to_render[k][2].x)),
|
||||
(int)(0.33*(triangles_to_render[k][0].y+triangles_to_render[k][1].y+triangles_to_render[k][2].y)),
|
||||
75/4, 105/4, 0
|
||||
);
|
||||
}*/
|
||||
for(int k = 0; k < 0*triangles_i; k++) {
|
||||
for(int l = 0; l < 3; l++) {
|
||||
drawNumberToRenderer(renderer, digits,
|
||||
(int)(proj_pt_distance_to_camera(triangles_og_coords[k][l])),
|
||||
(int)(triangles_to_render[k][l].x),
|
||||
(int)(triangles_to_render[k][l].y),
|
||||
75/5, 105/5, 0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------------- //
|
||||
|
|
|
@ -71,6 +71,8 @@ void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
|
|||
ret->green = 192;
|
||||
ret->blue = 0;
|
||||
coins += *hp;
|
||||
player_hp -= 10*(*hp);
|
||||
fade_dmg = 255 ;
|
||||
*hp = 0 ;
|
||||
}
|
||||
}
|
|
@ -80,7 +80,7 @@ void build_starting_chunk(int chx, int chy) {
|
|||
new->chunk_x = chx ;
|
||||
new->chunk_y = chy ;
|
||||
new->map = malloc(sizeof(cube_0)*8);
|
||||
new->map_size = 8 ;
|
||||
new->map_size = 9 ;
|
||||
new->tps = malloc(sizeof(teleporter)*4);
|
||||
new->tps_size = 4 ;
|
||||
|
||||
|
@ -92,6 +92,7 @@ void build_starting_chunk(int chx, int chy) {
|
|||
new->map[5] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
|
||||
new->map[6] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
|
||||
new->map[7] = create_cube_0(4.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
|
||||
new->map[8] = create_cube_0(10.0, 10.0, 10.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 128, 255);
|
||||
|
||||
new->tps[0] = create_teleporter(-1.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 0, 0 , chx-1, chy , 2.5, 2.5, 2.5);
|
||||
new->tps[1] = create_teleporter(2.0, 1.0, -1.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 255, 0, chx , chy-1, 2.5, 2.5, 2.5);
|
||||
|
|
|
@ -56,7 +56,7 @@ int main(int argc, char** argv) {
|
|||
init_hashtbl() ;
|
||||
init_draworder() ;
|
||||
trInit();
|
||||
parse_rooms(4);
|
||||
parse_rooms(5);
|
||||
import_digits(rend) ;
|
||||
import_letters(rend) ;
|
||||
sim_time = 0.0 ;
|
||||
|
@ -81,8 +81,11 @@ int main(int argc, char** argv) {
|
|||
|
||||
drawCurrentRoom(rend);
|
||||
drawData(rend) ;
|
||||
drawHPbar(rend);
|
||||
finish = clock();
|
||||
|
||||
fade_dmg = max(fade_dmg-5, 0);
|
||||
|
||||
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
|
||||
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
|
||||
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
|
||||
|
|
25
src/move.c
25
src/move.c
|
@ -24,6 +24,8 @@ double speed = 0.22 ;
|
|||
double min_dist = 0.7 ;
|
||||
// ---------------------------------------------------------------------------------------------------- //
|
||||
|
||||
int player_hp ;
|
||||
|
||||
double camx ;
|
||||
double camy ;
|
||||
double camz ;
|
||||
|
@ -34,6 +36,7 @@ double rot_vt ;
|
|||
double tan_fov ;
|
||||
|
||||
int draw_type ;
|
||||
int fade_dmg ;
|
||||
|
||||
bool has_changed ;
|
||||
|
||||
|
@ -44,6 +47,8 @@ void init_csts() {
|
|||
rot_hz = 0.0 ;
|
||||
rot_vt = 180.0 ;
|
||||
draw_type = 0 ;
|
||||
player_hp = 1000 ;
|
||||
fade_dmg = 0;
|
||||
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
|
||||
}
|
||||
|
||||
|
@ -200,3 +205,23 @@ void playerActions(float dtime) {
|
|||
rot_vt += sensitivity ;
|
||||
}
|
||||
}
|
||||
|
||||
void drawHPbar(SDL_Renderer* renderer) {
|
||||
SDL_Rect r ;
|
||||
r.x = 1400 ;
|
||||
r.y = 100 ;
|
||||
r.w = 50 ;
|
||||
r.h = 800 ;
|
||||
SDL_SetRenderDrawColor(renderer, 96, 96, 96, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
r.x += 10 ;
|
||||
r.w -= 20 ;
|
||||
r.y += 10 ;
|
||||
r.h -= 20 ;
|
||||
SDL_SetRenderDrawColor(renderer, 32, 32, 32, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
r.y = 110+(780*(1000-player_hp))/1000 ;
|
||||
r.h = 780-r.y+110 ;
|
||||
SDL_SetRenderDrawColor(renderer, fade_dmg, 255-fade_dmg, 32, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
}
|
|
@ -5,4 +5,6 @@ void init_csts();
|
|||
bool is_colliding(float dtime);
|
||||
void playerActions(float dtime);
|
||||
|
||||
void drawHPbar(SDL_Renderer* renderer);
|
||||
|
||||
#endif
|
|
@ -116,6 +116,12 @@ extern int draw_type ;
|
|||
extern double draw_constant ;
|
||||
|
||||
extern pt_2d** triangles_to_render ;
|
||||
extern pt_2d** triangles_og_coords ;
|
||||
extern double* triangles_areas;
|
||||
extern int triangles_i ;
|
||||
|
||||
extern int player_hp ;
|
||||
|
||||
extern int fade_dmg ;
|
||||
|
||||
#endif
|
314
src/triangles.c
314
src/triangles.c
|
@ -153,195 +153,6 @@ bool multipleTrianglesIntersection(pt_2d* tri1, int len1, pt_2d* tri2, int len2,
|
|||
return false ;
|
||||
}
|
||||
|
||||
pt_2d to_fpoint(double x0, double y0, double z0) {
|
||||
pt_2d res;
|
||||
res.x = x0;
|
||||
res.y = y0;
|
||||
res.z = z0;
|
||||
return res;
|
||||
}
|
||||
|
||||
int returnTriangle(
|
||||
double x0, double y0, double z0,
|
||||
double x1, double y1, double z1,
|
||||
double x2, double y2, double z2,
|
||||
pt_2d* retarr
|
||||
) {
|
||||
double px0; double py0; double pz0;
|
||||
double px1; double py1; double pz1;
|
||||
double px2; double py2; double pz2;
|
||||
double fpx0; double fpy0; double fpz0;
|
||||
double fpx1; double fpy1; double fpz1;
|
||||
double mpx0; double mpy0; double mpz0;
|
||||
double mpx1; double mpy1; double mpz1;
|
||||
project_to_camera(x0, y0, z0, &px0, &py0, &pz0);
|
||||
project_to_camera(x1, y1, z1, &px1, &py1, &pz1);
|
||||
project_to_camera(x2, y2, z2, &px2, &py2, &pz2);
|
||||
if(pz0 >= (draw_constant + draw_incr) && pz1 >= (draw_constant + draw_incr) && pz2 >= (draw_constant + draw_incr)) {
|
||||
retarr[0] = to_fpoint(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, pz0);
|
||||
retarr[1] = to_fpoint(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, pz1);
|
||||
retarr[2] = to_fpoint(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, pz2);
|
||||
return 3;
|
||||
} else if((pz0 >= (draw_constant + draw_incr)) + (pz1 >= (draw_constant + draw_incr)) + (pz2 >= (draw_constant + draw_incr)) == 2) {
|
||||
if(pz0 < (draw_constant + draw_incr)) {
|
||||
// pz1 >= (draw_constant + draw_incr) and pz2 >+ (draw_constant + draw_incr)
|
||||
fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ;
|
||||
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
|
||||
// 1-0 segment
|
||||
project_to_camera(
|
||||
convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 0-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
} else if(pz1 < (draw_constant + draw_incr)) {
|
||||
// pz0 >= (draw_constant + draw_incr) and pz2 >+ (draw_constant + draw_incr)
|
||||
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
|
||||
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
|
||||
// 0-1 segment
|
||||
project_to_camera(
|
||||
convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 1-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
} else /*if(pz2 < (draw_constant + draw_incr))*/ {
|
||||
// pz1 >= (draw_constant + draw_incr) and pz0 >+ (draw_constant + draw_incr)
|
||||
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
|
||||
fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ;
|
||||
// 0-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
&mpx0, &mpy0, &mpz0) ;
|
||||
|
||||
// 1-2 segment
|
||||
project_to_camera(
|
||||
convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
&mpx1, &mpy1, &mpz1) ;
|
||||
}
|
||||
|
||||
retarr[0] = to_fpoint(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, fpz0);
|
||||
retarr[1] = to_fpoint(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, mpz0);
|
||||
retarr[2] = to_fpoint(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, fpz1);
|
||||
retarr[3] = to_fpoint(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, mpz0);
|
||||
retarr[4] = to_fpoint(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, mpz1);
|
||||
retarr[5] = to_fpoint(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, fpz1);
|
||||
return 6;
|
||||
} else if((pz0 >= (draw_constant + draw_incr)) + (pz1 >= (draw_constant + draw_incr)) + (pz2 >= (draw_constant + draw_incr)) == 1) {
|
||||
if(pz0 >= (draw_constant + draw_incr)) {
|
||||
project_to_camera(
|
||||
convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
|
||||
&px1, &py1, &pz1);
|
||||
project_to_camera(
|
||||
convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
|
||||
&px2, &py2, &pz2);
|
||||
} else if(pz1 >= (draw_constant + draw_incr)) {
|
||||
project_to_camera(
|
||||
convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
|
||||
&px0, &py0, &pz0);
|
||||
project_to_camera(
|
||||
convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
|
||||
&px2, &py2, &pz2);
|
||||
} else if(pz2 >= (draw_constant + draw_incr)) {
|
||||
project_to_camera(
|
||||
convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
|
||||
&px0, &py0, &pz0);
|
||||
project_to_camera(
|
||||
convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
|
||||
&px1, &py1, &pz1);
|
||||
}
|
||||
|
||||
retarr[0] = to_fpoint(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, pz0);
|
||||
retarr[1] = to_fpoint(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, pz1);
|
||||
retarr[2] = to_fpoint(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, pz2);
|
||||
return 3;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret) {
|
||||
// trig is either 0 or 1
|
||||
// returns the length of the result
|
||||
if(sf == 0 || sf == 1) { // x
|
||||
if(trig == 0) {
|
||||
return returnTriangle(
|
||||
c.x + c.w*(sf==0), c.y, c.z,
|
||||
c.x + c.w*(sf==0), c.y + c.h, c.z,
|
||||
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
|
||||
ret
|
||||
);
|
||||
} else {
|
||||
return returnTriangle(
|
||||
c.x + c.w*(sf==0), c.y, c.z,
|
||||
c.x + c.w*(sf==0), c.y, c.z + c.d,
|
||||
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
|
||||
ret
|
||||
);
|
||||
}
|
||||
} else if(sf == 2 || sf == 3) { // y
|
||||
if(trig == 0) {
|
||||
return returnTriangle(
|
||||
c.x, c.y + c.h*(sf==2), c.z,
|
||||
c.x + c.w, c.y + c.h*(sf==2), c.z,
|
||||
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
|
||||
ret
|
||||
);
|
||||
} else {
|
||||
return returnTriangle(
|
||||
c.x, c.y + c.h*(sf==2), c.z,
|
||||
c.x, c.y + c.h*(sf==2), c.z + c.d,
|
||||
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
|
||||
ret
|
||||
);
|
||||
}
|
||||
} else { // z
|
||||
if(trig == 0) {
|
||||
return returnTriangle(
|
||||
c.x, c.y, c.z + c.d*(sf==4),
|
||||
c.x + c.w, c.y, c.z + c.d*(sf==4),
|
||||
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
|
||||
ret
|
||||
);
|
||||
} else {
|
||||
return returnTriangle(
|
||||
c.x, c.y, c.z + c.d*(sf==4),
|
||||
c.x, c.y + c.h, c.z + c.d*(sf==4),
|
||||
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
|
||||
ret
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int visibleSurfaces(double x0, double y0, double z0, cube_0 cb, int* dOrd) {
|
||||
// returns the number of surfaces that should be drawn, as well as filling dOrd for said surfaces :
|
||||
// 0 = +x ; 1 = -x
|
||||
|
@ -450,12 +261,8 @@ bool point_in_triangle(pt_2d pt, pt_2d v1, pt_2d v2, pt_2d v3, double* thetaA, d
|
|||
|
||||
bool seg_seg_inter(pt_2d p1, pt_2d p2, pt_2d p3, pt_2d p4, double* ret0, double* ret1) {
|
||||
double deno = (p4.x - p3.x)*(p2.y - p1.y) - (p4.y - p3.y)*(p2.x - p1.x);
|
||||
if(0.0 <= deno && deno <= 0.001) {
|
||||
//printf("%lf -->", deno);
|
||||
deno = 0.001 ;
|
||||
} else if(-0.001 <= deno && deno <= 0.0) {
|
||||
//printf("%lf -->", deno);
|
||||
deno = -0.001 ;
|
||||
if(absf(deno) <= 0.0001) {
|
||||
return false;
|
||||
}
|
||||
//printf("%lf\n", deno);
|
||||
double alpha =
|
||||
|
@ -471,49 +278,98 @@ bool seg_seg_inter(pt_2d p1, pt_2d p2, pt_2d p3, pt_2d p4, double* ret0, double*
|
|||
return (alpha >= 0.0 && alpha <= 1.0 && beta >= 0.0 && beta <= 1.0);
|
||||
}
|
||||
|
||||
void print_tri(pt_2d* t) {
|
||||
printf("T :\n (%lf, %lf, %lf)\n (%lf, %lf, %lf)\n (%lf, %lf, %lf)\n", t[0].x, t[0].y, t[0].z, t[1].x, t[1].y, t[1].z, t[2].x, t[2].y, t[2].z);
|
||||
}
|
||||
|
||||
double triangle_dist(pt_2d* tri) {
|
||||
return (tri[0].z + tri[1].z + tri[2].z);
|
||||
}
|
||||
|
||||
bool delta_get2(pt_2d* tri1, pt_2d* tri2, double* ret) {
|
||||
if(triangleIntersection(tri1, tri2)) {
|
||||
if(ret != NULL) {
|
||||
*ret = triangle_dist(tri2) - triangle_dist(tri1);
|
||||
void get_barycentric(pt_2d p, pt_2d* tri, double* retu, double* retv, double* retw) {
|
||||
pt_2d a = tri[0] ; pt_2d b = tri[1] ; pt_2d c = tri[2] ;
|
||||
pt_2d v0 = vect_diff(b, a);
|
||||
pt_2d v1 = vect_diff(c, a);
|
||||
pt_2d v2 = vect_diff(p, a);
|
||||
double d00 = dot2D(v0, v0);
|
||||
double d01 = dot2D(v0, v1);
|
||||
double d11 = dot2D(v1, v1);
|
||||
double d20 = dot2D(v2, v0);
|
||||
double d21 = dot2D(v2, v1);
|
||||
double denom = d00 * d11 - d01 * d01;
|
||||
if(retv != NULL) {
|
||||
*retv = (d11 * d20 - d01 * d21) / denom;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false ;
|
||||
if(retw != NULL) {
|
||||
*retw = (d00 * d21 - d01 * d20) / denom;
|
||||
}
|
||||
if(retu != NULL) {
|
||||
*retu = 1.0 - *retv - *retw ;
|
||||
}
|
||||
}
|
||||
|
||||
bool delta_get(pt_2d* tri1, pt_2d* tri2, double* ret) {
|
||||
double th1 = 0.0;
|
||||
double th2 = 0.0;
|
||||
double th3 = 0.0;
|
||||
for(int k = 0; k < 3; k++) { // pt
|
||||
if(point_in_triangle(tri1[k], tri2[0], tri2[1], tri2[2], &th1, &th2, &th3)) {
|
||||
//printf("%lf, %lf, %lf\n", th1, th2, th3);
|
||||
if(ret != NULL) {*ret = convex_tri(tri2[0].z, th1, tri2[1].z, th2, tri2[2].z, th3) - tri1[k].z;}
|
||||
return true;
|
||||
bool pt_equal(pt_2d p1, pt_2d p2, double epsilon) {
|
||||
return (absf(p2.x - p1.x) <= epsilon && absf(p2.y - p1.y) <= epsilon && absf(p2.z - p1.z) <= epsilon) ;
|
||||
}
|
||||
|
||||
bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og) {
|
||||
if(pt_equal(p, tri[0], 0.0001) || pt_equal(p, tri[1], 0.0001) || pt_equal(p, tri[2], 0.0001)) {
|
||||
return false;
|
||||
}
|
||||
if(point_in_triangle(tri2[k], tri1[0], tri1[1], tri1[2], &th1, &th2, &th3)) {
|
||||
//printf("%lf, %lf, %lf\n", th1, th2, th3);
|
||||
if(ret != NULL) {*ret = tri2[k].z - convex_tri(tri1[0].z, th1, tri1[1].z, th2, tri1[2].z, th3);}
|
||||
return true;
|
||||
double u = 0.0 ;
|
||||
double v = 0.0 ;
|
||||
double w = 0.0 ;
|
||||
get_barycentric(p, tri, &u, &v, &w);
|
||||
pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w);
|
||||
if(renderer != NULL && (u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) {
|
||||
if(mid.z >= 0.4) {
|
||||
SDL_Rect r;
|
||||
r.x = (int)(1500.0 * (1.0 + (mid.x / (1.5 * mid.z * tan_fov))) / 2.0) -2;
|
||||
r.y = (int)(1000.0 * (1.0 + (mid.y / (mid.z * tan_fov))) / 2.0) -2;
|
||||
r.w = 4 ;
|
||||
r.h = 4 ;
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
}
|
||||
}
|
||||
for(int k1 = 0; k1 < 3; k1++) { // seg
|
||||
if((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) {
|
||||
return (proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(ogp));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool nonzero(double tha, double thb, double thc, double epsilon) {
|
||||
return (absf(tha) > epsilon && absf(thb) > epsilon && absf(thc) > epsilon);
|
||||
}
|
||||
|
||||
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
|
||||
for(int k = 0; k < 3; k++) {
|
||||
pt_2d p = tri1[k];
|
||||
if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) {
|
||||
return false;
|
||||
}
|
||||
double u = 0.0 ;
|
||||
double v = 0.0 ;
|
||||
double w = 0.0 ;
|
||||
get_barycentric(p, tri2, &u, &v, &w);
|
||||
pt_2d mid = convex_pt2d_tri(og2[0], u, og2[1], v, og2[2], w);
|
||||
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
|
||||
return !(proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(og1[k]));
|
||||
}
|
||||
}
|
||||
for(int k = 0; k < 3; k++) {
|
||||
pt_2d p = tri2[k];
|
||||
if(pt_equal(p, tri1[0], 0.0001) || pt_equal(p, tri1[1], 0.0001) || pt_equal(p, tri1[2], 0.0001)) {
|
||||
return false;
|
||||
}
|
||||
double u = 0.0 ;
|
||||
double v = 0.0 ;
|
||||
double w = 0.0 ;
|
||||
get_barycentric(p, tri1, &u, &v, &w);
|
||||
pt_2d mid = convex_pt2d_tri(og1[0], u, og1[1], v, og1[2], w);
|
||||
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
|
||||
return !(proj_pt_distance_to_camera(mid) >= proj_pt_distance_to_camera(og2[k]));
|
||||
}
|
||||
}
|
||||
for(int k1 = 0; k1 < 3; k1++) {
|
||||
for(int k2 = 0; k2 < 3; k2++) {
|
||||
double th1, th2;
|
||||
if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) {
|
||||
//print_tri(tri1);
|
||||
//print_tri(tri2);
|
||||
//printf("%lf, %lf\n\n", th1, th2);
|
||||
if(ret != NULL) {*ret = convex_pt(tri2[k2].z, tri2[(k2+1)%3].z, th2) - convex_pt(tri1[k1].z, tri1[(k1+1)%3].z, th1);}
|
||||
return true;
|
||||
pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1);
|
||||
pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2);
|
||||
return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,8 @@ bool triangleIntersection(pt_2d* tri1, pt_2d* tri2);
|
|||
bool triangleIntersectionDec(pt_2d t1_1, pt_2d t1_2, pt_2d t1_3, pt_2d t2_1, pt_2d t2_2, pt_2d t2_3);
|
||||
bool multipleTrianglesIntersection(pt_2d* tri1, int len1, pt_2d* tri2, int len2, int* ret1, int* ret2);
|
||||
|
||||
int returnTriangle(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, pt_2d* retarr);
|
||||
int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret);
|
||||
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2);
|
||||
|
||||
bool delta_get(pt_2d* tri1, pt_2d* tri2, double* ret);
|
||||
bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og);
|
||||
|
||||
#endif
|
|
@ -11,6 +11,6 @@ Teleporters :
|
|||
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
||||
|
||||
Weight :
|
||||
50
|
||||
20
|
||||
|
||||
$
|
|
@ -0,0 +1,14 @@
|
|||
Blocks :
|
||||
[-10.0, 0.0, -10.0, 20.0, 0.9, 20.0, 0.0, 0.0, 192, 192, 192]
|
||||
[-10.0, 7.1, -10.0, 20.0, 0.9, 20.0, 0.0, 0.0, 192, 192, 192]
|
||||
|
||||
Teleporters :
|
||||
[-10.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 255, 0, 0; 0, -1]
|
||||
[-5.0, 1.0, -10.0, 10.0, 2.0, 1.0, 0.0, 0.0, 255, 255, 0; -1, 0]
|
||||
[9.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 0, 255, 0; 0, 1]
|
||||
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
||||
|
||||
Weight :
|
||||
10
|
||||
|
||||
$
|
Loading…
Reference in New Issue