6#define CLIP_XMIN (1<<0)
7#define CLIP_XMAX (1<<1)
8#define CLIP_YMIN (1<<2)
9#define CLIP_YMAX (1<<3)
10#define CLIP_ZMIN (1<<4)
11#define CLIP_ZMAX (1<<5)
51static void gl_add_select1(
GLContext *c,
int z1,
int z2,
int z3)
80 q->
pc.X=p0->
pc.X+(p1->
pc.X-p0->
pc.X)*t;
81 q->
pc.Y=p0->
pc.Y+(p1->
pc.Y-p0->
pc.Y)*t;
82 q->
pc.Z=p0->
pc.Z+(p1->
pc.Z-p0->
pc.Z)*t;
83 q->
pc.W=p0->
pc.W+(p1->
pc.W-p0->
pc.W)*t;
96static inline int ClipLine1(
float denom,
float num,
float *tmin,
float *tmax)
102 if (t>*tmax)
return 0;
103 if (t>*tmin) *tmin=t;
104 }
else if (denom<0) {
106 if (t<*tmin)
return 0;
107 if (t<*tmax) *tmax=t;
108 }
else if (num>0)
return 0;
114 float dx,
dy,dz,dw,
x1,y1,z1,w1;
122 if ( (cc1 | cc2) == 0) {
124 gl_add_select1(c,p1->
zp.
z,p2->
zp.
z,p2->
zp.
z);
131 }
else if ( (cc1&cc2) != 0 ) {
136 dz=p2->
pc.Z-p1->
pc.Z;
137 dw=p2->
pc.W-p1->
pc.W;
145 if (ClipLine1(
dx+dw,-
x1-w1,&tmin,&tmax) &&
146 ClipLine1(-
dx+dw,
x1-w1,&tmin,&tmax) &&
147 ClipLine1(
dy+dw,-y1-w1,&tmin,&tmax) &&
148 ClipLine1(-
dy+dw,y1-w1,&tmin,&tmax) &&
149 ClipLine1(dz+dw,-z1-w1,&tmin,&tmax) &&
150 ClipLine1(-dz+dw,z1-w1,&tmin,&tmax)) {
152 interpolate(&q1,p1,p2,tmin);
153 interpolate(&q2,p1,p2,tmax);
177#define clip_func(name,sign,dir,dir1,dir2) \
178static float name(V4 *c,V4 *a,V4 *b) \
180 float t,dX,dY,dZ,dW,den;\
185 den = -(sign d ## dir) + dW;\
187 else t = ( sign a->dir - a->W) / den;\
188 c->dir1 = a->dir1 + t * d ## dir1;\
189 c->dir2 = a->dir2 + t * d ## dir2;\
190 c->W = a->W + t * dW;\
209float (*clip_proc[6])(
V4 *,
V4 *,
V4 *)= {
215static inline void updateTmp(
GLContext *c,
238static void gl_draw_triangle_clip(
GLContext *c,
244 int co,c_and,cc[3],front;
251 co=cc[0] | cc[1] | cc[2];
256 norm=(float)(p1->
zp.
x-p0->
zp.
x)*(float)(p2->
zp.
y-p0->
zp.
y)-
259 if (norm == 0)
return;
268 if (front == 0)
return;
271 if (front != 0)
return;
285 c_and=cc[0] & cc[1] & cc[2];
287 gl_draw_triangle_clip(c,p0,p1,p2,0);
292static void gl_draw_triangle_clip(
GLContext *c,
295 int co,c_and,co1,cc[3],edge_flag_tmp,clip_mask;
303 co=cc[0] | cc[1] | cc[2];
307 c_and=cc[0] & cc[1] & cc[2];
309 if (c_and!=0)
return;
312 while (clip_bit < 6 && (co & (1 << clip_bit)) == 0) {
320 printf(
"%f %f %f %f\n",p0->
pc.X,p0->
pc.Y,p0->
pc.Z,p0->
pc.W);
321 printf(
"%f %f %f %f\n",p1->
pc.X,p1->
pc.Y,p1->
pc.Z,p1->
pc.W);
322 printf(
"%f %f %f %f\n",p2->
pc.X,p2->
pc.Y,p2->
pc.Z,p2->
pc.W);
327 clip_mask = 1 << clip_bit;
328 co1=(cc[0] ^ cc[1] ^ cc[2]) & clip_mask;
333 if (cc[0] & clip_mask) { q[0]=p0; q[1]=p1; q[2]=p2; }
334 else if (cc[1] & clip_mask) { q[0]=p1; q[1]=p2; q[2]=p0; }
335 else { q[0]=p2; q[1]=p0; q[2]=p1; }
337 tt=clip_proc[clip_bit](&tmp1.
pc,&q[0]->
pc,&q[1]->
pc);
338 updateTmp(c,&tmp1,q[0],q[1],tt);
340 tt=clip_proc[clip_bit](&tmp2.
pc,&q[0]->
pc,&q[2]->
pc);
341 updateTmp(c,&tmp2,q[0],q[2],tt);
346 gl_draw_triangle_clip(c,&tmp1,q[1],q[2],clip_bit+1);
351 gl_draw_triangle_clip(c,&tmp2,&tmp1,q[2],clip_bit+1);
355 if ((cc[0] & clip_mask)==0) { q[0]=p0; q[1]=p1; q[2]=p2; }
356 else if ((cc[1] & clip_mask)==0) { q[0]=p1; q[1]=p2; q[2]=p0; }
357 else { q[0]=p2; q[1]=p0; q[2]=p1; }
359 tt=clip_proc[clip_bit](&tmp1.
pc,&q[0]->
pc,&q[1]->
pc);
360 updateTmp(c,&tmp1,q[0],q[1],tt);
362 tt=clip_proc[clip_bit](&tmp2.
pc,&q[0]->
pc,&q[2]->
pc);
363 updateTmp(c,&tmp2,q[0],q[2],tt);
367 gl_draw_triangle_clip(c,q[0],&tmp1,&tmp2,clip_bit+1);
376 gl_add_select1(c,p0->
zp.
z,p1->
zp.
z,p2->
zp.
z);
380int count_triangles,count_triangles_textured,count_pixels;
398 count_pixels+=abs(norm)/2;
405 count_triangles_textured++;
void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
void gl_draw_point(GLContext *c, GLVertex *p0)
void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
void gl_transform_to_viewport(GLContext *c, GLVertex *v)
#define clip_func(name, sign, dir, dir1, dir2)
void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
void gl_draw_line(GLContext *c, GLVertex *p1, GLVertex *p2)
void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
void gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax)
gl_draw_triangle_func draw_triangle_back
unsigned int longcurrent_color[3]
gl_draw_triangle_func draw_triangle_front
GLTexture * current_texture
GLImage images[MAX_TEXTURE_LEVELS]
void ZB_setTexture(ZBuffer *zb, PIXEL *texture)
#define ZB_POINT_GREEN_MIN
void ZB_plot(ZBuffer *zb, ZBufferPoint *p)
#define ZB_POINT_BLUE_MAX
void ZB_fillTriangleMappingPerspective(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2)
void ZB_fillTriangleFlat(ZBuffer *zb, ZBufferPoint *p1, ZBufferPoint *p2, ZBufferPoint *p3)
#define ZB_POINT_GREEN_MAX
#define ZB_POINT_BLUE_MIN
void ZB_fillTriangleSmooth(ZBuffer *zb, ZBufferPoint *p1, ZBufferPoint *p2, ZBufferPoint *p3)
void ZB_line_z(ZBuffer *zb, ZBufferPoint *p1, ZBufferPoint *p2)
void ZB_line(ZBuffer *zb, ZBufferPoint *p1, ZBufferPoint *p2)