]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_models.c
added surfaceparm pointlight to known surfaceparm list
[divverent/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3 #include "r_shadow.h"
4
5 void GL_Models_Init(void)
6 {
7 }
8
9 aliaslayer_t r_aliasnoskinlayers[2] = {{ALIASLAYER_DIFFUSE, NULL, NULL}, {ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS, NULL, NULL}};
10 aliasskin_t r_aliasnoskin = {0, 2, r_aliasnoskinlayers};
11 aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
12 {
13         model_t *model = ent->model;
14         if (model->numskins)
15         {
16                 int s = ent->skinnum;
17                 if ((unsigned int)s >= (unsigned int)model->numskins)
18                         s = 0;
19                 if (model->skinscenes[s].framecount > 1)
20                         s = model->skinscenes[s].firstframe + (int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
21                 else
22                         s = model->skinscenes[s].firstframe;
23                 if (s >= mesh->num_skins)
24                         s = 0;
25                 return mesh->data_skins + s;
26         }
27         else
28         {
29                 r_aliasnoskinlayers[0].texture = r_notexture;
30                 return &r_aliasnoskin;
31         }
32 }
33
34 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
35 {
36         int c, fullbright, layernum, firstpass;
37         float tint[3], fog, ifog, colorscale, ambientcolor4f[4], diffusecolor[3], diffusenormal[3];
38         float *vertex3f, *normal3f;
39         vec3_t diff;
40         qbyte *bcolor;
41         rmeshstate_t m;
42         const entity_render_t *ent = calldata1;
43         aliasmesh_t *mesh = ent->model->alias.aliasdata_meshes + calldata2;
44         aliaslayer_t *layer;
45         aliasskin_t *skin;
46
47         R_Mesh_Matrix(&ent->matrix);
48
49         fog = 0;
50         if (fogenabled)
51         {
52                 VectorSubtract(ent->origin, r_vieworigin, diff);
53                 fog = DotProduct(diff,diff);
54                 if (fog < 0.01f)
55                         fog = 0.01f;
56                 fog = exp(fogdensity/fog);
57                 if (fog > 1)
58                         fog = 1;
59                 if (fog < 0.01f)
60                         fog = 0;
61                 // fog method: darken, additive fog
62                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
63                 // 2. render fog as additive
64         }
65         ifog = 1 - fog;
66
67         firstpass = true;
68         skin = R_FetchAliasSkin(ent, mesh);
69
70         vertex3f = NULL;
71         normal3f = NULL;
72         for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
73         {
74                 if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
75                 {
76                         if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
77                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
78                          || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
79                          ||  (layer->flags & ALIASLAYER_SPECULAR)
80                          || ((layer->flags & ALIASLAYER_DIFFUSE) && (ent->flags & RENDER_LIGHT) && r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT) && r_ambient.integer <= 0))
81                                 continue;
82                 }
83                 if (!firstpass || (ent->effects & EF_ADDITIVE))
84                 {
85                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
86                         GL_DepthMask(false);
87                 }
88                 else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
89                 {
90                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
91                         GL_DepthMask(false);
92                 }
93                 else
94                 {
95                         GL_BlendFunc(GL_ONE, GL_ZERO);
96                         GL_DepthMask(true);
97                 }
98                 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
99                 firstpass = false;
100                 colorscale = 1.0f;
101
102                 memset(&m, 0, sizeof(m));
103                 if (layer->texture != NULL)
104                 {
105                         m.tex[0] = R_GetTexture(layer->texture);
106                         m.pointer_texcoord[0] = mesh->data_texcoord2f;
107                         if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
108                         {
109                                 colorscale *= 0.25f;
110                                 m.texrgbscale[0] = 4;
111                         }
112                 }
113                 if (!vertex3f)
114                 {
115                         if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
116                                 vertex3f = mesh->data_basevertex3f;
117                         else
118                         {
119                                 vertex3f = varray_vertex3f;
120                                 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
121                         }
122                 }
123                 m.pointer_vertex = vertex3f;
124
125                 c_alias_polys += mesh->num_triangles;
126                 if (layer->flags & ALIASLAYER_FOG)
127                 {
128                         colorscale *= fog;
129                         GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
130                 }
131                 else
132                 {
133                         fullbright = !(layer->flags & ALIASLAYER_DIFFUSE) || !(ent->flags & RENDER_LIGHT);
134                         if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
135                         {
136                                 // 128-224 are backwards ranges
137                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
138                                         c = (ent->colormap & 0xF) << 4;
139                                 else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
140                                         c = (ent->colormap & 0xF0);
141                                 c += (c >= 128 && c < 224) ? 4 : 12;
142                                 bcolor = (qbyte *) (&palette_complete[c]);
143                                 fullbright = fullbright || c >= 224;
144                                 VectorScale(bcolor, (1.0f / 255.0f), tint);
145                         }
146                         else
147                                 tint[0] = tint[1] = tint[2] = 1;
148                         tint[0] *= ent->colormod[0];
149                         tint[1] *= ent->colormod[1];
150                         tint[2] *= ent->colormod[2];
151                         if (!fullbright && !(ent->flags & RENDER_TRANSPARENT))
152                                 colorscale *= r_lightmapintensity;
153                         colorscale *= ifog;
154                         if (fullbright)
155                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
156                         else if (R_LightModel(ambientcolor4f, diffusecolor, diffusenormal, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
157                         {
158                                 m.pointer_color = varray_color4f;
159                                 if (normal3f == NULL)
160                                 {
161                                         if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
162                                                 normal3f = mesh->data_basenormal3f;
163                                         else
164                                         {
165                                                 normal3f = varray_normal3f;
166                                                 Mod_BuildNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, normal3f);
167                                         }
168                                 }
169                                 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, vertex3f, normal3f, varray_color4f);
170                         }
171                         else
172                                 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
173                 }
174                 R_Mesh_State(&m);
175                 GL_LockArrays(0, mesh->num_vertices);
176                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
177                 GL_LockArrays(0, 0);
178         }
179 }
180
181 void R_Model_Alias_Draw(entity_render_t *ent)
182 {
183         int meshnum;
184         aliasmesh_t *mesh;
185         if (ent->alpha < (1.0f / 64.0f))
186                 return; // basically completely transparent
187
188         c_models++;
189
190         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
191         {
192                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
193                         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : ent->origin, R_DrawAliasModelCallback, ent, meshnum);
194                 else
195                         R_DrawAliasModelCallback(ent, meshnum);
196         }
197 }
198
199 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
200 {
201         int meshnum;
202         vec3_t lightmins, lightmaxs;
203         aliasmesh_t *mesh;
204         aliasskin_t *skin;
205         float projectdistance, *vertex3f;
206         if (!(ent->flags & RENDER_SHADOW))
207                 return;
208         lightmins[0] = relativelightorigin[0] - lightradius;
209         lightmins[1] = relativelightorigin[1] - lightradius;
210         lightmins[2] = relativelightorigin[2] - lightradius;
211         lightmaxs[0] = relativelightorigin[0] + lightradius;
212         lightmaxs[1] = relativelightorigin[1] + lightradius;
213         lightmaxs[2] = relativelightorigin[2] + lightradius;
214         // check the box in modelspace, it was already checked in worldspace
215         if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
216                 return;
217         projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
218         if (projectdistance > 0.1)
219         {
220                 R_Mesh_Matrix(&ent->matrix);
221                 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
222                 {
223                         skin = R_FetchAliasSkin(ent, mesh);
224                         if (skin->flags & ALIASSKIN_TRANSPARENT)
225                                 continue;
226                         if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
227                                 vertex3f = mesh->data_basevertex3f;
228                         else
229                         {
230                                 vertex3f = varray_vertex3f;
231                                 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
232                         }
233                         // identify lit faces within the bounding box
234                         R_Shadow_PrepareShadowMark(mesh->num_triangles);
235                         R_Shadow_MarkVolumeFromBox(0, mesh->num_triangles, vertex3f, mesh->data_element3i, relativelightorigin, lightmins, lightmaxs, ent->model->normalmins, ent->model->normalmaxs);
236                         R_Shadow_VolumeFromList(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
237                 }
238         }
239 }
240
241 void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
242 {
243         int c, meshnum, layernum;
244         float fog, ifog, lightcolor2[3], ambientscale2, diffusescale2, specularscale2;
245         float *vertex3f, *svector3f, *tvector3f, *normal3f;
246         vec3_t diff;
247         qbyte *bcolor;
248         aliasmesh_t *mesh;
249         aliaslayer_t *layer;
250         aliasskin_t *skin;
251
252         R_Mesh_Matrix(&ent->matrix);
253
254         fog = 0;
255         if (fogenabled)
256         {
257                 VectorSubtract(ent->origin, r_vieworigin, diff);
258                 fog = DotProduct(diff,diff);
259                 if (fog < 0.01f)
260                         fog = 0.01f;
261                 fog = exp(fogdensity/fog);
262                 if (fog > 1)
263                         fog = 1;
264                 if (fog < 0.01f)
265                         fog = 0;
266                 // fog method: darken, additive fog
267                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
268                 // 2. render fog as additive
269         }
270         ifog = 1 - fog;
271
272         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
273         {
274                 skin = R_FetchAliasSkin(ent, mesh);
275                 // FIXME: transparent skins need to be lit during the transparent render
276                 if (skin->flags & ALIASSKIN_TRANSPARENT)
277                         continue;
278                 vertex3f = NULL;
279                 svector3f = NULL;
280                 tvector3f = NULL;
281                 normal3f = NULL;
282                 for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
283                 {
284                         if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
285                          || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
286                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
287                                 continue;
288                         lightcolor2[0] = lightcolor[0] * ifog;
289                         lightcolor2[1] = lightcolor[1] * ifog;
290                         lightcolor2[2] = lightcolor[2] * ifog;
291                         ambientscale2 = 0;
292                         diffusescale2 = 0;
293                         specularscale2 = 0;
294                         if (layer->flags & ALIASLAYER_SPECULAR)
295                                 specularscale2 = specularscale;
296                         if (layer->flags & ALIASLAYER_DIFFUSE)
297                         {
298                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
299                                 {
300                                         // 128-224 are backwards ranges
301                                         c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
302                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
303                                         if (c < 224)
304                                         {
305                                                 bcolor = (qbyte *) (&palette_complete[c]);
306                                                 lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
307                                                 lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
308                                                 lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
309                                                 ambientscale2 = ambientscale;
310                                                 diffusescale2 = diffusescale;
311                                         }
312                                 }
313                                 else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
314                                 {
315                                         // 128-224 are backwards ranges
316                                         c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
317                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
318                                         if (c < 224)
319                                         {
320                                                 bcolor = (qbyte *) (&palette_complete[c]);
321                                                 lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
322                                                 lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
323                                                 lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
324                                                 ambientscale2 = ambientscale;
325                                                 diffusescale2 = diffusescale;
326                                         }
327                                 }
328                                 else
329                                 {
330                                         ambientscale2 = ambientscale;
331                                         diffusescale2 = diffusescale;
332                                 }
333                         }
334                         if (!(ambientscale2 + diffusescale2 + specularscale2) || VectorLength2(lightcolor2) <= 0.01)
335                                 continue;
336                         c_alias_polys += mesh->num_triangles;
337                         if (!vertex3f)
338                         {
339                                 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
340                                 {
341                                         vertex3f = mesh->data_basevertex3f;
342                                         svector3f = mesh->data_basesvector3f;
343                                         tvector3f = mesh->data_basetvector3f;
344                                         normal3f = mesh->data_basenormal3f;
345                                 }
346                                 else
347                                 {
348                                         vertex3f = varray_vertex3f;
349                                         svector3f = varray_svector3f;
350                                         tvector3f = varray_tvector3f;
351                                         normal3f = varray_normal3f;
352                                         Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
353                                         Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_texcoord2f, mesh->data_element3i, svector3f, tvector3f, normal3f);
354                                 }
355                         }
356                         // TODO: make layer have ->gloss as well as ->texture, and merge specular layer for common non-colormapped case?
357                         R_Shadow_RenderLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, vertex3f, svector3f, tvector3f, normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor2, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, layer->texture, lightcubemap, ambientscale2, diffusescale2, specularscale2);
358                 }
359         }
360 }
361