4 // LordHavoc: vertex array
12 void makechrometexture()
18 fractalnoise(noise, 64);
20 // convert to RGBA data
21 for (i = 0;i < 64*64;i++)
23 data[i][0] = data[i][1] = data[i][2] = noise[i];
27 chrometexture = GL_LoadTexture ("chrometexture", 64, 64, &data[0][0], true, false, 4);
30 void gl_models_start()
32 // allocate vertex processing arrays
33 aliasvert = malloc(sizeof(float[MD2MAX_VERTS][3]));
34 aliasvertnorm = malloc(sizeof(float[MD2MAX_VERTS][3]));
35 aliasvertcolor = malloc(sizeof(byte[MD2MAX_VERTS][4]));
36 aliasvertcolor2 = malloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
40 void gl_models_shutdown()
45 free(aliasvertcolor2);
50 R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown);
53 extern vec3_t softwaretransform_x;
54 extern vec3_t softwaretransform_y;
55 extern vec3_t softwaretransform_z;
56 extern vec_t softwaretransform_scale;
57 extern vec3_t softwaretransform_offset;
58 void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1, vec3_t translate1, trivert2 *verts2, vec3_t scale2, vec3_t translate2)
61 vec3_t point, matrix_x, matrix_y, matrix_z;
65 VectorScale(softwaretransform_x, softwaretransform_scale, matrix_x);
66 VectorScale(softwaretransform_y, softwaretransform_scale, matrix_y);
67 VectorScale(softwaretransform_z, softwaretransform_scale, matrix_z);
68 if (lerp < 0) lerp = 0;
69 if (lerp > 1) lerp = 1;
72 float ilerp, ilerp127, lerp127, scalex1, scalex2, translatex, scaley1, scaley2, translatey, scalez1, scalez2, translatez;
73 if (lerp < 0) lerp = 0;
74 if (lerp > 1) lerp = 1;
76 ilerp127 = ilerp * (1.0 / 127.0);
77 lerp127 = lerp * (1.0 / 127.0);
78 // calculate combined interpolation variables
79 scalex1 = scale1[0] * ilerp;scalex2 = scale2[0] * lerp;translatex = translate1[0] * ilerp + translate2[0] * lerp;
80 scaley1 = scale1[1] * ilerp;scaley2 = scale2[1] * lerp;translatey = translate1[1] * ilerp + translate2[1] * lerp;
81 scalez1 = scale1[2] * ilerp;scalez2 = scale2[2] * lerp;translatez = translate1[2] * ilerp + translate2[2] * lerp;
83 for (i = 0;i < vertcount;i++)
85 // rotate, scale, and translate the vertex locations
86 point[0] = verts1->v[0] * scalex1 + verts2->v[0] * scalex2 + translatex;
87 point[1] = verts1->v[1] * scaley1 + verts2->v[1] * scaley2 + translatey;
88 point[2] = verts1->v[2] * scalez1 + verts2->v[2] * scalez2 + translatez;
89 *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
90 *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
91 *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
93 point[0] = verts1->n[0] * ilerp127 + verts2->n[0] * lerp127;
94 point[1] = verts1->n[1] * ilerp127 + verts2->n[1] * lerp127;
95 point[2] = verts1->n[2] * ilerp127 + verts2->n[2] * lerp127;
96 *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
97 *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
98 *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
105 for (i = 0;i < vertcount;i++)
107 // rotate, scale, and translate the vertex locations
108 point[0] = verts1->v[0] * scale1[0] + translate1[0];
109 point[1] = verts1->v[1] * scale1[1] + translate1[1];
110 point[2] = verts1->v[2] * scale1[2] + translate1[2];
111 *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
112 *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
113 *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
114 // rotate the normals
115 point[0] = verts1->n[0] * (1.0f / 127.0f);
116 point[1] = verts1->n[1] * (1.0f / 127.0f);
117 point[2] = verts1->n[2] * (1.0f / 127.0f);
118 *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
119 *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
120 *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
126 float R_CalcAnimLerp(entity_t *ent, int pose, float lerpscale)
128 if (ent->draw_lastmodel == ent->model && ent->draw_lerpstart <= cl.time)
130 if (pose != ent->draw_pose)
132 ent->draw_lastpose = ent->draw_pose;
133 ent->draw_pose = pose;
134 ent->draw_lerpstart = cl.time;
138 return ((cl.time - ent->draw_lerpstart) * lerpscale);
140 else // uninitialized
142 ent->draw_lastmodel = ent->model;
143 ent->draw_lastpose = ent->draw_pose = pose;
144 ent->draw_lerpstart = cl.time;
149 extern cvar_t gl_vertexarrays;
150 extern qboolean lighthalf;
151 void GL_DrawModelMesh(int skin, byte *colors, maliashdr_t *maliashdr)
154 glBindTexture(GL_TEXTURE_2D, skin);
158 glColor3f(0.5f, 0.5f, 0.5f);
160 glColor3f(1.0f, 1.0f, 1.0f);
162 if (gl_vertexarrays.value)
166 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
167 glEnableClientState(GL_COLOR_ARRAY);
170 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
171 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
173 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
175 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
178 glDisableClientState(GL_COLOR_ARRAY);
182 unsigned short *in, index;
184 in = (void *)((int) maliashdr + maliashdr->tridata);
185 glBegin(GL_TRIANGLES);
186 tex = (void *)((int) maliashdr + maliashdr->texdata);
187 for (i = 0;i < maliashdr->numtris * 3;i++)
190 glTexCoord2f(tex[index*2], tex[index*2+1]);
192 glColor4f(colors[index*4] * (1.0f / 255.0f), colors[index*4+1] * (1.0f / 255.0f), colors[index*4+2] * (1.0f / 255.0f), colors[index*4+3] * (1.0f / 255.0f));
193 glVertex3fv(&aliasvert[index*3]);
197 // leave it in a state for additional passes
200 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
203 void R_TintModel(byte *in, byte *out, int verts, byte *color)
209 for (i = 0;i < verts;i++)
211 out[0] = (byte) ((in[0] * r) >> 8);
212 out[1] = (byte) ((in[1] * g) >> 8);
213 out[2] = (byte) ((in[2] * b) >> 8);
226 extern vec3_t lightspot;
227 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
228 void R_DrawAliasFrame (maliashdr_t *maliashdr, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, int frame, int *skin, int colormap, int effects, int flags)
231 float lerpscale, lerp;
232 maliasframe_t *frameinfo;
234 softwaretransformforentity(ent);
236 if ((frame >= maliashdr->numframes) || (frame < 0))
238 Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
242 frameinfo = ((maliasframe_t *)((int) maliashdr + maliashdr->framedata)) + frame;
243 pose = frameinfo->start;
245 if (frameinfo->length > 1)
247 lerpscale = frameinfo->rate;
248 pose += (int)(cl.time * frameinfo->rate) % frameinfo->length;
253 lerp = R_CalcAnimLerp(ent, pose, lerpscale);
255 R_AliasLerpVerts(maliashdr->numverts, lerp, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_lastpose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_pose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin);
257 // prep the vertex array as early as possible
258 if (gl_vertexarrays.value)
260 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
261 glEnableClientState(GL_VERTEX_ARRAY);
264 R_LightModel(maliashdr->numverts, org, color);
266 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
267 glShadeModel(GL_SMOOTH);
268 if (effects & EF_ADDITIVE)
270 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
274 else if (alpha != 1.0)
276 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
288 if (!skin[0] && !skin[1] && !skin[2] && !skin[3])
289 GL_DrawModelMesh(0, NULL, maliashdr);
294 GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
297 c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
298 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
299 GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
303 c = colormap & 0xF0 ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
304 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
305 GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
307 if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
312 if (!skin[3] && !skin[4])
313 GL_DrawModelMesh(0, NULL, maliashdr);
316 if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
317 if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
324 glDisable (GL_TEXTURE_2D);
325 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
327 glDepthMask(0); // disable zbuffer updates
329 VectorSubtract(org, r_refdef.vieworg, diff);
330 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
332 if (gl_vertexarrays.value)
334 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
339 in = (void *)((int) maliashdr + maliashdr->tridata);
340 glBegin(GL_TRIANGLES);
341 for (i = 0;i < maliashdr->numtris * 3;i++)
342 glVertex3fv(&aliasvert[*in++ * 3]);
346 glEnable (GL_TEXTURE_2D);
349 if (gl_vertexarrays.value)
350 glDisableClientState(GL_VERTEX_ARRAY);
352 if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
354 // flatten it to make a shadow
355 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
357 for (i = 0;i < maliashdr->numverts;i++, av+=3)
360 glDisable (GL_TEXTURE_2D);
361 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
363 glDepthMask(0); // disable zbuffer updates
364 glColor4f (0,0,0,0.5 * alpha);
366 if (gl_vertexarrays.value)
368 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
369 glEnableClientState(GL_VERTEX_ARRAY);
370 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
371 glDisableClientState(GL_VERTEX_ARRAY);
376 in = (void *)((int) maliashdr + maliashdr->tridata);
377 glBegin(GL_TRIANGLES);
378 for (i = 0;i < maliashdr->numtris * 3;i++)
379 glVertex3fv(&aliasvert[*in++ * 3]);
383 glEnable (GL_TEXTURE_2D);
387 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
398 void R_DrawQ2AliasFrame (md2mem_t *pheader, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, int frame, int skin, int effects, int flags)
402 md2memframe_t *frame1, *frame2;
404 glBindTexture(GL_TEXTURE_2D, skin);
406 softwaretransformforentity(ent);
408 if ((frame >= pheader->num_frames) || (frame < 0))
410 Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
414 lerp = R_CalcAnimLerp(ent, frame, 10);
416 frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_lastpose));
417 frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_pose));
418 R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
420 R_LightModel(pheader->num_xyz, org, color);
422 if (gl_vertexarrays.value)
424 // LordHavoc: big mess...
425 // using arrays only slightly, although it is enough to prevent duplicates
426 // (saving half the transforms)
427 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
428 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
429 glEnableClientState(GL_VERTEX_ARRAY);
430 glEnableClientState(GL_COLOR_ARRAY);
432 order = (int *)((int)pheader + pheader->ofs_glcmds);
435 if (!(count = *order++))
438 glBegin(GL_TRIANGLE_STRIP);
441 glBegin(GL_TRIANGLE_FAN);
446 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
447 qglArrayElement(order[2]);
453 glDisableClientState(GL_COLOR_ARRAY);
454 glDisableClientState(GL_VERTEX_ARRAY);
458 order = (int *)((int)pheader + pheader->ofs_glcmds);
461 if (!(count = *order++))
464 glBegin(GL_TRIANGLE_STRIP);
467 glBegin(GL_TRIANGLE_FAN);
472 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
473 glColor4f(aliasvertcolor[order[2] * 4] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 1] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 2] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 3] * (1.0f / 255.0f));
474 glVertex3fv(&aliasvert[order[2] * 3]);
483 glDisable (GL_TEXTURE_2D);
484 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
486 glDepthMask(0); // disable zbuffer updates
489 VectorSubtract(org, r_refdef.vieworg, diff);
490 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
493 if (gl_vertexarrays.value)
495 // LordHavoc: big mess...
496 // using arrays only slightly, although it is enough to prevent duplicates
497 // (saving half the transforms)
498 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
499 glEnableClientState(GL_VERTEX_ARRAY);
501 order = (int *)((int)pheader + pheader->ofs_glcmds);
504 if (!(count = *order++))
507 glBegin(GL_TRIANGLE_STRIP);
510 glBegin(GL_TRIANGLE_FAN);
515 qglArrayElement(order[2]);
521 glDisableClientState(GL_VERTEX_ARRAY);
525 order = (int *)((int)pheader + pheader->ofs_glcmds);
528 if (!(count = *order++))
531 glBegin(GL_TRIANGLE_STRIP);
534 glBegin(GL_TRIANGLE_FAN);
539 glVertex3fv(&aliasvert[order[2] * 3]);
546 glEnable (GL_TEXTURE_2D);
550 if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
553 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
555 for (i = 0;i < pheader->num_xyz;i++, av+=3)
558 glDisable (GL_TEXTURE_2D);
559 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
561 glDepthMask(0); // disable zbuffer updates
562 glColor4f (0,0,0,0.5 * alpha);
564 if (gl_vertexarrays.value)
566 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
567 glEnableClientState(GL_VERTEX_ARRAY);
571 if (!(count = *order++))
574 glBegin(GL_TRIANGLE_STRIP);
577 glBegin(GL_TRIANGLE_FAN);
582 qglArrayElement(order[2]);
588 glDisableClientState(GL_VERTEX_ARRAY);
594 if (!(count = *order++))
597 glBegin(GL_TRIANGLE_STRIP);
600 glBegin(GL_TRIANGLE_FAN);
605 glVertex3fv(&aliasvert[order[2] * 3]);
612 glEnable (GL_TEXTURE_2D);
616 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
621 int modeldlightbits[8];
622 extern int r_dlightframecount;
623 extern void R_LightPoint (vec3_t color, vec3_t p);
631 void R_DrawAliasModel (entity_t *ent, int cull, float alpha, model_t *clmodel, int frame, int skin, vec3_t org, int effects, int flags, int colormap)
634 vec3_t mins, maxs, color;
639 if (alpha < (1.0 / 64.0))
640 return; // basically completely transparent
642 VectorAdd (org, clmodel->mins, mins);
643 VectorAdd (org, clmodel->maxs, maxs);
645 if (cull && R_CullBox (mins, maxs))
648 leaf = Mod_PointInLeaf (org, cl.worldmodel);
649 if (leaf->dlightframe == r_dlightframecount)
650 for (i = 0;i < 8;i++)
651 modeldlightbits[i] = leaf->dlightbits[i];
653 for (i = 0;i < 8;i++)
654 modeldlightbits[i] = 0;
656 // get lighting information
658 if ((flags & EF_FULLBRIGHT) || (effects & EF_FULLBRIGHT))
659 color[0] = color[1] = color[2] = 256;
662 // HACK HACK HACK -- no fullbright colors, so make torches full light
663 if (!strcmp (clmodel->name, "progs/flame2.mdl") || !strcmp (clmodel->name, "progs/flame.mdl") )
664 color[0] = color[1] = color[2] = 128;
666 R_LightPoint (color, org);
669 glDisable(GL_ALPHA_TEST);
671 if (frame < 0 || frame >= clmodel->numframes)
674 Con_DPrintf("invalid skin number %d for model %s\n", frame, clmodel->name);
677 if (skin < 0 || skin >= clmodel->numskins)
680 Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
683 modelheader = Mod_Extradata (clmodel);
686 // int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
687 // int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
688 int *skinanimrange = clmodel->skinanimrange + skin * 2;
689 int *skinanim = clmodel->skinanim;
690 i = skinanimrange[0];
691 if (skinanimrange[1] > 1) // animated
692 i += ((int) (cl.time * 10) % skinanimrange[1]);
693 skinset = skinanim + i*5;
696 glEnable (GL_TEXTURE_2D);
698 c_alias_polys += clmodel->numtris;
699 if (clmodel->aliastype == ALIASTYPE_MD2)
700 R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset[0], effects, flags);
702 R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset, colormap, effects, flags);