From b7ee9ba07d394fa1d454646c33fe06e4dd5c384a Mon Sep 17 00:00:00 2001 From: "Zephaniah E. Hull" Date: Fri, 6 Apr 2001 02:57:26 +0000 Subject: [PATCH] gl_draw.c: gl_lightmode now uses a callback. gl_dyn_part.c: Now uses a (non-locked) vertex array to reduce GL calls. Gives a slight speed gain, I'll optimize it a bit more soon. gl_rlight.c: Fixed much of the dlightframecount issues. (This gives cleaner code and a speed up.) gl_rmain.c: A slight reorg of the dlight call order. gl_rmisc.c: Enable some of the vertex arrays. (vertex, texcoord, color.) gl_rsurf.c: Reworked R_BuildLightMap, smaller now code wise, also more optimized, and it builds a 0-2 lightmap in more cases now. Playing with GL_UploadLightmap. For non-lighthalf non-mtex lightmaps use a 0-2 range. (Makes gl_lightmode look a /lot/ better.) gl_screen.c: More stuff with the gl_lightmode callback. r_view.c: R_PushDlights is called elsewhere now. sw_rmain.c: Moved the R_PushDlights call. Also killed the unused PASSAGES define. --- qw/include/render.h | 3 - qw/source/gl_draw.c | 28 +++++---- qw/source/gl_dyn_part.c | 89 +++++++++++++++++++---------- qw/source/gl_rlight.c | 22 ++----- qw/source/gl_rmain.c | 2 + qw/source/gl_rmisc.c | 4 ++ qw/source/gl_rsurf.c | 123 ++++++++++++++-------------------------- qw/source/gl_screen.c | 19 ++----- qw/source/r_view.c | 2 - qw/source/sw_rmain.c | 10 +--- 10 files changed, 141 insertions(+), 161 deletions(-) diff --git a/qw/include/render.h b/qw/include/render.h index a529597c2..56cf89e15 100644 --- a/qw/include/render.h +++ b/qw/include/render.h @@ -68,9 +68,6 @@ typedef struct entity_s struct efrag_s *efrag; // linked list of efrags (FIXME) int visframe; // last frame this entity was found in an active leaf, only used for static objects - int dlightframe; // dynamic lighting - int dlightbits; - float colormod[3]; // color tint for model float alpha; // opacity (alpha) of the model float scale; // size scaler of the model diff --git a/qw/source/gl_draw.c b/qw/source/gl_draw.c index 70f5a9d68..88c478aac 100644 --- a/qw/source/gl_draw.c +++ b/qw/source/gl_draw.c @@ -251,6 +251,19 @@ Draw_TextBox (int x, int y, int width, int lines) extern void glrmain_init (void); extern void glrsurf_init (void); extern void GL_TextureMode_f (void); +extern void R_ForceLightUpdate (void); + +void +gl_lightmode_callback (cvar_t *cvar) +{ + if (cvar->int_val) { + lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 128; + } else { + lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 255; + } + + R_ForceLightUpdate (); +} void Draw_Init (void) @@ -266,14 +279,7 @@ Draw_Init (void) Cvar_Set (gl_lightmode, "0"); } - lighthalf = gl_lightmode->int_val != 0; // to avoid re-rendering all - // lightmaps on first frame - - if (lighthalf) { - lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 128; - } else { - lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 255; - } + gl_lightmode_callback(gl_lightmode); Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f, "Texture mipmap quality."); @@ -305,12 +311,12 @@ Draw_Init (void) glrsurf_init (); } - void Draw_Init_Cvars (void) { - gl_lightmode = Cvar_Get ("gl_lightmode", "1", CVAR_ARCHIVE, 0, - "Lighting mode (0 = GLQuake style, 1 = new style)"); + gl_lightmode = Cvar_Get ("gl_lightmode", "1", CVAR_ARCHIVE, + gl_lightmode_callback, + "Lighting mode (0 = GLQuake style, 1 = new style)"); gl_max_size = Cvar_Get ("gl_max_size", "1024", CVAR_NONE, 0, "Texture dimension"); gl_picmip = Cvar_Get ("gl_picmip", "0", CVAR_NONE, 0, "Dimensions of displayed textures. 0 is normal, 1 is half, 2 is 1/4"); gl_constretch = Cvar_Get ("gl_constretch", "0", CVAR_ARCHIVE, 0, diff --git a/qw/source/gl_dyn_part.c b/qw/source/gl_dyn_part.c index e2435a8b6..e131b7dd1 100644 --- a/qw/source/gl_dyn_part.c +++ b/qw/source/gl_dyn_part.c @@ -66,9 +66,15 @@ typedef struct particle_s { ptype_t type; } particle_t; +typedef struct varray_s { + float texcoord[2]; + unsigned char color[4]; + float vertex[3]; +} varray_t; static particle_t *particles, **freeparticles; static short r_numparticles, numparticles; +//static varray_t *vertex_array; extern qboolean lighthalf; @@ -148,11 +154,13 @@ R_MaxParticlesCheck (cvar_t *var) // and the compiler doesn't know when we do bad things with them. free (particles); free (freeparticles); + //free (vertex_array); particles = (particle_t *) calloc (r_numparticles, sizeof (particle_t)); freeparticles = (particle_t **) calloc (r_numparticles, sizeof (particle_t*)); + //vertex_array = (float *) calloc(r_numparticles, sizeof (varray_t)); R_ClearParticles(); } @@ -558,11 +566,21 @@ R_DrawParticles (void) vec3_t up, right; float scale; particle_t *part; - int activeparticles, maxparticle, j, k; + int activeparticles, maxparticle, j, k, vnum; + varray_t vertex_array[4]; // LordHavoc: particles should not affect zbuffer glDepthMask (GL_FALSE); + glInterleavedArrays (GL_T2F_C4UB_V3F, 0, (void *) &(vertex_array[0])); + + vertex_array[0].texcoord[0] = 0; vertex_array[0].texcoord[1] = 1; + vertex_array[1].texcoord[0] = 0; vertex_array[1].texcoord[1] = 0; + vertex_array[2].texcoord[0] = 1; vertex_array[2].texcoord[1] = 0; + vertex_array[3].texcoord[0] = 1; vertex_array[3].texcoord[1] = 1; + + vnum = 0; + VectorScale (vup, 1.5, up); VectorScale (vright, 1.5, right); @@ -592,38 +610,51 @@ R_DrawParticles (void) at = (byte *) & d_8to24table[(byte) part->color]; alpha = part->alpha; - if (lighthalf) - glColor4ub ((byte) ((int) at[0] >> 1), - (byte) ((int) at[1] >> 1), - (byte) ((int) at[2] >> 1), alpha); - else - glColor4ub (at[0], at[1], at[2], alpha); + if (lighthalf) { + vertex_array[0].color[0] = (byte) ((int) at[0] >> 1); + vertex_array[0].color[1] = (byte) ((int) at[1] >> 1); + vertex_array[0].color[2] = (byte) ((int) at[2] >> 1); + } else { + VectorCopy(at, vertex_array[0].color); + } + vertex_array[0].color[3] = alpha; + + memcpy(vertex_array[1].color, vertex_array[0].color, 4); + memcpy(vertex_array[2].color, vertex_array[0].color, 4); + memcpy(vertex_array[3].color, vertex_array[0].color, 4); scale = part->scale; + vertex_array[0].vertex[0] = + (part->org[0] + ((up[0] + right[0]) * scale)); + vertex_array[0].vertex[1] = + (part->org[1] + ((up[1] + right[1]) * scale)); + vertex_array[0].vertex[2] = + (part->org[2] + ((up[2] + right[2]) * scale)); + + vertex_array[1].vertex[0] = + (part->org[0] + (up[0] * -scale) + (right[0] * scale)); + vertex_array[1].vertex[1] = + (part->org[1] + (up[1] * -scale) + (right[1] * scale)); + vertex_array[1].vertex[2] = + (part->org[2] + (up[2] * -scale) + (right[2] * scale)); + + vertex_array[2].vertex[0] = + (part->org[0] + ((up[0] + right[0]) * -scale)); + vertex_array[2].vertex[1] = + (part->org[1] + ((up[1] + right[1]) * -scale)); + vertex_array[2].vertex[2] = + (part->org[2] + ((up[2] + right[2]) * -scale)); + + vertex_array[3].vertex[0] = + (part->org[0] + (up[0] * scale) + (right[0] * -scale)); + vertex_array[3].vertex[1] = + (part->org[1] + (up[1] * scale) + (right[1] * -scale)); + vertex_array[3].vertex[2] = + (part->org[2] + (up[2] * scale) + (right[2] * -scale)); + glBindTexture (GL_TEXTURE_2D, part->tex); - glBegin (GL_QUADS); - glTexCoord2f (0, 1); - glVertex3f ((part->org[0] + ((up[0] + right[0]) * scale)), - (part->org[1] + ((up[1] + right[1]) * scale)), - (part->org[2] + ((up[2] + right[2]) * scale))); - - glTexCoord2f (0, 0); - glVertex3f ((part->org[0] + (up[0] * -scale) + (right[0] * scale)), - (part->org[1] + (up[1] * -scale) + (right[1] * scale)), - (part->org[2] + (up[2] * -scale) + (right[2] * scale))); - - glTexCoord2f (1, 0); - glVertex3f ((part->org[0] + ((up[0] + right[0]) * -scale)), - (part->org[1] + ((up[1] + right[1]) * -scale)), - (part->org[2] + ((up[2] + right[2]) * -scale))); - - glTexCoord2f (1, 1); - glVertex3f ((part->org[0] + (up[0] * scale) + (right[0] * -scale)), - (part->org[1] + (up[1] * scale) + (right[1] * -scale)), - (part->org[2] + (up[2] * scale) + (right[2] * -scale))); - - glEnd (); + glDrawArrays (GL_QUADS, 0, 4); } for (i = 0; i < 3; i++) diff --git a/qw/source/gl_rlight.c b/qw/source/gl_rlight.c index 372e2769a..0f9850293 100644 --- a/qw/source/gl_rlight.c +++ b/qw/source/gl_rlight.c @@ -41,10 +41,6 @@ #include "glquake.h" -int r_dlightframecount; -extern qboolean lighthalf; - - /* R_AnimateLight */ @@ -168,8 +164,6 @@ R_RenderDlights (void) if (!gl_dlight_polyblend->int_val) return; - r_dlightframecount = r_framecount + 1; // because the count hasn't - // advanced yet for this frame glDepthMask (GL_FALSE); glDisable (GL_TEXTURE_2D); glBlendFunc (GL_ONE, GL_ONE); @@ -264,13 +258,12 @@ R_MarkLights (vec3_t lightorigin, dlight_t *light, int bit, mnode_t *node) t = l - t; // compare to minimum light if ((s * s + t * t + dist * dist) < maxdist) { - if (surf->dlightframe != r_dlightframecount) // not dynamic - // until now - { + if (surf->dlightframe != r_framecount) { + surf->dlightframe = r_framecount; surf->dlightbits = bit; - surf->dlightframe = r_dlightframecount; - } else // already dynamic + } else { surf->dlightbits |= bit; + } } } @@ -282,10 +275,9 @@ R_MarkLights (vec3_t lightorigin, dlight_t *light, int bit, mnode_t *node) R_MarkLights (lightorigin, light, bit, node->children[1]); } - /* - R_PushDlights -*/ + R_PushDlights + */ void R_PushDlights (vec3_t entorigin) { @@ -296,8 +288,6 @@ R_PushDlights (vec3_t entorigin) if (!gl_dlight_lightmap->int_val) return; - r_dlightframecount = r_framecount + 1; // because the count hasn't - // advanced yet for this frame l = cl_dlights; for (i = 0; i < MAX_DLIGHTS; i++, l++) { diff --git a/qw/source/gl_rmain.c b/qw/source/gl_rmain.c index 73122f6f4..64266733d 100644 --- a/qw/source/gl_rmain.c +++ b/qw/source/gl_rmain.c @@ -1205,6 +1205,8 @@ R_RenderView (void) R_SetupGL (); + R_PushDlights (vec3_origin); + R_MarkLeaves (); // done here so we know if we're in // water diff --git a/qw/source/gl_rmisc.c b/qw/source/gl_rmisc.c index e4d28289a..1e2e27c9b 100644 --- a/qw/source/gl_rmisc.c +++ b/qw/source/gl_rmisc.c @@ -201,6 +201,10 @@ R_Init (void) texture_extension_number += MAX_CLIENTS; player_fb_textures = texture_extension_number; texture_extension_number += MAX_CACHED_SKINS; + + glEnableClientState (GL_COLOR_ARRAY); + glEnableClientState (GL_VERTEX_ARRAY); + glEnableClientState (GL_TEXTURE_COORD_ARRAY); } void diff --git a/qw/source/gl_rsurf.c b/qw/source/gl_rsurf.c index cb79f6ea3..d3e6954d5 100644 --- a/qw/source/gl_rsurf.c +++ b/qw/source/gl_rsurf.c @@ -94,7 +94,7 @@ glrsurf_init (void) memset (&lightmaps, 0, sizeof (lightmaps)); } -void +static void R_RecursiveLightUpdate (mnode_t *node) { int c; @@ -220,8 +220,7 @@ void R_BuildLightMap (msurface_t *surf, byte * dest, int stride) { int smax, tmax; - int t; - int i, j, size; + int i, j, size, shift; byte *lightmap; unsigned int scale; int maps; @@ -265,85 +264,46 @@ R_BuildLightMap (msurface_t *surf, byte * dest, int stride) // bound and shift stride -= smax * lightmap_bytes; bl = blocklights; + + if (gl_mtex_active && !lighthalf) { + shift = 7; // 0-1 lightmap range. + } else { + shift = 8; // 0-2 lightmap range. + } + switch (lightmap_bytes) { case 4: - if (lighthalf) { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - *dest++ = 255; - } - } - } else { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - *dest++ = 255; - } + for (i = 0; i < tmax; i++, dest += stride) { + for (j = 0; j < smax; j++) { + dest[0] = bound(0, bl[0] >> shift, 255); + dest[1] = bound(0, bl[1] >> shift, 255); + dest[2] = bound(0, bl[2] >> shift, 255); + dest[3] = 255; + dest += 4; + bl += 3; } } break; case 3: - if (lighthalf) { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 8; - *dest++ = bound (0, t, 255); - } - } - } else { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - t = (int) *bl++ >> 7; - *dest++ = bound (0, t, 255); - } + for (i = 0; i < tmax; i++, dest += stride) { + for (j = 0; j < smax; j++) { + dest[0] = bound(0, bl[0] >> shift, 255); + dest[1] = bound(0, bl[1] >> shift, 255); + dest[2] = bound(0, bl[2] >> shift, 255); + dest += 3; + bl += 3; } } break; case 1: - if (lighthalf) { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 8; - t2 = bound (0, t, 255); - t = (int) *bl++ >> 8; - t2 += bound (0, t, 255); - t = (int) *bl++ >> 8; - t2 += bound (0, t, 255); - t2 *= (1.0 / 3.0); - *dest++ = t2; - } - } - } else { - for (i = 0; i < tmax; i++, dest += stride) { - for (j = 0; j < smax; j++) { - t = (int) *bl++ >> 7; - t2 = bound (0, t, 255); - t = (int) *bl++ >> 7; - t2 += bound (0, t, 255); - t = (int) *bl++ >> 7; - t2 += bound (0, t, 255); - t2 *= (1.0 / 3.0); - *dest++ = t2; - } + for (i = 0; i < tmax; i++, dest += stride) { + for (j = 0; j < smax; j++) { + t2 = bound (0, bl[0] >> shift, 255); + t2 += bound (0, bl[1] >> shift, 255); + t2 += bound (0, bl[2] >> shift, 255); + t2 *= (1.0 / 3.0); + *dest++ = t2; + bl += 3; } } break; @@ -399,9 +359,14 @@ QF_glMultiTexCoord2fARB qglMultiTexCoord2f = NULL; void GL_UploadLightmap (int i, int x, int y, int w, int h) { + /* glTexSubImage2D (GL_TEXTURE_2D, 0, 0, y, BLOCK_WIDTH, h, gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps[i] + (y * BLOCK_WIDTH) * lightmap_bytes); + */ + glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes, BLOCK_WIDTH, + BLOCK_HEIGHT, 0, gl_lightmap_format, + GL_UNSIGNED_BYTE, lightmaps[i]); } /* @@ -440,9 +405,7 @@ R_DrawMultitexturePoly (msurface_t *s) if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps]) goto dynamic; - if (s->dlightframe == r_framecount // dynamic this frame - || s->cached_dlight) // dynamic previously - { + if ((s->dlightframe = r_framecount) || s->cached_dlight) { dynamic: R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + @@ -487,7 +450,11 @@ R_BlendLightmaps (void) glDepthMask (GL_FALSE); // don't bother writing Z - glBlendFunc (GL_ZERO, GL_SRC_COLOR); + if (lighthalf) + glBlendFunc (GL_ZERO, GL_SRC_COLOR); + else + glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR); + glColor3f (1, 1, 1); for (i = 0; i < MAX_LIGHTMAPS; i++) { @@ -590,9 +557,7 @@ R_RenderBrushPoly (msurface_t *fa) if (d_lightstylevalue[fa->styles[maps]] != fa->cached_light[maps]) goto dynamic; - if (fa->dlightframe == r_framecount // dynamic this frame - || fa->cached_dlight) // dynamic previously - { + if ((fa->dlightframe == r_framecount) || fa->cached_dlight) { dynamic: if (r_dynamic->int_val) { lightmap_modified[fa->lightmaptexturenum] = true; diff --git a/qw/source/gl_screen.c b/qw/source/gl_screen.c index b7505734a..942204cbb 100644 --- a/qw/source/gl_screen.c +++ b/qw/source/gl_screen.c @@ -976,15 +976,6 @@ SCR_UpdateScreen (void) // do 3D refresh drawing, and then update the screen - if (lighthalf != gl_lightmode->int_val) { - lighthalf = gl_lightmode->int_val; - if (lighthalf) - lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 128; - else - lighthalf_v[0] = lighthalf_v[1] = lighthalf_v[2] = 255; - R_ForceLightUpdate (); - } - SCR_SetUpToDrawConsole (); V_RenderView (); @@ -1024,12 +1015,14 @@ SCR_UpdateScreen (void) glDisable (GL_TEXTURE_2D); Cvar_SetValue (brightness, bound (1, brightness->value, 5)); - if (lighthalf) // LordHavoc: render was done at half - // - // brightness + if (lighthalf) { // LordHavoc: render was done at half + // brightness f = brightness->value * 2; - else + } else { + Cvar_SetValue (brightness, bound (1, brightness->value, 5)); f = brightness->value; + } + if (f >= 1.002) { // Make sure we don't get bit by // roundoff errors glBlendFunc (GL_DST_COLOR, GL_ONE); diff --git a/qw/source/r_view.c b/qw/source/r_view.c index f201f0bae..ef2ab927d 100644 --- a/qw/source/r_view.c +++ b/qw/source/r_view.c @@ -719,8 +719,6 @@ V_RenderView (void) V_CalcRefdef (); } - R_PushDlights (vec3_origin); - R_RenderView (); } diff --git a/qw/source/sw_rmain.c b/qw/source/sw_rmain.c index d16002ec3..10cd83b59 100644 --- a/qw/source/sw_rmain.c +++ b/qw/source/sw_rmain.c @@ -52,7 +52,6 @@ #include "QF/sys.h" #include "view.h" -//define PASSAGES void *colormap; vec3_t viewlightvec; @@ -178,9 +177,6 @@ cvar_t *gl_sky_divide; extern cvar_t *scr_fov; -void CreatePassages (void); -void SetVisibilityByPassages (void); - void R_NetGraph (void); void R_ZGraph (void); @@ -1005,14 +1001,12 @@ R_RenderView_ (void) if (r_timegraph->int_val || r_speeds->int_val || r_dspeeds->int_val) r_time1 = Sys_DoubleTime (); + R_PushDlights (vec3_origin); + R_SetupFrame (); -#ifdef PASSAGES - SetVisibilityByPassages (); -#else R_MarkLeaves (); // done here so we know if we're in // water -#endif // make FDIV fast. This reduces timing precision after we've been running for a // while, so we don't do it globally. This also sets chop mode, and we do it