1 | /************************************************************************
|
---|
2 | filename: OgreCEGUIRenderer.cpp
|
---|
3 | created: 11/5/2004
|
---|
4 | author: Paul D Turner
|
---|
5 |
|
---|
6 | purpose: Implementation of Renderer class for Ogre engine
|
---|
7 | *************************************************************************/
|
---|
8 | /*************************************************************************
|
---|
9 | Crazy Eddie's GUI System (http://www.cegui.org.uk)
|
---|
10 | Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
|
---|
11 |
|
---|
12 | This library is free software; you can redistribute it and/or
|
---|
13 | modify it under the terms of the GNU Lesser General Public
|
---|
14 | License as published by the Free Software Foundation; either
|
---|
15 | version 2.1 of the License, or (at your option) any later version.
|
---|
16 |
|
---|
17 | This library is distributed in the hope that it will be useful,
|
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | Lesser General Public License for more details.
|
---|
21 |
|
---|
22 | You should have received a copy of the GNU Lesser General Public
|
---|
23 | License along with this library; if not, write to the Free Software
|
---|
24 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
25 | *************************************************************************/
|
---|
26 |
|
---|
27 | #include <CEGUI/CEGUIImagesetManager.h>
|
---|
28 | #include <CEGUI/CEGUIImageset.h>
|
---|
29 | #include <CEGUI/CEGUIImage.h>
|
---|
30 | #include <CEGUI/CEGUIExceptions.h>
|
---|
31 | #include <CEGUI/CEGUISystem.h>
|
---|
32 |
|
---|
33 | #include "OgreCEGUIRenderer.h"
|
---|
34 | #include "OgreCEGUITexture.h"
|
---|
35 | #include "OgreCEGUIResourceProvider.h"
|
---|
36 |
|
---|
37 | #include <OgreRenderSystem.h>
|
---|
38 | #include <OgreRoot.h>
|
---|
39 | #include <OgreHardwareBufferManager.h>
|
---|
40 | #include <OgreRenderWindow.h>
|
---|
41 |
|
---|
42 | // Start of CEGUI namespace section
|
---|
43 | namespace CEGUI
|
---|
44 | {
|
---|
45 | /*************************************************************************
|
---|
46 | Constants definitions
|
---|
47 | *************************************************************************/
|
---|
48 | const size_t OgreCEGUIRenderer::VERTEX_PER_QUAD = 6;
|
---|
49 | const size_t OgreCEGUIRenderer::VERTEX_PER_TRIANGLE = 3;
|
---|
50 | const size_t OgreCEGUIRenderer::VERTEXBUFFER_INITIAL_CAPACITY = 256;
|
---|
51 | const size_t OgreCEGUIRenderer::UNDERUSED_FRAME_THRESHOLD = 50000; // halfs buffer every 8 minutes on 100fps
|
---|
52 |
|
---|
53 | /*************************************************************************
|
---|
54 | Utility function to create a render operation and vertex buffer to render quads
|
---|
55 | *************************************************************************/
|
---|
56 | void createQuadRenderOp(Ogre::RenderOperation &d_render_op,
|
---|
57 | Ogre::HardwareVertexBufferSharedPtr &d_buffer, size_t nquads)
|
---|
58 | {
|
---|
59 | using namespace Ogre;
|
---|
60 | // Create and initialise the Ogre specific parts required for use in rendering later.
|
---|
61 | d_render_op.vertexData = new VertexData;
|
---|
62 | d_render_op.vertexData->vertexStart = 0;
|
---|
63 |
|
---|
64 | // setup vertex declaration for the vertex format we use
|
---|
65 | VertexDeclaration* vd = d_render_op.vertexData->vertexDeclaration;
|
---|
66 | size_t vd_offset = 0;
|
---|
67 | vd->addElement(0, vd_offset, VET_FLOAT3, VES_POSITION);
|
---|
68 | vd_offset += VertexElement::getTypeSize(VET_FLOAT3);
|
---|
69 | vd->addElement(0, vd_offset, VET_COLOUR, VES_DIFFUSE);
|
---|
70 | vd_offset += VertexElement::getTypeSize(VET_COLOUR);
|
---|
71 | vd->addElement(0, vd_offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
|
---|
72 |
|
---|
73 | // create hardware vertex buffer
|
---|
74 | d_buffer = HardwareBufferManager::getSingleton().createVertexBuffer(vd->getVertexSize(0), nquads,
|
---|
75 | HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE, false);
|
---|
76 |
|
---|
77 | // bind vertex buffer
|
---|
78 | d_render_op.vertexData->vertexBufferBinding->setBinding(0, d_buffer);
|
---|
79 |
|
---|
80 | // complete render operation basic initialisation
|
---|
81 | d_render_op.operationType = RenderOperation::OT_TRIANGLE_LIST;
|
---|
82 | d_render_op.useIndexes = false;
|
---|
83 | }
|
---|
84 | void destroyQuadRenderOp(Ogre::RenderOperation &d_render_op,
|
---|
85 | Ogre::HardwareVertexBufferSharedPtr &d_buffer)
|
---|
86 | {
|
---|
87 | delete d_render_op.vertexData;
|
---|
88 | d_render_op.vertexData = 0;
|
---|
89 | d_buffer.setNull();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*************************************************************************
|
---|
93 | Constructor
|
---|
94 | *************************************************************************/
|
---|
95 | OgreCEGUIRenderer::OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id, bool post_queue, uint max_quads, Ogre::SceneType scene_type)
|
---|
96 | {
|
---|
97 | constructor_impl(window, queue_id, post_queue, max_quads);
|
---|
98 |
|
---|
99 | // hook into ogre rendering system
|
---|
100 | setTargetSceneManager(scene_type);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /*************************************************************************
|
---|
105 | Constructor (specifying scene manager)
|
---|
106 | *************************************************************************/
|
---|
107 | OgreCEGUIRenderer::OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id, bool post_queue, uint max_quads, Ogre::SceneManager* scene_manager)
|
---|
108 | {
|
---|
109 | constructor_impl(window, queue_id, post_queue, max_quads);
|
---|
110 |
|
---|
111 | // hook into ogre rendering system
|
---|
112 | setTargetSceneManager(scene_manager);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /*************************************************************************
|
---|
117 | Destructor
|
---|
118 | *************************************************************************/
|
---|
119 | OgreCEGUIRenderer::~OgreCEGUIRenderer(void)
|
---|
120 | {
|
---|
121 | setTargetSceneManager(NULL);
|
---|
122 |
|
---|
123 | if (d_ourlistener)
|
---|
124 | {
|
---|
125 | delete d_ourlistener;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // cleanup vertex data we allocated in constructor
|
---|
129 | destroyQuadRenderOp(d_render_op, d_buffer);
|
---|
130 | destroyQuadRenderOp(d_direct_render_op, d_direct_buffer);
|
---|
131 |
|
---|
132 | destroyAllTextures();
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /*************************************************************************
|
---|
137 | add's a quad to the list to be rendered
|
---|
138 | *************************************************************************/
|
---|
139 | void OgreCEGUIRenderer::addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode)
|
---|
140 | {
|
---|
141 | // if not queueing, render directly (as in, right now!). This is used for the mouse cursor.
|
---|
142 | if (!d_queueing)
|
---|
143 | {
|
---|
144 | renderQuadDirect(dest_rect, z, tex, texture_rect, colours, quad_split_mode);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | d_sorted = false;
|
---|
149 | QuadInfo quad;
|
---|
150 |
|
---|
151 | // set quad position, flipping y co-ordinates, and applying appropriate texel origin offset
|
---|
152 | quad.position.d_left = dest_rect.d_left;
|
---|
153 | quad.position.d_right = dest_rect.d_right;
|
---|
154 | quad.position.d_top = d_display_area.getHeight() - dest_rect.d_top;
|
---|
155 | quad.position.d_bottom = d_display_area.getHeight() - dest_rect.d_bottom;
|
---|
156 | quad.position.offset(d_texelOffset);
|
---|
157 |
|
---|
158 | // convert quad co-ordinates for a -1 to 1 co-ordinate system.
|
---|
159 | quad.position.d_left /= (d_display_area.getWidth() * 0.5f);
|
---|
160 | quad.position.d_right /= (d_display_area.getWidth() * 0.5f);
|
---|
161 | quad.position.d_top /= (d_display_area.getHeight() * 0.5f);
|
---|
162 | quad.position.d_bottom /= (d_display_area.getHeight() * 0.5f);
|
---|
163 | quad.position.offset(Point(-1.0f, -1.0f));
|
---|
164 |
|
---|
165 | quad.z = -1 + z;
|
---|
166 | quad.texture = ((OgreCEGUITexture*)tex)->getOgreTexture();
|
---|
167 | quad.texPosition = texture_rect;
|
---|
168 |
|
---|
169 | // covert colours for ogre, note that top / bottom are switched.
|
---|
170 | quad.topLeftCol = colourToOgre(colours.d_bottom_left);
|
---|
171 | quad.topRightCol = colourToOgre(colours.d_bottom_right);
|
---|
172 | quad.bottomLeftCol = colourToOgre(colours.d_top_left);
|
---|
173 | quad.bottomRightCol = colourToOgre(colours.d_top_right);
|
---|
174 |
|
---|
175 | // set quad split mode
|
---|
176 | quad.splitMode = quad_split_mode;
|
---|
177 |
|
---|
178 | d_quadlist.insert(quad);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | /*************************************************************************
|
---|
185 | perform final rendering for all queued renderable quads.
|
---|
186 | *************************************************************************/
|
---|
187 | void OgreCEGUIRenderer::doRender(void)
|
---|
188 | {
|
---|
189 | // Render if overlays enabled and the quad list is not empty
|
---|
190 | if (d_render_sys->_getViewport()->getOverlaysEnabled() && !d_quadlist.empty())
|
---|
191 | {
|
---|
192 | /// Quad list needs to be sorted and thus the vertex buffer rebuilt. If not, we can
|
---|
193 | /// reuse the vertex buffer resulting in a nice speed gain.
|
---|
194 | if(!d_sorted)
|
---|
195 | {
|
---|
196 | sortQuads();
|
---|
197 | /// Resize vertex buffer if it is too small
|
---|
198 | size_t size = d_buffer->getNumVertices();
|
---|
199 | size_t requestedSize = d_quadlist.size()*VERTEX_PER_QUAD;
|
---|
200 | if(size < requestedSize)
|
---|
201 | {
|
---|
202 | /// Double buffer size until smaller than requested size
|
---|
203 | while(size < requestedSize)
|
---|
204 | size = size * 2;
|
---|
205 | /// Reallocate the buffer
|
---|
206 | destroyQuadRenderOp(d_render_op, d_buffer);
|
---|
207 | createQuadRenderOp(d_render_op, d_buffer, size);
|
---|
208 | }
|
---|
209 | else if(requestedSize < size/2 && d_underused_framecount >= UNDERUSED_FRAME_THRESHOLD)
|
---|
210 | {
|
---|
211 | /// Resize vertex buffer if it has been to big for too long
|
---|
212 | size = size / 2;
|
---|
213 | destroyQuadRenderOp(d_render_op, d_buffer);
|
---|
214 | createQuadRenderOp(d_render_op, d_buffer, size);
|
---|
215 | /// Reset underused framecount so it takes another UNDERUSED_FRAME_THRESHOLD to half again
|
---|
216 | d_underused_framecount = 0;
|
---|
217 | }
|
---|
218 | /// Fill the buffer
|
---|
219 | QuadVertex* buffmem;
|
---|
220 | buffmem = (QuadVertex*)d_buffer->lock(Ogre::HardwareVertexBuffer::HBL_DISCARD);
|
---|
221 | // iterate over each quad in the list
|
---|
222 | for (QuadList::iterator i = d_quadlist.begin(); i != d_quadlist.end(); ++i)
|
---|
223 | {
|
---|
224 | const QuadInfo& quad = (*i);
|
---|
225 | // setup Vertex 1...
|
---|
226 | buffmem->x = quad.position.d_left;
|
---|
227 | buffmem->y = quad.position.d_bottom;
|
---|
228 | buffmem->z = quad.z;
|
---|
229 | buffmem->diffuse = quad.topLeftCol;
|
---|
230 | buffmem->tu1 = quad.texPosition.d_left;
|
---|
231 | buffmem->tv1 = quad.texPosition.d_bottom;
|
---|
232 | ++buffmem;
|
---|
233 |
|
---|
234 | // setup Vertex 2...
|
---|
235 |
|
---|
236 | // top-left to bottom-right diagonal
|
---|
237 | if (quad.splitMode == TopLeftToBottomRight)
|
---|
238 | {
|
---|
239 | buffmem->x = quad.position.d_right;
|
---|
240 | buffmem->y = quad.position.d_bottom;
|
---|
241 | buffmem->z = quad.z;
|
---|
242 | buffmem->diffuse = quad.topRightCol;
|
---|
243 | buffmem->tu1 = quad.texPosition.d_right;
|
---|
244 | buffmem->tv1 = quad.texPosition.d_bottom;
|
---|
245 | }
|
---|
246 | // bottom-left to top-right diagonal
|
---|
247 | else
|
---|
248 | {
|
---|
249 | buffmem->x = quad.position.d_right;
|
---|
250 | buffmem->y = quad.position.d_top;
|
---|
251 | buffmem->z = quad.z;
|
---|
252 | buffmem->diffuse = quad.bottomRightCol;
|
---|
253 | buffmem->tu1 = quad.texPosition.d_right;
|
---|
254 | buffmem->tv1 = quad.texPosition.d_top;
|
---|
255 | }
|
---|
256 | ++buffmem;
|
---|
257 |
|
---|
258 | // setup Vertex 3...
|
---|
259 | buffmem->x = quad.position.d_left;
|
---|
260 | buffmem->y = quad.position.d_top;
|
---|
261 | buffmem->z = quad.z;
|
---|
262 | buffmem->diffuse = quad.bottomLeftCol;
|
---|
263 | buffmem->tu1 = quad.texPosition.d_left;
|
---|
264 | buffmem->tv1 = quad.texPosition.d_top;
|
---|
265 | ++buffmem;
|
---|
266 |
|
---|
267 | // setup Vertex 4...
|
---|
268 | buffmem->x = quad.position.d_right;
|
---|
269 | buffmem->y = quad.position.d_bottom;
|
---|
270 | buffmem->z = quad.z;
|
---|
271 | buffmem->diffuse = quad.topRightCol;
|
---|
272 | buffmem->tu1 = quad.texPosition.d_right;
|
---|
273 | buffmem->tv1 = quad.texPosition.d_bottom;
|
---|
274 | ++buffmem;
|
---|
275 |
|
---|
276 | // setup Vertex 5...
|
---|
277 | buffmem->x = quad.position.d_right;
|
---|
278 | buffmem->y = quad.position.d_top;
|
---|
279 | buffmem->z = quad.z;
|
---|
280 | buffmem->diffuse = quad.bottomRightCol;
|
---|
281 | buffmem->tu1 = quad.texPosition.d_right;
|
---|
282 | buffmem->tv1 = quad.texPosition.d_top;
|
---|
283 | ++buffmem;
|
---|
284 |
|
---|
285 | // setup Vertex 6...
|
---|
286 |
|
---|
287 | // top-left to bottom-right diagonal
|
---|
288 | if (quad.splitMode == TopLeftToBottomRight)
|
---|
289 | {
|
---|
290 | buffmem->x = quad.position.d_left;
|
---|
291 | buffmem->y = quad.position.d_top;
|
---|
292 | buffmem->z = quad.z;
|
---|
293 | buffmem->diffuse = quad.bottomLeftCol;
|
---|
294 | buffmem->tu1 = quad.texPosition.d_left;
|
---|
295 | buffmem->tv1 = quad.texPosition.d_top;
|
---|
296 | }
|
---|
297 | // bottom-left to top-right diagonal
|
---|
298 | else
|
---|
299 | {
|
---|
300 | buffmem->x = quad.position.d_left;
|
---|
301 | buffmem->y = quad.position.d_bottom;
|
---|
302 | buffmem->z = quad.z;
|
---|
303 | buffmem->diffuse = quad.topLeftCol;
|
---|
304 | buffmem->tu1 = quad.texPosition.d_left;
|
---|
305 | buffmem->tv1 = quad.texPosition.d_bottom;
|
---|
306 | }
|
---|
307 | ++buffmem;
|
---|
308 | }
|
---|
309 |
|
---|
310 | // ensure we leave the buffer in the unlocked state
|
---|
311 | d_buffer->unlock();
|
---|
312 | }
|
---|
313 |
|
---|
314 | /// Render the buffer
|
---|
315 | initRenderStates();
|
---|
316 | d_bufferPos = 0;
|
---|
317 |
|
---|
318 | // Iterate over each quad in the list and render it
|
---|
319 | QuadList::iterator i = d_quadlist.begin();
|
---|
320 | while(i != d_quadlist.end())
|
---|
321 | {
|
---|
322 |
|
---|
323 | d_currTexture = i->texture;
|
---|
324 | d_render_op.vertexData->vertexStart = d_bufferPos;
|
---|
325 | for (; i != d_quadlist.end(); ++i)
|
---|
326 | {
|
---|
327 | const QuadInfo& quad = (*i);
|
---|
328 | if (d_currTexture != quad.texture)
|
---|
329 | /// If it has a different texture, render this quad in next operation
|
---|
330 | break;
|
---|
331 | d_bufferPos += VERTEX_PER_QUAD;
|
---|
332 | }
|
---|
333 | d_render_op.vertexData->vertexCount = d_bufferPos - d_render_op.vertexData->vertexStart;
|
---|
334 | /// Set texture, and do the render
|
---|
335 | d_render_sys->_setTexture(0, true, d_currTexture->getName());
|
---|
336 | d_render_sys->_render(d_render_op);
|
---|
337 | }
|
---|
338 |
|
---|
339 | }
|
---|
340 | /// Count frames to check if utilization of vertex buffer was below half the capacity for 500,000 frames
|
---|
341 | if(d_bufferPos < d_buffer->getNumVertices()/2)
|
---|
342 | d_underused_framecount++;
|
---|
343 | else
|
---|
344 | d_underused_framecount = 0;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | /*************************************************************************
|
---|
349 | clear the queue
|
---|
350 | *************************************************************************/
|
---|
351 | void OgreCEGUIRenderer::clearRenderList(void)
|
---|
352 | {
|
---|
353 | d_sorted = true;
|
---|
354 | d_quadlist.clear();
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /*************************************************************************
|
---|
359 | create an empty texture
|
---|
360 | *************************************************************************/
|
---|
361 | Texture* OgreCEGUIRenderer::createTexture(void)
|
---|
362 | {
|
---|
363 | OgreCEGUITexture* tex = new OgreCEGUITexture(this);
|
---|
364 | d_texturelist.push_back(tex);
|
---|
365 | return tex;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /*************************************************************************
|
---|
370 | create a texture and load it with the specified file.
|
---|
371 | *************************************************************************/
|
---|
372 | Texture* OgreCEGUIRenderer::createTexture(const String& filename, const String& resourceGroup)
|
---|
373 | {
|
---|
374 | OgreCEGUITexture* tex = (OgreCEGUITexture*)createTexture();
|
---|
375 | tex->loadFromFile(filename, resourceGroup);
|
---|
376 |
|
---|
377 | return tex;
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /*************************************************************************
|
---|
382 | create a texture and set it to the specified size
|
---|
383 | *************************************************************************/
|
---|
384 | Texture* OgreCEGUIRenderer::createTexture(float size)
|
---|
385 | {
|
---|
386 | OgreCEGUITexture* tex = (OgreCEGUITexture*)createTexture();
|
---|
387 | tex->setOgreTextureSize((uint)size);
|
---|
388 |
|
---|
389 | return tex;
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | /*************************************************************************
|
---|
394 | destroy the given texture
|
---|
395 | *************************************************************************/
|
---|
396 | void OgreCEGUIRenderer::destroyTexture(Texture* texture)
|
---|
397 | {
|
---|
398 | if (texture != NULL)
|
---|
399 | {
|
---|
400 | OgreCEGUITexture* tex = (OgreCEGUITexture*)texture;
|
---|
401 |
|
---|
402 | d_texturelist.remove(tex);
|
---|
403 | delete tex;
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | /*************************************************************************
|
---|
409 | destroy all textures still active
|
---|
410 | *************************************************************************/
|
---|
411 | void OgreCEGUIRenderer::destroyAllTextures(void)
|
---|
412 | {
|
---|
413 | while (!d_texturelist.empty())
|
---|
414 | {
|
---|
415 | destroyTexture(*(d_texturelist.begin()));
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | /*************************************************************************
|
---|
421 | setup states etc
|
---|
422 | *************************************************************************/
|
---|
423 | void OgreCEGUIRenderer::initRenderStates(void)
|
---|
424 | {
|
---|
425 | using namespace Ogre;
|
---|
426 |
|
---|
427 | // set-up matrices
|
---|
428 | d_render_sys->_setWorldMatrix(Matrix4::IDENTITY);
|
---|
429 | d_render_sys->_setViewMatrix(Matrix4::IDENTITY);
|
---|
430 | d_render_sys->_setProjectionMatrix(Matrix4::IDENTITY);
|
---|
431 |
|
---|
432 | // initialise render settings
|
---|
433 | d_render_sys->setLightingEnabled(false);
|
---|
434 | d_render_sys->_setDepthBufferParams(false, false);
|
---|
435 | d_render_sys->_setCullingMode(CULL_NONE);
|
---|
436 | d_render_sys->_setFog(FOG_NONE);
|
---|
437 | d_render_sys->_setColourBufferWriteEnabled(true, true, true, true);
|
---|
438 | d_render_sys->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
|
---|
439 | d_render_sys->unbindGpuProgram(GPT_VERTEX_PROGRAM);
|
---|
440 | d_render_sys->setShadingType(SO_GOURAUD);
|
---|
441 | d_render_sys->_setRasterisationMode(SDL_SOLID);
|
---|
442 |
|
---|
443 | // initialise texture settings
|
---|
444 | d_render_sys->_setTextureCoordCalculation(0, TEXCALC_NONE);
|
---|
445 | d_render_sys->_setTextureCoordSet(0, 0);
|
---|
446 | d_render_sys->_setTextureUnitFiltering(0, FO_LINEAR, FO_LINEAR, FO_POINT);
|
---|
447 | d_render_sys->_setTextureAddressingMode(0, TextureUnitState::TAM_CLAMP);
|
---|
448 | d_render_sys->_setTextureMatrix(0, Matrix4::IDENTITY);
|
---|
449 | d_render_sys->_setAlphaRejectSettings(CMPF_ALWAYS_PASS, 0);
|
---|
450 | d_render_sys->_setTextureBlendMode(0, d_colourBlendMode);
|
---|
451 | d_render_sys->_setTextureBlendMode(0, d_alphaBlendMode);
|
---|
452 | d_render_sys->_disableTextureUnitsFrom(1);
|
---|
453 |
|
---|
454 | // enable alpha blending
|
---|
455 | d_render_sys->_setSceneBlending(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA);
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 |
|
---|
460 | /*************************************************************************
|
---|
461 | sort quads list according to texture
|
---|
462 | *************************************************************************/
|
---|
463 | void OgreCEGUIRenderer::sortQuads(void)
|
---|
464 | {
|
---|
465 | if (!d_sorted)
|
---|
466 | {
|
---|
467 | d_sorted = true;
|
---|
468 | }
|
---|
469 |
|
---|
470 | }
|
---|
471 |
|
---|
472 | /*************************************************************************
|
---|
473 | render a quad directly to the display
|
---|
474 | *************************************************************************/
|
---|
475 | void OgreCEGUIRenderer::renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode)
|
---|
476 | {
|
---|
477 | if (d_render_sys->_getViewport()->getOverlaysEnabled())
|
---|
478 | {
|
---|
479 | z = -1 + z;
|
---|
480 |
|
---|
481 | Rect final_rect;
|
---|
482 |
|
---|
483 | // set quad position, flipping y co-ordinates, and applying appropriate texel origin offset
|
---|
484 | final_rect.d_left = dest_rect.d_left;
|
---|
485 | final_rect.d_right = dest_rect.d_right;
|
---|
486 | final_rect.d_top = d_display_area.getHeight() - dest_rect.d_top;
|
---|
487 | final_rect.d_bottom = d_display_area.getHeight() - dest_rect.d_bottom;
|
---|
488 | final_rect.offset(d_texelOffset);
|
---|
489 |
|
---|
490 | // convert quad co-ordinates for a -1 to 1 co-ordinate system.
|
---|
491 | final_rect.d_left /= (d_display_area.getWidth() * 0.5f);
|
---|
492 | final_rect.d_right /= (d_display_area.getWidth() * 0.5f);
|
---|
493 | final_rect.d_top /= (d_display_area.getHeight() * 0.5f);
|
---|
494 | final_rect.d_bottom /= (d_display_area.getHeight() * 0.5f);
|
---|
495 | final_rect.offset(Point(-1.0f, -1.0f));
|
---|
496 |
|
---|
497 | // convert colours for ogre, note that top / bottom are switched.
|
---|
498 | uint32 topLeftCol = colourToOgre(colours.d_bottom_left);
|
---|
499 | uint32 topRightCol = colourToOgre(colours.d_bottom_right);
|
---|
500 | uint32 bottomLeftCol = colourToOgre(colours.d_top_left);
|
---|
501 | uint32 bottomRightCol= colourToOgre(colours.d_top_right);
|
---|
502 |
|
---|
503 | QuadVertex* buffmem = (QuadVertex*)d_direct_buffer->lock(Ogre::HardwareVertexBuffer::HBL_DISCARD);
|
---|
504 |
|
---|
505 | // setup Vertex 1...
|
---|
506 | buffmem->x = final_rect.d_left;
|
---|
507 | buffmem->y = final_rect. d_bottom;
|
---|
508 | buffmem->z = z;
|
---|
509 | buffmem->diffuse = topLeftCol;
|
---|
510 | buffmem->tu1 = texture_rect.d_left;
|
---|
511 | buffmem->tv1 = texture_rect.d_bottom;
|
---|
512 | ++buffmem;
|
---|
513 |
|
---|
514 | // setup Vertex 2...
|
---|
515 |
|
---|
516 | // top-left to bottom-right diagonal
|
---|
517 | if (quad_split_mode == TopLeftToBottomRight)
|
---|
518 | {
|
---|
519 | buffmem->x = final_rect.d_right;
|
---|
520 | buffmem->y = final_rect.d_bottom;
|
---|
521 | buffmem->z = z;
|
---|
522 | buffmem->diffuse = topRightCol;
|
---|
523 | buffmem->tu1 = texture_rect.d_right;
|
---|
524 | buffmem->tv1 = texture_rect.d_bottom;
|
---|
525 | ++buffmem;
|
---|
526 | }
|
---|
527 | // bottom-left to top-right diagonal
|
---|
528 | else
|
---|
529 | {
|
---|
530 | buffmem->x = final_rect.d_right;
|
---|
531 | buffmem->y = final_rect.d_top;
|
---|
532 | buffmem->z = z;
|
---|
533 | buffmem->diffuse = bottomRightCol;
|
---|
534 | buffmem->tu1 = texture_rect.d_right;
|
---|
535 | buffmem->tv1 = texture_rect.d_top;
|
---|
536 | ++buffmem;
|
---|
537 | }
|
---|
538 |
|
---|
539 | // setup Vertex 3...
|
---|
540 | buffmem->x = final_rect.d_left;
|
---|
541 | buffmem->y = final_rect.d_top;
|
---|
542 | buffmem->z = z;
|
---|
543 | buffmem->diffuse = bottomLeftCol;
|
---|
544 | buffmem->tu1 = texture_rect.d_left;
|
---|
545 | buffmem->tv1 = texture_rect.d_top;
|
---|
546 | ++buffmem;
|
---|
547 |
|
---|
548 | // setup Vertex 4...
|
---|
549 | buffmem->x = final_rect.d_right;
|
---|
550 | buffmem->y = final_rect.d_bottom;
|
---|
551 | buffmem->z = z;
|
---|
552 | buffmem->diffuse = topRightCol;
|
---|
553 | buffmem->tu1 = texture_rect.d_right;
|
---|
554 | buffmem->tv1 = texture_rect.d_bottom;
|
---|
555 | ++buffmem;
|
---|
556 |
|
---|
557 | // setup Vertex 5...
|
---|
558 | buffmem->x = final_rect.d_right;
|
---|
559 | buffmem->y = final_rect.d_top;
|
---|
560 | buffmem->z = z;
|
---|
561 | buffmem->diffuse = bottomRightCol;
|
---|
562 | buffmem->tu1 = texture_rect.d_right;
|
---|
563 | buffmem->tv1 = texture_rect.d_top;
|
---|
564 | ++buffmem;
|
---|
565 |
|
---|
566 | // setup Vertex 6...
|
---|
567 |
|
---|
568 | // top-left to bottom-right diagonal
|
---|
569 | if (quad_split_mode == TopLeftToBottomRight)
|
---|
570 | {
|
---|
571 | buffmem->x = final_rect.d_left;
|
---|
572 | buffmem->y = final_rect.d_top;
|
---|
573 | buffmem->z = z;
|
---|
574 | buffmem->diffuse = bottomLeftCol;
|
---|
575 | buffmem->tu1 = texture_rect.d_left;
|
---|
576 | buffmem->tv1 = texture_rect.d_top;
|
---|
577 | }
|
---|
578 | // bottom-left to top-right diagonal
|
---|
579 | else
|
---|
580 | {
|
---|
581 | buffmem->x = final_rect.d_left;
|
---|
582 | buffmem->y = final_rect.d_bottom;
|
---|
583 | buffmem->z = z;
|
---|
584 | buffmem->diffuse = topLeftCol;
|
---|
585 | buffmem->tu1 = texture_rect.d_left;
|
---|
586 | buffmem->tv1 = texture_rect.d_bottom;
|
---|
587 | }
|
---|
588 |
|
---|
589 | d_direct_buffer->unlock();
|
---|
590 |
|
---|
591 | //
|
---|
592 | // perform rendering...
|
---|
593 | //
|
---|
594 | initRenderStates();
|
---|
595 | d_render_sys->_setTexture(0, true, ((OgreCEGUITexture*)tex)->getOgreTexture()->getName());
|
---|
596 | d_direct_render_op.vertexData->vertexCount = VERTEX_PER_QUAD;
|
---|
597 | d_render_sys->_render(d_direct_render_op);
|
---|
598 | }
|
---|
599 |
|
---|
600 | }
|
---|
601 |
|
---|
602 | /*************************************************************************
|
---|
603 | convert ARGB colour value to whatever the Ogre render system is
|
---|
604 | expecting.
|
---|
605 | *************************************************************************/
|
---|
606 | uint32 OgreCEGUIRenderer::colourToOgre(const colour& col) const
|
---|
607 | {
|
---|
608 | Ogre::ColourValue cv(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha());
|
---|
609 |
|
---|
610 | uint32 final;
|
---|
611 | d_render_sys->convertColourValue(cv, &final);
|
---|
612 |
|
---|
613 | return final;
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 | /*************************************************************************
|
---|
618 | Set the scene manager to be used for rendering the GUI.
|
---|
619 | *************************************************************************/
|
---|
620 | void OgreCEGUIRenderer::setTargetSceneManager(Ogre::SceneType scene_type)
|
---|
621 | {
|
---|
622 | setTargetSceneManager(d_ogre_root->getSceneManager(scene_type));
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | /*************************************************************************
|
---|
627 | Set the scene manager to be used for rendering the GUI.
|
---|
628 | *************************************************************************/
|
---|
629 | void OgreCEGUIRenderer::setTargetSceneManager(Ogre::SceneManager* scene_manager)
|
---|
630 | {
|
---|
631 | // unhook from current scene manager.
|
---|
632 | if (d_sceneMngr != NULL)
|
---|
633 | {
|
---|
634 | d_sceneMngr->removeRenderQueueListener(d_ourlistener);
|
---|
635 | d_sceneMngr = NULL;
|
---|
636 | }
|
---|
637 |
|
---|
638 | // hook new scene manager if that is not NULL
|
---|
639 | if (scene_manager != NULL)
|
---|
640 | {
|
---|
641 | d_sceneMngr = scene_manager;
|
---|
642 | d_sceneMngr->addRenderQueueListener(d_ourlistener);
|
---|
643 | }
|
---|
644 |
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | /*************************************************************************
|
---|
649 | Set the target render queue for GUI rendering.
|
---|
650 | *************************************************************************/
|
---|
651 | void OgreCEGUIRenderer::setTargetRenderQueue(Ogre::RenderQueueGroupID queue_id, bool post_queue)
|
---|
652 | {
|
---|
653 | d_queue_id = queue_id;
|
---|
654 | d_post_queue = post_queue;
|
---|
655 |
|
---|
656 | if (d_ourlistener != NULL)
|
---|
657 | {
|
---|
658 | d_ourlistener->setTargetRenderQueue(queue_id);
|
---|
659 | d_ourlistener->setPostRenderQueue(post_queue);
|
---|
660 | }
|
---|
661 |
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /*************************************************************************
|
---|
666 | perform main work of the constructor
|
---|
667 | *************************************************************************/
|
---|
668 | void OgreCEGUIRenderer::constructor_impl(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id, bool post_queue, uint max_quads)
|
---|
669 | {
|
---|
670 | using namespace Ogre;
|
---|
671 |
|
---|
672 | // initialise the renderer fields
|
---|
673 | d_queueing = true;
|
---|
674 | d_queue_id = queue_id;
|
---|
675 | d_currTexture.isNull();
|
---|
676 | d_post_queue = post_queue;
|
---|
677 | d_sceneMngr = NULL;
|
---|
678 | d_bufferPos = 0;
|
---|
679 | d_sorted = true;
|
---|
680 | d_ogre_root = Root::getSingletonPtr();
|
---|
681 | d_render_sys = d_ogre_root->getRenderSystem();
|
---|
682 |
|
---|
683 | // Create and initialise the Ogre specific parts required for use in rendering later.
|
---|
684 | // Main GUI
|
---|
685 | createQuadRenderOp(d_render_op, d_buffer, VERTEXBUFFER_INITIAL_CAPACITY);
|
---|
686 | d_underused_framecount = 0;
|
---|
687 |
|
---|
688 | // Mouse cursor
|
---|
689 | createQuadRenderOp(d_direct_render_op, d_direct_buffer, VERTEX_PER_QUAD);
|
---|
690 |
|
---|
691 | // Discover display settings and setup d_display_area
|
---|
692 | d_display_area.d_left = 0;
|
---|
693 | d_display_area.d_top = 0;
|
---|
694 | d_display_area.d_right = window->getWidth();
|
---|
695 | d_display_area.d_bottom = window->getHeight();
|
---|
696 |
|
---|
697 | // initialise required texel offset
|
---|
698 | d_texelOffset = Point((float)d_render_sys->getHorizontalTexelOffset(), -(float)d_render_sys->getVerticalTexelOffset());
|
---|
699 |
|
---|
700 | // create listener which will handler the rendering side of things for us.
|
---|
701 | d_ourlistener = new CEGUIRQListener(this, queue_id, post_queue);
|
---|
702 |
|
---|
703 | // Initialise blending modes to be used.
|
---|
704 | d_colourBlendMode.blendType = Ogre::LBT_COLOUR;
|
---|
705 | d_colourBlendMode.source1 = Ogre::LBS_TEXTURE;
|
---|
706 | d_colourBlendMode.source2 = Ogre::LBS_DIFFUSE;
|
---|
707 | d_colourBlendMode.operation = Ogre::LBX_MODULATE;
|
---|
708 |
|
---|
709 | d_alphaBlendMode.blendType = Ogre::LBT_ALPHA;
|
---|
710 | d_alphaBlendMode.source1 = Ogre::LBS_TEXTURE;
|
---|
711 | d_alphaBlendMode.source2 = Ogre::LBS_DIFFUSE;
|
---|
712 | d_alphaBlendMode.operation = Ogre::LBX_MODULATE;
|
---|
713 | }
|
---|
714 |
|
---|
715 |
|
---|
716 | /*************************************************************************
|
---|
717 | Create a texture from an existing Ogre::TexturePtr object
|
---|
718 | *************************************************************************/
|
---|
719 | Texture* OgreCEGUIRenderer::createTexture(Ogre::TexturePtr& texture)
|
---|
720 | {
|
---|
721 | OgreCEGUITexture* t = (OgreCEGUITexture*)createTexture();
|
---|
722 |
|
---|
723 | if (!texture.isNull())
|
---|
724 | {
|
---|
725 | t->setOgreTexture(texture);
|
---|
726 | }
|
---|
727 |
|
---|
728 | return t;
|
---|
729 |
|
---|
730 | }
|
---|
731 |
|
---|
732 | /*************************************************************************
|
---|
733 | Create a resource provider object
|
---|
734 | *************************************************************************/
|
---|
735 | ResourceProvider* OgreCEGUIRenderer::createResourceProvider(void)
|
---|
736 | {
|
---|
737 | d_resourceProvider = new OgreCEGUIResourceProvider();
|
---|
738 | return d_resourceProvider;
|
---|
739 | }
|
---|
740 |
|
---|
741 | /*************************************************************************
|
---|
742 | Callback from Ogre invoked before other stuff in our target queue
|
---|
743 | is rendered
|
---|
744 | *************************************************************************/
|
---|
745 | void CEGUIRQListener::renderQueueStarted(Ogre::RenderQueueGroupID id, bool& skipThisQueue)
|
---|
746 | {
|
---|
747 | if ((!d_post_queue) && (d_queue_id == id))
|
---|
748 | {
|
---|
749 | CEGUI::System::getSingleton().renderGUI();
|
---|
750 | }
|
---|
751 |
|
---|
752 | }
|
---|
753 |
|
---|
754 |
|
---|
755 | /*************************************************************************
|
---|
756 | Callback from Ogre invoked after other stuff in our target queue
|
---|
757 | is rendered
|
---|
758 | *************************************************************************/
|
---|
759 | void CEGUIRQListener::renderQueueEnded(Ogre::RenderQueueGroupID id, bool& repeatThisQueue)
|
---|
760 | {
|
---|
761 | if ((d_post_queue) && (d_queue_id == id))
|
---|
762 | {
|
---|
763 | CEGUI::System::getSingleton().renderGUI();
|
---|
764 | }
|
---|
765 |
|
---|
766 | }
|
---|
767 |
|
---|
768 | } // End of CEGUI namespace section
|
---|