diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CSkyBoxSceneNode.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CSkyBoxSceneNode.cpp | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CSkyBoxSceneNode.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CSkyBoxSceneNode.cpp new file mode 100644 index 0000000..756fab9 --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CSkyBoxSceneNode.cpp | |||
@@ -0,0 +1,263 @@ | |||
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 "CSkyBoxSceneNode.h" | ||
6 | #include "IVideoDriver.h" | ||
7 | #include "ISceneManager.h" | ||
8 | #include "ICameraSceneNode.h" | ||
9 | #include "S3DVertex.h" | ||
10 | #include "os.h" | ||
11 | |||
12 | namespace irr | ||
13 | { | ||
14 | namespace scene | ||
15 | { | ||
16 | |||
17 | //! constructor | ||
18 | CSkyBoxSceneNode::CSkyBoxSceneNode(video::ITexture* top, video::ITexture* bottom, video::ITexture* left, | ||
19 | video::ITexture* right, video::ITexture* front, video::ITexture* back, ISceneNode* parent, ISceneManager* mgr, s32 id) | ||
20 | : ISceneNode(parent, mgr, id) | ||
21 | { | ||
22 | #ifdef _DEBUG | ||
23 | setDebugName("CSkyBoxSceneNode"); | ||
24 | #endif | ||
25 | |||
26 | setAutomaticCulling(scene::EAC_OFF); | ||
27 | Box.MaxEdge.set(0,0,0); | ||
28 | Box.MinEdge.set(0,0,0); | ||
29 | |||
30 | // create indices | ||
31 | |||
32 | Indices[0] = 0; | ||
33 | Indices[1] = 1; | ||
34 | Indices[2] = 2; | ||
35 | Indices[3] = 3; | ||
36 | |||
37 | // create material | ||
38 | |||
39 | video::SMaterial mat; | ||
40 | mat.Lighting = false; | ||
41 | mat.ZBuffer = video::ECFN_NEVER; | ||
42 | mat.ZWriteEnable = false; | ||
43 | mat.AntiAliasing=0; | ||
44 | mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; | ||
45 | mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; | ||
46 | |||
47 | /* Hey, I am no artist, but look at that | ||
48 | cool ASCII art I made! ;) | ||
49 | |||
50 | -111 111 | ||
51 | /6--------/5 y | ||
52 | / | / | ^ z | ||
53 | / | 11-1 | | / | ||
54 | -11-1 3---------2 | |/ | ||
55 | | 7- - -| -4 1-11 *---->x | ||
56 | | -1-11 | / 3-------|2 | ||
57 | |/ | / | //| | ||
58 | 0---------1/ | // | | ||
59 | -1-1-1 1-1-1 |// | | ||
60 | 0--------1 | ||
61 | */ | ||
62 | |||
63 | video::ITexture* tex = front; | ||
64 | if (!tex) tex = left; | ||
65 | if (!tex) tex = back; | ||
66 | if (!tex) tex = right; | ||
67 | if (!tex) tex = top; | ||
68 | if (!tex) tex = bottom; | ||
69 | |||
70 | const f32 onepixel = tex?(1.0f / (tex->getSize().Width * 1.5f)) : 0.0f; | ||
71 | const f32 t = 1.0f - onepixel; | ||
72 | const f32 o = 0.0f + onepixel; | ||
73 | |||
74 | // create front side | ||
75 | |||
76 | Material[0] = mat; | ||
77 | Material[0].setTexture(0, front); | ||
78 | Vertices[0] = video::S3DVertex(-1,-1,-1, 0,0,1, video::SColor(255,255,255,255), t, t); | ||
79 | Vertices[1] = video::S3DVertex( 1,-1,-1, 0,0,1, video::SColor(255,255,255,255), o, t); | ||
80 | Vertices[2] = video::S3DVertex( 1, 1,-1, 0,0,1, video::SColor(255,255,255,255), o, o); | ||
81 | Vertices[3] = video::S3DVertex(-1, 1,-1, 0,0,1, video::SColor(255,255,255,255), t, o); | ||
82 | |||
83 | // create left side | ||
84 | |||
85 | Material[1] = mat; | ||
86 | Material[1].setTexture(0, left); | ||
87 | Vertices[4] = video::S3DVertex( 1,-1,-1, -1,0,0, video::SColor(255,255,255,255), t, t); | ||
88 | Vertices[5] = video::S3DVertex( 1,-1, 1, -1,0,0, video::SColor(255,255,255,255), o, t); | ||
89 | Vertices[6] = video::S3DVertex( 1, 1, 1, -1,0,0, video::SColor(255,255,255,255), o, o); | ||
90 | Vertices[7] = video::S3DVertex( 1, 1,-1, -1,0,0, video::SColor(255,255,255,255), t, o); | ||
91 | |||
92 | // create back side | ||
93 | |||
94 | Material[2] = mat; | ||
95 | Material[2].setTexture(0, back); | ||
96 | Vertices[8] = video::S3DVertex( 1,-1, 1, 0,0,-1, video::SColor(255,255,255,255), t, t); | ||
97 | Vertices[9] = video::S3DVertex(-1,-1, 1, 0,0,-1, video::SColor(255,255,255,255), o, t); | ||
98 | Vertices[10] = video::S3DVertex(-1, 1, 1, 0,0,-1, video::SColor(255,255,255,255), o, o); | ||
99 | Vertices[11] = video::S3DVertex( 1, 1, 1, 0,0,-1, video::SColor(255,255,255,255), t, o); | ||
100 | |||
101 | // create right side | ||
102 | |||
103 | Material[3] = mat; | ||
104 | Material[3].setTexture(0, right); | ||
105 | Vertices[12] = video::S3DVertex(-1,-1, 1, 1,0,0, video::SColor(255,255,255,255), t, t); | ||
106 | Vertices[13] = video::S3DVertex(-1,-1,-1, 1,0,0, video::SColor(255,255,255,255), o, t); | ||
107 | Vertices[14] = video::S3DVertex(-1, 1,-1, 1,0,0, video::SColor(255,255,255,255), o, o); | ||
108 | Vertices[15] = video::S3DVertex(-1, 1, 1, 1,0,0, video::SColor(255,255,255,255), t, o); | ||
109 | |||
110 | // create top side | ||
111 | |||
112 | Material[4] = mat; | ||
113 | Material[4].setTexture(0, top); | ||
114 | Vertices[16] = video::S3DVertex( 1, 1,-1, 0,-1,0, video::SColor(255,255,255,255), t, t); | ||
115 | Vertices[17] = video::S3DVertex( 1, 1, 1, 0,-1,0, video::SColor(255,255,255,255), o, t); | ||
116 | Vertices[18] = video::S3DVertex(-1, 1, 1, 0,-1,0, video::SColor(255,255,255,255), o, o); | ||
117 | Vertices[19] = video::S3DVertex(-1, 1,-1, 0,-1,0, video::SColor(255,255,255,255), t, o); | ||
118 | |||
119 | // create bottom side | ||
120 | |||
121 | Material[5] = mat; | ||
122 | Material[5].setTexture(0, bottom); | ||
123 | Vertices[20] = video::S3DVertex( 1,-1, 1, 0,1,0, video::SColor(255,255,255,255), o, o); | ||
124 | Vertices[21] = video::S3DVertex( 1,-1,-1, 0,1,0, video::SColor(255,255,255,255), t, o); | ||
125 | Vertices[22] = video::S3DVertex(-1,-1,-1, 0,1,0, video::SColor(255,255,255,255), t, t); | ||
126 | Vertices[23] = video::S3DVertex(-1,-1, 1, 0,1,0, video::SColor(255,255,255,255), o, t); | ||
127 | } | ||
128 | |||
129 | |||
130 | //! renders the node. | ||
131 | void CSkyBoxSceneNode::render() | ||
132 | { | ||
133 | video::IVideoDriver* driver = SceneManager->getVideoDriver(); | ||
134 | scene::ICameraSceneNode* camera = SceneManager->getActiveCamera(); | ||
135 | |||
136 | if (!camera || !driver) | ||
137 | return; | ||
138 | |||
139 | if ( !camera->isOrthogonal() ) | ||
140 | { | ||
141 | // draw perspective skybox | ||
142 | |||
143 | core::matrix4 translate(AbsoluteTransformation); | ||
144 | translate.setTranslation(camera->getAbsolutePosition()); | ||
145 | |||
146 | // Draw the sky box between the near and far clip plane | ||
147 | const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f; | ||
148 | core::matrix4 scale; | ||
149 | scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance)); | ||
150 | |||
151 | driver->setTransform(video::ETS_WORLD, translate * scale); | ||
152 | |||
153 | for (s32 i=0; i<6; ++i) | ||
154 | { | ||
155 | driver->setMaterial(Material[i]); | ||
156 | driver->drawIndexedTriangleFan(&Vertices[i*4], 4, Indices, 2); | ||
157 | } | ||
158 | } | ||
159 | else | ||
160 | { | ||
161 | // draw orthogonal skybox, | ||
162 | // simply choose one texture and draw it as 2d picture. | ||
163 | // there could be better ways to do this, but currently I think this is ok. | ||
164 | |||
165 | core::vector3df lookVect = camera->getTarget() - camera->getAbsolutePosition(); | ||
166 | lookVect.normalize(); | ||
167 | core::vector3df absVect( core::abs_(lookVect.X), | ||
168 | core::abs_(lookVect.Y), | ||
169 | core::abs_(lookVect.Z)); | ||
170 | |||
171 | int idx = 0; | ||
172 | |||
173 | if ( absVect.X >= absVect.Y && absVect.X >= absVect.Z ) | ||
174 | { | ||
175 | // x direction | ||
176 | idx = lookVect.X > 0 ? 0 : 2; | ||
177 | } | ||
178 | else | ||
179 | if ( absVect.Y >= absVect.X && absVect.Y >= absVect.Z ) | ||
180 | { | ||
181 | // y direction | ||
182 | idx = lookVect.Y > 0 ? 4 : 5; | ||
183 | } | ||
184 | else | ||
185 | if ( absVect.Z >= absVect.X && absVect.Z >= absVect.Y ) | ||
186 | { | ||
187 | // z direction | ||
188 | idx = lookVect.Z > 0 ? 1 : 3; | ||
189 | } | ||
190 | |||
191 | video::ITexture* tex = Material[idx].getTexture(0); | ||
192 | |||
193 | if ( tex ) | ||
194 | { | ||
195 | core::rect<s32> rctDest(core::position2d<s32>(-1,0), | ||
196 | core::dimension2di(driver->getCurrentRenderTargetSize())); | ||
197 | core::rect<s32> rctSrc(core::position2d<s32>(0,0), | ||
198 | core::dimension2di(tex->getSize())); | ||
199 | |||
200 | driver->draw2DImage(tex, rctDest, rctSrc); | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | |||
206 | |||
207 | //! returns the axis aligned bounding box of this node | ||
208 | const core::aabbox3d<f32>& CSkyBoxSceneNode::getBoundingBox() const | ||
209 | { | ||
210 | return Box; | ||
211 | } | ||
212 | |||
213 | |||
214 | void CSkyBoxSceneNode::OnRegisterSceneNode() | ||
215 | { | ||
216 | if (IsVisible) | ||
217 | SceneManager->registerNodeForRendering(this, ESNRP_SKY_BOX); | ||
218 | |||
219 | ISceneNode::OnRegisterSceneNode(); | ||
220 | } | ||
221 | |||
222 | |||
223 | //! returns the material based on the zero based index i. To get the amount | ||
224 | //! of materials used by this scene node, use getMaterialCount(). | ||
225 | //! This function is needed for inserting the node into the scene hirachy on a | ||
226 | //! optimal position for minimizing renderstate changes, but can also be used | ||
227 | //! to directly modify the material of a scene node. | ||
228 | video::SMaterial& CSkyBoxSceneNode::getMaterial(u32 i) | ||
229 | { | ||
230 | return Material[i]; | ||
231 | } | ||
232 | |||
233 | |||
234 | //! returns amount of materials used by this scene node. | ||
235 | u32 CSkyBoxSceneNode::getMaterialCount() const | ||
236 | { | ||
237 | return 6; | ||
238 | } | ||
239 | |||
240 | |||
241 | //! Creates a clone of this scene node and its children. | ||
242 | ISceneNode* CSkyBoxSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager) | ||
243 | { | ||
244 | if (!newParent) newParent = Parent; | ||
245 | if (!newManager) newManager = SceneManager; | ||
246 | |||
247 | CSkyBoxSceneNode* nb = new CSkyBoxSceneNode(0,0,0,0,0,0, newParent, | ||
248 | newManager, ID); | ||
249 | |||
250 | nb->cloneMembers(this, newManager); | ||
251 | |||
252 | for (u32 i=0; i<6; ++i) | ||
253 | nb->Material[i] = Material[i]; | ||
254 | |||
255 | if ( newParent ) | ||
256 | nb->drop(); | ||
257 | return nb; | ||
258 | } | ||
259 | |||
260 | |||
261 | } // end namespace scene | ||
262 | } // end namespace irr | ||
263 | |||