]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_models.c
-Fixed a wrong parameter type in netconn.
[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         if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
71         {
72                 vertex3f = mesh->data_basevertex3f;
73                 normal3f = mesh->data_basenormal3f;
74         }
75         else
76         {
77                 vertex3f = varray_vertex3f;
78                 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
79                 normal3f = NULL;
80         }
81         for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
82         {
83                 if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
84                 {
85                         if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
86                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
87                          || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
88                          ||  (layer->flags & ALIASLAYER_SPECULAR)
89                          || ((layer->flags & ALIASLAYER_DIFFUSE) && (ent->flags & RENDER_LIGHT) && r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT) && r_ambient.integer <= 0))
90                                 continue;
91                 }
92                 if (!firstpass || (ent->effects & EF_ADDITIVE))
93                 {
94                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
95                         GL_DepthMask(false);
96                 }
97                 else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
98                 {
99                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
100                         GL_DepthMask(false);
101                 }
102                 else
103                 {
104                         GL_BlendFunc(GL_ONE, GL_ZERO);
105                         GL_DepthMask(true);
106                 }
107                 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
108                 firstpass = false;
109                 colorscale = 1.0f;
110
111                 memset(&m, 0, sizeof(m));
112                 if (layer->texture != NULL)
113                 {
114                         m.tex[0] = R_GetTexture(layer->texture);
115                         m.pointer_texcoord[0] = mesh->data_texcoord2f;
116                         if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
117                         {
118                                 colorscale *= 0.25f;
119                                 m.texrgbscale[0] = 4;
120                         }
121                 }
122                 m.pointer_vertex = vertex3f;
123
124                 c_alias_polys += mesh->num_triangles;
125                 if (layer->flags & ALIASLAYER_FOG)
126                 {
127                         colorscale *= fog;
128                         GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
129                 }
130                 else
131                 {
132                         fullbright = !(layer->flags & ALIASLAYER_DIFFUSE) || !(ent->flags & RENDER_LIGHT);
133                         if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
134                         {
135                                 // 128-224 are backwards ranges
136                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
137                                         c = (ent->colormap & 0xF) << 4;
138                                 else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
139                                         c = (ent->colormap & 0xF0);
140                                 c += (c >= 128 && c < 224) ? 4 : 12;
141                                 bcolor = (qbyte *) (&palette_complete[c]);
142                                 fullbright = fullbright || c >= 224;
143                                 VectorScale(bcolor, (1.0f / 255.0f), tint);
144                         }
145                         else
146                                 tint[0] = tint[1] = tint[2] = 1;
147                         tint[0] *= ent->colormod[0];
148                         tint[1] *= ent->colormod[1];
149                         tint[2] *= ent->colormod[2];
150                         if (!fullbright && !(ent->flags & RENDER_TRANSPARENT))
151                                 colorscale *= r_lightmapintensity;
152                         colorscale *= ifog;
153                         if (fullbright)
154                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
155                         else if (R_LightModel(ambientcolor4f, diffusecolor, diffusenormal, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
156                         {
157                                 m.pointer_color = varray_color4f;
158                                 if (normal3f == NULL)
159                                 {
160                                         normal3f = varray_normal3f;
161                                         Mod_BuildNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, normal3f);
162                                 }
163                                 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, vertex3f, normal3f, varray_color4f);
164                         }
165                         else
166                                 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
167                 }
168                 R_Mesh_State(&m);
169                 GL_LockArrays(0, mesh->num_vertices);
170                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
171                 GL_LockArrays(0, 0);
172         }
173 }
174
175 void R_Model_Alias_Draw(entity_render_t *ent)
176 {
177         int meshnum;
178         aliasmesh_t *mesh;
179         if (ent->alpha < (1.0f / 64.0f))
180                 return; // basically completely transparent
181
182         c_models++;
183
184         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
185         {
186                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
187                         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : ent->origin, R_DrawAliasModelCallback, ent, meshnum);
188                 else
189                         R_DrawAliasModelCallback(ent, meshnum);
190         }
191 }
192
193 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
194 {
195         int meshnum;
196         vec3_t lightmins, lightmaxs;
197         aliasmesh_t *mesh;
198         aliasskin_t *skin;
199         float projectdistance, *vertex3f;
200         if (!(ent->flags & RENDER_SHADOW))
201                 return;
202         lightmins[0] = relativelightorigin[0] - lightradius;
203         lightmins[1] = relativelightorigin[1] - lightradius;
204         lightmins[2] = relativelightorigin[2] - lightradius;
205         lightmaxs[0] = relativelightorigin[0] + lightradius;
206         lightmaxs[1] = relativelightorigin[1] + lightradius;
207         lightmaxs[2] = relativelightorigin[2] + lightradius;
208         // check the box in modelspace, it was already checked in worldspace
209         if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
210                 return;
211         projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
212         if (projectdistance > 0.1)
213         {
214                 R_Mesh_Matrix(&ent->matrix);
215                 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
216                 {
217                         skin = R_FetchAliasSkin(ent, mesh);
218                         if (skin->flags & ALIASSKIN_TRANSPARENT)
219                                 continue;
220                         if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
221                                 vertex3f = mesh->data_basevertex3f;
222                         else
223                         {
224                                 vertex3f = varray_vertex3f;
225                                 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
226                         }
227                         // identify lit faces within the bounding box
228                         R_Shadow_PrepareShadowMark(mesh->num_triangles);
229                         R_Shadow_MarkVolumeFromBox(0, mesh->num_triangles, vertex3f, mesh->data_element3i, relativelightorigin, lightmins, lightmaxs, ent->model->normalmins, ent->model->normalmaxs);
230                         R_Shadow_VolumeFromList(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
231                 }
232         }
233 }
234
235 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)
236 {
237         int c, meshnum, layernum;
238         float fog, ifog, lightcolor2[3];
239         float *vertex3f, *svector3f, *tvector3f, *normal3f;
240         vec3_t diff;
241         qbyte *bcolor;
242         aliasmesh_t *mesh;
243         aliaslayer_t *layer;
244         aliasskin_t *skin;
245
246         R_Mesh_Matrix(&ent->matrix);
247
248         fog = 0;
249         if (fogenabled)
250         {
251                 VectorSubtract(ent->origin, r_vieworigin, diff);
252                 fog = DotProduct(diff,diff);
253                 if (fog < 0.01f)
254                         fog = 0.01f;
255                 fog = exp(fogdensity/fog);
256                 if (fog > 1)
257                         fog = 1;
258                 if (fog < 0.01f)
259                         fog = 0;
260                 // fog method: darken, additive fog
261                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
262                 // 2. render fog as additive
263         }
264         ifog = 1 - fog;
265
266         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
267         {
268                 skin = R_FetchAliasSkin(ent, mesh);
269                 // FIXME: transparent skins need to be lit during the transparent render
270                 if (skin->flags & ALIASSKIN_TRANSPARENT)
271                         continue;
272                 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
273                 {
274                         vertex3f = mesh->data_basevertex3f;
275                         svector3f = mesh->data_basesvector3f;
276                         tvector3f = mesh->data_basetvector3f;
277                         normal3f = mesh->data_basenormal3f;
278                 }
279                 else
280                 {
281                         vertex3f = varray_vertex3f;
282                         svector3f = varray_svector3f;
283                         tvector3f = varray_tvector3f;
284                         normal3f = varray_normal3f;
285                         Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
286                         Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_texcoord2f, mesh->data_element3i, svector3f, tvector3f, normal3f);
287                 }
288                 for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
289                 {
290                         if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
291                          || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
292                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
293                                 continue;
294                         lightcolor2[0] = lightcolor[0] * ifog;
295                         lightcolor2[1] = lightcolor[1] * ifog;
296                         lightcolor2[2] = lightcolor[2] * ifog;
297                         if (layer->flags & ALIASLAYER_SPECULAR)
298                         {
299                                 c_alias_polys += mesh->num_triangles;
300                                 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, 0, 0, specularscale);
301                         }
302                         else if (layer->flags & ALIASLAYER_DIFFUSE)
303                         {
304                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
305                                 {
306                                         // 128-224 are backwards ranges
307                                         c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
308                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
309                                         if (c >= 224)
310                                                 continue;
311                                         bcolor = (qbyte *) (&palette_complete[c]);
312                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
313                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
314                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
315                                 }
316                                 else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
317                                 {
318                                         // 128-224 are backwards ranges
319                                         c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
320                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
321                                         if (c >= 224)
322                                                 continue;
323                                         bcolor = (qbyte *) (&palette_complete[c]);
324                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
325                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
326                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
327                                 }
328                                 c_alias_polys += mesh->num_triangles;
329                                 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, ambientscale, diffusescale, 0);
330                         }
331                 }
332         }
333 }
334