]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_models.c
changed crosshair_static default to 1
[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 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(0, 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(0, 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(0, 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(0, 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(0, 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(0, 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         m.pointer_color = NULL;
226
227         if (doglow)
228         {
229                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
230                 GL_DepthMask(false);
231                 m.tex[0] = R_GetTexture(texture->skin.glow);
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(0, 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                 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], fog * ent->alpha);
246                 R_Mesh_State(&m);
247                 c_alias_polys += mesh->num_triangles;
248                 GL_LockArrays(0, mesh->num_vertices);
249                 R_Mesh_Draw(0, mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
250                 GL_LockArrays(0, 0);
251         }
252 }
253
254 void R_Model_Alias_Draw(entity_render_t *ent)
255 {
256         int meshnum;
257         aliasmesh_t *mesh;
258         if (ent->alpha < (1.0f / 64.0f))
259                 return; // basically completely transparent
260
261         c_models++;
262
263         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
264         {
265                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->skin.fog)
266                         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : ent->origin, R_DrawAliasModelCallback, ent, meshnum);
267                 else
268                         R_DrawAliasModelCallback(ent, meshnum);
269         }
270 }
271
272 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)
273 {
274         int meshnum;
275         aliasmesh_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                 R_Mesh_Matrix(&ent->matrix);
287                 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
288                 {
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_basevertex3f;
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, 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)
308 {
309         int c, meshnum;
310         float fog, ifog, lightcolorbase[3], lightcolorpants[3], lightcolorshirt[3];
311         float *vertex3f, *svector3f, *tvector3f, *normal3f;
312         vec3_t diff;
313         qbyte *bcolor;
314         aliasmesh_t *mesh;
315         texture_t *texture;
316
317         R_Mesh_Matrix(&ent->matrix);
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 (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
367         {
368                 texture = R_FetchAliasSkin(ent, mesh);
369                 // FIXME: transparent skins need to be lit during the transparent render
370                 if (texture->skin.fog)
371                         continue;
372                 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
373                 {
374                         vertex3f = mesh->data_basevertex3f;
375                         svector3f = mesh->data_basesvector3f;
376                         tvector3f = mesh->data_basetvector3f;
377                         normal3f = mesh->data_basenormal3f;
378                 }
379                 else
380                 {
381                         vertex3f = varray_vertex3f;
382                         svector3f = varray_svector3f;
383                         tvector3f = varray_tvector3f;
384                         normal3f = varray_normal3f;
385                         Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
386                         Mod_BuildTextureVectorsAndNormals(0, mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_texcoord2f, mesh->data_element3i, svector3f, tvector3f, normal3f);
387                 }
388                 c_alias_polys += mesh->num_triangles;
389                 R_Shadow_RenderLighting(0, mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, vertex3f, svector3f, tvector3f, normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightcolorbase, lightcolorpants, lightcolorshirt, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, (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, lightcubemap, ambientscale, diffusescale, specularscale);
390         }
391 }
392