4 // LordHavoc: vertex array
12 void makechrometexture()
18 fractalnoise(noise, 64, 16);
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)
156 glBindTexture(GL_TEXTURE_2D, skin);
160 glColor3f(0.5f, 0.5f, 0.5f);
162 glColor3f(1.0f, 1.0f, 1.0f);
164 if (gl_vertexarrays.value)
168 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
169 glEnableClientState(GL_COLOR_ARRAY);
172 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
173 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
175 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
177 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
180 glDisableClientState(GL_COLOR_ARRAY);
184 unsigned short *in, index;
186 in = (void *)((int) maliashdr + maliashdr->tridata);
187 glBegin(GL_TRIANGLES);
188 tex = (void *)((int) maliashdr + maliashdr->texdata);
189 for (i = 0;i < maliashdr->numtris * 3;i++)
192 glTexCoord2f(tex[index*2], tex[index*2+1]);
194 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));
195 glVertex3fv(&aliasvert[index*3]);
199 // leave it in a state for additional passes
202 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
205 void R_TintModel(byte *in, byte *out, int verts, byte *color)
211 for (i = 0;i < verts;i++)
213 out[0] = (byte) ((in[0] * r) >> 8);
214 out[1] = (byte) ((in[1] * g) >> 8);
215 out[2] = (byte) ((in[2] * b) >> 8);
228 extern vec3_t lightspot;
229 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
230 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)
233 float lerpscale, lerp;
234 maliasframe_t *frameinfo;
236 softwaretransformforentity(ent);
238 if ((frame >= maliashdr->numframes) || (frame < 0))
240 Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
244 frameinfo = ((maliasframe_t *)((int) maliashdr + maliashdr->framedata)) + frame;
245 pose = frameinfo->start;
247 if (frameinfo->length > 1)
249 lerpscale = frameinfo->rate;
250 pose += (int)(cl.time * frameinfo->rate) % frameinfo->length;
255 lerp = R_CalcAnimLerp(ent, pose, lerpscale);
257 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);
259 // prep the vertex array as early as possible
262 if (gl_vertexarrays.value)
264 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
265 glEnableClientState(GL_VERTEX_ARRAY);
269 R_LightModel(maliashdr->numverts, org, color);
273 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
274 glShadeModel(GL_SMOOTH);
275 if (effects & EF_ADDITIVE)
277 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
281 else if (alpha != 1.0)
283 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
295 if (!skin[0] && !skin[1] && !skin[2] && !skin[3])
296 GL_DrawModelMesh(0, NULL, maliashdr);
301 GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
304 c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
305 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
306 GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
310 c = colormap & 0xF0 ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
311 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
312 GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
314 if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
319 if (!skin[3] && !skin[4])
320 GL_DrawModelMesh(0, NULL, maliashdr);
323 if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
324 if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
331 glDisable (GL_TEXTURE_2D);
332 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
334 glDepthMask(0); // disable zbuffer updates
336 VectorSubtract(org, r_refdef.vieworg, diff);
337 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
339 if (gl_vertexarrays.value)
341 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
346 in = (void *)((int) maliashdr + maliashdr->tridata);
347 glBegin(GL_TRIANGLES);
348 for (i = 0;i < maliashdr->numtris * 3;i++)
349 glVertex3fv(&aliasvert[*in++ * 3]);
353 glEnable (GL_TEXTURE_2D);
356 if (gl_vertexarrays.value)
357 glDisableClientState(GL_VERTEX_ARRAY);
359 if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
361 // flatten it to make a shadow
362 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
364 for (i = 0;i < maliashdr->numverts;i++, av+=3)
367 glDisable (GL_TEXTURE_2D);
368 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
370 glDepthMask(0); // disable zbuffer updates
371 glColor4f (0,0,0,0.5 * alpha);
373 if (gl_vertexarrays.value)
375 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
376 glEnableClientState(GL_VERTEX_ARRAY);
377 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
378 glDisableClientState(GL_VERTEX_ARRAY);
383 in = (void *)((int) maliashdr + maliashdr->tridata);
384 glBegin(GL_TRIANGLES);
385 for (i = 0;i < maliashdr->numtris * 3;i++)
386 glVertex3fv(&aliasvert[*in++ * 3]);
390 glEnable (GL_TEXTURE_2D);
394 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
405 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)
409 md2memframe_t *frame1, *frame2;
412 glBindTexture(GL_TEXTURE_2D, skin);
414 softwaretransformforentity(ent);
416 if ((frame >= pheader->num_frames) || (frame < 0))
418 Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
422 lerp = R_CalcAnimLerp(ent, frame, 10);
424 frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_lastpose));
425 frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_pose));
426 R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
428 R_LightModel(pheader->num_xyz, org, color);
432 if (gl_vertexarrays.value)
434 // LordHavoc: big mess...
435 // using arrays only slightly, although it is enough to prevent duplicates
436 // (saving half the transforms)
437 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
438 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
439 glEnableClientState(GL_VERTEX_ARRAY);
440 glEnableClientState(GL_COLOR_ARRAY);
442 order = (int *)((int)pheader + pheader->ofs_glcmds);
445 if (!(count = *order++))
448 glBegin(GL_TRIANGLE_STRIP);
451 glBegin(GL_TRIANGLE_FAN);
456 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
457 qglArrayElement(order[2]);
463 glDisableClientState(GL_COLOR_ARRAY);
464 glDisableClientState(GL_VERTEX_ARRAY);
468 order = (int *)((int)pheader + pheader->ofs_glcmds);
471 if (!(count = *order++))
474 glBegin(GL_TRIANGLE_STRIP);
477 glBegin(GL_TRIANGLE_FAN);
482 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
483 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));
484 glVertex3fv(&aliasvert[order[2] * 3]);
493 glDisable (GL_TEXTURE_2D);
494 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
496 glDepthMask(0); // disable zbuffer updates
499 VectorSubtract(org, r_refdef.vieworg, diff);
500 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
503 if (gl_vertexarrays.value)
505 // LordHavoc: big mess...
506 // using arrays only slightly, although it is enough to prevent duplicates
507 // (saving half the transforms)
508 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
509 glEnableClientState(GL_VERTEX_ARRAY);
511 order = (int *)((int)pheader + pheader->ofs_glcmds);
514 if (!(count = *order++))
517 glBegin(GL_TRIANGLE_STRIP);
520 glBegin(GL_TRIANGLE_FAN);
525 qglArrayElement(order[2]);
531 glDisableClientState(GL_VERTEX_ARRAY);
535 order = (int *)((int)pheader + pheader->ofs_glcmds);
538 if (!(count = *order++))
541 glBegin(GL_TRIANGLE_STRIP);
544 glBegin(GL_TRIANGLE_FAN);
549 glVertex3fv(&aliasvert[order[2] * 3]);
556 glEnable (GL_TEXTURE_2D);
560 if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
563 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
565 for (i = 0;i < pheader->num_xyz;i++, av+=3)
568 glDisable (GL_TEXTURE_2D);
569 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
571 glDepthMask(0); // disable zbuffer updates
572 glColor4f (0,0,0,0.5 * alpha);
574 if (gl_vertexarrays.value)
576 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
577 glEnableClientState(GL_VERTEX_ARRAY);
581 if (!(count = *order++))
584 glBegin(GL_TRIANGLE_STRIP);
587 glBegin(GL_TRIANGLE_FAN);
592 qglArrayElement(order[2]);
598 glDisableClientState(GL_VERTEX_ARRAY);
604 if (!(count = *order++))
607 glBegin(GL_TRIANGLE_STRIP);
610 glBegin(GL_TRIANGLE_FAN);
615 glVertex3fv(&aliasvert[order[2] * 3]);
622 glEnable (GL_TEXTURE_2D);
626 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
631 int modeldlightbits[8];
632 extern int r_dlightframecount;
633 extern void R_LightPoint (vec3_t color, vec3_t p);
641 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)
644 vec3_t mins, maxs, color;
649 if (alpha < (1.0 / 64.0))
650 return; // basically completely transparent
652 VectorAdd (org, clmodel->mins, mins);
653 VectorAdd (org, clmodel->maxs, maxs);
655 if (cull && R_CullBox (mins, maxs))
658 leaf = Mod_PointInLeaf (org, cl.worldmodel);
659 if (leaf->dlightframe == r_dlightframecount)
660 for (i = 0;i < 8;i++)
661 modeldlightbits[i] = leaf->dlightbits[i];
663 for (i = 0;i < 8;i++)
664 modeldlightbits[i] = 0;
666 // get lighting information
668 if ((flags & EF_FULLBRIGHT) || (effects & EF_FULLBRIGHT))
669 color[0] = color[1] = color[2] = 256;
671 R_LightPoint (color, org);
674 glDisable(GL_ALPHA_TEST);
676 if (frame < 0 || frame >= clmodel->numframes)
679 Con_DPrintf("invalid skin number %d for model %s\n", frame, clmodel->name);
682 if (skin < 0 || skin >= clmodel->numskins)
685 Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
688 modelheader = Mod_Extradata (clmodel);
691 // int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
692 // int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
693 int *skinanimrange = clmodel->skinanimrange + skin * 2;
694 int *skinanim = clmodel->skinanim;
695 i = skinanimrange[0];
696 if (skinanimrange[1] > 1) // animated
697 i += ((int) (cl.time * 10) % skinanimrange[1]);
698 skinset = skinanim + i*5;
702 glEnable (GL_TEXTURE_2D);
704 c_alias_polys += clmodel->numtris;
705 if (clmodel->aliastype == ALIASTYPE_MD2)
706 R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset[0], effects, flags);
708 R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset, colormap, effects, flags);