// updated each time the surface is added to the stack,
// for quicker comparisons.
float wcurrent;
- // this is a linked list of all edges belonging to this surface,
- // used to remove them if this is a non-solid surface that is
- // marked visible (it can not hide anything, so it is useless)
-// struct clipedge_s *edgechain;
}
clipsurf_t;
#endif
float r_clip_viewmatrix[3][3], r_clip_viewmulx, r_clip_viewmuly, r_clip_viewcenterx, r_clip_viewcentery;
-// REMOVELATER
-//float xscale, yscale, xscaleinv, yscaleinv;
-//float r_clip_nearclipdist, r_clip_nearclipdist2;
tinyplane_t r_clip_viewplane[5];
+mempool_t *r_clip_mempool;
+
void R_Clip_MakeViewMatrix(void)
{
float pixelaspect, screenaspect, horizontalfieldofview, verticalfieldofview;
VectorCopy (vright, r_clip_viewmatrix[0]);
VectorNegate (vup, r_clip_viewmatrix[1]);
VectorCopy (vpn, r_clip_viewmatrix[2]);
-// r_clip_nearclipdist = DotProduct(r_origin, vpn) + 4.0f;
-// r_clip_nearclipdist2 = r_clip_nearclipdist - 8.0f;
VectorCopy (vpn, r_clip_viewplane[0].normal);
r_clip_viewplane[0].dist = DotProduct(r_origin, vpn);
memcpy(&r_clip_viewplane[1], &frustum[0], sizeof(tinyplane_t));
memcpy(&r_clip_viewplane[2], &frustum[1], sizeof(tinyplane_t));
memcpy(&r_clip_viewplane[3], &frustum[2], sizeof(tinyplane_t));
memcpy(&r_clip_viewplane[4], &frustum[3], sizeof(tinyplane_t));
-// REMOVELATER
-// maxscreenscaleinv = (1.0f / max(clipwidth, clipheight)) * horizontalfieldofview * 0.5f;
-// xscale = clipwidth / horizontalfieldofview;
-// xscaleinv = 1.0 / xscale;
-// yscale = xscale * pixelaspect;
-// yscaleinv = 1.0 / yscale;
}
void R_Clip_StartFrame(void)
{
int i;
int newwidth, newheight, newmaxedges, newmaxsurfs;
- newwidth = bound(80, (int) r_clipwidth.value, vid.realwidth * 2);
- newheight = bound(60, (int) r_clipheight.value, vid.realheight * 2);
- newmaxedges = bound(128, (int) r_clipedges.value, 262144);
- newmaxsurfs = bound(32, (int) r_clipsurfaces.value, 65536);
+ newwidth = bound(80, r_clipwidth.integer, vid.realwidth * 2);
+ newheight = bound(60, r_clipheight.integer, vid.realheight * 2);
+ newmaxedges = bound(128, r_clipedges.integer, 262144);
+ newmaxsurfs = bound(32, r_clipsurfaces.integer, 65536);
if (newwidth != clipwidth || newheight != clipheight || maxclipedges != newmaxedges || maxclipsurfs != newmaxsurfs)
{
#if CLIPTEST
if (clipbuffer)
- qfree(clipbuffer);
+ Mem_Free(clipbuffer);
#endif
if (clipedges)
- qfree(clipedges);
+ Mem_Free(clipedges);
if (clipsurfs)
- qfree(clipsurfs);
+ Mem_Free(clipsurfs);
if (newedges)
- qfree(newedges);
+ Mem_Free(newedges);
if (removeedges)
- qfree(removeedges);
+ Mem_Free(removeedges);
clipwidth = newwidth;
clipheight = newheight;
maxclipedges = newmaxedges;
maxclipsurfs = newmaxsurfs;
#if CLIPTEST
- clipbuffer = qmalloc(clipwidth * clipheight * sizeof(clippixel_t));
+ clipbuffer = Mem_Alloc(r_clip_mempool, clipwidth * clipheight * sizeof(clippixel_t));
#endif
- clipedges = qmalloc(maxclipedges * sizeof(clipedge_t));
- clipsurfs = qmalloc(maxclipsurfs * sizeof(clipsurf_t));
- newedges = qmalloc(clipheight * sizeof(clipedge_t));
- removeedges = qmalloc(clipheight * sizeof(clipedge_t *));
+ clipedges = Mem_Alloc(r_clip_mempool, maxclipedges * sizeof(clipedge_t));
+ clipsurfs = Mem_Alloc(r_clip_mempool, maxclipsurfs * sizeof(clipsurf_t));
+ newedges = Mem_Alloc(r_clip_mempool, clipheight * sizeof(clipedge_t));
+ removeedges = Mem_Alloc(r_clip_mempool, clipheight * sizeof(clipedge_t *));
clipedgesend = clipedges + maxclipedges;
clipsurfsend = clipsurfs + maxclipsurfs;
}
void r_clip_start(void)
{
+ r_clip_mempool = Mem_AllocPool("R_Clip");
}
void r_clip_shutdown(void)
{
+ Mem_FreePool(&r_clip_mempool);
#if CLIPTEST
- if (clipbuffer)
- qfree(clipbuffer);
clipbuffer = NULL;
#endif
- if (clipsurfs)
- qfree(clipsurfs);
clipsurfs = NULL;
- if (clipedges)
- qfree(clipedges);
clipedges = NULL;
- if (newedges)
- qfree(newedges);
newedges = NULL;
- if (removeedges)
- qfree(removeedges);
removeedges = NULL;
clipwidth = -1;
clipheight = -1;
number = DotProduct(p->normal, p->normal);
if (number >= 0.1f)
{
- *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
+ *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1);
y = y * (1.5f - (number * 0.5f * y * y));
VectorScale(p->normal, y, p->normal);
p->dist = DotProduct(point1, p->normal);
return false;
}
-/*
-int R_Clip_TriangleToDoublePlane(double *point1, double *point2, double *point3, tinydoubleplane_t *p)
-{
- double y, number;
- double v1[3], v2[3];
- VectorSubtract(point1, point2, v1);
- VectorSubtract(point3, point2, v2);
- CrossProduct(v1, v2, p->normal);
- number = DotProduct(p->normal, p->normal);
- if (number >= 0.1)
- {
- y = 1.0 / sqrt(number);
- VectorScale(p->normal, y, p->normal);
- p->dist = DotProduct(point1, p->normal);
- return true;
- }
- else
- return false;
-}
-*/
int R_Clip_ClipPolygonToPlane(float *in, float *out, int inpoints, int stride, tinyplane_t *plane)
{
float *prevpoint, prevdist, dist, dot;
// begin with the last point, then enter the loop with the first point as current
- prevpoint = (float *) ((byte *)in + stride * (inpoints - 1));
+ prevpoint = (float *) ((qbyte *)in + stride * (inpoints - 1));
prevdist = DotProduct(prevpoint, plane->normal) - plane->dist;
prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
i = 0;
prevpoint = in;
prevdist = dist;
prevside = side;
- (byte *)in += stride;
+ (qbyte *)in += stride;
begin:
dist = DotProduct(in, plane->normal) - plane->dist;
float deltax, deltay, vx, vy, vz, fx;
int i, j, k, nextvert, temp, topy, bottomy, height, addededges;
clipedge_t *pedge;
-// tinydoubleplane_t plane;
tinyplane_t localplane;
-// tinyplane_t testplane;
float distinv;
-// if (!solid)
-// return;
-
if (polyplane == NULL)
{
polyplane = &localplane;
// calculate the plane for the polygon
- if (!R_Clip_TriangleToPlane((float *) points, (float *) ((byte *)points + stride), (float *) ((byte *)points + 2 * stride), polyplane))
+ if (!R_Clip_TriangleToPlane((float *) points, (float *) ((qbyte *)points + stride), (float *) ((qbyte *)points + 2 * stride), polyplane))
{
for (i = 0;i < numverts;i++)
for (j = i + 1;j < numverts;j++)
for (k = j + 1;k < numverts;k++)
- if (R_Clip_TriangleToPlane((float *) ((byte *)points + i * stride), (float *) ((byte *)points + j * stride), (float *) ((byte *)points + k * stride), polyplane))
+ if (R_Clip_TriangleToPlane((float *) ((qbyte *)points + i * stride), (float *) ((qbyte *)points + j * stride), (float *) ((qbyte *)points + k * stride), polyplane))
goto valid1;
return; // gave up
valid1:;
if (DotProduct(r_origin, polyplane->normal) < (polyplane->dist + 0.5f))
return;
}
-#if 0 // debugging (validates planes passed in)
- else
- {
- // calculate the plane for the polygon
- if (!R_Clip_TriangleToPlane((float *) points, (float *) ((byte *)points + stride), (float *) ((byte *)points + 2 * stride), &localplane))
- {
- for (i = 0;i < numverts;i++)
- for (j = i + 1;j < numverts;j++)
- for (k = j + 1;k < numverts;k++)
- if (R_Clip_TriangleToPlane((float *) ((byte *)points + i * stride), (float *) ((byte *)points + j * stride), (float *) ((byte *)points + k * stride), &localplane))
- goto valid4;
- return; // gave up
- valid4:;
- }
-
-// if ((DotProduct(r_origin, polyplane->normal) < (polyplane->dist + 0.5f)) != (DotProduct(r_origin, localplane.normal) < (localplane.dist + 0.5f)))
- if (DotProduct(polyplane->normal, localplane.normal) < 0.9f)
- {
- Con_Printf("*\n");
- return;
- }
- }
-#endif
// for adaptive limits
needededges += numverts;
if (pavailedge + numverts > clipedgesend)
return;
-#if 1
for (i = 0;i < numverts;i++)
{
vx = tempverts2[i][0] - r_origin[0];
screenverts[i][1] = (r_clip_viewmatrix[1][0] * vx + r_clip_viewmatrix[1][1] * vy + r_clip_viewmatrix[1][2] * vz) * r_clip_viewmuly * screenverts[i][2] + r_clip_viewcentery;
}
- /*
- if (polyplane != NULL)
- {
- */
- /*
- distinv = 1.0f / (polyplane->dist - DotProduct(r_origin, polyplane->normal));
- pavailsurf->wstepx = DotProduct(r_clip_viewmatrix[0], polyplane->normal) * xscaleinv * distinv;
- pavailsurf->wstepy = DotProduct(r_clip_viewmatrix[1], polyplane->normal) * yscaleinv * distinv;
- pavailsurf->w00 = DotProduct(r_clip_viewmatrix[2], polyplane->normal) * distinv - r_clip_viewcenterx * pavailsurf->wstepx - r_clip_viewcentery * pavailsurf->wstepy;
- */
- /*
- }
- else
- {
- */
- // calculate the plane for the polygon
- if (!R_Clip_TriangleToPlane(screenverts[0], screenverts[1], screenverts[2], &localplane))
- {
- for (i = 0;i < numverts;i++)
- for (j = i + 1;j < numverts;j++)
- for (k = j + 1;k < numverts;k++)
- if (R_Clip_TriangleToPlane(screenverts[i], screenverts[j], screenverts[k], &localplane))
- goto valid;
- return; // gave up
- valid:;
- }
-
- // Set up the 1/z gradients from the polygon, calculating the
- // base value at screen coordinate 0,0 so we can use screen
- // coordinates directly when calculating 1/z from the gradients
- distinv = 1.0f / localplane.normal[2];
- pavailsurf->wstepx = -(localplane.normal[0] * distinv);
- pavailsurf->wstepy = -(localplane.normal[1] * distinv);
- pavailsurf->w00 = localplane.dist * distinv;
- /*
- }
- */
- // REMOVELATER
- /*
- prevdist = z1 * plane.normal[2] - plane.dist;
- dist = z2 * plane.normal[2] - plane.dist;
- d = prevdist / (prevdist - dist);
- zc = z1 + d * (z2 - z1);
-
- prevdist = plane.normal[0] + z1 * plane.normal[2] - plane.dist;
- dist = plane.normal[0] + z2 * plane.normal[2] - plane.dist;
- d = prevdist / (prevdist - dist);
- zx = (z1 + d * (z2 - z1)) - zc;
-
- prevdist = plane.normal[1] + z1 * plane.normal[2] - plane.dist;
- dist = plane.normal[1] + z2 * plane.normal[2] - plane.dist;
- d = prevdist / (prevdist - dist);
- zy = (z1 + d * (z2 - z1)) - zc;
- */
-
- /*
- zc = (-plane.dist) / ((-plane.dist) - (plane.normal[2] - plane.dist));
- zx = ((plane.normal[0] - plane.dist) / ((plane.normal[0] - plane.dist) - (plane.normal[0] + plane.normal[2] - plane.dist))) - zc;
- zy = ((plane.normal[1] - plane.dist) / ((plane.normal[1] - plane.dist) - (plane.normal[1] + plane.normal[2] - plane.dist))) - zc;
- */
-
-// zc = (plane.dist / plane.normal[2]);
-// zx = -(plane.normal[0] / plane.normal[2]);
-// zy = -(plane.normal[1] / plane.normal[2]);
-// zy = ((plane.normal[1] - plane.dist) / (-plane.normal[2])) + ((plane.dist) / (-plane.normal[2]));
-#else // REMOVELATER
- for (i = 0;i < numverts;i++)
- {
- vx = tempverts2[i][0] - r_origin[0];
- vy = tempverts2[i][1] - r_origin[1];
- vz = tempverts2[i][2] - r_origin[2];
- screenverts[i][0] = r_clip_viewmatrix[0][0] * vx + r_clip_viewmatrix[0][1] * vy + r_clip_viewmatrix[0][2] * vz;
- screenverts[i][1] = r_clip_viewmatrix[1][0] * vx + r_clip_viewmatrix[1][1] * vy + r_clip_viewmatrix[1][2] * vz;
- screenverts[i][2] = r_clip_viewmatrix[2][0] * vx + r_clip_viewmatrix[2][1] * vy + r_clip_viewmatrix[2][2] * vz;
- }
-
- // REMOVELATER
// calculate the plane for the polygon
- for (i = 0;i < numverts;i++)
- for (j = i + 1;j < numverts;j++)
- for (k = j + 1;k < numverts;k++)
- if (R_Clip_TriangleToDoublePlane(screenverts[i], screenverts[j], screenverts[k], &plane))
- goto valid2;
- return; // gave up
-valid2:;
-
- distinv = 1.0f / plane.dist;
- pavailsurf->d_zistepx = plane.normal[0] * xscaleinv * distinv;
- pavailsurf->d_zistepy = -plane.normal[1] * yscaleinv * distinv;
- pavailsurf->d_ziorigin = plane.normal[2] * distinv - r_clip_viewcenterx * pavailsurf->wstepx - r_clip_viewcentery * pavailsurf->wstepy;
-
- for (i = 0;i < numverts;i++)
+ if (!R_Clip_TriangleToPlane(screenverts[0], screenverts[1], screenverts[2], &localplane))
{
- screenverts[i][2] = 1.0f / (screenverts[i][2]);
- screenverts[i][0] = screenverts[i][0] * r_clip_viewmulx * screenverts[i][2] + r_clip_viewcenterx;
- screenverts[i][1] = screenverts[i][1] * r_clip_viewmuly * screenverts[i][2] + r_clip_viewcentery;
- // REMOVELATER
-// if (screenverts[i][0] < -0.5)
-// screenverts[i][0] = -0.5;
-// if (screenverts[i][0] > (clipwidth - 0.5))
-// screenverts[i][0] = clipwidth - 0.5;
-// if (screenverts[i][1] < -0.5)
-// screenverts[i][1] = -0.5;
-// if (screenverts[i][1] > (clipheight - 0.5))
-// screenverts[i][1] = clipheight - 0.5;
-// if (screenverts[i][2] <= 0.0)
-// Con_Printf("R_Clip_AddPolygon: vertex z <= 0!\n");
+ for (i = 0;i < numverts;i++)
+ for (j = i + 1;j < numverts;j++)
+ for (k = j + 1;k < numverts;k++)
+ if (R_Clip_TriangleToPlane(screenverts[i], screenverts[j], screenverts[k], &localplane))
+ goto valid;
+ return; // gave up
+valid:;
}
-#endif
+
+ // Set up the 1/z gradients from the polygon, calculating the
+ // base value at screen coordinate 0,0 so we can use screen
+ // coordinates directly when calculating 1/z from the gradients
+ distinv = 1.0f / localplane.normal[2];
+ pavailsurf->wstepx = -(localplane.normal[0] * distinv);
+ pavailsurf->wstepy = -(localplane.normal[1] * distinv);
+ pavailsurf->w00 = localplane.dist * distinv;
addededges = false;
pavailsurf++;
}
-/////////////////////////////////////////////////////////////////////
// Scan all the edges in the global edge table into spans.
-/////////////////////////////////////////////////////////////////////
void ScanEdges (void)
{
int y, rescan;
fx = pedge->x;
// Calculate the surface's 1/z value at this pixel, and cache the y depth for quick compares later
w = (psurf->wcurrent = psurf->w00 + psurf->wstepy * fy) + psurf->wstepx * fx;
-// if (w < 0)
-// w = 0;
// See if that makes it a new top surface
psurf2 = surfstack.next;
w2 = psurf2->wcurrent + psurf2->wstepx * fx;
-// if (w2 < 0 && psurf2 != &surfstack)
-// w2 = 0;
if (w >= w2)
{
{
psurf2 = psurf2->next;
w2 = psurf2->wcurrent + psurf2->wstepx * fx;
-// if (w2 < 0 && psurf2 != &surfstack)
-// w2 = 0;
}
while (w < w2);
void R_Clip_DisplayBuffer(void)
{
-#if CLIPTEST
- int i;
- static int firstupload = true;
- byte clipbuffertex[256*256], *b;
- if (!r_render.value)
- return;
- if (clipwidth > 256 || clipheight > 256)
- return;
- glBlendFunc(GL_ONE, GL_ONE);
- glBindTexture(GL_TEXTURE_2D, 8000);
- if (firstupload)
- {
- memset(clipbuffertex, 0, sizeof(clipbuffertex));
- glTexImage2D(GL_TEXTURE_2D, 0, 1, 256, 256, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, clipbuffertex);
- }
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- if (lighthalf)
- glColor3f(0.5, 0.5, 0.5);
- else
- glColor3f(1, 1, 1);
- firstupload = false;
- b = clipbuffertex;
- for (i = 0;i < clipwidth*clipheight;i++)
- {
- if (clipbuffer[i].w > 0)
- *b++ = bound(0, (int) (clipbuffer[i].w * 4096.0f), 255);
- else
- *b++ = 0;
- }
- glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, clipwidth, clipheight, GL_LUMINANCE, GL_UNSIGNED_BYTE, clipbuffertex);
- glBegin (GL_QUADS);
- glTexCoord2f (0 , 0 );glVertex2f (0 , 0 );
- glTexCoord2f (clipwidth / 256.0f, 0 );glVertex2f (vid.conwidth, 0 );
- glTexCoord2f (clipwidth / 256.0f, clipheight / 256.0f);glVertex2f (vid.conwidth, vid.conheight);
- glTexCoord2f (0 , clipheight / 256.0f);glVertex2f (0 , vid.conheight);
- glEnd ();
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-#endif
}
float boxpoints[4*3];
callback, nativedata, nativedata2, &clipboxplane[5]
);
}
+