utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / MyBillboardSceneNode.cpp
1 // Copyright (C) 2002-2010 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "MyBillboardSceneNode.h"\r
6 #include "IVideoDriver.h"\r
7 #include "ISceneManager.h"\r
8 #include "ICameraSceneNode.h"\r
9 \r
10 namespace irr\r
11 {\r
12 namespace scene\r
13 {\r
14 \r
15 //! constructor\r
16 MyBillboardSceneNode::MyBillboardSceneNode(ISceneNode* parent,\r
17                 ISceneManager* mgr, s32 id,\r
18                 const core::vector3df& position, const core::dimension2d<f32>& size)\r
19         : IBillboardSceneNode(parent, mgr, id, position)\r
20 {\r
21         #ifdef _DEBUG\r
22         setDebugName("MyBillboardSceneNode");\r
23         #endif\r
24 \r
25         setSize(size);\r
26 \r
27         indices[0] = 0;\r
28         indices[1] = 2;\r
29         indices[2] = 1;\r
30         indices[3] = 0;\r
31         indices[4] = 3;\r
32         indices[5] = 2;\r
33 \r
34         video::SColor colorTop = video::SColor(0xFFFFFFFF);\r
35         video::SColor colorBottom = video::SColor(0xFFFFFFFF);\r
36 \r
37         vertices[0].TCoords.set(1.0f, 1.0f);\r
38         vertices[0].Color = colorBottom;\r
39 \r
40         vertices[1].TCoords.set(1.0f, 0.0f);\r
41         vertices[1].Color = colorTop;\r
42 \r
43         vertices[2].TCoords.set(0.0f, 0.0f);\r
44         vertices[2].Color = colorTop;\r
45 \r
46         vertices[3].TCoords.set(0.0f, 1.0f);\r
47         vertices[3].Color = colorBottom;\r
48 }\r
49 \r
50 \r
51 //! pre render event\r
52 void MyBillboardSceneNode::OnRegisterSceneNode()\r
53 {\r
54         if (IsVisible)\r
55                 SceneManager->registerNodeForRendering(this);\r
56 \r
57         ISceneNode::OnRegisterSceneNode();\r
58 }\r
59 \r
60 \r
61 //! render\r
62 void MyBillboardSceneNode::render()\r
63 {\r
64         video::IVideoDriver* driver = SceneManager->getVideoDriver();\r
65         ICameraSceneNode* camera = SceneManager->getActiveCamera();\r
66 \r
67         if (!camera || !driver)\r
68                 return;\r
69 \r
70         // make billboard look to camera\r
71 \r
72         core::vector3df pos = getAbsolutePosition();\r
73 \r
74         core::vector3df campos = camera->getAbsolutePosition();\r
75         core::vector3df target = camera->getTarget();\r
76         core::vector3df up = camera->getUpVector();\r
77         core::vector3df view = target - campos;\r
78         view.normalize();\r
79 \r
80         core::vector3df horizontal = up.crossProduct(view);\r
81         if ( horizontal.getLength() == 0 )\r
82         {\r
83                 horizontal.set(up.Y,up.X,up.Z);\r
84         }\r
85         horizontal.normalize();\r
86         horizontal *= 0.5f * Size.Width;\r
87 \r
88         core::vector3df vertical = horizontal.crossProduct(view);\r
89         vertical.normalize();\r
90         vertical *= 0.5f * Size.Height;\r
91 \r
92         view *= -1.0f;\r
93 \r
94         for (s32 i=0; i<4; ++i)\r
95                 vertices[i].Normal = view;\r
96 \r
97         vertices[0].Pos = pos + horizontal + vertical;\r
98         vertices[1].Pos = pos + horizontal - vertical;\r
99         vertices[2].Pos = pos - horizontal - vertical;\r
100         vertices[3].Pos = pos - horizontal + vertical;\r
101 \r
102         // draw\r
103 \r
104         if ( DebugDataVisible & scene::EDS_BBOX )\r
105         {\r
106                 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);\r
107                 video::SMaterial m;\r
108                 m.Lighting = false;\r
109                 driver->setMaterial(m);\r
110                 driver->draw3DBox(BBox, video::SColor(0,208,195,152));\r
111         }\r
112 \r
113         driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);\r
114 \r
115         driver->setMaterial(Material);\r
116 \r
117         driver->drawIndexedTriangleList(vertices, 4, indices, 2);\r
118 }\r
119 \r
120 \r
121 //! returns the axis aligned bounding box of this node\r
122 const core::aabbox3d<f32>& MyBillboardSceneNode::getBoundingBox() const\r
123 {\r
124         return BBox;\r
125 }\r
126 \r
127 \r
128 //! sets the size of the billboard\r
129 void MyBillboardSceneNode::setSize(const core::dimension2d<f32>& size)\r
130 {\r
131         Size = size;\r
132 \r
133         if (Size.Width == 0.0f)\r
134                 Size.Width = 1.0f;\r
135 \r
136         if (Size.Height == 0.0f )\r
137                 Size.Height = 1.0f;\r
138 \r
139         f32 avg = (size.Width + size.Height)/6;\r
140         BBox.MinEdge.set(-avg,-avg,-avg);\r
141         BBox.MaxEdge.set(avg,avg,avg);\r
142 }\r
143 \r
144 \r
145 video::SMaterial& MyBillboardSceneNode::getMaterial(u32 i)\r
146 {\r
147         return Material;\r
148 }\r
149 \r
150 \r
151 //! returns amount of materials used by this scene node.\r
152 u32 MyBillboardSceneNode::getMaterialCount() const\r
153 {\r
154         return 1;\r
155 }\r
156 \r
157 \r
158 //! gets the size of the billboard\r
159 const core::dimension2d<f32>& MyBillboardSceneNode::getSize() const\r
160 {\r
161         return Size;\r
162 }\r
163 \r
164 \r
165 //! Set the color of all vertices of the billboard\r
166 //! \param overallColor: the color to set\r
167 void MyBillboardSceneNode::setColor(const video::SColor & overallColor)\r
168 {\r
169         for(u32 vertex = 0; vertex < 4; ++vertex)\r
170                 vertices[vertex].Color = overallColor;\r
171 }\r
172 \r
173 \r
174 //! Set the color of the top and bottom vertices of the billboard\r
175 //! \param topColor: the color to set the top vertices\r
176 //! \param bottomColor: the color to set the bottom vertices\r
177 void MyBillboardSceneNode::setColor(const video::SColor & topColor, const video::SColor & bottomColor)\r
178 {\r
179         vertices[0].Color = bottomColor;\r
180         vertices[1].Color = topColor;\r
181         vertices[2].Color = topColor;\r
182         vertices[3].Color = bottomColor;\r
183 }\r
184 \r
185 \r
186 //! Gets the color of the top and bottom vertices of the billboard\r
187 //! \param[out] topColor: stores the color of the top vertices\r
188 //! \param[out] bottomColor: stores the color of the bottom vertices\r
189 void MyBillboardSceneNode::getColor(video::SColor & topColor, video::SColor & bottomColor) const\r
190 {\r
191         bottomColor = vertices[0].Color;\r
192         topColor = vertices[1].Color;\r
193 }\r
194         \r
195 void MyBillboardSceneNode::setTCoords(u32 i, core::vector2d<f32> c)\r
196 {\r
197         vertices[i].TCoords = c;\r
198 }\r
199 \r
200 } // end namespace scene\r
201 } // end namespace irr\r
202 \r