]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_models.c
Added two cvars (sbar_alpha_bg and sbar_alpha_fg) to replace sbar_alpha, one controls...
[xonotic/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 void R_Model_Alias_GetMesh_Vertex3f(const entity_render_t *ent, const aliasmesh_t *mesh, float *out3f)
10 {
11         if (mesh->num_vertexboneweights)
12         {
13                 int i, k, blends;
14                 aliasvertexboneweight_t *v;
15                 float *out, *matrix, m[12], bonepose[256][12];
16                 // vertex weighted skeletal
17                 // interpolate matrices and concatenate them to their parents
18                 for (i = 0;i < ent->model->alias.aliasnum_bones;i++)
19                 {
20                         for (k = 0;k < 12;k++)
21                                 m[k] = 0;
22                         for (blends = 0;blends < 4 && ent->frameblend[blends].lerp > 0;blends++)
23                         {
24                                 matrix = ent->model->alias.aliasdata_poses + (ent->frameblend[blends].frame * ent->model->alias.aliasnum_bones + i) * 12;
25                                 for (k = 0;k < 12;k++)
26                                         m[k] += matrix[k] * ent->frameblend[blends].lerp;
27                         }
28                         if (ent->model->alias.aliasdata_bones[i].parent >= 0)
29                                 R_ConcatTransforms(bonepose[ent->model->alias.aliasdata_bones[i].parent], m, bonepose[i]);
30                         else
31                                 for (k = 0;k < 12;k++)
32                                         bonepose[i][k] = m[k];
33                 }
34                 // blend the vertex bone weights
35                 memset(out3f, 0, mesh->num_vertices * sizeof(float[3]));
36                 v = mesh->data_vertexboneweights;
37                 for (i = 0;i < mesh->num_vertexboneweights;i++, v++)
38                 {
39                         out = out3f + v->vertexindex * 3;
40                         matrix = bonepose[v->boneindex];
41                         // FIXME: this can very easily be optimized with SSE or 3DNow
42                         out[0] += v->origin[0] * matrix[0] + v->origin[1] * matrix[1] + v->origin[2] * matrix[ 2] + v->origin[3] * matrix[ 3];
43                         out[1] += v->origin[0] * matrix[4] + v->origin[1] * matrix[5] + v->origin[2] * matrix[ 6] + v->origin[3] * matrix[ 7];
44                         out[2] += v->origin[0] * matrix[8] + v->origin[1] * matrix[9] + v->origin[2] * matrix[10] + v->origin[3] * matrix[11];
45                 }                                                                                                              
46         }
47         else
48         {
49                 int i, vertcount;
50                 float lerp1, lerp2, lerp3, lerp4;
51                 const float *vertsbase, *verts1, *verts2, *verts3, *verts4;
52                 // vertex morph
53                 vertsbase = mesh->data_morphvertex3f;
54                 vertcount = mesh->num_vertices;
55                 verts1 = vertsbase + ent->frameblend[0].frame * vertcount * 3;
56                 lerp1 = ent->frameblend[0].lerp;
57                 if (ent->frameblend[1].lerp)
58                 {
59                         verts2 = vertsbase + ent->frameblend[1].frame * vertcount * 3;
60                         lerp2 = ent->frameblend[1].lerp;
61                         if (ent->frameblend[2].lerp)
62                         {
63                                 verts3 = vertsbase + ent->frameblend[2].frame * vertcount * 3;
64                                 lerp3 = ent->frameblend[2].lerp;
65                                 if (ent->frameblend[3].lerp)
66                                 {
67                                         verts4 = vertsbase + ent->frameblend[3].frame * vertcount * 3;
68                                         lerp4 = ent->frameblend[3].lerp;
69                                         for (i = 0;i < vertcount * 3;i++)
70                                                 VectorMAMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, lerp4, verts4 + i, out3f + i);
71                                 }
72                                 else
73                                         for (i = 0;i < vertcount * 3;i++)
74                                                 VectorMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, out3f + i);
75                         }
76                         else
77                                 for (i = 0;i < vertcount * 3;i++)
78                                         VectorMAM(lerp1, verts1 + i, lerp2, verts2 + i, out3f + i);
79                 }
80                 else
81                         memcpy(out3f, verts1, vertcount * sizeof(float[3]));
82         }
83 }
84
85 aliaslayer_t r_aliasnoskinlayers[2] = {{ALIASLAYER_DIFFUSE, NULL, NULL}, {ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS, NULL, NULL}};
86 aliasskin_t r_aliasnoskin = {0, 2, r_aliasnoskinlayers};
87 aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
88 {
89         model_t *model = ent->model;
90         if (model->numskins)
91         {
92                 int s = ent->skinnum;
93                 if ((unsigned int)s >= (unsigned int)model->numskins)
94                         s = 0;
95                 if (model->skinscenes[s].framecount > 1)
96                         s = model->skinscenes[s].firstframe + (int) (cl.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
97                 else
98                         s = model->skinscenes[s].firstframe;
99                 if (s >= mesh->num_skins)
100                         s = 0;
101                 return mesh->data_skins + s;
102         }
103         else
104         {
105                 r_aliasnoskinlayers[0].texture = r_notexture;
106                 return &r_aliasnoskin;
107         }
108 }
109
110 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
111 {
112         int c, fullbright, layernum, firstpass, generatenormals = true;
113         float tint[3], fog, ifog, colorscale, ambientcolor4f[4], diffusecolor[3], diffusenormal[3];
114         vec3_t diff;
115         qbyte *bcolor;
116         rmeshstate_t m;
117         const entity_render_t *ent = calldata1;
118         aliasmesh_t *mesh = ent->model->alias.aliasdata_meshes + calldata2;
119         aliaslayer_t *layer;
120         aliasskin_t *skin;
121
122         R_Mesh_Matrix(&ent->matrix);
123
124         fog = 0;
125         if (fogenabled)
126         {
127                 VectorSubtract(ent->origin, r_vieworigin, diff);
128                 fog = DotProduct(diff,diff);
129                 if (fog < 0.01f)
130                         fog = 0.01f;
131                 fog = exp(fogdensity/fog);
132                 if (fog > 1)
133                         fog = 1;
134                 if (fog < 0.01f)
135                         fog = 0;
136                 // fog method: darken, additive fog
137                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
138                 // 2. render fog as additive
139         }
140         ifog = 1 - fog;
141
142         firstpass = true;
143         skin = R_FetchAliasSkin(ent, mesh);
144         R_Model_Alias_GetMesh_Vertex3f(ent, mesh, varray_vertex3f);
145         for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
146         {
147                 if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
148                 {
149                         if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
150                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
151                          || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
152                          ||  (layer->flags & ALIASLAYER_SPECULAR)
153                          || ((layer->flags & ALIASLAYER_DIFFUSE) && (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0 && r_ambient.integer <= 0 && r_fullbright.integer == 0 && !(ent->effects & EF_FULLBRIGHT))))
154                                 continue;
155                 }
156                 if (!firstpass || (ent->effects & EF_ADDITIVE))
157                 {
158                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
159                         GL_DepthMask(false);
160                 }
161                 else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
162                 {
163                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164                         GL_DepthMask(false);
165                 }
166                 else
167                 {
168                         GL_BlendFunc(GL_ONE, GL_ZERO);
169                         GL_DepthMask(true);
170                 }
171                 GL_DepthTest(true);
172                 firstpass = false;
173                 colorscale = 1.0f;
174
175                 memset(&m, 0, sizeof(m));
176                 if (layer->texture != NULL)
177                 {
178                         m.tex[0] = R_GetTexture(layer->texture);
179                         m.pointer_texcoord[0] = mesh->data_texcoord2f;
180                         if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
181                         {
182                                 colorscale *= 0.25f;
183                                 m.texrgbscale[0] = 4;
184                         }
185                 }
186                 m.pointer_vertex = varray_vertex3f;
187
188                 c_alias_polys += mesh->num_triangles;
189                 if (layer->flags & ALIASLAYER_FOG)
190                 {
191                         colorscale *= fog;
192                         GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
193                 }
194                 else
195                 {
196                         fullbright = !(layer->flags & ALIASLAYER_DIFFUSE) || r_fullbright.integer || (ent->effects & EF_FULLBRIGHT);
197                         if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
198                         {
199                                 // 128-224 are backwards ranges
200                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
201                                         c = (ent->colormap & 0xF) << 4;
202                                 else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
203                                         c = (ent->colormap & 0xF0);
204                                 c += (c >= 128 && c < 224) ? 4 : 12;
205                                 bcolor = (qbyte *) (&palette_complete[c]);
206                                 fullbright = fullbright || c >= 224;
207                                 VectorScale(bcolor, (1.0f / 255.0f), tint);
208                         }
209                         else
210                                 tint[0] = tint[1] = tint[2] = 1;
211                         if (r_shadow_realtime_world.integer && !fullbright)
212                                 VectorScale(tint, r_shadow_realtime_world_lightmaps.value, tint);
213                         colorscale *= ifog;
214                         if (fullbright)
215                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
216                         else
217                         {
218                                 if (R_LightModel(ambientcolor4f, diffusecolor, diffusenormal, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
219                                 {
220                                         m.pointer_color = varray_color4f;
221                                         if (generatenormals)
222                                         {
223                                                 generatenormals = false;
224                                                 Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, varray_vertex3f, mesh->data_texcoord2f, mesh->data_element3i, NULL, NULL, varray_normal3f);
225                                         }
226                                         R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, varray_vertex3f, varray_normal3f, varray_color4f);
227                                 }
228                                 else
229                                         GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
230                         }
231                 }
232                 R_Mesh_State(&m);
233                 GL_LockArrays(0, mesh->num_vertices);
234                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
235                 GL_LockArrays(0, 0);
236         }
237 }
238
239 void R_Model_Alias_Draw(entity_render_t *ent)
240 {
241         int meshnum;
242         aliasmesh_t *mesh;
243         if (ent->alpha < (1.0f / 64.0f))
244                 return; // basically completely transparent
245
246         c_models++;
247
248         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
249         {
250                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
251                         R_MeshQueue_AddTransparent(ent->origin, R_DrawAliasModelCallback, ent, meshnum);
252                 else
253                         R_DrawAliasModelCallback(ent, meshnum);
254         }
255 }
256
257 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
258 {
259         int meshnum;
260         aliasmesh_t *mesh;
261         aliasskin_t *skin;
262         float projectdistance;
263         if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
264                 return;
265         projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
266         if (projectdistance > 0.1)
267         {
268                 R_Mesh_Matrix(&ent->matrix);
269                 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
270                 {
271                         skin = R_FetchAliasSkin(ent, mesh);
272                         if (skin->flags & ALIASSKIN_TRANSPARENT)
273                                 continue;
274                         R_Model_Alias_GetMesh_Vertex3f(ent, mesh, varray_vertex3f);
275                         R_Shadow_VolumeFromSphere(mesh->num_vertices, mesh->num_triangles, varray_vertex3f, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, projectdistance, lightradius);
276                 }
277         }
278 }
279
280 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)
281 {
282         int c, meshnum, layernum;
283         float fog, ifog, lightcolor2[3];
284         vec3_t diff;
285         qbyte *bcolor;
286         aliasmesh_t *mesh;
287         aliaslayer_t *layer;
288         aliasskin_t *skin;
289
290         if (ent->effects & (EF_ADDITIVE | EF_FULLBRIGHT) || ent->alpha < 1)
291                 return;
292
293         R_Mesh_Matrix(&ent->matrix);
294
295         fog = 0;
296         if (fogenabled)
297         {
298                 VectorSubtract(ent->origin, r_vieworigin, diff);
299                 fog = DotProduct(diff,diff);
300                 if (fog < 0.01f)
301                         fog = 0.01f;
302                 fog = exp(fogdensity/fog);
303                 if (fog > 1)
304                         fog = 1;
305                 if (fog < 0.01f)
306                         fog = 0;
307                 // fog method: darken, additive fog
308                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
309                 // 2. render fog as additive
310         }
311         ifog = 1 - fog;
312
313         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
314         {
315                 skin = R_FetchAliasSkin(ent, mesh);
316                 if (skin->flags & ALIASSKIN_TRANSPARENT)
317                         continue;
318                 R_Model_Alias_GetMesh_Vertex3f(ent, mesh, varray_vertex3f);
319                 Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, varray_vertex3f, mesh->data_texcoord2f, mesh->data_element3i, varray_svector3f, varray_tvector3f, varray_normal3f);
320                 for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
321                 {
322                         if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
323                          || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
324                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
325                                 continue;
326                         lightcolor2[0] = lightcolor[0] * ifog;
327                         lightcolor2[1] = lightcolor[1] * ifog;
328                         lightcolor2[2] = lightcolor[2] * ifog;
329                         if (layer->flags & ALIASLAYER_SPECULAR)
330                         {
331                                 c_alias_polys += mesh->num_triangles;
332                                 R_Shadow_RenderLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, varray_vertex3f, varray_svector3f, varray_tvector3f, varray_normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor2, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, layer->texture, lightcubemap, LIGHTING_SPECULAR);
333                         }
334                         else if (layer->flags & ALIASLAYER_DIFFUSE)
335                         {
336                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
337                                 {
338                                         // 128-224 are backwards ranges
339                                         c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
340                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
341                                         if (c >= 224)
342                                                 continue;
343                                         bcolor = (qbyte *) (&palette_complete[c]);
344                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
345                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
346                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
347                                 }
348                                 else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
349                                 {
350                                         // 128-224 are backwards ranges
351                                         c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
352                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
353                                         if (c >= 224)
354                                                 continue;
355                                         bcolor = (qbyte *) (&palette_complete[c]);
356                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
357                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
358                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
359                                 }
360                                 c_alias_polys += mesh->num_triangles;
361                                 R_Shadow_RenderLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, varray_vertex3f, varray_svector3f, varray_tvector3f, varray_normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor2, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, layer->texture, lightcubemap, LIGHTING_DIFFUSE);
362                         }
363                 }
364         }
365 }
366