aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp367
1 files changed, 367 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp
new file mode 100644
index 0000000..ea47277
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/CTRTextureGouraudNoZ.cpp
@@ -0,0 +1,367 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "IrrCompileConfig.h"
6#include "CTRTextureGouraud.h"
7#include "SColor.h"
8
9#ifdef _IRR_COMPILE_WITH_SOFTWARE_
10
11namespace irr
12{
13namespace video
14{
15
16class CTRTextureGouraudNoZ : public CTRTextureGouraud
17{
18public:
19
20 CTRTextureGouraudNoZ()
21 : CTRTextureGouraud(0)
22 {
23 #ifdef _DEBUG
24 setDebugName("CTRGouraudWireNoZ");
25 #endif
26 }
27
28 //! draws an indexed triangle list
29 virtual void drawIndexedTriangleList(S2DVertex* vertices, s32 vertexCount, const u16* indexList, s32 triangleCount)
30 {
31 const S2DVertex *v1, *v2, *v3;
32
33 u16 color;
34 f32 tmpDiv; // temporary division factor
35 f32 longest; // saves the longest span
36 s32 height; // saves height of triangle
37 u16* targetSurface; // target pointer where to plot pixels
38 s32 spanEnd; // saves end of spans
39 f32 leftdeltaxf; // amount of pixels to increase on left side of triangle
40 f32 rightdeltaxf; // amount of pixels to increase on right side of triangle
41 s32 leftx, rightx; // position where we are
42 f32 leftxf, rightxf; // same as above, but as f32 values
43 s32 span; // current span
44 u16 *hSpanBegin, *hSpanEnd; // pointer used when plotting pixels
45 s32 leftR, leftG, leftB, rightR, rightG, rightB; // color values
46 s32 leftStepR, leftStepG, leftStepB,
47 rightStepR, rightStepG, rightStepB; // color steps
48 s32 spanR, spanG, spanB, spanStepR, spanStepG, spanStepB; // color interpolating values while drawing a span.
49 s32 leftTx, rightTx, leftTy, rightTy; // texture interpolating values
50 s32 leftTxStep, rightTxStep, leftTyStep, rightTyStep; // texture interpolating values
51 s32 spanTx, spanTy, spanTxStep, spanTyStep; // values of Texturecoords when drawing a span
52 core::rect<s32> TriangleRect;
53
54 lockedSurface = (u16*)RenderTarget->lock();
55 lockedTexture = (u16*)Texture->lock();
56
57 for (s32 i=0; i<triangleCount; ++i)
58 {
59 v1 = &vertices[*indexList];
60 ++indexList;
61 v2 = &vertices[*indexList];
62 ++indexList;
63 v3 = &vertices[*indexList];
64 ++indexList;
65
66 // back face culling
67
68 if (BackFaceCullingEnabled)
69 {
70 s32 z = ((v3->Pos.X - v1->Pos.X) * (v3->Pos.Y - v2->Pos.Y)) -
71 ((v3->Pos.Y - v1->Pos.Y) * (v3->Pos.X - v2->Pos.X));
72
73 if (z < 0)
74 continue;
75 }
76
77 //near plane clipping
78
79 if (v1->ZValue<0 && v2->ZValue<0 && v3->ZValue<0)
80 continue;
81
82 // sort for width for inscreen clipping
83
84 if (v1->Pos.X > v2->Pos.X) swapVertices(&v1, &v2);
85 if (v1->Pos.X > v3->Pos.X) swapVertices(&v1, &v3);
86 if (v2->Pos.X > v3->Pos.X) swapVertices(&v2, &v3);
87
88 if ((v1->Pos.X - v3->Pos.X) == 0)
89 continue;
90
91 TriangleRect.UpperLeftCorner.X = v1->Pos.X;
92 TriangleRect.LowerRightCorner.X = v3->Pos.X;
93
94 // sort for height for faster drawing.
95
96 if (v1->Pos.Y > v2->Pos.Y) swapVertices(&v1, &v2);
97 if (v1->Pos.Y > v3->Pos.Y) swapVertices(&v1, &v3);
98 if (v2->Pos.Y > v3->Pos.Y) swapVertices(&v2, &v3);
99
100 TriangleRect.UpperLeftCorner.Y = v1->Pos.Y;
101 TriangleRect.LowerRightCorner.Y = v3->Pos.Y;
102
103 if (!TriangleRect.isRectCollided(ViewPortRect))
104 continue;
105
106 // calculate height of triangle
107 height = v3->Pos.Y - v1->Pos.Y;
108 if (!height)
109 continue;
110
111 // calculate longest span
112
113 longest = (v2->Pos.Y - v1->Pos.Y) / (f32)height * (v3->Pos.X - v1->Pos.X) + (v1->Pos.X - v2->Pos.X);
114
115 spanEnd = v2->Pos.Y;
116 span = v1->Pos.Y;
117 leftxf = (f32)v1->Pos.X;
118 rightxf = (f32)v1->Pos.X;
119
120 leftR = rightR = video::getRed(v1->Color)<<8;
121 leftG = rightG = video::getGreen(v1->Color)<<8;
122 leftB = rightB = video::getBlue(v1->Color)<<8;
123 leftTx = rightTx = v1->TCoords.X;
124 leftTy = rightTy = v1->TCoords.Y;
125
126 targetSurface = lockedSurface + span * SurfaceWidth;
127
128 if (longest < 0.0f)
129 {
130 tmpDiv = 1.0f / (f32)(v2->Pos.Y - v1->Pos.Y);
131 rightdeltaxf = (v2->Pos.X - v1->Pos.X) * tmpDiv;
132 rightStepR = (s32)(((s32)(video::getRed(v2->Color)<<8) - rightR) * tmpDiv);
133 rightStepG = (s32)(((s32)(video::getGreen(v2->Color)<<8) - rightG) * tmpDiv);
134 rightStepB = (s32)(((s32)(video::getBlue(v2->Color)<<8) - rightB) * tmpDiv);
135 rightTxStep = (s32)((v2->TCoords.X - rightTx) * tmpDiv);
136 rightTyStep = (s32)((v2->TCoords.Y - rightTy) * tmpDiv);
137
138 tmpDiv = 1.0f / (f32)height;
139 leftdeltaxf = (v3->Pos.X - v1->Pos.X) * tmpDiv;
140 leftStepR = (s32)(((s32)(video::getRed(v3->Color)<<8) - leftR) * tmpDiv);
141 leftStepG = (s32)(((s32)(video::getGreen(v3->Color)<<8) - leftG) * tmpDiv);
142 leftStepB = (s32)(((s32)(video::getBlue(v3->Color)<<8) - leftB) * tmpDiv);
143 leftTxStep = (s32)((v3->TCoords.X - leftTx) * tmpDiv);
144 leftTyStep = (s32)((v3->TCoords.Y - leftTy) * tmpDiv);
145 }
146 else
147 {
148 tmpDiv = 1.0f / (f32)height;
149 rightdeltaxf = (v3->Pos.X - v1->Pos.X) * tmpDiv;
150 rightStepR = (s32)(((s32)(video::getRed(v3->Color)<<8) - rightR) * tmpDiv);
151 rightStepG = (s32)(((s32)(video::getGreen(v3->Color)<<8) - rightG) * tmpDiv);
152 rightStepB = (s32)(((s32)(video::getBlue(v3->Color)<<8) - rightB) * tmpDiv);
153 rightTxStep = (s32)((v3->TCoords.X - rightTx) * tmpDiv);
154 rightTyStep = (s32)((v3->TCoords.Y - rightTy) * tmpDiv);
155
156 tmpDiv = 1.0f / (f32)(v2->Pos.Y - v1->Pos.Y);
157 leftdeltaxf = (v2->Pos.X - v1->Pos.X) * tmpDiv;
158 leftStepR = (s32)(((s32)(video::getRed(v2->Color)<<8) - leftR) * tmpDiv);
159 leftStepG = (s32)(((s32)(video::getGreen(v2->Color)<<8) - leftG) * tmpDiv);
160 leftStepB = (s32)(((s32)(video::getBlue(v2->Color)<<8) - leftB) * tmpDiv);
161 leftTxStep = (s32)((v2->TCoords.X - leftTx) * tmpDiv);
162 leftTyStep = (s32)((v2->TCoords.Y - leftTy) * tmpDiv);
163 }
164
165
166 // do it twice, once for the first half of the triangle,
167 // end then for the second half.
168
169 for (s32 triangleHalf=0; triangleHalf<2; ++triangleHalf)
170 {
171 if (spanEnd > ViewPortRect.LowerRightCorner.Y)
172 spanEnd = ViewPortRect.LowerRightCorner.Y;
173
174 // if the span <0, than we can skip these spans,
175 // and proceed to the next spans which are really on the screen.
176 if (span < ViewPortRect.UpperLeftCorner.Y)
177 {
178 // we'll use leftx as temp variable
179 if (spanEnd < ViewPortRect.UpperLeftCorner.Y)
180 {
181 leftx = spanEnd - span;
182 span = spanEnd;
183 }
184 else
185 {
186 leftx = ViewPortRect.UpperLeftCorner.Y - span;
187 span = ViewPortRect.UpperLeftCorner.Y;
188 }
189
190 leftxf += leftdeltaxf*leftx;
191 rightxf += rightdeltaxf*leftx;
192 targetSurface += SurfaceWidth*leftx;
193
194 leftR += leftStepR*leftx;
195 leftG += leftStepG*leftx;
196 leftB += leftStepB*leftx;
197 rightR += rightStepR*leftx;
198 rightG += rightStepG*leftx;
199 rightB += rightStepB*leftx;
200
201 leftTx += leftTxStep*leftx;
202 leftTy += leftTyStep*leftx;
203 rightTx += rightTxStep*leftx;
204 rightTy += rightTyStep*leftx;
205 }
206
207
208 // the main loop. Go through every span and draw it.
209
210 while (span < spanEnd)
211 {
212 leftx = (s32)(leftxf);
213 rightx = (s32)(rightxf + 0.5f);
214
215 // perform some clipping
216 // thanks to a correction by hybrid
217 // calculations delayed to correctly propagate to textures etc.
218 s32 tDiffLeft=0, tDiffRight=0;
219 if (leftx<ViewPortRect.UpperLeftCorner.X)
220 tDiffLeft=ViewPortRect.UpperLeftCorner.X-leftx;
221 else
222 if (leftx>ViewPortRect.LowerRightCorner.X)
223 tDiffLeft=ViewPortRect.LowerRightCorner.X-leftx;
224
225 if (rightx<ViewPortRect.UpperLeftCorner.X)
226 tDiffRight=ViewPortRect.UpperLeftCorner.X-rightx;
227 else
228 if (rightx>ViewPortRect.LowerRightCorner.X)
229 tDiffRight=ViewPortRect.LowerRightCorner.X-rightx;
230
231 // draw the span
232 if (rightx + tDiffRight - leftx - tDiffLeft)
233 {
234 tmpDiv = 1.0f / (f32)(rightx - leftx);
235
236 spanStepR = (s32)((rightR - leftR) * tmpDiv);
237 spanR = leftR+tDiffLeft*spanStepR;
238 spanStepG = (s32)((rightG - leftG) * tmpDiv);
239 spanG = leftG+tDiffLeft*spanStepG;
240 spanStepB = (s32)((rightB - leftB) * tmpDiv);
241 spanB = leftB+tDiffLeft*spanStepB;
242
243 spanTxStep = (s32)((rightTx - leftTx) * tmpDiv);
244 spanTx = leftTx + tDiffLeft*spanTxStep;
245 spanTyStep = (s32)((rightTy - leftTy) * tmpDiv);
246 spanTy = leftTy+tDiffLeft*spanTyStep;
247
248 hSpanBegin = targetSurface + leftx+tDiffLeft;
249 hSpanEnd = targetSurface + rightx+tDiffRight;
250
251 while (hSpanBegin < hSpanEnd)
252 {
253 color = lockedTexture[((spanTy>>8)&textureYMask) * lockedTextureWidth + ((spanTx>>8)&textureXMask)];
254 *hSpanBegin = video::RGB16(video::getRed(color) * (spanR>>8) >>2,
255 video::getGreen(color) * (spanG>>8) >>2,
256 video::getBlue(color) * (spanB>>8) >>2);
257
258 spanR += spanStepR;
259 spanG += spanStepG;
260 spanB += spanStepB;
261
262 spanTx += spanTxStep;
263 spanTy += spanTyStep;
264
265 ++hSpanBegin;
266 }
267 }
268
269 leftxf += leftdeltaxf;
270 rightxf += rightdeltaxf;
271 ++span;
272 targetSurface += SurfaceWidth;
273
274 leftR += leftStepR;
275 leftG += leftStepG;
276 leftB += leftStepB;
277 rightR += rightStepR;
278 rightG += rightStepG;
279 rightB += rightStepB;
280
281 leftTx += leftTxStep;
282 leftTy += leftTyStep;
283 rightTx += rightTxStep;
284 rightTy += rightTyStep;
285 }
286
287 if (triangleHalf>0) // break, we've gout only two halves
288 break;
289
290
291 // setup variables for second half of the triangle.
292
293 if (longest < 0.0f)
294 {
295 tmpDiv = 1.0f / (v3->Pos.Y - v2->Pos.Y);
296
297 rightdeltaxf = (v3->Pos.X - v2->Pos.X) * tmpDiv;
298 rightxf = (f32)v2->Pos.X;
299
300 rightR = video::getRed(v2->Color)<<8;
301 rightG = video::getGreen(v2->Color)<<8;
302 rightB = video::getBlue(v2->Color)<<8;
303 rightStepR = (s32)(((s32)(video::getRed(v3->Color)<<8) - rightR) * tmpDiv);
304 rightStepG = (s32)(((s32)(video::getGreen(v3->Color)<<8) - rightG) * tmpDiv);
305 rightStepB = (s32)(((s32)(video::getBlue(v3->Color)<<8) - rightB) * tmpDiv);
306
307 rightTx = v2->TCoords.X;
308 rightTy = v2->TCoords.Y;
309 rightTxStep = (s32)((v3->TCoords.X - rightTx) * tmpDiv);
310 rightTyStep = (s32)((v3->TCoords.Y - rightTy) * tmpDiv);
311 }
312 else
313 {
314 tmpDiv = 1.0f / (v3->Pos.Y - v2->Pos.Y);
315
316 leftdeltaxf = (v3->Pos.X - v2->Pos.X) * tmpDiv;
317 leftxf = (f32)v2->Pos.X;
318
319 leftR = video::getRed(v2->Color)<<8;
320 leftG = video::getGreen(v2->Color)<<8;
321 leftB = video::getBlue(v2->Color)<<8;
322 leftStepR = (s32)(((s32)(video::getRed(v3->Color)<<8) - leftR) * tmpDiv);
323 leftStepG = (s32)(((s32)(video::getGreen(v3->Color)<<8) - leftG) * tmpDiv);
324 leftStepB = (s32)(((s32)(video::getBlue(v3->Color)<<8) - leftB) * tmpDiv);
325
326 leftTx = v2->TCoords.X;
327 leftTy = v2->TCoords.Y;
328 leftTxStep = (s32)((v3->TCoords.X - leftTx) * tmpDiv);
329 leftTyStep = (s32)((v3->TCoords.Y - leftTy) * tmpDiv);
330 }
331
332
333 spanEnd = v3->Pos.Y;
334 }
335
336 }
337
338 RenderTarget->unlock();
339 Texture->unlock();
340 }
341
342};
343
344} // end namespace video
345} // end namespace irr
346
347#endif // _IRR_COMPILE_WITH_SOFTWARE_
348
349namespace irr
350{
351namespace video
352{
353
354//! creates a flat triangle renderer
355ITriangleRenderer* createTriangleRendererTextureGouraudNoZ()
356{
357 #ifdef _IRR_COMPILE_WITH_SOFTWARE_
358 return new CTRTextureGouraudNoZ();
359 #else
360 return 0;
361 #endif // _IRR_COMPILE_WITH_SOFTWARE_
362}
363
364} // end namespace video
365} // end namespace irr
366
367