From a6260629d2dbe98351a8c55f197c721894046857 Mon Sep 17 00:00:00 2001 From: havoc Date: Sun, 17 Oct 2004 11:40:40 +0000 Subject: [PATCH] now using all-new Q3 patch tesselation code, matching Quake3 pretty well git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@4660 d7cf8633-e32d-0410-b094-e92efae38249 --- curves.c | 494 ++++++++++++++------------------------------------ curves.h | 20 +- model_brush.c | 138 ++++++-------- 3 files changed, 197 insertions(+), 455 deletions(-) diff --git a/curves.c b/curves.c index 3fdb26c5..66b72734 100644 --- a/curves.c +++ b/curves.c @@ -1,420 +1,190 @@ -// this code written by Forest Hale, on 2003-08-23, and placed into public domain -// this code deals with quadratic splines (minimum of 3 points), the same kind used in Quake3 maps. - -// LordHavoc's rant on misuse of the name 'bezier': many people seem to think that bezier is a generic term for splines, but it is not, it is a term for a specific type of bspline (4 control points, cubic bspline), bsplines are the generalization of the bezier spline to support dimensions other than just cubic. -// this implements Quadratic BSpline surfaces - -#include -#include "curves.h" -#include "zone.h" - -#if 0 -void QuadraticBSplineSubdivideFloat(int inpoints, int components, const float *in, int instride, float *out, int outstride) +/* +this code written by Forest Hale, on 2004-10-17, and placed into public domain +this implements Quadratic BSpline surfaces as seen in Quake3 by id Software + +a small rant on misuse of the name 'bezier': many people seem to think that +bezier is a generic term for splines, but it is not, it is a term for a +specific type of bspline (4 control points, cubic bspline), bsplines are the +generalization of the bezier spline to support dimensions other than cubic. + +example equations for 1-5 control point bsplines being sampled as t=0...1 +1: flat (0th dimension) +o = a +2: linear (1st dimension) +o = a * (1 - t) + b * t +3: quadratic bspline (2nd dimension) +o = a * (1 - t) * (1 - t) + 2 * b * (1 - t) * t + c * t * t +4: cubic (bezier) bspline (3rd dimension) +o = a * (1 - t) * (1 - t) * (1 - t) + 3 * b * (1 - t) * (1 - t) * t + 3 * c * (1 - t) * t * t + d * t * t * t +5: quartic bspline (4th dimension) +o = a * (1 - t) * (1 - t) * (1 - t) * (1 - t) + 4 * b * (1 - t) * (1 - t) * (1 - t) * t + 6 * c * (1 - t) * (1 - t) * t * t + 4 * d * (1 - t) * t * t * t + e * t * t * t * t + +arbitrary dimension bspline +double factorial(int n) { - int s; - // the input (control points) is read as a stream of points, and buffered - // by the cpprev, cpcurr, and cpnext variables (to allow subdivision in - // overlapping memory buffers, even subdivision in-place with pre-spaced - // control points in the buffer) - // the output (resulting curve) is written as a stream of points - // this subdivision is meant to be repeated until the desired flatness - // level is reached - if (components == 1 && instride == (int)sizeof(float) && outstride == instride) - { - // simple case, single component and no special stride - float cpprev0 = 0, cpcurr0 = 0, cpnext0; - cpnext0 = *in++; - for (s = 0;s < inpoints - 1;s++) - { - cpprev0 = cpcurr0; - cpcurr0 = cpnext0; - if (s < inpoints - 1) - cpnext0 = *in++; - if (s > 0) - { - // 50% flattened control point - // cp1 = average(cp1, average(cp0, cp2)); - *out++ = (cpcurr0 + (cpprev0 + cpnext0) * 0.5f) * 0.5f; - } - else - { - // copy the control point directly - *out++ = cpcurr0; - } - // midpoint - // mid = average(cp0, cp1); - *out++ = (cpcurr0 + cpnext0) * 0.5f; - } - // copy the final control point - *out++ = cpnext0; - } - else - { - // multiple components or stride is used (complex case) - int c; - float cpprev[4], cpcurr[4], cpnext[4]; - // check if there are too many components for the buffers - if (components > 1) - { - // more components can be handled, but slowly, by calling self multiple times... - for (c = 0;c < components;c++, in++, out++) - QuadraticBSplineSubdivideFloat(inpoints, 1, in, instride, out, outstride); - return; - } - for (c = 0;c < components;c++) - cpnext[c] = in[c]; - (unsigned char *)in += instride; - for (s = 0;s < inpoints - 1;s++) - { - for (c = 0;c < components;c++) - cpprev[c] = cpcurr[c]; - for (c = 0;c < components;c++) - cpcurr[c] = cpnext[c]; - for (c = 0;c < components;c++) - cpnext[c] = in[c]; - (unsigned char *)in += instride; - // the end points are copied as-is - if (s > 0) - { - // 50% flattened control point - // cp1 = average(cp1, average(cp0, cp2)); - for (c = 0;c < components;c++) - out[c] = (cpcurr[c] + (cpprev[c] + cpnext[c]) * 0.5f) * 0.5f; - } - else - { - // copy the control point directly - for (c = 0;c < components;c++) - out[c] = cpcurr[c]; - } - (unsigned char *)out += outstride; - // midpoint - // mid = average(cp0, cp1); - for (c = 0;c < components;c++) - out[c] = (cpcurr[c] + cpnext[c]) * 0.5f; - (unsigned char *)out += outstride; - } - // copy the final control point - for (c = 0;c < components;c++) - out[c] = cpnext[c]; - //(unsigned char *)out += outstride; - } + int i; + double f; + f = 1; + for (i = 1;i < n;i++) + f = f * i; + return f; } - -// note: out must have enough room! -// (see finalwidth/finalheight calcs below) -void QuadraticBSplinePatchSubdivideFloatBuffer(int cpwidth, int cpheight, int xlevel, int ylevel, int components, const float *in, float *out) +double bsplinesample(int dimensions, double t, double *param) { - int finalwidth, finalheight, xstep, ystep, x, y, c; - float *o; - - // error out on various bogus conditions - if (xlevel < 0 || ylevel < 0 || xlevel > 16 || ylevel > 16 || cpwidth < 3 || cpheight < 3) - return; - - xstep = 1 << xlevel; - ystep = 1 << ylevel; - finalwidth = (cpwidth - 1) * xstep + 1; - finalheight = (cpheight - 1) * ystep + 1; - - for (y = 0;y < finalheight;y++) - for (x = 0;x < finalwidth;x++) - for (c = 0, o = out + (y * finalwidth + x) * components;c < components;c++) - o[c] = 0; - - if (xlevel == 1 && ylevel == 0) - { - for (y = 0;y < finalheight;y++) - QuadraticBSplineSubdivideFloat(cpwidth, components, in + y * cpwidth * components, sizeof(float) * components, out + y * finalwidth * components, sizeof(float) * components); - return; - } - if (xlevel == 0 && ylevel == 1) - { - for (x = 0;x < finalwidth;x++) - QuadraticBSplineSubdivideFloat(cpheight, components, in + x * components, sizeof(float) * cpwidth * components, out + x * components, sizeof(float) * finalwidth * components); - return; - } + double o = 0; + for (i = 0;i < dimensions + 1;i++) + o += param[i] * factorial(dimensions)/(factorial(i)*factorial(dimensions-i)) * pow(t, i) * pow(1 - t, dimensions - i); +} +*/ - // copy control points into correct positions in destination buffer - for (y = 0;y < finalheight;y += ystep) - for (x = 0;x < finalwidth;x += xstep) - for (c = 0, o = out + (y * finalwidth + x) * components;c < components;c++) - o[c] = *in++; +#include +#include "curves.h" - // subdivide in place in the destination buffer - while (xstep > 1 || ystep > 1) - { - if (xstep > 1) - { - xstep >>= 1; - for (y = 0;y < finalheight;y += ystep) - QuadraticBSplineSubdivideFloat(cpwidth, components, out + y * finalwidth * components, sizeof(float) * xstep * 2 * components, out + y * finalwidth * components, sizeof(float) * xstep * components); - cpwidth = (cpwidth - 1) * 2 + 1; - } - if (ystep > 1) - { - ystep >>= 1; - for (x = 0;x < finalwidth;x += xstep) - QuadraticBSplineSubdivideFloat(cpheight, components, out + x * components, sizeof(float) * ystep * 2 * finalwidth * components, out + x * components, sizeof(float) * ystep * finalwidth * components); - cpheight = (cpheight - 1) * 2 + 1; - } - } -} -#elif 1 -void QuadraticBSplinePatchSubdivideFloatBuffer(int cpwidth, int cpheight, int xlevel, int ylevel, int components, const float *in, float *out) +// usage: +// to expand a 5x5 patch to 21x21 vertices (4x4 tesselation), one might use this call: +// Q3PatchSubdivideFloat(3, sizeof(float[3]), outvertices, 5, 5, sizeof(float[3]), patchvertices, 4, 4); +void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputvertices, int patchwidth, int patchheight, int inputstride, float *patchvertices, int tesselationwidth, int tesselationheight) { - int c, x, y, outwidth, outheight, halfstep, xstep, ystep; - float prev, curr, next; - xstep = 1 << xlevel; - ystep = 1 << ylevel; - outwidth = ((cpwidth - 1) * xstep) + 1; - outheight = ((cpheight - 1) * ystep) + 1; - for (y = 0;y < cpheight;y++) - for (x = 0;x < cpwidth;x++) - for (c = 0;c < components;c++) - out[(y * ystep * outwidth + x * xstep) * components + c] = in[(y * cpwidth + x) * components + c]; - while (xstep > 1 || ystep > 1) + int k, l, x, y, component, outputwidth = (patchwidth-1)*tesselationwidth+1; + float px, py, *v0, *v1, a, b, c, *cp[3][3], temp[3][64]; + // iterate over the individual 3x3 quadratic spline surfaces one at a time + // expanding them to fill the output array (with some overlap to ensure + // the edges are filled) + for (k = 0;k < patchheight-1;k += 2) { - if (xstep >= ystep) + for (l = 0;l < patchwidth-1;l += 2) { - // subdivide on X - halfstep = xstep >> 1; - for (y = 0;y < outheight;y += ystep) + // set up control point pointers for quicker lookup later + for (y = 0;y < 3;y++) + for (x = 0;x < 3;x++) + cp[y][x] = (float *)((unsigned char *)patchvertices + ((k+y)*patchwidth+(l+x)) * inputstride); + // for each row... + for (y = 0;y <= tesselationheight*2;y++) { - for (c = 0;c < components;c++) + // calculate control points for this row by collapsing the 3 + // rows of control points to one row using py + py = (float)y / (float)(tesselationheight*2); + // calculate quadratic spline weights for py + a = ((1.0f - py) * (1.0f - py)); + b = ((1.0f - py) * (2.0f * py)); + c = (( py) * ( py)); + for (component = 0;component < numcomponents;component++) { - x = xstep; - // fetch first two control points - prev = out[(y * outwidth + (x - xstep)) * components + c]; - curr = out[(y * outwidth + x) * components + c]; - // create first midpoint - out[(y * outwidth + (x - halfstep)) * components + c] = (curr + prev) * 0.5f; - for (;x < outwidth - xstep;x += xstep, prev = curr, curr = next) - { - // fetch next control point - next = out[(y * outwidth + (x + xstep)) * components + c]; - // flatten central control point - out[(y * outwidth + x) * components + c] = (curr + (prev + next) * 0.5f) * 0.5f; - // create following midpoint - out[(y * outwidth + (x + halfstep)) * components + c] = (curr + next) * 0.5f; - } + temp[0][component] = cp[0][0][component] * a + cp[1][0][component] * b + cp[2][0][component] * c; + temp[1][component] = cp[0][1][component] * a + cp[1][1][component] * b + cp[2][1][component] * c; + temp[2][component] = cp[0][2][component] * a + cp[1][2][component] * b + cp[2][2][component] * c; } - } - xstep >>= 1; - } - else - { - // subdivide on Y - halfstep = ystep >> 1; - for (x = 0;x < outwidth;x += xstep) - { - for (c = 0;c < components;c++) + // fetch a pointer to the beginning of the output vertex row + v = (float *)((unsigned char *)outputvertices + ((k * tesselationheight + y) * outputwidth + l * tesselationwidth) * outputstride); + // for each column of the row... + for (x = 0;x <= (tesselationwidth*2);x++) { - y = ystep; - // fetch first two control points - prev = out[((y - ystep) * outwidth + x) * components + c]; - curr = out[(y * outwidth + x) * components + c]; - // create first midpoint - out[((y - halfstep) * outwidth + x) * components + c] = (curr + prev) * 0.5f; - for (;y < outheight - ystep;y += ystep, prev = curr, curr = next) - { - // fetch next control point - next = out[((y + ystep) * outwidth + x) * components + c]; - // flatten central control point - out[(y * outwidth + x) * components + c] = (curr + (prev + next) * 0.5f) * 0.5f; - // create following midpoint - out[((y + halfstep) * outwidth + x) * components + c] = (curr + next) * 0.5f; - } + // calculate point based on the row control points + px = (float)x / (float)(tesselationwidth*2); + // calculate quadratic spline weights for px + // (could be precalculated) + a = ((1.0f - px) * (1.0f - px)); + b = ((1.0f - px) * (2.0f * px)); + c = (( px) * ( px)); + for (component = 0;component < numcomponents;component++) + v[component] = temp[0][component] * a + temp[1][component] * b + temp[2][component] * c; + // advance to next output vertex using outputstride + // (the next vertex may not be directly following this + // one, as this may be part of a larger structure) + v = (float *)((unsigned char *)v + outputstride); } } - ystep >>= 1; } } - // flatten control points on X - for (y = 0;y < outheight;y += ystep) - { - for (c = 0;c < components;c++) - { - x = xstep; - // fetch first two control points - prev = out[(y * outwidth + (x - xstep)) * components + c]; - curr = out[(y * outwidth + x) * components + c]; - for (;x < outwidth - xstep;x += xstep, prev = curr, curr = next) - { - // fetch next control point - next = out[(y * outwidth + (x + xstep)) * components + c]; - // flatten central control point - out[(y * outwidth + x) * components + c] = (curr + (prev + next) * 0.5f) * 0.5f; - } - } - } - // flatten control points on Y - for (x = 0;x < outwidth;x += xstep) - { - for (c = 0;c < components;c++) - { - y = ystep; - // fetch first two control points - prev = out[((y - ystep) * outwidth + x) * components + c]; - curr = out[(y * outwidth + x) * components + c]; - for (;y < outheight - ystep;y += ystep, prev = curr, curr = next) - { - // fetch next control point - next = out[((y + ystep) * outwidth + x) * components + c]; - // flatten central control point - out[(y * outwidth + x) * components + c] = (curr + (prev + next) * 0.5f) * 0.5f; - } - } - } - - /* - for (y = ystep;y < outheight - ystep;y += ystep) - { - for (c = 0;c < components;c++) - { - for (x = xstep, outp = out + (y * outwidth + x) * components + c, prev = outp[-xstep * components], curr = outp[0], next = outp[xstep * components];x < outwidth;x += xstep, outp += ystep * outwidth * components, prev = curr, curr = next, next = outp[xstep * components]) - { - // midpoint - outp[-halfstep * components] = (prev + curr) * 0.5f; - // flatten control point - outp[0] = (curr + (prev + next) * 0.5f) * 0.5f; - // next midpoint (only needed for end segment) - outp[halfstep * components] = (next + curr) * 0.5f; - } - } - } - */ -} -#else -// unfinished code -void QuadraticBSplinePatchSubdivideFloatBuffer(int cpwidth, int cpheight, int xlevel, int ylevel, int components, const float *in, float *out) -{ - int outwidth, outheight; - outwidth = ((cpwidth - 1) << xlevel) + 1; - outheight = ((cpheight - 1) << ylevel) + 1; - for (y = 0;y < cpheight;y++) +#if 0 + // enable this if you want results printed out + printf("vertices[%i][%i] =\n{\n", (patchheight-1)*tesselationheight+1, (patchwidth-1)*tesselationwidth+1); + for (y = 0;y < (patchheight-1)*tesselationheight+1;y++) { - for (x = 0;x < cpwidth;x++) + for (x = 0;x < (patchwidth-1)*tesselationwidth+1;x++) { - for (c = 0;c < components;c++) - { - inp = in + (y * cpwidth + x) * components + c; - outp = out + ((y< level1tolerance;level++) - deviation *= 0.25f; - return level; + return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1; } -int QuadraticBSplinePatchSubdivisionLevelOnX(int cpwidth, int cpheight, int components, const float *in, float level1tolerance, int levellimit) -{ - return QuadraticBSplinePatchSubdivisionLevelForDeviation(QuadraticBSplinePatchLargestDeviationOnX(cpwidth, cpheight, components, in), level1tolerance, levellimit); -} - -int QuadraticBSplinePatchSubdivisionLevelOnY(int cpwidth, int cpheight, int components, const float *in, float level1tolerance, int levellimit) -{ - return QuadraticBSplinePatchSubdivisionLevelForDeviation(QuadraticBSplinePatchLargestDeviationOnY(cpwidth, cpheight, components, in), level1tolerance, levellimit); -} - -/* - // 1: flat (0th dimension) - o = a - // 2: linear (1st dimension) - o = a * (1 - t) + b * t - // 3: quadratic bspline (2nd dimension) - o = a * (1 - t) * (1 - t) + 2 * b * (1 - t) * t + c * t * t - // 4: cubic (bezier) bspline (3rd dimension) - o = a * (1 - t) * (1 - t) * (1 - t) + 3 * b * (1 - t) * (1 - t) * t + 3 * c * (1 - t) * t * t + d * t * t * t - // 5: quartic bspline (4th dimension) - o = a * (1 - t) * (1 - t) * (1 - t) * (1 - t) + 4 * b * (1 - t) * (1 - t) * (1 - t) * t + 6 * c * (1 - t) * (1 - t) * t * t + 4 * d * (1 - t) * t * t * t + e * t * t * t * t - - // n: arbitrary dimension bspline -double factorial(int n) -{ - int i; - double f; - f = 1; - for (i = 1;i < n;i++) - f = f * i; - return f; -} -double bsplinesample(int dimensions, double t, double *param) +// calculates elements for a grid of vertices +// (such as those produced by Q3PatchTesselate) +// (note: width and height are the actual vertex size, this produces +// (width-1)*(height-1)*2 triangles, 3 elements each) +void Q3PatchTriangleElements(int *elements, int width, int height) { - double o = 0; - for (i = 0;i < dimensions + 1;i++) - o += param[i] * factorial(dimensions)/(factorial(i)*factorial(dimensions-i)) * pow(t, i) * pow(1 - t, dimensions - i); + int x, y, row0, row1; + for (y = 0;y < height - 1;y++) + { + row0 = (y + 0) * width; + row1 = (y + 1) * width; + for (x = 0;x < width - 1;x++) + { + *elements++ = row0; + *elements++ = row1; + *elements++ = row0 + 1; + *elements++ = row1; + *elements++ = row1 + 1; + *elements++ = row0 + 1; + row0++; + row1++; + } + } } -*/ diff --git a/curves.h b/curves.h index 8924c4a5..48d01219 100644 --- a/curves.h +++ b/curves.h @@ -2,13 +2,19 @@ #ifndef CURVES_H #define CURVES_H -void QuadraticBSplineSubdivideFloat(int inpoints, int components, const float *in, int instride, float *out, int outstride); -void QuadraticBSplinePatchSubdivideFloatBuffer(int cpwidth, int cpheight, int xlevel, int ylevel, int components, const float *in, float *out); -float QuadraticBSplinePatchLargestDeviationOnX(int cpwidth, int cpheight, int components, const float *in); -float QuadraticBSplinePatchLargestDeviationOnY(int cpwidth, int cpheight, int components, const float *in); -int QuadraticBSplinePatchSubdivisionLevelForDeviation(float deviation, float level1tolerance, int levellimit); -int QuadraticBSplinePatchSubdivisionLevelOnX(int cpwidth, int cpheight, int components, const float *in, float level1tolerance, int levellimit); -int QuadraticBSplinePatchSubdivisionLevelOnY(int cpwidth, int cpheight, int components, const float *in, float level1tolerance, int levellimit); +// usage: +// to expand a 5x5 patch to 21x21 vertices (4x4 tesselation), one might use this call: +// Q3PatchSubdivideFloat(3, sizeof(float[3]), outvertices, 5, 5, sizeof(float[3]), patchvertices, 4, 4); +void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputvertices, int patchwidth, int patchheight, int inputstride, float *patchvertices, int tesselationwidth, int tesselationheight); +// returns how much tesselation of each segment is needed to remain under tolerance +int Q3PatchTesselationOnX(int patchwidth, int patchheight, int components, const float *in, float tolerance); +// returns how much tesselation of each segment is needed to remain under tolerance +int Q3PatchTesselationOnY(int patchwidth, int patchheight, int components, const float *in, float tolerance); +// calculates elements for a grid of vertices +// (such as those produced by Q3PatchTesselate) +// (note: width and height are the actual vertex size, this produces +// (width-1)*(height-1)*2 triangles, 3 elements each) +void Q3PatchTriangleElements(int *elements, int width, int height); #endif diff --git a/model_brush.c b/model_brush.c index 847abf38..86d65ea4 100644 --- a/model_brush.c +++ b/model_brush.c @@ -35,12 +35,12 @@ cvar_t r_miplightmaps = {CVAR_SAVE, "r_miplightmaps", "0"}; cvar_t r_lightmaprgba = {0, "r_lightmaprgba", "1"}; cvar_t r_nosurftextures = {0, "r_nosurftextures", "0"}; cvar_t r_subdivisions_tolerance = {0, "r_subdivisions_tolerance", "4"}; -cvar_t r_subdivisions_minlevel = {0, "r_subdivisions_minlevel", "0"}; -cvar_t r_subdivisions_maxlevel = {0, "r_subdivisions_maxlevel", "10"}; +cvar_t r_subdivisions_mintess = {0, "r_subdivisions_mintess", "1"}; +cvar_t r_subdivisions_maxtess = {0, "r_subdivisions_maxtess", "1024"}; cvar_t r_subdivisions_maxvertices = {0, "r_subdivisions_maxvertices", "65536"}; cvar_t r_subdivisions_collision_tolerance = {0, "r_subdivisions_collision_tolerance", "15"}; -cvar_t r_subdivisions_collision_minlevel = {0, "r_subdivisions_collision_minlevel", "0"}; -cvar_t r_subdivisions_collision_maxlevel = {0, "r_subdivisions_collision_maxlevel", "10"}; +cvar_t r_subdivisions_collision_mintess = {0, "r_subdivisions_collision_mintess", "1"}; +cvar_t r_subdivisions_collision_maxtess = {0, "r_subdivisions_collision_maxtess", "1024"}; cvar_t r_subdivisions_collision_maxvertices = {0, "r_subdivisions_collision_maxvertices", "4225"}; cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1"}; cvar_t mod_q3bsp_optimizedtraceline = {0, "mod_q3bsp_optimizedtraceline", "1"}; @@ -56,12 +56,12 @@ void Mod_BrushInit(void) Cvar_RegisterVariable(&r_lightmaprgba); Cvar_RegisterVariable(&r_nosurftextures); Cvar_RegisterVariable(&r_subdivisions_tolerance); - Cvar_RegisterVariable(&r_subdivisions_minlevel); - Cvar_RegisterVariable(&r_subdivisions_maxlevel); + Cvar_RegisterVariable(&r_subdivisions_mintess); + Cvar_RegisterVariable(&r_subdivisions_maxtess); Cvar_RegisterVariable(&r_subdivisions_maxvertices); Cvar_RegisterVariable(&r_subdivisions_collision_tolerance); - Cvar_RegisterVariable(&r_subdivisions_collision_minlevel); - Cvar_RegisterVariable(&r_subdivisions_collision_maxlevel); + Cvar_RegisterVariable(&r_subdivisions_collision_mintess); + Cvar_RegisterVariable(&r_subdivisions_collision_maxtess); Cvar_RegisterVariable(&r_subdivisions_collision_maxvertices); Cvar_RegisterVariable(&mod_q3bsp_curves_collisions); Cvar_RegisterVariable(&mod_q3bsp_optimizedtraceline); @@ -3674,7 +3674,6 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l) { if (!strcasecmp(parameter[0], "blendfunc")) { - Con_Printf("!\n"); if (numparameters == 2 && !strcasecmp(parameter[1], "add")) flags2 |= Q3TEXTUREFLAG_ADDITIVE; else if (numparameters == 3 && !strcasecmp(parameter[1], "gl_one") && !strcasecmp(parameter[2], "gl_one")) @@ -4080,7 +4079,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) { q3dface_t *in; q3msurface_t *out; - int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xlevel, ylevel, row0, row1, x, y, *e, finalvertices, finaltriangles, firstvertex, firstelement, type; + int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xtess, ytess, finalvertices, finaltriangles, firstvertex, firstelement, type, oldnumtriangles, oldnumtriangles2; //int *originalelement3i; //int *originalneighbor3i; float *originalvertex3f; @@ -4131,7 +4130,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) n = LittleLong(in->effectindex); if (n < -1 || n >= loadmodel->brushq3.num_effects) { - Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid effectindex %i (%i effects)\n", i, out->texture->name, n, loadmodel->brushq3.num_effects); + if (developer.integer >= 2) + Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid effectindex %i (%i effects)\n", i, out->texture->name, n, loadmodel->brushq3.num_effects); n = -1; } if (n == -1) @@ -4195,7 +4195,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) case Q3FACETYPE_PATCH: patchsize[0] = LittleLong(in->specific.patch.patchsize[0]); patchsize[1] = LittleLong(in->specific.patch.patchsize[1]); - if (patchsize[0] < 1 || patchsize[1] < 1) + if (patchsize[0] < 3 || patchsize[1] < 3 || !(patchsize[0] & 1) || !(patchsize[1] & 1) || patchsize[0] * patchsize[1] >= min(r_subdivisions_maxvertices.integer, r_subdivisions_collision_maxvertices.integer)) { Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid patchsize %ix%i\n", i, out->texture->name, patchsize[0], patchsize[1]); out->num_vertices = 0; @@ -4224,24 +4224,24 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) //originalneighbor3i = out->data_neighbor3i; */ // convert patch to Q3FACETYPE_MESH - xlevel = QuadraticBSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10); - ylevel = QuadraticBSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10); + xtess = Q3PatchTesselationOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value); + ytess = Q3PatchTesselationOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value); // bound to user settings - xlevel = bound(r_subdivisions_minlevel.integer, xlevel, r_subdivisions_maxlevel.integer); - ylevel = bound(r_subdivisions_minlevel.integer, ylevel, r_subdivisions_maxlevel.integer); + xtess = bound(r_subdivisions_mintess.integer, xtess, r_subdivisions_maxtess.integer); + ytess = bound(r_subdivisions_mintess.integer, ytess, r_subdivisions_maxtess.integer); // bound to sanity settings - xlevel = bound(0, xlevel, 10); - ylevel = bound(0, ylevel, 10); + xtess = bound(1, xtess, 1024); + ytess = bound(1, ytess, 1024); // bound to user limit on vertices - while ((xlevel > 0 || ylevel > 0) && (((patchsize[0] - 1) << xlevel) + 1) * (((patchsize[1] - 1) << ylevel) + 1) > min(r_subdivisions_maxvertices.integer, 262144)) + while ((xtess > 1 || ytess > 1) && (((patchsize[0] - 1) * xtess) + 1) * (((patchsize[1] - 1) * ytess) + 1) > min(r_subdivisions_maxvertices.integer, 262144)) { - if (xlevel > ylevel) - xlevel--; + if (xtess > ytess) + xtess--; else - ylevel--; + ytess--; } - finalwidth = ((patchsize[0] - 1) << xlevel) + 1; - finalheight = ((patchsize[1] - 1) << ylevel) + 1; + finalwidth = ((patchsize[0] - 1) * xtess) + 1; + finalheight = ((patchsize[1] - 1) * ytess) + 1; finalvertices = finalwidth * finalheight; finaltriangles = (finalwidth - 1) * (finalheight - 1) * 2; out->data_vertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[20]) * finalvertices); @@ -4260,30 +4260,12 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) out->num_triangles = finaltriangles; // generate geometry // (note: normals are skipped because they get recalculated) - QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_vertex3f); - QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordtexture2f, out->data_texcoordtexture2f); - QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordlightmap2f, out->data_texcoordlightmap2f); - QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 4, originalcolor4f, out->data_color4f); - // generate elements - e = out->data_element3i; - for (y = 0;y < finalheight - 1;y++) - { - row0 = (y + 0) * finalwidth; - row1 = (y + 1) * finalwidth; - for (x = 0;x < finalwidth - 1;x++) - { - *e++ = row0; - *e++ = row1; - *e++ = row0 + 1; - *e++ = row1; - *e++ = row1 + 1; - *e++ = row0 + 1; - row0++; - row1++; - } - } - out->num_triangles = Mod_RemoveDegenerateTriangles(out->num_triangles, out->data_element3i, out->data_element3i, out->data_vertex3f); - if (developer.integer) + Q3PatchTesselateFloat(3, sizeof(float[3]), out->data_vertex3f, patchsize[0], patchsize[1], sizeof(float[3]), originalvertex3f, xtess, ytess); + Q3PatchTesselateFloat(2, sizeof(float[2]), out->data_texcoordtexture2f, patchsize[0], patchsize[1], sizeof(float[2]), originaltexcoordtexture2f, xtess, ytess); + Q3PatchTesselateFloat(2, sizeof(float[2]), out->data_texcoordlightmap2f, patchsize[0], patchsize[1], sizeof(float[2]), originaltexcoordlightmap2f, xtess, ytess); + Q3PatchTesselateFloat(4, sizeof(float[4]), out->data_color4f, patchsize[0], patchsize[1], sizeof(float[4]), originalcolor4f, xtess, ytess); + Q3PatchTriangleElements(out->data_element3i, finalwidth, finalheight); + if (developer.integer >= 2) { if (out->num_triangles < finaltriangles) Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve subdivided to %i vertices / %i triangles, %i degenerate triangles removed (leaving %i)\n", patchsize[0], patchsize[1], out->num_vertices, finaltriangles, finaltriangles - out->num_triangles, out->num_triangles); @@ -4292,60 +4274,44 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l) } // q3map does not put in collision brushes for curves... ugh // build the lower quality collision geometry - xlevel = QuadraticBSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10); - ylevel = QuadraticBSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10); + xtess = Q3PatchTesselationOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value); + ytess = Q3PatchTesselationOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value); // bound to user settings - xlevel = bound(r_subdivisions_collision_minlevel.integer, xlevel, r_subdivisions_collision_maxlevel.integer); - ylevel = bound(r_subdivisions_collision_minlevel.integer, ylevel, r_subdivisions_collision_maxlevel.integer); + xtess = bound(r_subdivisions_collision_mintess.integer, xtess, r_subdivisions_collision_maxtess.integer); + ytess = bound(r_subdivisions_collision_mintess.integer, ytess, r_subdivisions_collision_maxtess.integer); // bound to sanity settings - xlevel = bound(0, xlevel, 10); - ylevel = bound(0, ylevel, 10); + xtess = bound(1, xtess, 1024); + ytess = bound(1, ytess, 1024); // bound to user limit on vertices - while ((xlevel > 0 || ylevel > 0) && (((patchsize[0] - 1) << xlevel) + 1) * (((patchsize[1] - 1) << ylevel) + 1) > min(r_subdivisions_collision_maxvertices.integer, 262144)) + while ((xtess > 1 || ytess > 1) && (((patchsize[0] - 1) * xtess) + 1) * (((patchsize[1] - 1) * ytess) + 1) > min(r_subdivisions_collision_maxvertices.integer, 262144)) { - if (xlevel > ylevel) - xlevel--; + if (xtess > ytess) + xtess--; else - ylevel--; + ytess--; } - finalwidth = ((patchsize[0] - 1) << xlevel) + 1; - finalheight = ((patchsize[1] - 1) << ylevel) + 1; + finalwidth = ((patchsize[0] - 1) * xtess) + 1; + finalheight = ((patchsize[1] - 1) * ytess) + 1; finalvertices = finalwidth * finalheight; finaltriangles = (finalwidth - 1) * (finalheight - 1) * 2; + out->data_collisionvertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[3]) * finalvertices); out->data_collisionelement3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * finaltriangles); out->num_collisionvertices = finalvertices; out->num_collisiontriangles = finaltriangles; - QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_collisionvertex3f); - // generate elements - e = out->data_collisionelement3i; - for (y = 0;y < finalheight - 1;y++) - { - row0 = (y + 0) * finalwidth; - row1 = (y + 1) * finalwidth; - for (x = 0;x < finalwidth - 1;x++) - { - *e++ = row0; - *e++ = row1; - *e++ = row0 + 1; - *e++ = row1; - *e++ = row1 + 1; - *e++ = row0 + 1; - row0++; - row1++; - } - } + Q3PatchTesselateFloat(3, sizeof(float[3]), out->data_collisionvertex3f, patchsize[0], patchsize[1], sizeof(float[3]), originalvertex3f, xtess, ytess); + Q3PatchTriangleElements(out->data_collisionelement3i, finalwidth, finalheight); + + oldnumtriangles = out->num_triangles; + oldnumtriangles2 = out->num_collisiontriangles; + out->num_triangles = Mod_RemoveDegenerateTriangles(out->num_triangles, out->data_element3i, out->data_element3i, out->data_vertex3f); out->num_collisiontriangles = Mod_RemoveDegenerateTriangles(out->num_collisiontriangles, out->data_collisionelement3i, out->data_collisionelement3i, out->data_collisionvertex3f); if (developer.integer) - { - if (out->num_collisiontriangles < finaltriangles) - Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve subdivided for collisions to %i vertices / %i triangles, %i degenerate triangles removed (leaving %i)\n", patchsize[0], patchsize[1], out->num_collisionvertices, finaltriangles, finaltriangles - out->num_collisiontriangles, out->num_collisiontriangles); - else - Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve subdivided for collisions to %i vertices / %i triangles\n", patchsize[0], patchsize[1], out->num_collisionvertices, out->num_collisiontriangles); - } + Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve became %i:%i vertices / %i:%i triangles (%i:%i degenerate)\n", patchsize[0], patchsize[1], out->num_vertices, out->num_collisionvertices, oldnumtriangles, oldnumtriangles2, oldnumtriangles - out->num_triangles, oldnumtriangles2 - out->num_collisiontriangles); break; case Q3FACETYPE_FLARE: - Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): Q3FACETYPE_FLARE not supported (yet)\n", i, out->texture->name); + if (developer.integer >= 2) + Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): Q3FACETYPE_FLARE not supported (yet)\n", i, out->texture->name); // don't render it out->num_vertices = 0; out->num_triangles = 0; -- 2.39.2