r_surface.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1997-2001 Id Software, Inc.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "r_local.h"
00027 #include "r_lightmap.h"
00028 #include "r_light.h"
00029 #include "r_error.h"
00030 #include "r_draw.h"
00031 
00032 static inline void R_UseMaterial (const material_t *material)
00033 {
00034     static float last_b, last_p, last_s, last_h;
00035     float b, p, s, h;
00036 
00037     if (r_state.active_material == material)
00038         return;
00039 
00040     r_state.active_material = material;
00041 
00042     if (!r_state.active_material)
00043         return;
00044 
00045     b = r_state.active_material->bump * r_bumpmap->value;
00046     if (b != last_b)
00047         R_ProgramParameter1f("BUMP", b);
00048     last_b = b;
00049 
00050     p = r_state.active_material->parallax * r_parallax->value;
00051     if (p != last_p)
00052         R_ProgramParameter1f("PARALLAX", p);
00053     last_p = p;
00054 
00055     h = r_state.active_material->hardness * r_hardness->value;
00056     if (h != last_h)
00057         R_ProgramParameter1f("HARDNESS", h);
00058     last_h = h;
00059 
00060     s = r_state.active_material->specular * r_specular->value;
00061     if (s != last_s)
00062         R_ProgramParameter1f("SPECULAR", s);
00063     last_s = s;
00064 }
00065 
00066 void R_SetSurfaceBumpMappingParameters (const mBspSurface_t *surf, const image_t *normalMap)
00067 {
00068     if (!r_state.lighting_enabled)
00069         return;
00070 
00071     if (!r_bumpmap->value)
00072         return;
00073 
00074     assert(surf);
00075 
00076     if (normalMap && (surf->flags & MSURF_LIGHTMAP)) {
00077         const image_t *image = surf->texinfo->image;
00078         R_BindDeluxemapTexture(surf->deluxemap_texnum);
00079         R_EnableBumpmap(normalMap, qtrue);
00080         R_UseMaterial(&image->material);
00081     } else {
00082         R_EnableBumpmap(NULL, qfalse);
00083         R_UseMaterial(NULL);
00084     }
00085 }
00086 
00091 static void R_SetSurfaceState (const mBspSurface_t *surf)
00092 {
00093     const model_t* mod = r_mapTiles[surf->tile];
00094     image_t *image;
00095 
00096     if (r_state.blend_enabled) {  /* alpha blend */
00097         vec4_t color = {1.0, 1.0, 1.0, 1.0};
00098         switch (surf->texinfo->flags & (SURF_BLEND33 | SURF_BLEND66)) {
00099         case SURF_BLEND33:
00100             color[3] = 0.33;
00101             break;
00102         case SURF_BLEND66:
00103             color[3] = 0.66;
00104             break;
00105         }
00106 
00107         R_Color(color);
00108     }
00109 
00110     R_SetArrayState(mod);
00111 
00112     image = surf->texinfo->image;
00113     R_BindTexture(image->texnum);  /* texture */
00114 
00115     if (texunit_lightmap.enabled) {  /* lightmap */
00116         if (surf->flags & MSURF_LIGHTMAP)
00117             R_BindLightmapTexture(surf->lightmap_texnum);
00118     }
00119 
00120     R_SetSurfaceBumpMappingParameters(surf, image->normalmap);
00121 
00122     if (image->glowmap)
00123         R_EnableGlowMap(image->glowmap, qtrue);
00124     else
00125         R_EnableGlowMap(NULL, qfalse);
00126 
00127     R_CheckError();
00128 }
00129 
00134 static inline void R_DrawSurface (const mBspSurface_t *surf)
00135 {
00136     glDrawArrays(GL_POLYGON, surf->index, surf->numedges);
00137 
00138     if (r_showbox->integer == 2)
00139         R_DrawBoundingBox(surf->mins, surf->maxs);
00140 
00141     refdef.brushCount++;
00142 }
00143 
00150 static void R_DrawSurfaces (const mBspSurfaces_t *surfs)
00151 {
00152     int i;
00153 
00154     for (i = 0; i < surfs->count; i++) {
00155         if (surfs->surfaces[i]->frame != r_locals.frame)
00156             continue;
00157 
00158         R_SetSurfaceState(surfs->surfaces[i]);
00159         R_DrawSurface(surfs->surfaces[i]);
00160     }
00161 
00162     /* reset state */
00163     if (r_state.bumpmap_enabled)
00164         R_EnableBumpmap(NULL, qfalse);
00165 
00166     if (r_state.glowmap_enabled)
00167         R_EnableGlowMap(NULL, qfalse);
00168 
00169     /* and restore array pointers */
00170     R_ResetArrayState();
00171 
00172     R_Color(NULL);
00173 }
00174 
00179 void R_DrawOpaqueSurfaces (const mBspSurfaces_t *surfs)
00180 {
00181     if (!surfs->count)
00182         return;
00183 
00184     R_EnableTexture(&texunit_lightmap, qtrue);
00185 
00186     R_EnableLighting(r_state.world_program, qtrue);
00187     R_DrawSurfaces(surfs);
00188     R_EnableLighting(NULL, qfalse);
00189 
00190     R_EnableTexture(&texunit_lightmap, qfalse);
00191 }
00192 
00197 void R_DrawOpaqueWarpSurfaces (mBspSurfaces_t *surfs)
00198 {
00199     if (!surfs->count)
00200         return;
00201 
00202     R_EnableWarp(r_state.warp_program, qtrue);
00203     R_DrawSurfaces(surfs);
00204     R_EnableWarp(NULL, qfalse);
00205 }
00206 
00207 void R_DrawAlphaTestSurfaces (mBspSurfaces_t *surfs)
00208 {
00209     if (!surfs->count)
00210         return;
00211 
00212     R_EnableAlphaTest(qtrue);
00213     R_EnableLighting(r_state.world_program, qtrue);
00214     R_DrawSurfaces(surfs);
00215     R_EnableLighting(NULL, qfalse);
00216     R_EnableAlphaTest(qfalse);
00217 }
00218 
00223 void R_DrawBlendSurfaces (const mBspSurfaces_t *surfs)
00224 {
00225     if (!surfs->count)
00226         return;
00227 
00228     assert(r_state.blend_enabled);
00229     R_EnableTexture(&texunit_lightmap, qtrue);
00230     R_DrawSurfaces(surfs);
00231     R_EnableTexture(&texunit_lightmap, qfalse);
00232 }
00233 
00238 void R_DrawBlendWarpSurfaces (mBspSurfaces_t *surfs)
00239 {
00240     if (!surfs->count)
00241         return;
00242 
00243     assert(r_state.blend_enabled);
00244     R_EnableWarp(r_state.warp_program, qtrue);
00245     R_DrawSurfaces(surfs);
00246     R_EnableWarp(NULL, qfalse);
00247 }

Generated by  doxygen 1.6.2