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(0, 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(0, 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(0, 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;
225 m.pointer_color = NULL;
229 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
231 m.tex[0] = R_GetTexture(texture->skin.glow);
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 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], fog * ent->alpha);
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);
254 void R_Model_Alias_Draw(entity_render_t *ent)
258 if (ent->alpha < (1.0f / 64.0f))
259 return; // basically completely transparent
263 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
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);
268 R_DrawAliasModelCallback(ent, meshnum);
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)
277 float projectdistance, *vertex3f;
278 if (!(ent->flags & RENDER_SHADOW))
280 // check the box in modelspace, it was already checked in worldspace
281 if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
283 projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
284 if (projectdistance > 0.1)
286 R_Mesh_Matrix(&ent->matrix);
287 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
289 texture = R_FetchAliasSkin(ent, mesh);
290 if (texture->skin.fog)
292 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
293 vertex3f = mesh->data_basevertex3f;
296 vertex3f = varray_vertex3f;
297 Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, mesh, vertex3f);
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);
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)
310 float fog, ifog, lightcolorbase[3], lightcolorpants[3], lightcolorshirt[3];
311 float *vertex3f, *svector3f, *tvector3f, *normal3f;
317 R_Mesh_Matrix(&ent->matrix);
322 VectorSubtract(ent->origin, r_vieworigin, diff);
323 fog = DotProduct(diff,diff);
326 fog = exp(fogdensity/fog);
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
337 VectorScale(lightcolor, ifog, lightcolorbase);
338 if (VectorLength2(lightcolorbase) < 0.001)
340 VectorClear(lightcolorpants);
341 VectorClear(lightcolorshirt);
342 if (ent->colormap >= 0)
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
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);
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
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);
366 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
368 texture = R_FetchAliasSkin(ent, mesh);
369 // FIXME: transparent skins need to be lit during the transparent render
370 if (texture->skin.fog)
372 if (ent->frameblend[0].frame == 0 && ent->frameblend[0].lerp == 1)
374 vertex3f = mesh->data_basevertex3f;
375 svector3f = mesh->data_basesvector3f;
376 tvector3f = mesh->data_basetvector3f;
377 normal3f = mesh->data_basenormal3f;
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);
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);