spingle/source/gl_warp.c

273 lines
6.5 KiB
C
Raw Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2010-2014 QuakeSpasm developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//gl_warp.c -- warping animation support
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
extern cvar_t r_drawflat;
cvar_t r_oldwater = {"r_oldwater", "0", CVAR_ARCHIVE};
cvar_t r_waterquality = {"r_waterquality", "8", CVAR_NONE};
cvar_t r_waterwarp = {"r_waterwarp", "1", CVAR_NONE};
2019-11-25 16:49:58 -08:00
int32_t gl_warpimagesize;
2019-11-24 20:45:15 -08:00
float load_subdivide_size; //johnfitz -- remember what subdivide_size value was when this map was loaded
2019-11-25 17:40:18 -08:00
float turbsin[] =
2019-11-24 20:45:15 -08:00
{
#include "gl_warp_sin.h"
};
2019-12-02 04:24:20 -08:00
#define WARPCALC(s,t) ((s + turbsin[(int32_t)((t*2)+(cl.time*(128.0/PI))) & 255]) * (1.0/64)) //johnfitz -- correct warp
#define WARPCALC2(s,t) ((s + turbsin[(int32_t)((t*0.125+cl.time)*(128.0/PI)) & 255]) * (1.0/64)) //johnfitz -- old warp
2019-11-24 20:45:15 -08:00
//==============================================================================
//
// OLD-STYLE WATER
//
//==============================================================================
2019-11-25 17:40:18 -08:00
extern qmodel_t *loadmodel;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
msurface_t *warpface;
2019-11-24 20:45:15 -08:00
cvar_t gl_subdivide_size = {"gl_subdivide_size", "128", CVAR_ARCHIVE};
2019-11-25 17:40:18 -08:00
void BoundPoly(int32_t numverts, float *verts, vec3_t mins, vec3_t maxs)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i, j;
float *v;
2019-11-24 20:45:15 -08:00
mins[0] = mins[1] = mins[2] = 999999999;
maxs[0] = maxs[1] = maxs[2] = -999999999;
v = verts;
2019-11-25 17:40:18 -08:00
for(i = 0 ; i < numverts ; i++)
for(j = 0 ; j < 3 ; j++, v++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(*v < mins[j])
2019-11-24 20:45:15 -08:00
mins[j] = *v;
2019-11-25 17:40:18 -08:00
if(*v > maxs[j])
2019-11-24 20:45:15 -08:00
maxs[j] = *v;
}
}
2019-11-25 17:40:18 -08:00
void SubdividePolygon(int32_t numverts, float *verts)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i, j, k;
vec3_t mins, maxs;
float m;
float *v;
vec3_t front[64], back[64];
int32_t f, b;
float dist[64];
float frac;
glpoly_t *poly;
float s, t;
if(numverts > 60)
Sys_Error("numverts = %" PRIi32, numverts);
2019-11-25 17:40:18 -08:00
BoundPoly(numverts, verts, mins, maxs);
for(i = 0 ; i < 3 ; i++)
2019-11-24 20:45:15 -08:00
{
m = (mins[i] + maxs[i]) * 0.5;
2019-11-25 17:40:18 -08:00
m = gl_subdivide_size.value * floor(m / gl_subdivide_size.value + 0.5);
if(maxs[i] - m < 8)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
if(m - mins[i] < 8)
2019-11-24 20:45:15 -08:00
continue;
// cut it
v = verts + i;
2019-11-25 17:40:18 -08:00
for(j = 0 ; j < numverts ; j++, v += 3)
2019-11-24 20:45:15 -08:00
dist[j] = *v - m;
// wrap cases
dist[j] = dist[0];
2019-11-25 17:40:18 -08:00
v -= i;
VectorCopy(verts, v);
2019-11-24 20:45:15 -08:00
f = b = 0;
v = verts;
2019-11-25 17:40:18 -08:00
for(j = 0 ; j < numverts ; j++, v += 3)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(dist[j] >= 0)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
VectorCopy(v, front[f]);
2019-11-24 20:45:15 -08:00
f++;
}
2019-11-25 17:40:18 -08:00
if(dist[j] <= 0)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
VectorCopy(v, back[b]);
2019-11-24 20:45:15 -08:00
b++;
}
2019-11-25 17:40:18 -08:00
if(dist[j] == 0 || dist[j + 1] == 0)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
if((dist[j] > 0) != (dist[j + 1] > 0))
2019-11-24 20:45:15 -08:00
{
// clip point
2019-11-25 17:40:18 -08:00
frac = dist[j] / (dist[j] - dist[j + 1]);
for(k = 0 ; k < 3 ; k++)
front[f][k] = back[b][k] = v[k] + frac * (v[3 + k] - v[k]);
2019-11-24 20:45:15 -08:00
f++;
b++;
}
}
2019-11-25 17:40:18 -08:00
SubdividePolygon(f, front[0]);
SubdividePolygon(b, back[0]);
2019-11-24 20:45:15 -08:00
return;
}
poly = Hunk_AllocName(sizeof(glpoly_t) + (numverts - 4) * VERTEXSIZE * sizeof(float), __func__);
2019-11-24 20:45:15 -08:00
poly->next = warpface->polys->next;
warpface->polys->next = poly;
poly->numverts = numverts;
2019-11-25 17:40:18 -08:00
for(i = 0 ; i < numverts ; i++, verts += 3)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
VectorCopy(verts, poly->verts[i]);
s = DotProduct(verts, warpface->texinfo->vecs[0]);
t = DotProduct(verts, warpface->texinfo->vecs[1]);
2019-11-24 20:45:15 -08:00
poly->verts[i][3] = s;
poly->verts[i][4] = t;
}
}
/*
================
GL_SubdivideSurface
================
*/
2019-11-25 17:40:18 -08:00
void GL_SubdivideSurface(msurface_t *fa)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
vec3_t verts[64];
int32_t i;
2019-11-24 20:45:15 -08:00
warpface = fa;
//the first poly in the chain is the undivided poly for newwater rendering.
//grab the verts from that.
2019-11-25 17:40:18 -08:00
for(i = 0; i < fa->polys->numverts; i++)
VectorCopy(fa->polys->verts[i], verts[i]);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
SubdividePolygon(fa->polys->numverts, verts[0]);
2019-11-24 20:45:15 -08:00
}
/*
================
DrawWaterPoly -- johnfitz
================
*/
2019-11-25 17:40:18 -08:00
void DrawWaterPoly(glpoly_t *p)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
float *v;
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(load_subdivide_size > 48)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
glBegin(GL_POLYGON);
2019-11-24 20:45:15 -08:00
v = p->verts[0];
2019-11-25 17:40:18 -08:00
for(i = 0 ; i < p->numverts ; i++, v += VERTEXSIZE)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
glTexCoord2f(WARPCALC2(v[3], v[4]), WARPCALC2(v[4], v[3]));
glVertex3fv(v);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
glEnd();
2019-11-24 20:45:15 -08:00
}
else
{
2019-11-25 17:40:18 -08:00
glBegin(GL_POLYGON);
2019-11-24 20:45:15 -08:00
v = p->verts[0];
2019-11-25 17:40:18 -08:00
for(i = 0 ; i < p->numverts ; i++, v += VERTEXSIZE)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
glTexCoord2f(WARPCALC(v[3], v[4]), WARPCALC(v[4], v[3]));
glVertex3fv(v);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
glEnd();
2019-11-24 20:45:15 -08:00
}
}
//==============================================================================
//
// RENDER-TO-FRAMEBUFFER WATER
//
//==============================================================================
/*
=============
R_UpdateWarpTextures -- johnfitz -- each frame, update warping textures
=============
*/
2019-11-25 17:40:18 -08:00
void R_UpdateWarpTextures(void)
2019-11-24 20:45:15 -08:00
{
texture_t *tx;
2019-11-25 16:49:58 -08:00
int32_t i;
2019-11-24 20:45:15 -08:00
float x, y, x2, warptess;
2019-11-25 17:40:18 -08:00
if(r_oldwater.value || cl.paused || r_drawflat_cheatsafe || r_lightmap_cheatsafe)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
warptess = 128.0 / CLAMP(3.0, floor(r_waterquality.value), 64.0);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0; i < cl.worldmodel->numtextures; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!(tx = cl.worldmodel->textures[i]))
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
if(!tx->update_warp)
2019-11-24 20:45:15 -08:00
continue;
//render warp
2019-11-25 17:40:18 -08:00
GL_SetCanvas(CANVAS_WARPIMAGE);
GL_Bind(tx->gltexture);
for(x = 0.0; x < 128.0; x = x2)
2019-11-24 20:45:15 -08:00
{
x2 = x + warptess;
2019-11-25 17:40:18 -08:00
glBegin(GL_TRIANGLE_STRIP);
for(y = 0.0; y < 128.01; y += warptess) // .01 for rounding errors
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
glTexCoord2f(WARPCALC(x, y), WARPCALC(y, x));
glVertex2f(x, y);
glTexCoord2f(WARPCALC(x2, y), WARPCALC(y, x2));
glVertex2f(x2, y);
2019-11-24 20:45:15 -08:00
}
glEnd();
}
//copy to texture
2019-11-25 17:40:18 -08:00
GL_Bind(tx->warpimage);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, glx, gly + glheight - gl_warpimagesize, gl_warpimagesize, gl_warpimagesize);
2019-11-24 20:45:15 -08:00
tx->update_warp = false;
}
// ericw -- workaround for osx 10.6 driver bug when using FSAA. R_Clear only clears the warpimage part of the screen.
GL_SetCanvas(CANVAS_DEFAULT);
//if warp render went down into sbar territory, we need to be sure to refresh it next frame
2019-11-25 17:40:18 -08:00
if(gl_warpimagesize + sb_lines > glheight)
Sbar_Changed();
2019-11-24 20:45:15 -08:00
//if viewsize is less than 100, we need to redraw the frame around the viewport
scr_tileclear_updates = 0;
}