File: cs_178.html

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (425 lines) | stat: -rw-r--r-- 11,819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Created by texi2html 1.64 -->
<!-- 
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>
 
-->
<HTML>
<HEAD>
<TITLE>Crystal Space: Texture Mapping Together</TITLE>

<META NAME="description" CONTENT="Crystal Space: Texture Mapping Together">
<META NAME="keywords" CONTENT="Crystal Space: Texture Mapping Together">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.64">

</HEAD>

<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">

<A NAME="SEC384"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_177.html#SEC383"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_179.html#SEC385"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_173.html#SEC376"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_174.html#SEC377"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_181.html#SEC390"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<HR SIZE=1>
<H4> 7.6.6.4 Bringing it All Together </H4>
<!--docid::SEC384::-->
<P>

Using all this information we have enough to correctly map a texture on
screen.  Let's disregard clipping for the moment and just explain all the
steps from the original object space polygon until the final texture mapped
polygon on screen.
</P><P>

We will assume that the polygon (and the texture) has already been transformed
from object to world space.  So we start with a world space polygon, <EM>Pw</EM>.
</P><P>

First all vertices of the polygon are transformed to camera space (note that
in Crystal Space this is done earlier since vertices are shared for one
sector.  This text ignores that and just concentrates on one polygon) with the
equation:
</P><P>

<BLOCKQUOTE>
<EM>Vc = Mwc * (Vw - Vwc)</EM>
</BLOCKQUOTE>
<P>

(Also note that at this point you could discard vertices because they are
behind the view plane (or <EM>Z = 0</EM>).  We assume here that the polygon is
completely visible so this does not matter.)
</P><P>

At this point we do perspective correction on the polygon.  This means that we
create a new 2-dimensional polygon with vertices <EM>Vs</EM> (in screen space)
using the following equations:
</P><P>

<BLOCKQUOTE>
<EM>Vs.x = (F * Vc.x) / Vc.z</EM><BR>
<EM>Vs.y = (F * Vc.y) / Vc.z</EM>
</BLOCKQUOTE>
<P>

Now we create the matrix to transform camera space to texture space starting
from the matrix to transform world space to texture space.  Given:
</P><P>

<BLOCKQUOTE>
<EM>Vc = Mwc * (Vw - Vwc)</EM>
</BLOCKQUOTE>
<P>

We calculate (using the inverse matrix of <EM>Mwc</EM>):
</P><P>

<BLOCKQUOTE>
<TABLE>
<TR><TD><EM>Mcw * Vc + Vwc = Vw</EM> </TD><TD> (Equation 1)</TD>
</TR></TABLE>
</BLOCKQUOTE>
<P>

Given also:
</P><P>

<BLOCKQUOTE>
<TABLE>
<TR><TD><EM>Vt = Mwt * (Vw - Vwt)</EM> </TD><TD> (Equation 2)</TD>
</TR></TABLE>
</BLOCKQUOTE>
<P>

We substitute (1) into (2) to get:
</P><P>

<BLOCKQUOTE>
<EM>Vt = Mwt * (Mcw * Vc + Vwc - Vwt)</EM>
</BLOCKQUOTE>
<P>

This can also be re-written as:
</P><P>

<BLOCKQUOTE>
<EM>Vt = Mwt * (Mcw * Vc + Mcw * Mwc * (Vwc - Vwt))</EM>
</BLOCKQUOTE>
<P>

Which simplifies to:
</P><P>

<BLOCKQUOTE>
<EM>Vt = Mwt * Mcw * (Vc + Mwc * (Vwc - Vwt))</EM>
</BLOCKQUOTE>
<P>

If we say that:
</P><P>

<BLOCKQUOTE>
<EM>Mct = Mwt * Mcw</EM><BR>
<EM>Vct = Mwc * (Vwt - Vwc)</EM>
</BLOCKQUOTE>
<P>

We then get:
</P><P>

<BLOCKQUOTE>
<EM>Vt = Mct * (Vc - Vct)</EM>
</BLOCKQUOTE>
<P>

And this is the equation transforming camera space to texture space.
</P><P>

Then we need to transform the world space plane equation to a camera space
plane equation.  This we do as follows.
</P><P>

The plane vector
<EM>Nw = (Aw,Bw,Cw)</EM>
is transformed to
<EM>Nc = (Ac,Bc,Cc)</EM>
using the following equation:
</P><P>

<BLOCKQUOTE>
<EM>Nc = Mwc * Nw</EM>
</BLOCKQUOTE>
<P>

Using the first vertex of the polygon in camera space coordinates (<EM>Vc</EM>)
we then calculate <EM>Dc</EM> as follows.
</P><P>

Since the plane equation in camera space is equal to:
</P><P>

<BLOCKQUOTE>
<EM>Ac * Vc.x + Bc * Vc.y + Cc * Vc.z + Dc = 0</EM>
</BLOCKQUOTE>
<P>

For every vertex <EM>Vc</EM> on the polygon we can calculate the missing <EM>Dc</EM>
as follows:
</P><P>

<BLOCKQUOTE>
<EM>Dc = -Ac * Vc.x - Bc * Vc.y - Cc * Vc.z</EM>
</BLOCKQUOTE>
<P>

Using this information (the polygon in perspective corrected 2D coordinates,
the transformation from camera space to texture space and the plane equation
in camera space) we can draw the polygon on the screen and perform correct
texture mapping.  This happens as follows.
</P><P>

From the perspective correction equations:
</P><P>

<BLOCKQUOTE>
<EM>Vs.x = (F * Vc.x) / Vc.z</EM><BR>
<EM>Vs.y = (F * Vc.y) / Vc.z</EM>
</BLOCKQUOTE>
<P>

We can invert them to:
</P><P>

<BLOCKQUOTE>
<TABLE>
<TR><TD><EM>Vc.x = (Vs.x * Vc.z) / F</EM> </TD><TD> (Equation 3)</TD>
</TR>
<TR><TD><EM>Vc.y = (Vs.y * Vc.z) / F</EM> </TD><TD> (Equation 4)</TD>
</TR></TABLE>
</BLOCKQUOTE>
<P>

We can now substitute (3) and (4) into the following equation:
</P><P>

<BLOCKQUOTE>
<EM>Ac * Vc.x + Bc * Vc.y + Cc * Vc.z + Dc = 0</EM>
</BLOCKQUOTE>
<P>

And get:
</P><P>

<BLOCKQUOTE>
<EM>(Ac * Vs.x * Vc.z) / F + (Bc * Vs.y * Vc.z) / F + (F * Cc * Vc.z) / F = -Dc</EM>
</BLOCKQUOTE>
<P>

Or:
</P><P>

<BLOCKQUOTE>
<EM>-(Ac * Vs.x) / (F*Dc) - (Bc * Vs.y) / (F*Dc) - Cc / (F*Dc) = 1 / Vc.z</EM>
</BLOCKQUOTE>
<P>

This equation is very important.  From this it follows that <EM>1/z</EM> linear
is in screen space and this can be used for perspective correct texture
mapping.  Lets define the following three new variables:
</P><P>

<BLOCKQUOTE>
<EM>M = -Ac / (F * Dc)</EM><BR>
<EM>N = -Bc / (F * Dc)</EM><BR>
<EM>O = -Cc / Dc</EM>
</BLOCKQUOTE>
<P>

So the <EM>1/z</EM> equation in linear screen space is then written as:
</P><P>

<BLOCKQUOTE>
<TABLE>
<TR><TD><EM>1 / Vc.z = M * Vs.x + N * Vs.y + O</EM> </TD><TD> (Equation 5)</TD>
</TR></TABLE>
</BLOCKQUOTE>
<P>

So now we can easily calculate <EM>1/z</EM> at every point in screen space.  But
we also need to calculate the texture coordinates (<EM>u,v</EM>) or <EM>Vt</EM>.
Let's call the individual fields of the transformation matrix <EM>Mct</EM> as
follows:
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>      / m11 m12 m13 \
Mct = | m21 m22 m23 |   Vct = (v1 v2 v3)
      \ m31 m32 m33 /
</pre></td></tr></table></P><P>

For simplicity let's use <EM>u</EM> for <EM>Vt.u</EM> and <EM>v</EM> for <EM>Vt.v</EM>
(the <EM>u,v</EM> texture coordinates).  Let us also use <EM>x</EM>, <EM>y</EM>,
and <EM>z</EM> for <EM>Vc.x</EM>, <EM>Vc.y</EM>, <EM>Vc.z</EM> respectively.
</P><P>

Then from:
</P><P>

<BLOCKQUOTE>
<EM>Vt = Mct * (Vc - Vct)</EM>
</BLOCKQUOTE>
<P>

We get:
</P><P>

<BLOCKQUOTE>
<EM>u = m11 * (x - v1) + m12 * (y - v2) + m13 * (z - v3)</EM><BR>
<EM>v = m21 * (x - v1) + m22 * (y - v2) + m23 * (z - v3)</EM>
</BLOCKQUOTE>
<P>

This can be rewritten as:
</P><P>

<BLOCKQUOTE>
<EM>u = m11 * x + m12 * y + m13 * z - (m11 * v1 + m12 * v2 + m13 * v3)</EM><BR>
<EM>v = m21 * x + m22 * y + m23 * z - (m21 * v1 + m22 * v2 + m23 * v3)</EM>
</BLOCKQUOTE>
<P>

To simplify let's introduce a couple new variables to take the place of
complicated expressions from the above equations.
</P><P>

<BLOCKQUOTE>
<EM>P = - (m11 * v1 + m12 * v2 + m13 * v3)</EM><BR>
<EM>Q = - (m21 * v1 + m22 * v2 + m23 * v3)</EM>
</BLOCKQUOTE>
<P>

And we have:
</P><P>

<BLOCKQUOTE>
<EM>u = m11 * x + m12 * y + m13 * z + P</EM><BR>
<EM>v = m21 * x + m22 * y + m23 * z + Q</EM>
</BLOCKQUOTE>
<P>

As earlier, we substitute the inverse perspective correction equations (3)
and (4) into the previous equations and we get:
</P><P>

<BLOCKQUOTE>
<EM>u = (m11 * Vs.x * z) / F + (m12 * Vs.y * z) / F + m13 * z + P</EM><BR>
<EM>v = (m21 * Vs.x * z) / F + (m22 * Vs.y * z) / F + m23 * z + Q</EM>
</BLOCKQUOTE>
<P>

And then rewrite as:
</P><P>

<BLOCKQUOTE>
<EM>u / z = (m11 * Vs.x) / F + (m12 * Vs.y) / F + m13 + P / z</EM><BR>
<EM>v / z = (m21 * Vs.x) / V + (m22 * Vs.y) / F + m23 + Q / z</EM>
</BLOCKQUOTE>
<P>

Substitute the linear <EM>1/z</EM> equation (5) into this to get:
</P><P>

<BLOCKQUOTE>
<EM>u / z = (m11 * Vs.x) / F + (m12 * Vs.y) / F + m13 + P * (M * Vs.x + N * Vs.y + O)</EM><BR>
<EM>v / z = (m21 * Vs.x) / F + (m22 * Vs.y) / F + m23 + Q * (M * Vs.x + N * Vs.y + O)</EM>
</BLOCKQUOTE>
<P>

Rewrite as:
</P><P>

<BLOCKQUOTE>
<EM>u / z = (m11 * Vs.x + m12 * Vs.y + F * (m13 + P * (M*Vs.x + N*Vs.y + O)) / F</EM><BR>
<EM>v / z = (m21 * Vs.x + m22 * Vs.y + F * (m23 + Q * (M*Vs.x + N*Vs.y + O)) / F</EM>
</BLOCKQUOTE>
<P>

And finally rewrite as:
</P><P>

<BLOCKQUOTE>
<EM>u / z = (m11 / F + P * M) * Vs.x + (m12 / F + P * N) * Vs.y + (m13 + P * O)</EM><BR>
<EM>v / z = (m21 / F + Q * M) * Vs.x + (m22 / F + Q * N) * Vs.y + (m23 + Q * O)</EM>
</BLOCKQUOTE>
<P>

These are again two important equations because they state that <EM>u/z</EM> and
<EM>v/z</EM> are also linear in screen space.  Using this we can easily
calculate (<EM>u,v</EM>) at every screen space point.
</P><P>

Now let's define:
</P><P>

<BLOCKQUOTE>
<EM>J1 = m11 / F + P * M</EM><BR>
<EM>J2 = m12 / F + P * N</EM><BR>
<EM>J3 = m13 + P * O</EM><BR>
<EM>K1 = m21 / F + Q * M</EM><BR>
<EM>K2 = m22 / F + Q * N</EM><BR>
<EM>K3 = m23 + Q * O</EM>
</BLOCKQUOTE>
<P>

Then we have the following three equations:
</P><P>

<BLOCKQUOTE>
<EM>1 / z = M * Vs.x + N * Vs.y + O</EM><BR>
<EM>u / z = J1 * Vs.x + J2 * Vs.y + J3</EM><BR>
<EM>v / z = K1 * Vs.x + K2 * Vs.y + K3</EM>
</BLOCKQUOTE>
<P>

With these three important equations we can do all texture mapping we want.
With the first equation we can calculate <EM>1/z</EM>.  This is useful for
Z-buffering and also for calculating (<EM>u,v</EM>) from the two other
equations.
</P><P>

<A NAME="Texture Mapping Instructions"></A>
<HR SIZE=1>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_177.html#SEC383"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_179.html#SEC385"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_173.html#SEC376"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_174.html#SEC377"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_181.html#SEC390"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>  
<FONT SIZE="-1">
This document was generated

using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html
"><I>texi2html</I></A>

</BODY>
</HTML>