5 void GL_Models_Init(void)
9 static texture_t r_aliasnotexture;
10 static texture_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
12 model_t *model = ent->model;
16 if ((unsigned int)s >= (unsigned int)model->numskins)
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;
21 s = model->skinscenes[s].firstframe;
22 if (s >= mesh->num_skins)
24 return mesh->data_skins + s;
28 memset(&r_aliasnotexture, 0, sizeof(r_aliasnotexture));
29 r_aliasnotexture.skin.base = r_texture_notexture;
30 return &r_aliasnotexture;
34 static void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
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;
42 const entity_render_t *ent = calldata1;
43 aliasmesh_t *mesh = ent->model->alias.aliasdata_meshes + calldata2;
46 R_Mesh_Matrix(&ent->matrix);
51 VectorSubtract(ent->origin, r_vieworigin, diff);
52 fog = DotProduct(diff,diff);
55 fog = exp(fogdensity/fog);
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
66 VectorScale(ent->colormod, ifog, colorbase);
67 VectorClear(colorpants);
68 VectorClear(colorshirt);
69 fbbase = ent->effects & EF_FULLBRIGHT;
72 if (ent->colormap >= 0)
74 // 128-224 are backwards ranges
75 c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
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;
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);
92 texture = R_FetchAliasSkin(ent, mesh);
94 if ((ent->effects & EF_ADDITIVE))
96 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
99 else if (texture->skin.fog || ent->alpha != 1.0)
101 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106 GL_BlendFunc(GL_ONE, GL_ZERO);
109 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
111 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
113 vertex3f = mesh->data_basevertex3f;
114 normal3f = mesh->data_basenormal3f;
118 vertex3f = varray_vertex3f;
119 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
123 doglow = texture->skin.glow != NULL;
125 memset(&m, 0, sizeof(m));
126 m.pointer_vertex = vertex3f;
127 m.pointer_texcoord[0] = mesh->data_texcoord2f;
128 if (gl_combine.integer)
131 m.texrgbscale[0] = 4;
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;
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))
141 m.pointer_color = varray_color4f;
142 if (normal3f == NULL)
144 normal3f = varray_normal3f;
145 Mod_BuildNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, normal3f);
147 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, vertex3f, normal3f, varray_color4f);
150 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
151 if (gl_combine.integer && doglow)
154 m.tex[1] = R_GetTexture(texture->skin.glow);
155 m.pointer_texcoord[1] = mesh->data_texcoord2f;
156 m.texcombinergb[1] = GL_ADD;
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);
164 m.pointer_texcoord[1] = NULL;
165 m.texcombinergb[1] = 0;
167 VectorScale(colorpants, colorscale, tint);
168 if (ent->colormap >= 0 && texture->skin.pants && VectorLength2(tint) >= 0.001)
170 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
172 m.tex[0] = R_GetTexture(texture->skin.pants);
173 m.pointer_color = NULL;
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))
178 m.pointer_color = varray_color4f;
179 if (normal3f == NULL)
181 normal3f = varray_normal3f;
182 Mod_BuildNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, normal3f);
184 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, vertex3f, normal3f, varray_color4f);
187 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
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);
195 VectorScale(colorshirt, colorscale, tint);
196 if (ent->colormap >= 0 && texture->skin.shirt && VectorLength2(tint) >= 0.001)
198 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
200 m.tex[0] = R_GetTexture(texture->skin.shirt);
201 m.pointer_color = NULL;
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))
206 m.pointer_color = varray_color4f;
207 if (normal3f == NULL)
209 normal3f = varray_normal3f;
210 Mod_BuildNormals(mesh->num_vertices, mesh->num_triangles, vertex3f, mesh->data_element3i, normal3f);
212 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, vertex3f, normal3f, varray_color4f);
215 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
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);
224 m.texrgbscale[0] = 0;
226 if (texture->skin.glow)
228 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
230 m.tex[0] = R_GetTexture(texture->skin.glow);
231 m.pointer_color = NULL;
232 GL_Color(1, 1, 1, ent->alpha);
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);
242 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
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);
248 c_alias_polys += mesh->num_triangles;
249 GL_LockArrays(0, mesh->num_vertices);
250 R_Mesh_Draw(0, mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
255 void R_Model_Alias_Draw(entity_render_t *ent)
259 if (ent->alpha < (1.0f / 64.0f))
260 return; // basically completely transparent
264 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
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);
269 R_DrawAliasModelCallback(ent, meshnum);
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)
278 float projectdistance, *vertex3f;
279 if (!(ent->flags & RENDER_SHADOW))
281 // check the box in modelspace, it was already checked in worldspace
282 if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
284 projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
285 if (projectdistance > 0.1)
287 R_Mesh_Matrix(&ent->matrix);
288 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
290 texture = R_FetchAliasSkin(ent, mesh);
291 if (texture->skin.fog)
293 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
294 vertex3f = mesh->data_basevertex3f;
297 vertex3f = varray_vertex3f;
298 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
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);
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)
311 float fog, ifog, lightcolorbase[3], lightcolorpants[3], lightcolorshirt[3];
312 float *vertex3f, *svector3f, *tvector3f, *normal3f;
318 R_Mesh_Matrix(&ent->matrix);
323 VectorSubtract(ent->origin, r_vieworigin, diff);
324 fog = DotProduct(diff,diff);
327 fog = exp(fogdensity/fog);
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
338 VectorScale(lightcolor, ifog, lightcolorbase);
339 if (VectorLength2(lightcolorbase) < 0.001)
341 VectorClear(lightcolorpants);
342 VectorClear(lightcolorshirt);
343 if (ent->colormap >= 0)
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
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);
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
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);
367 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
369 texture = R_FetchAliasSkin(ent, mesh);
370 // FIXME: transparent skins need to be lit during the transparent render
371 if (texture->skin.fog)
373 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
375 vertex3f = mesh->data_basevertex3f;
376 svector3f = mesh->data_basesvector3f;
377 tvector3f = mesh->data_basetvector3f;
378 normal3f = mesh->data_basenormal3f;
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);
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)
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);