/src/ogre/OgreMain/src/OgreRectangle2D.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | |
30 | | namespace Ogre { |
31 | 0 | #define POSITION_BINDING 0 |
32 | 0 | #define NORMAL_BINDING 1 |
33 | 0 | #define TEXCOORD_BINDING 2 |
34 | | |
35 | | Rectangle2D::Rectangle2D(bool includeTextureCoords, Ogre::HardwareBuffer::Usage vBufUsage) |
36 | 0 | : SimpleRenderable() |
37 | 0 | { |
38 | 0 | _initRectangle2D(includeTextureCoords, vBufUsage); |
39 | 0 | } |
40 | | |
41 | | Rectangle2D::Rectangle2D(const String& name, bool includeTextureCoords, Ogre::HardwareBuffer::Usage vBufUsage) |
42 | 0 | : SimpleRenderable(name) |
43 | 0 | { |
44 | 0 | _initRectangle2D(includeTextureCoords, vBufUsage); |
45 | 0 | } |
46 | | |
47 | | void Rectangle2D::_initRectangle2D(bool includeTextureCoords, Ogre::HardwareBuffer::Usage vBufUsage) |
48 | 0 | { |
49 | | // use identity projection and view matrices |
50 | 0 | mUseIdentityProjection = true; |
51 | 0 | mUseIdentityView = true; |
52 | |
|
53 | 0 | mBox.setInfinite(); // screenspace -> never culled |
54 | |
|
55 | 0 | mRenderOp.vertexData = OGRE_NEW VertexData(); |
56 | |
|
57 | 0 | mRenderOp.indexData = 0; |
58 | 0 | mRenderOp.vertexData->vertexCount = 4; |
59 | 0 | mRenderOp.vertexData->vertexStart = 0; |
60 | 0 | mRenderOp.operationType = RenderOperation::OT_TRIANGLE_STRIP; |
61 | 0 | mRenderOp.useIndexes = false; |
62 | |
|
63 | 0 | VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration; |
64 | 0 | VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding; |
65 | |
|
66 | 0 | decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION); |
67 | |
|
68 | 0 | auto& hbm = HardwareBufferManager::getSingleton(); |
69 | 0 | auto vbuf = |
70 | 0 | hbm.createVertexBuffer(decl->getVertexSize(POSITION_BINDING), mRenderOp.vertexData->vertexCount, vBufUsage); |
71 | | |
72 | | // Bind buffer |
73 | 0 | bind->setBinding(POSITION_BINDING, vbuf); |
74 | |
|
75 | 0 | setCorners(-1, 1, 1, -1, false); |
76 | |
|
77 | 0 | decl->addElement(NORMAL_BINDING, 0, VET_FLOAT3, VES_NORMAL); |
78 | |
|
79 | 0 | vbuf = |
80 | 0 | hbm.createVertexBuffer(decl->getVertexSize(NORMAL_BINDING), mRenderOp.vertexData->vertexCount, vBufUsage); |
81 | |
|
82 | 0 | bind->setBinding(NORMAL_BINDING, vbuf); |
83 | |
|
84 | 0 | setNormals({0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}); |
85 | |
|
86 | 0 | if (includeTextureCoords) |
87 | 0 | { |
88 | 0 | decl->addElement(TEXCOORD_BINDING, 0, VET_FLOAT2, VES_TEXTURE_COORDINATES); |
89 | |
|
90 | 0 | auto tvbuf = hbm.createVertexBuffer(decl->getVertexSize(TEXCOORD_BINDING), |
91 | 0 | mRenderOp.vertexData->vertexCount, vBufUsage); |
92 | | |
93 | | // Bind buffer |
94 | 0 | bind->setBinding(TEXCOORD_BINDING, tvbuf); |
95 | | |
96 | | // Set up basic tex coordinates |
97 | 0 | setDefaultUVs(); |
98 | 0 | } |
99 | | |
100 | | // set basic white material |
101 | 0 | mMaterial = MaterialManager::getSingleton().getDefaultMaterial(false); |
102 | 0 | mMaterial->load(); |
103 | 0 | } |
104 | | |
105 | | Rectangle2D::~Rectangle2D() |
106 | 0 | { |
107 | 0 | OGRE_DELETE mRenderOp.vertexData; |
108 | 0 | } |
109 | | |
110 | | void Rectangle2D::setCorners(float left, float top, float right, float bottom, bool updateAABB) |
111 | 0 | { |
112 | 0 | HardwareVertexBufferSharedPtr vbuf = |
113 | 0 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING); |
114 | 0 | HardwareBufferLockGuard vbufLock(vbuf, HardwareBuffer::HBL_DISCARD); |
115 | 0 | float* pFloat = static_cast<float*>(vbufLock.pData); |
116 | |
|
117 | 0 | *pFloat++ = left; |
118 | 0 | *pFloat++ = top; |
119 | 0 | *pFloat++ = -1; |
120 | |
|
121 | 0 | *pFloat++ = left; |
122 | 0 | *pFloat++ = bottom; |
123 | 0 | *pFloat++ = -1; |
124 | |
|
125 | 0 | *pFloat++ = right; |
126 | 0 | *pFloat++ = top; |
127 | 0 | *pFloat++ = -1; |
128 | |
|
129 | 0 | *pFloat++ = right; |
130 | 0 | *pFloat++ = bottom; |
131 | 0 | *pFloat++ = -1; |
132 | |
|
133 | 0 | if(updateAABB) |
134 | 0 | { |
135 | 0 | mBox.setExtents( |
136 | 0 | std::min(left, right), std::min(top, bottom), 0, |
137 | 0 | std::max(left, right), std::max(top, bottom), 0); |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | void Rectangle2D::setNormals(const Vector3& topLeft, const Vector3& bottomLeft, const Vector3& topRight, |
142 | | const Vector3& bottomRight) |
143 | 0 | { |
144 | 0 | HardwareVertexBufferSharedPtr vbuf = |
145 | 0 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(NORMAL_BINDING); |
146 | 0 | HardwareBufferLockGuard vbufLock(vbuf, HardwareBuffer::HBL_DISCARD); |
147 | 0 | float* pFloat = static_cast<float*>(vbufLock.pData); |
148 | |
|
149 | 0 | *pFloat++ = topLeft.x; |
150 | 0 | *pFloat++ = topLeft.y; |
151 | 0 | *pFloat++ = topLeft.z; |
152 | |
|
153 | 0 | *pFloat++ = bottomLeft.x; |
154 | 0 | *pFloat++ = bottomLeft.y; |
155 | 0 | *pFloat++ = bottomLeft.z; |
156 | |
|
157 | 0 | *pFloat++ = topRight.x; |
158 | 0 | *pFloat++ = topRight.y; |
159 | 0 | *pFloat++ = topRight.z; |
160 | |
|
161 | 0 | *pFloat++ = bottomRight.x; |
162 | 0 | *pFloat++ = bottomRight.y; |
163 | 0 | *pFloat++ = bottomRight.z; |
164 | 0 | } |
165 | | |
166 | | void Rectangle2D::setUVs(const Vector2& topLeft, const Vector2& bottomLeft, const Vector2& topRight, |
167 | | const Vector2& bottomRight) |
168 | 0 | { |
169 | 0 | OgreAssert(mRenderOp.vertexData->vertexDeclaration->getElementCount() > TEXCOORD_BINDING, |
170 | 0 | "Vertex data wasn't built with UV buffer"); |
171 | | |
172 | 0 | HardwareVertexBufferSharedPtr vbuf = |
173 | 0 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(TEXCOORD_BINDING); |
174 | 0 | HardwareBufferLockGuard vbufLock(vbuf, HardwareBuffer::HBL_DISCARD); |
175 | 0 | float* pFloat = static_cast<float*>(vbufLock.pData); |
176 | |
|
177 | 0 | *pFloat++ = topLeft.x; |
178 | 0 | *pFloat++ = topLeft.y; |
179 | |
|
180 | 0 | *pFloat++ = bottomLeft.x; |
181 | 0 | *pFloat++ = bottomLeft.y; |
182 | |
|
183 | 0 | *pFloat++ = topRight.x; |
184 | 0 | *pFloat++ = topRight.y; |
185 | |
|
186 | 0 | *pFloat++ = bottomRight.x; |
187 | 0 | *pFloat++ = bottomRight.y; |
188 | 0 | } |
189 | | |
190 | | void Rectangle2D::setDefaultUVs() |
191 | 0 | { |
192 | 0 | setUVs( Vector2::ZERO, Vector2::UNIT_Y, Vector2::UNIT_X, Vector2::UNIT_SCALE ); |
193 | 0 | } |
194 | | |
195 | | // Override this method to prevent parent transforms (rotation,translation,scale) |
196 | | void Rectangle2D::getWorldTransforms( Matrix4* xform ) const |
197 | 0 | { |
198 | | // return identity matrix to prevent parent transforms |
199 | 0 | *xform = Matrix4::IDENTITY; |
200 | 0 | } |
201 | | |
202 | | const String& Rectangle2D::getMovableType() const |
203 | 0 | { |
204 | 0 | return MOT_RECTANGLE2D; |
205 | 0 | } |
206 | | |
207 | | const String MOT_RECTANGLE2D = "Rectangle2D"; |
208 | | |
209 | | MovableObject* Rectangle2DFactory::createInstanceImpl(const String& name, const NameValuePairList* params) |
210 | 0 | { |
211 | 0 | bool includeTextureCoords = false; |
212 | 0 | if (params) |
213 | 0 | { |
214 | 0 | auto ni = params->find("includeTextureCoords"); |
215 | 0 | if (ni != params->end()) |
216 | 0 | StringConverter::parse(ni->second, includeTextureCoords); |
217 | 0 | } |
218 | 0 | return new Rectangle2D(includeTextureCoords); |
219 | 0 | } |
220 | | } |
221 | | |