Debian Codegraph @

editor/RoomUVs.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/RoomUVs.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Code to deal with texturing rooms
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 9 9/30/99 6:35p Matt
30 * Added code to scale the UVs of a texture based on the size of the
31 * texture when the texture is applied to a face. This will cause the
32 * applied texture to have the make 3D texel size at the old texture.
33 *
34 * 8 4/25/99 2:38p Gwar
35 * brought in HRoom.cpp and RoomUVs.cpp, and several misc game functions
36 * were added to globals.cpp to make it possible
37 *
38 * 7 10/21/98 1:27p Jason
39 * fixed texture u2,v2 sliding problems
40 *
41 * 6 10/07/98 3:19p Matt
42 * Cleaned up and fixed Mike's old texture UV calculation code. Who ever
43 * told that boy he could do 3D?
44 *
45 * 5 4/02/98 12:23p Jason
46 * trimmed some fat from our structures
47 *
48 * 4 12/23/97 1:33p Samir
49 * Added pserror.h
50 *
51 * 3 9/17/97 11:57a Matt
52 * Ripped out segment code
53 *
54 * 2 7/18/97 7:38p Jason
55 * finished room/terrain texture modifications
56 *
57 * $NoKeywords: $
58 */
59
60 #include "RoomUVs.h"
61
62 #ifndef NEWEDITOR
63 #include "d3edit.h"
64 #else
65 #include "..\neweditor\globals.h"
66 #endif
67
68 #include "pserror.h"
69
70 // returns the magnatude of the 2d vector <a,b>
71 static float zhypot(float a, float b) { return sqrt(a * a + b * b); }
72
73 // Given u,v coordinates at two vertices, assign u,v coordinates to the other vertices on a face.
74 // va, vb = face-relative vertex indices corresponding to uva, uvb. Ie, they are always in 0..num_verts_in_face
75 void AssignUVsToFace(room *rp, int facenum, roomUVL *uva, roomUVL *uvb, int va, int vb) {
76 face *fp = &rp->faces[facenum];
77 int nv = fp->num_verts;
78 int vlo, vhi;
79 vector fvec, rvec, tvec;
80 roomUVL ruvmag, fuvmag, uvlo, uvhi;
81 float fmag, mag01;
82 int i;
83
84 float saveu2[MAX_VERTS_PER_FACE], savev2[MAX_VERTS_PER_FACE];
85 for (i = 0; i < fp->num_verts; i++) {
86 saveu2[i] = fp->face_uvls[i].u2;
87 savev2[i] = fp->face_uvls[i].v2;
88 }
89
90 ASSERT((va < nv) && (vb < nv));
91 ASSERT((abs(va - vb) == 1) || (abs(va - vb) == nv - 1)); // make sure the verticies specify an edge
92
93 // We want vlo precedes vhi, ie vlo < vhi, or vlo = num_verts, vhi = 0
94 if (va == ((vb + 1) % nv)) { // va = vb + 1
95 vlo = vb;
96 vhi = va;
97 uvlo = *uvb;
98 uvhi = *uva;
99 } else {
100 vlo = va;
101 vhi = vb;
102 uvlo = *uva;
103 uvhi = *uvb;
104 }
105
106 ASSERT(((vlo + 1) % nv) == vhi); // If we are on an edge, then uvhi is one more than uvlo (mod num_verts)
107 fp->face_uvls[vlo] = uvlo;
108 fp->face_uvls[vhi] = uvhi;
109
110 // Now we have vlo precedes vhi, compute vertices ((vhi+1) % nv) and ((vhi+2) % nv)
111
112 // Assign u,v scale to a unit length right vector.
113 fmag = zhypot(uvhi.v - uvlo.v, uvhi.u - uvlo.u);
114 if (fmag < 0.001) {
115 // mprintf(0,"Warning: fmag = %7.3f, using approximate u,v values\n",f2fl(fmag));
116 ruvmag.u = 256.0;
117 ruvmag.v = 256.0;
118 fuvmag.u = 256.0;
119 fuvmag.v = 256.0;
120 } else {
121 ruvmag.u = uvhi.v - uvlo.v;
122 ruvmag.v = uvlo.u - uvhi.u;
123
124 fuvmag.u = uvhi.u - uvlo.u;
125 fuvmag.v = uvhi.v - uvlo.v;
126 }
127
128 // Get pointers to our verts
129 vector *vv0 = &rp->verts[fp->face_verts[vlo]], *vv1 = &rp->verts[fp->face_verts[vhi]];
130
131 // Get forward vector from our edge
132 fvec = *vv1 - *vv0;
133 mag01 = vm_NormalizeVector(&fvec);
134
135 // Check for bad vector
136 if (mag01 < 0.001) {
137 OutrageMessageBox("U, V bogosity in room #%i, probably on face #%i. CLEAN UP YOUR MESS!", ROOMNUM(rp), facenum);
138 return;
139 }
140
141 // Get right vector from the cross product of the forward vec with the surface normal
142 rvec = fvec ^ fp->normal;
143
144 // Normalize uv values
145 ruvmag.u /= mag01;
146 ruvmag.v /= mag01;
147 fuvmag.u /= mag01;
148 fuvmag.v /= mag01;
149
150 // Compute UVs for each point
151 for (i = 1; i < nv - 1; i++) {
152 int fv = (vhi + i) % nv; // vert index in face
153 int rv = fp->face_verts[fv]; // vert index in room
154
155 // Get the vector for this edge
156 tvec = rp->verts[rv] - *vv0;
157
158 // Project the current edge onto our forward & right vectors
159 float rproj = tvec * rvec, fproj = tvec * fvec;
160
161 // Compute and assign UV values
162 fp->face_uvls[fv].u = uvlo.u + (ruvmag.u * rproj) + (fuvmag.u * fproj);
163 fp->face_uvls[fv].v = uvlo.v + (ruvmag.v * rproj) + (fuvmag.v * fproj);
164 }
165 }
166
167 // Stretches the UVS of a face
168 void StretchRoomUVs(room *rp, int facenum, int edge) {
169 roomUVL uv0, uv1;
170 int v0, v1;
171 face *fp = &rp->faces[facenum];
172
173 int i;
174 float saveu2[MAX_VERTS_PER_FACE], savev2[MAX_VERTS_PER_FACE];
175 for (i = 0; i < fp->num_verts; i++) {
176 saveu2[i] = fp->face_uvls[i].u2;
177 savev2[i] = fp->face_uvls[i].v2;
178 }
179
180 v0 = edge;
181 v1 = (v0 + 1) % rp->faces[facenum].num_verts;
182
183 uv0 = rp->faces[facenum].face_uvls[v0]; // copy uv AND l
184 uv1 = rp->faces[facenum].face_uvls[v1];
185
186 AssignUVsToFace(rp, facenum, &uv0, &uv1, v0, v1);
187
188 for (i = 0; i < fp->num_verts; i++) {
189 fp->face_uvls[i].u2 = saveu2[i];
190 fp->face_uvls[i].v2 = savev2[i];
191 }
192 }
193
194 // Scale all the UV values in a face from the center point (as defined by averaging the u & v values)
195 void ScaleFaceUVs(room *rp, int facenum, float scale) {
196 face *fp = &rp->faces[facenum];
197
198 int i;
199 float midu = 0, midv = 0;
200
201 for (i = 0; i < fp->num_verts; i++) {
202 midu += fp->face_uvls[i].u;
203 midv += fp->face_uvls[i].v;
204 }
205
206 midu /= fp->num_verts;
207 midv /= fp->num_verts;
208
209 for (i = 0; i < fp->num_verts; i++) {
210 fp->face_uvls[i].u = midu + (fp->face_uvls[i].u - midu) * scale;
211 fp->face_uvls[i].v = midv + (fp->face_uvls[i].v - midv) * scale;
212 }
213
214 World_changed = 1;
215 }