Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void R_BuildFrameNormals (aliashdr_t *hdr, trivertx_t *verts, dtriangle_t *triangles, vertexnormals_t *vnorms)
- {
- for (int i = 0; i < hdr->numverts; i++)
- {
- // no normals initially
- Vector3Clear (vnorms[i].normal);
- vnorms[i].numnormals = 0;
- }
- for (int i = 0; i < hdr->numtris; i++)
- {
- float triverts[3][3];
- float vtemp1[3], vtemp2[3], normal[3];
- int *vertindexes = triangles[i].vertindex;
- // undo the vertex rotation from modelgen.c here too
- for (int j = 0; j < 3; j++)
- {
- triverts[j][0] = (float) verts[vertindexes[j]].v[1] * hdr->scale[1] + hdr->scale_origin[1];
- triverts[j][1] = -((float) verts[vertindexes[j]].v[0] * hdr->scale[0] + hdr->scale_origin[0]);
- triverts[j][2] = (float) verts[vertindexes[j]].v[2] * hdr->scale[2] + hdr->scale_origin[2];
- }
- // calc the per-triangle normal
- Vector3Subtract (vtemp1, triverts[0], triverts[1]);
- Vector3Subtract (vtemp2, triverts[2], triverts[1]);
- Vector3Cross (normal, vtemp1, vtemp2);
- Vector3Normalize (normal);
- // rotate the normal so the model faces down the positive x axis
- float newnormal[3] = {-normal[1], normal[0], normal[2]};
- // and accumulate it into the calculated normals array
- for (int j = 0; j < 3; j++)
- {
- Vector3Add (vnorms[vertindexes[j]].normal, vnorms[vertindexes[j]].normal, newnormal);
- vnorms[vertindexes[j]].numnormals++;
- }
- }
- // copy out normals
- for (int i = 0; i < hdr->numverts; i++)
- {
- // numnormals was checked for > 0 in modelgen.c so we shouldn't need to do it again
- // here but we do anyway just in case a rogue modder has used a bad modelling tool
- if (vnorms[i].numnormals > 0)
- {
- Vector3Scalef (vnorms[i].normal, vnorms[i].normal, (float) vnorms[i].numnormals);
- Vector3Normalize (vnorms[i].normal);
- }
- else Vector3Set (vnorms[i].normal, 0.0f, 0.0f, 1.0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement