draw_cube: complete cube wireframe
This commit is contained in:
parent
149ab4c305
commit
8b50f2e6a2
110
main.c
110
main.c
|
@ -17,6 +17,15 @@ typedef struct Point3d
|
|||
float z;
|
||||
} Point3d;
|
||||
|
||||
typedef struct Cam
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
float h;
|
||||
} Cam;
|
||||
|
||||
typedef struct Quad
|
||||
{
|
||||
Point3d p[4];
|
||||
|
@ -38,7 +47,7 @@ typedef struct Cube
|
|||
float d;
|
||||
} Cube;
|
||||
|
||||
Point2d rasterize(Point3d cam,Point3d p)
|
||||
Point2d rasterize(Cam cam,Point3d p)
|
||||
{
|
||||
Point2d p2={0};
|
||||
if(!p.z)return p2; // divide by zero error
|
||||
|
@ -59,38 +68,44 @@ void print_point2d(Point2d p)
|
|||
printf("(%g,%g)\n",p.x,p.y);
|
||||
}
|
||||
|
||||
#define cube_wire(cam,r)\
|
||||
do{\
|
||||
if(r.z>cam.z){\
|
||||
Point2d _p[4];\
|
||||
Point3d _p3[4];\
|
||||
_p3[0]=(Point3d){.x=r.x,.y=r.y,.z=r.z};\
|
||||
_p3[1]=(Point3d){.x=r.x+r.w,.y=r.y,.z=r.z};\
|
||||
_p3[2]=(Point3d){.x=r.x,.y=r.y+r.h,.z=r.z};\
|
||||
_p3[3]=(Point3d){.x=r.x+r.w,.y=r.y+r.h,.z=r.z};\
|
||||
for(int i=0;i<4;++i)\
|
||||
_p[i]=rasterize(cam,_p3[i]);\
|
||||
SDL_RenderDrawLine(ren,width/2+_p[0].x,height/2-_p[0].y,width/2+_p[1].x,height/2-_p[1].y);\
|
||||
SDL_RenderDrawLine(ren,width/2+_p[2].x,height/2-_p[2].y,width/2+_p[3].x,height/2-_p[3].y);\
|
||||
SDL_RenderDrawLine(ren,width/2+_p[0].x,height/2-_p[0].y,width/2+_p[2].x,height/2-_p[2].y);\
|
||||
SDL_RenderDrawLine(ren,width/2+_p[1].x,height/2-_p[1].y,width/2+_p[3].x,height/2-_p[3].y);\
|
||||
}\
|
||||
}while(0)
|
||||
void draw_cube(SDL_Renderer*ren,Cam cam,Point3d r,int w)
|
||||
{
|
||||
Point2d p2[8];
|
||||
Point3d p3[8];
|
||||
float f=0.01;
|
||||
|
||||
if(!ren)return;
|
||||
|
||||
p3[0]=(Point3d){.x=r.x,.y=r.y,.z=r.z};
|
||||
p3[1]=(Point3d){.x=r.x+w,.y=r.y,.z=r.z};
|
||||
p3[2]=(Point3d){.x=r.x,.y=r.y+w,.z=r.z};
|
||||
p3[3]=(Point3d){.x=r.x+w,.y=r.y+w,.z=r.z};
|
||||
p3[4]=(Point3d){.x=r.x,.y=r.y,.z=r.z+w*f};
|
||||
p3[5]=(Point3d){.x=r.x+w,.y=r.y,.z=r.z+w*f};
|
||||
p3[6]=(Point3d){.x=r.x,.y=r.y+w,.z=r.z+w*f};
|
||||
p3[7]=(Point3d){.x=r.x+w,.y=r.y+w,.z=r.z+w*f};
|
||||
|
||||
for(int i=0;i<8;++i)
|
||||
p2[i]=rasterize(cam,p3[i]);
|
||||
|
||||
#define l(i,x1,y1,x2,y2) if(p3[i].z>cam.z&&p3[i+1].z>cam.z)SDL_RenderDrawLine(ren,cam.w/2+x1,cam.h/2+y1,cam.w/2+x2,cam.h/2+y2)
|
||||
#define dl(a,b) l(a,p2[a].x,p2[a].y,p2[b].x,p2[b].y)
|
||||
dl(0,1);dl(0,2);dl(2,3);dl(1,3);
|
||||
dl(4,5);dl(4,6);dl(6,7);dl(5,7);
|
||||
dl(0,4);dl(1,5);dl(2,6);dl(3,7);
|
||||
#undef l
|
||||
#undef dl
|
||||
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Point2d p2;
|
||||
Point3d p3={.x=2,.y=3,.z=1};
|
||||
Point3d cam={0};
|
||||
Quad q1;
|
||||
Quad q2;
|
||||
Cam cam={.x=15,.y=10,.z=0.5,.w=320,.h=240};
|
||||
SDL_Renderer*ren;
|
||||
SDL_Window*win;
|
||||
bool running=true;
|
||||
const int height=240;
|
||||
const int width=320;
|
||||
Cube cube={.x=5,.y=4,.w=20,.h=20,.z=1};
|
||||
Cube cube2={.x=5,.y=4,.w=20,.h=20,.z=2};
|
||||
|
||||
// Init SDL
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
@ -98,27 +113,6 @@ int main(void)
|
|||
SDL_WINDOWPOS_UNDEFINED,width,height,0);
|
||||
ren=SDL_CreateRenderer(win,-1,0);
|
||||
|
||||
q1.p[0]=(Point3d){.x=1,.y=20,.z=1};
|
||||
q1.p[1]=(Point3d){.x=20,.y=20,.z=1};
|
||||
q1.p[2]=(Point3d){.x=1,.y=1,.z=1};
|
||||
q1.p[3]=(Point3d){.x=20,.y=1,.z=1};
|
||||
|
||||
q2.p[0]=(Point3d){.x=1,.y=20,.z=2};
|
||||
q2.p[1]=(Point3d){.x=20,.y=20,.z=2};
|
||||
q2.p[2]=(Point3d){.x=1,.y=1,.z=2};
|
||||
q2.p[3]=(Point3d){.x=20,.y=1,.z=2};
|
||||
|
||||
/* for(int i=0;i<4;++i) */
|
||||
/* { */
|
||||
/* Point2d p=rasterize(q1.p[i]); */
|
||||
/* print_point2d(p); */
|
||||
/* } */
|
||||
|
||||
// Convert points
|
||||
print_point3d(p3);
|
||||
p2=rasterize(cam,p3);
|
||||
print_point2d(p2);
|
||||
|
||||
// Event loop
|
||||
while(running)
|
||||
{
|
||||
|
@ -164,31 +158,11 @@ int main(void)
|
|||
SDL_RenderDrawLine(ren,width/2,0,width/2,height);
|
||||
SDL_RenderDrawLine(ren,0,height/2,width,height/2);
|
||||
|
||||
// Draw points
|
||||
// Draw cube
|
||||
SDL_SetRenderDrawColor(ren,255,255,255,0);
|
||||
/* SDL_RenderDrawPoint(ren,p2.x,p2.y); */
|
||||
/* for(int i=0;i<4;++i) */
|
||||
/* { */
|
||||
/* Point2d p; */
|
||||
/* p=rasterize(q1.p[i]); */
|
||||
/* SDL_RenderDrawPoint(ren,width/2+p.x,height/2-p.y); */
|
||||
/* */
|
||||
/* p=rasterize(q2.p[i]); */
|
||||
/* SDL_RenderDrawPoint(ren,width/2+p.x,height/2-p.y); */
|
||||
/* } */
|
||||
|
||||
/* SDL_SetRenderDrawColor(ren,255,255,255,0); */
|
||||
/* quad_wire(q1); */
|
||||
/* SDL_SetRenderDrawColor(ren,192,192,192,0); */
|
||||
/* quad_wire(q2); */
|
||||
|
||||
SDL_SetRenderDrawColor(ren,255,255,255,0);
|
||||
cube_wire(cam,cube);
|
||||
SDL_SetRenderDrawColor(ren,192,192,192,0);
|
||||
cube_wire(cam,cube2);
|
||||
draw_cube(ren,cam,(Point3d){.x=1,.y=1,.z=1},10);
|
||||
|
||||
SDL_RenderPresent(ren);
|
||||
|
||||
usleep(20000);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user