Mail Archives: djgpp/1997/07/05/05:17:17
From: | T DOT Harte AT btinternet DOT com (Thomas Harte)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Clipping Allegro polygons at z=1
|
Date: | Mon, 30 Jun 1997 18:49:10 GMT
|
Organization: | BT Internet
|
Message-ID: | <33b7f92d.1439383@news.btinternet.com>
|
NNTP-Posting-Host: | host5-99-52-213.btinternet.com
|
Lines: | 123
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Hi. Earlier I posted code which allows you to draw a line anywhere on
screen, even crossing the line z=1, where functions without clipping would fail.
I said I would be back with the polygon version, and here I am!
It would be nice if something like this was in Allegro 2.3, although I'm
sure there are much better implementations that mine, and several ways to speed
up the code I have provided if you know what you're doing. I just read the
theory last night, and implemented it today. Anyway, the quad3d version (now
called clquad3d, 'cos it clips and that) looks like this :-
void clquad3d(BITMAP *bitmap, int type, BITMAP *texture, V3D_f v1, V3D_f v2,
V3D_f v3, V3D_f v4)
{
V3D_f points[5], opoints[4];
int numpoints=0,temp[2];
float ratio;
if(!(v1.z < 1 && v2.z < 1 && v3.z < 1 && v4.z < 1))
{
opoints[0] = v1;
opoints[1] = v2;
opoints[2] = v3;
opoints[3] = v4;
for(temp[0]=0;temp[0]<4;temp[0]++)
{
if(opoints[temp[0]].z >= 1)
{
points[numpoints]=opoints[temp[0]];
numpoints++;
}
temp[1]=temp[0]+1;
if(temp[1] == 4)
temp[1]=0;
if(opoints[temp[0]].z >= 1 && opoints[temp[1]].z < 1)
{
ratio=(1-opoints[temp[1]].z)/(opoints[temp[1]].z-opoints[temp[0]].z);
points[numpoints].x = ((opoints[temp[1]].x-opoints[temp[0]].x) *
ratio) + opoints[temp[1]].x;
points[numpoints].y = ((opoints[temp[1]].y-opoints[temp[0]].y) *
ratio) + opoints[temp[1]].y;
points[numpoints].u = ((opoints[temp[1]].u-opoints[temp[0]].u) *
ratio) + opoints[temp[1]].u;
points[numpoints].v = ((opoints[temp[1]].v-opoints[temp[0]].v) *
ratio) + opoints[temp[1]].v;
points[numpoints].c = ((opoints[temp[1]].c-opoints[temp[0]].c) *
ratio) + opoints[temp[1]].c;
points[numpoints].z = 1;
numpoints++;
}
if(opoints[temp[0]].z < 1 && opoints[temp[1]].z >= 1)
{
ratio=(1-opoints[temp[0]].z)/(opoints[temp[0]].z-opoints[temp[1]].z);
points[numpoints].x = ((opoints[temp[0]].x-opoints[temp[1]].x) *
ratio) + opoints[temp[0]].x;
points[numpoints].y = ((opoints[temp[0]].y-opoints[temp[1]].y) *
ratio) + opoints[temp[0]].y;
points[numpoints].u = ((opoints[temp[0]].u-opoints[temp[1]].u) *
ratio) + opoints[temp[0]].u;
points[numpoints].v = ((opoints[temp[0]].v-opoints[temp[1]].v) *
ratio) + opoints[temp[0]].v;
points[numpoints].c = ((opoints[temp[0]].c-opoints[temp[1]].c) *
ratio) + opoints[temp[0]].c;
points[numpoints].z = 1;
numpoints++;
}
}
for(temp[0]=0;temp[0]<numpoints;temp[0]++)
persp_project_f(points[temp[0]].x, points[temp[0]].y,
points[temp[0]].z,
&points[temp[0]].x, &points[temp[0]].y);
if(numpoints == 3)
triangle3d_f(bitmap, type, texture, &points[0], &points[1], &points[2]);
if(numpoints == 4)
quad3d_f(bitmap, type, texture, &points[0], &points[1], &points[2],
&points[3]);
if(numpoints == 5)
{
quad3d_f(bitmap, type, texture, &points[0], &points[1], &points[2],
&points[3]);
triangle3d_f(bitmap, type, texture, &points[0], &points[3],
&points[4]);
}
}
}
. . . . and I'm sure anyone who particularly wants to can adapt it for
triangles, or anything else that isn't a quad. Note that the last bit, which
says :-
if(numpoints == 5)
{
quad3d_f(bitmap, type, texture, &points[0], &points[1], &points[2],
&points[3]);
triangle3d_f(bitmap, type, texture, &points[0], &points[3],
&points[4]);
}
. . . . does a quad then a triangle because when I used polygon3d it
wouldn't compile. Besides it's not that important.
Shawn, as you're probably reading this anyway, could I right now request
an extra polygon mode POLYTYPE_VECTOR for the next Allegro? Sure - you can just
use line commands, but that gets complex when you want to switch between filled
and vector polygons as the program goes along.
-Thomas
http://www.softysoft.home.ml.org
- Raw text -