aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp
diff options
context:
space:
mode:
authorDavid Walter Seikel2013-01-13 17:24:39 +1000
committerDavid Walter Seikel2013-01-13 17:24:39 +1000
commit393b5cd1dc438872af89d334ef6e5fcc59f27d47 (patch)
tree6a14521219942a08a1b95cb2f5a923a9edd60f63 /libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp
parentAdd a note about rasters suggested start up code. (diff)
downloadSledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.zip
SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.gz
SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.bz2
SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.xz
Added Irrlicht 1.8, but without all the Windows binaries.
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp358
1 files changed, 358 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp
new file mode 100644
index 0000000..9edf908
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/CTRGouraud.cpp
@@ -0,0 +1,358 @@
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
8#ifdef _IRR_COMPILE_WITH_SOFTWARE_
9
10namespace irr
11{
12namespace video
13{
14
15
16class CTRGouraud : public CTRTextureGouraud
17{
18public:
19
20 CTRGouraud(IZBuffer* zbuffer)
21 : CTRTextureGouraud(zbuffer)
22 {
23 #ifdef _DEBUG
24 setDebugName("CTRGouraud");
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 f32 tmpDiv; // temporary division factor
34 f32 longest; // saves the longest span
35 s32 height; // saves height of triangle
36 u16* targetSurface; // target pointer where to plot pixels
37 s32 spanEnd; // saves end of spans
38 f32 leftdeltaxf; // amount of pixels to increase on left side of triangle
39 f32 rightdeltaxf; // amount of pixels to increase on right side of triangle
40 s32 leftx, rightx; // position where we are
41 f32 leftxf, rightxf; // same as above, but as f32 values
42 s32 span; // current span
43 u16 *hSpanBegin, *hSpanEnd; // pointer used when plotting pixels
44 s32 leftR, leftG, leftB, rightR, rightG, rightB; // color values
45 s32 leftStepR, leftStepG, leftStepB,
46 rightStepR, rightStepG, rightStepB; // color steps
47 s32 spanR, spanG, spanB, spanStepR, spanStepG, spanStepB; // color interpolating values while drawing a span.
48
49 core::rect<s32> TriangleRect;
50
51 s32 leftZValue, rightZValue;
52 s32 leftZStep, rightZStep;
53 s32 spanZValue, spanZStep; // ZValues when drawing a span
54 TZBufferType* zTarget, *spanZTarget; // target of ZBuffer;
55
56 lockedSurface = (u16*)RenderTarget->lock();
57 lockedZBuffer = ZBuffer->lock();
58
59 for (s32 i=0; i<triangleCount; ++i)
60 {
61 v1 = &vertices[*indexList];
62 ++indexList;
63 v2 = &vertices[*indexList];
64 ++indexList;
65 v3 = &vertices[*indexList];
66 ++indexList;
67
68 // back face culling
69
70 if (BackFaceCullingEnabled)
71 {
72 s32 z = ((v3->Pos.X - v1->Pos.X) * (v3->Pos.Y - v2->Pos.Y)) -
73 ((v3->Pos.Y - v1->Pos.Y) * (v3->Pos.X - v2->Pos.X));
74
75 if (z < 0)
76 continue;
77 }
78
79 //near plane clipping
80
81 if (v1->ZValue<0 && v2->ZValue<0 && v3->ZValue<0)
82 continue;
83
84 // sort for width for inscreen clipping
85
86 if (v1->Pos.X > v2->Pos.X) swapVertices(&v1, &v2);
87 if (v1->Pos.X > v3->Pos.X) swapVertices(&v1, &v3);
88 if (v2->Pos.X > v3->Pos.X) swapVertices(&v2, &v3);
89
90 if ((v1->Pos.X - v3->Pos.X) == 0)
91 continue;
92
93 TriangleRect.UpperLeftCorner.X = v1->Pos.X;
94 TriangleRect.LowerRightCorner.X = v3->Pos.X;
95
96 // sort for height for faster drawing.
97
98 if (v1->Pos.Y > v2->Pos.Y) swapVertices(&v1, &v2);
99 if (v1->Pos.Y > v3->Pos.Y) swapVertices(&v1, &v3);
100 if (v2->Pos.Y > v3->Pos.Y) swapVertices(&v2, &v3);
101
102 TriangleRect.UpperLeftCorner.Y = v1->Pos.Y;
103 TriangleRect.LowerRightCorner.Y = v3->Pos.Y;
104
105 if (!TriangleRect.isRectCollided(ViewPortRect))
106 continue;
107
108 // calculate height of triangle
109 height = v3->Pos.Y - v1->Pos.Y;
110 if (!height)
111 continue;
112
113 // calculate longest span
114
115 longest = (v2->Pos.Y - v1->Pos.Y) / (f32)height * (v3->Pos.X - v1->Pos.X) + (v1->Pos.X - v2->Pos.X);
116
117 spanEnd = v2->Pos.Y;
118 span = v1->Pos.Y;
119 leftxf = (f32)v1->Pos.X;
120 rightxf = (f32)v1->Pos.X;
121
122 leftZValue = v1->ZValue;
123 rightZValue = v1->ZValue;
124
125 leftR = rightR = video::getRed(v1->Color)<<11;
126 leftG = rightG = video::getGreen(v1->Color)<<11;
127 leftB = rightB = video::getBlue(v1->Color)<<11;
128
129 targetSurface = lockedSurface + span * SurfaceWidth;
130 zTarget = lockedZBuffer + span * SurfaceWidth;
131
132 if (longest < 0.0f)
133 {
134 tmpDiv = 1.0f / (f32)(v2->Pos.Y - v1->Pos.Y);
135 rightdeltaxf = (v2->Pos.X - v1->Pos.X) * tmpDiv;
136 rightZStep = (s32)((v2->ZValue - v1->ZValue) * tmpDiv);
137 rightStepR = (s32)(((s32)(video::getRed(v2->Color)<<11) - rightR) * tmpDiv);
138 rightStepG = (s32)(((s32)(video::getGreen(v2->Color)<<11) - rightG) * tmpDiv);
139 rightStepB = (s32)(((s32)(video::getBlue(v2->Color)<<11) - rightB) * tmpDiv);
140
141 tmpDiv = 1.0f / (f32)height;
142 leftdeltaxf = (v3->Pos.X - v1->Pos.X) * tmpDiv;
143 leftZStep = (s32)((v3->ZValue - v1->ZValue) * tmpDiv);
144 leftStepR = (s32)(((s32)(video::getRed(v3->Color)<<11) - leftR) * tmpDiv);
145 leftStepG = (s32)(((s32)(video::getGreen(v3->Color)<<11) - leftG) * tmpDiv);
146 leftStepB = (s32)(((s32)(video::getBlue(v3->Color)<<11) - leftB) * tmpDiv);
147 }
148 else
149 {
150 tmpDiv = 1.0f / (f32)height;
151 rightdeltaxf = (v3->Pos.X - v1->Pos.X) * tmpDiv;
152 rightZStep = (s32)((v3->ZValue - v1->ZValue) * tmpDiv);
153 rightStepR = (s32)(((s32)(video::getRed(v3->Color)<<11) - rightR) * tmpDiv);
154 rightStepG = (s32)(((s32)(video::getGreen(v3->Color)<<11) - rightG) * tmpDiv);
155 rightStepB = (s32)(((s32)(video::getBlue(v3->Color)<<11) - rightB) * tmpDiv);
156
157 tmpDiv = 1.0f / (f32)(v2->Pos.Y - v1->Pos.Y);
158 leftdeltaxf = (v2->Pos.X - v1->Pos.X) * tmpDiv;
159 leftZStep = (s32)((v2->ZValue - v1->ZValue) * tmpDiv);
160 leftStepR = (s32)(((s32)(video::getRed(v2->Color)<<11) - leftR) * tmpDiv);
161 leftStepG = (s32)(((s32)(video::getGreen(v2->Color)<<11) - leftG) * tmpDiv);
162 leftStepB = (s32)(((s32)(video::getBlue(v2->Color)<<11) - leftB) * 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 zTarget += SurfaceWidth*leftx;
194 leftZValue += leftZStep*leftx;
195 rightZValue += rightZStep*leftx;
196
197 leftR += leftStepR*leftx;
198 leftG += leftStepG*leftx;
199 leftB += leftStepB*leftx;
200 rightR += rightStepR*leftx;
201 rightG += rightStepG*leftx;
202 rightB += rightStepB*leftx;
203 }
204
205
206 // the main loop. Go through every span and draw it.
207
208 while (span < spanEnd)
209 {
210 leftx = (s32)(leftxf);
211 rightx = (s32)(rightxf + 0.5f);
212
213 // perform some clipping
214
215 // TODO: clipping is not correct when leftx is clipped.
216
217 if (leftx<ViewPortRect.UpperLeftCorner.X)
218 leftx = ViewPortRect.UpperLeftCorner.X;
219 else
220 if (leftx>ViewPortRect.LowerRightCorner.X)
221 leftx = ViewPortRect.LowerRightCorner.X;
222
223 if (rightx<ViewPortRect.UpperLeftCorner.X)
224 rightx = ViewPortRect.UpperLeftCorner.X;
225 else
226 if (rightx>ViewPortRect.LowerRightCorner.X)
227 rightx = ViewPortRect.LowerRightCorner.X;
228
229 // draw the span
230
231 if (rightx - leftx != 0)
232 {
233 tmpDiv = 1.0f / (rightx - leftx);
234 spanZValue = leftZValue;
235 spanZStep = (s32)((rightZValue - leftZValue) * tmpDiv);
236
237 hSpanBegin = targetSurface + leftx;
238 spanZTarget = zTarget + leftx;
239 hSpanEnd = targetSurface + rightx;
240
241 spanR = leftR;
242 spanG = leftG;
243 spanB = leftB;
244 spanStepR = (s32)((rightR - leftR) * tmpDiv);
245 spanStepG = (s32)((rightG - leftG) * tmpDiv);
246 spanStepB = (s32)((rightB - leftB) * tmpDiv);
247
248 while (hSpanBegin < hSpanEnd)
249 {
250 if (spanZValue > *spanZTarget)
251 {
252 *spanZTarget = spanZValue;
253 *hSpanBegin = video::RGB16(spanR>>8, spanG>>8, spanB>>8);
254 }
255
256 spanR += spanStepR;
257 spanG += spanStepG;
258 spanB += spanStepB;
259
260 spanZValue += spanZStep;
261 ++hSpanBegin;
262 ++spanZTarget;
263 }
264 }
265
266 leftxf += leftdeltaxf;
267 rightxf += rightdeltaxf;
268 ++span;
269 targetSurface += SurfaceWidth;
270 zTarget += SurfaceWidth;
271 leftZValue += leftZStep;
272 rightZValue += rightZStep;
273
274 leftR += leftStepR;
275 leftG += leftStepG;
276 leftB += leftStepB;
277 rightR += rightStepR;
278 rightG += rightStepG;
279 rightB += rightStepB;
280 }
281
282 if (triangleHalf>0) // break, we've gout only two halves
283 break;
284
285
286 // setup variables for second half of the triangle.
287
288 if (longest < 0.0f)
289 {
290 tmpDiv = 1.0f / (v3->Pos.Y - v2->Pos.Y);
291
292 rightdeltaxf = (v3->Pos.X - v2->Pos.X) * tmpDiv;
293 rightxf = (f32)v2->Pos.X;
294
295 rightZValue = v2->ZValue;
296 rightZStep = (s32)((v3->ZValue - v2->ZValue) * tmpDiv);
297
298 rightR = video::getRed(v2->Color)<<11;
299 rightG = video::getGreen(v2->Color)<<11;
300 rightB = video::getBlue(v2->Color)<<11;
301 rightStepR = (s32)(((s32)(video::getRed(v3->Color)<<11) - rightR) * tmpDiv);
302 rightStepG = (s32)(((s32)(video::getGreen(v3->Color)<<11) - rightG) * tmpDiv);
303 rightStepB = (s32)(((s32)(video::getBlue(v3->Color)<<11) - rightB) * tmpDiv);
304 }
305 else
306 {
307 tmpDiv = 1.0f / (v3->Pos.Y - v2->Pos.Y);
308
309 leftdeltaxf = (v3->Pos.X - v2->Pos.X) * tmpDiv;
310 leftxf = (f32)v2->Pos.X;
311
312 leftZValue = v2->ZValue;
313 leftZStep = (s32)((v3->ZValue - v2->ZValue) * tmpDiv);
314
315 leftR = video::getRed(v2->Color)<<11;
316 leftG = video::getGreen(v2->Color)<<11;
317 leftB = video::getBlue(v2->Color)<<11;
318 leftStepR = (s32)(((s32)(video::getRed(v3->Color)<<11) - leftR) * tmpDiv);
319 leftStepG = (s32)(((s32)(video::getGreen(v3->Color)<<11) - leftG) * tmpDiv);
320 leftStepB = (s32)(((s32)(video::getBlue(v3->Color)<<11) - leftB) * tmpDiv);
321 }
322
323
324 spanEnd = v3->Pos.Y;
325 }
326
327 }
328
329 RenderTarget->unlock();
330 ZBuffer->unlock();
331 }
332
333};
334
335} // end namespace video
336} // end namespace irr
337
338#endif // _IRR_COMPILE_WITH_SOFTWARE_
339
340namespace irr
341{
342namespace video
343{
344
345//! creates a flat triangle renderer
346ITriangleRenderer* createTriangleRendererGouraud(IZBuffer* zbuffer)
347{
348 #ifdef _IRR_COMPILE_WITH_SOFTWARE_
349 return new CTRGouraud(zbuffer);
350 #else
351 return 0;
352 #endif // _IRR_COMPILE_WITH_SOFTWARE_
353}
354
355} // end namespace video
356} // end namespace irr
357
358