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