📄 gl_rsurf.c
字号:
/*
Copyright (C) 1996-1997 Id Software, Inc.
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.
*/
// r_surf.c: surface-related refresh code
#include "quakedef.h"
int skytexturenum;
#ifndef GL_RGBA4
#define GL_RGBA4 0
#endif
int lightmap_bytes; // 1, 2, or 4
int lightmap_textures;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
//unsigned blocklights[18*18];
unsigned blocklights[18*18*3]; // (*3 for RGB)
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
#define BLOCK_WIDTH 128
#define BLOCK_HEIGHT 128
#define MAX_LIGHTMAPS 64
int active_lightmaps;
typedef struct glRect_s {
unsigned char l,t,w,h;
} glRect_t;
glpoly_t *lightmap_polys[MAX_LIGHTMAPS];
qboolean lightmap_modified[MAX_LIGHTMAPS];
glRect_t lightmap_rectchange[MAX_LIGHTMAPS];
int allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
// the lightmap texture data needs to be kept in
// main memory so texsubimage can update properly
byte lightmaps[4*MAX_LIGHTMAPS*BLOCK_WIDTH*BLOCK_HEIGHT];
// For gl_texsort 0
msurface_t *skychain = NULL;
msurface_t *waterchain = NULL;
void R_RenderDynamicLightmaps (msurface_t *fa);
/*
===============
R_AddDynamicLights
===============
*/
void R_AddDynamicLights (msurface_t *surf)
{
int lnum;
int sd, td;
float dist, rad, minlight;
vec3_t impact, local;
int s, t;
int i;
int smax, tmax;
mtexinfo_t *tex;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
float cred, cgreen, cblue, brightness;
unsigned *bl;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
tex = surf->texinfo;
for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
{
if ( !(surf->dlightbits & (1<<lnum) ) )
continue; // not lit by this light
rad = cl_dlights[lnum].radius;
dist = DotProduct (cl_dlights[lnum].origin, surf->plane->normal) -
surf->plane->dist;
rad -= fabs(dist);
minlight = cl_dlights[lnum].minlight;
if (rad < minlight)
continue;
minlight = rad - minlight;
for (i=0 ; i<3 ; i++)
{
impact[i] = cl_dlights[lnum].origin[i] -
surf->plane->normal[i]*dist;
}
local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3];
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3];
local[0] -= surf->texturemins[0];
local[1] -= surf->texturemins[1];
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
cred = cl_dlights[lnum].color[0] * 256.0f;
cgreen = cl_dlights[lnum].color[1] * 256.0f;
cblue = cl_dlights[lnum].color[2] * 256.0f;
bl = blocklights;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
for (t = 0 ; t<tmax ; t++)
{
td = local[1] - t*16;
if (td < 0)
td = -td;
for (s=0 ; s<smax ; s++)
{
sd = local[0] - s*16;
if (sd < 0)
sd = -sd;
if (sd > td)
dist = sd + (td>>1);
else
dist = td + (sd>>1);
if (dist < minlight)
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
// blocklights[t*smax + s] += (rad - dist)*256;
{
brightness = rad - dist;
bl[0] += (int) (brightness * cred);
bl[1] += (int) (brightness * cgreen);
bl[2] += (int) (brightness * cblue);
}
bl += 3;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
}
}
}
}
/*
===============
R_BuildLightMap
Combine and scale multiple lightmaps into the 8.8 format in blocklights
===============
*/
void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
{
int smax, tmax;
int t;
int i, j, size;
byte *lightmap;
unsigned scale;
int maps;
int lightadj[4];
unsigned *bl;
surf->cached_dlight = (surf->dlightframe == r_framecount);
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
size = smax*tmax;
lightmap = surf->samples;
// set to full bright if no light data
if (r_fullbright->value || !cl.worldmodel->lightdata)
{
bl = blocklights; // 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes
for (i=0 ; i<size ; i++)
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
// blocklights[i] = 255*256;
{
*bl++ = 255*256;
*bl++ = 255*256;
*bl++ = 255*256;
}
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
goto store;
}
// clear to no light
bl = blocklights; // 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes
for (i=0 ; i<size ; i++)
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
// blocklights[i] = 0;
{
*bl++ = 0;
*bl++ = 0;
*bl++ = 0;
}
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
// add all the lightmaps
if (lightmap)
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ; maps++)
{
scale = d_lightstylevalue[surf->styles[maps]];
surf->cached_light[maps] = scale; // 8.8 fraction
bl = blocklights; // 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes
for (i=0 ; i<size ; i++)
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
/*
blocklights[i] += lightmap[i] * scale;
lightmap += size; // skip to next lightmap
*/
{
*bl++ += *lightmap++ * scale;
*bl++ += *lightmap++ * scale;
*bl++ += *lightmap++ * scale;
}
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
}
// add all the dynamic lights
if (surf->dlightframe == r_framecount)
R_AddDynamicLights (surf);
// bound, invert, and shift
store:
switch (gl_lightmap_format)
{
case GL_RGBA:
stride -= (smax<<2);
bl = blocklights;
for (i=0 ; i<tmax ; i++, dest += stride)
{
for (j=0 ; j<smax ; j++)
{
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
/*
t = *bl++;
t >>= 7;
if (t > 255)
t = 255;
dest[3] = 255-t;
dest += 4;
*/
// positive lighting (would be 255-t if it were inverse like glquake was)
t = *bl++ >> 7;
if (t > 255)
t = 255;
*dest++ = t;
t = *bl++ >> 7;
if (t > 255)
t = 255;
*dest++ = t;
t = *bl++ >> 7;
if (t > 255)
t = 255;
*dest++ = t;
*dest++ = 255;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
}
}
break;
case GL_ALPHA:
case GL_LUMINANCE:
case GL_INTENSITY:
bl = blocklights;
for (i=0 ; i<tmax ; i++, dest += stride)
{
for (j=0 ; j<smax ; j++)
{
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
/*
t = *bl++;
t >>= 7;
*/
t = ((bl[0] + bl[1] + bl[2]) * 85) >> 15; // basically / 3, but faster and combined with >> 7 shift down, note: actual number would be 85.3333...
bl += 3;
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
if (t > 255)
t = 255;
dest[j] = 255-t;
}
}
break;
default:
Sys_Error ("Bad lightmap format");
}
}
/*
===============
R_TextureAnimation
Returns the proper texture for a given time and base texture
===============
*/
texture_t *R_TextureAnimation (texture_t *base)
{
int reletive;
int count;
if (currententity->frame)
{
if (base->alternate_anims)
base = base->alternate_anims;
}
if (!base->anim_total)
return base;
reletive = (int)(cl.time*10) % base->anim_total;
count = 0;
while (base->anim_min > reletive || base->anim_max <= reletive)
{
base = base->anim_next;
if (!base)
Sys_Error ("R_TextureAnimation: broken cycle");
if (++count > 100)
Sys_Error ("R_TextureAnimation: infinite cycle");
}
return base;
}
/*
=============================================================
BRUSH MODELS
=============================================================
*/
extern int solidskytexture;
extern int alphaskytexture;
extern float speedscale; // for top sky and bottom sky
void DrawGLWaterPoly (glpoly_t *p);
void DrawGLWaterPolyLightmap (glpoly_t *p);
lpMTexFUNC qglMTexCoord2fSGIS = NULL;
lpSelTexFUNC qglSelectTextureSGIS = NULL;
qboolean mtexenabled = false;
void GL_SelectTexture (GLenum target);
void GL_DisableMultitexture(void)
{
if (mtexenabled) {
glDisable(GL_TEXTURE_2D);
GL_SelectTexture(TEXTURE0_SGIS);
mtexenabled = false;
}
}
void GL_EnableMultitexture(void)
{
if (gl_mtexable) {
GL_SelectTexture(TEXTURE1_SGIS);
glEnable(GL_TEXTURE_2D);
mtexenabled = true;
}
}
#if 0
/*
================
R_DrawSequentialPoly
Systems that have fast state and texture changes can
just do everything as it passes with no need to sort
================
*/
void R_DrawSequentialPoly (msurface_t *s)
{
glpoly_t *p;
float *v;
int i;
texture_t *t;
//
// normal lightmaped poly
//
// 2000-01-02 Missing R_WATERWARP fix by Radix start
// if (! (s->flags & (SURF_DRAWSKY|SURF_DRAWTURB|SURF_UNDERWATER) ) )
if (! (s->flags & (SURF_DRAWSKY|SURF_DRAWTURB|(r_waterwarp->value ? SURF_UNDERWATER : 0)) ) )
// 2000-01-02 Missing R_WATERWARP fix by Radix end
{
p = s->polys;
t = R_TextureAnimation (s->texinfo->texture);
GL_Bind (t->gl_texturenum);
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[3], v[4]);
glVertex3fv (v);
}
glEnd ();
GL_Bind (lightmap_textures + s->lightmaptexturenum);
glEnable (GL_BLEND);
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[5], v[6]);
glVertex3fv (v);
}
glEnd ();
glDisable (GL_BLEND);
return;
}
//
// subdivided water surface warp
//
if (s->flags & SURF_DRAWTURB)
{
GL_Bind (s->texinfo->texture->gl_texturenum);
EmitWaterPolys (s);
return;
}
//
// subdivided sky warp
//
if (s->flags & SURF_DRAWSKY)
{
GL_Bind (solidskytexture);
speedscale = realtime*8;
speedscale -= (int)speedscale;
EmitSkyPolys (s);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GL_Bind (alphaskytexture);
speedscale = realtime*16;
speedscale -= (int)speedscale;
EmitSkyPolys (s);
if (gl_lightmap_format == GL_LUMINANCE)
glBlendFunc (GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
glDisable (GL_BLEND);
}
//
// underwater warped with lightmap
//
p = s->polys;
t = R_TextureAnimation (s->texinfo->texture);
GL_Bind (t->gl_texturenum);
DrawGLWaterPoly (p);
GL_Bind (lightmap_textures + s->lightmaptexturenum);
glEnable (GL_BLEND);
DrawGLWaterPolyLightmap (p);
glDisable (GL_BLEND);
}
#else
/*
================
R_DrawSequentialPoly
Systems that have fast state and texture changes can
just do everything as it passes with no need to sort
================
*/
void R_DrawSequentialPoly (msurface_t *s)
{
glpoly_t *p;
float *v;
int i;
texture_t *t;
vec3_t nv, dir;
float ss, ss2, length;
float s1, t1;
glRect_t *theRect;
//
// normal lightmaped poly
//
// 2000-01-02 Missing R_WATERWARP fix by Radix start
// if (! (s->flags & (SURF_DRAWSKY|SURF_DRAWTURB|SURF_UNDERWATER) ) )
if (! (s->flags & (SURF_DRAWSKY|SURF_DRAWTURB|(r_waterwarp->value ? SURF_UNDERWATER : 0)) ) )
// 2000-01-02 Missing R_WATERWARP fix by Radix end
{
R_RenderDynamicLightmaps (s);
if (gl_mtexable)
{
p = s->polys;
t = R_TextureAnimation (s->texinfo->texture);
// Binds world to texture env 0
GL_SelectTexture(TEXTURE0_SGIS);
GL_Bind (t->gl_texturenum);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Binds lightmap to texenv 1
GL_EnableMultitexture(); // Same as SelectTexture (TEXTURE1)
GL_Bind (lightmap_textures + s->lightmaptexturenum);
i = s->lightmaptexturenum;
if (lightmap_modified[i])
{
lightmap_modified[i] = false;
theRect = &lightmap_rectchange[i];
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect->t,
BLOCK_WIDTH, theRect->h, gl_lightmap_format, GL_UNSIGNED_BYTE,
lightmaps+(i* BLOCK_HEIGHT + theRect->t) *BLOCK_WIDTH*lightmap_bytes);
theRect->l = BLOCK_WIDTH;
theRect->t = BLOCK_HEIGHT;
theRect->h = 0;
theRect->w = 0;
}
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes start
if (gl_lightmap_format == GL_RGBA)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // not inverse lighting
else
// 2001-09-11 Colored lightning by LordHavoc/Sarcazm/Maddes end
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBegin(GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
qglMTexCoord2fSGIS (TEXTURE0_SGIS, v[3], v[4]);
qglMTexCoord2fSGIS (TEXTURE1_SGIS, v[5], v[6]);
glVertex3fv (v);
}
glEnd ();
// 1999-12-28 OpenGL fullbright fix by Neal White III start
if (t->flags & FLAG_HAS_GLOWMAP)
{
GL_DisableMultitexture();
GL_Bind (t->gl_glowtexnum);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE);
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[3], v[4]);
glVertex3fv (v);
}
glEnd ();
// restore it to Quake's default
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable (GL_BLEND);
}
// return;
// 1999-12-28 OpenGL fullbright fix by Neal White III end
}
else
{
p = s->polys;
t = R_TextureAnimation (s->texinfo->texture);
GL_Bind (t->gl_texturenum);
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[3], v[4]);
glVertex3fv (v);
}
glEnd ();
GL_Bind (lightmap_textures + s->lightmaptexturenum);
glEnable (GL_BLEND);
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[5], v[6]);
glVertex3fv (v);
}
glEnd ();
glDisable (GL_BLEND);
}
return;
}
//
// subdivided water surface warp
//
if (s->flags & SURF_DRAWTURB)
{
GL_DisableMultitexture();
GL_Bind (s->texinfo->texture->gl_texturenum);
EmitWaterPolys (s);
return;
}
//
// subdivided sky warp
//
if (s->flags & SURF_DRAWSKY)
{
GL_DisableMultitexture();
GL_Bind (solidskytexture);
speedscale = realtime*8;
speedscale -= (int)speedscale & ~127;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -