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