1 /***********************************************************************
2     created:    Sun Jan 11 2009
3     author:     Paul D Turner
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUIOpenGLESRenderer_h_
28 #define _CEGUIOpenGLESRenderer_h_
29 
30 #include "CEGUI/Base.h"
31 #include "CEGUI/Renderer.h"
32 #include "CEGUI/Size.h"
33 #include "CEGUI/Vector.h"
34 #include "CEGUI/RendererModules/OpenGLES/GLES.h"
35 #include <vector>
36 #include <map>
37 
38 #if (defined( __WIN32__ ) || defined( _WIN32 )) && !defined(CEGUI_STATIC)
39 #   ifdef CEGUIOPENGLESRENDERER_EXPORTS
40 #       define OPENGLES_GUIRENDERER_API __declspec(dllexport)
41 #   else
42 #       define OPENGLES_GUIRENDERER_API __declspec(dllimport)
43 #   endif
44 #else
45 #   define OPENGLES_GUIRENDERER_API
46 #endif
47 
48 #if defined(_MSC_VER)
49 #   pragma warning(push)
50 #   pragma warning(disable : 4251)
51 #endif
52 
53 
54 // Start of CEGUI namespace section
55 namespace CEGUI
56 {
57 class OpenGLESTexture;
58 class OpenGLESTextureTarget;
59 class OpenGLESGeometryBuffer;
60 class OGLTextureTargetFactory;
61 
62 /*!
63 \brief
64     Renderer class to interface with OpenGLES
65 */
66 class OPENGLES_GUIRENDERER_API OpenGLESRenderer : public Renderer
67 {
68 public:
69     //! Enumeration of valid texture target types.
70     enum TextureTargetType
71     {
72         //! Automatically choose the best type available.
73         TTT_AUTO,
74         //! Use targets based on frame buffer objects if available, else none.
75         TTT_FBO,
76         //! Use targets based on pbuffer support if available, else none.
77         TTT_PBUFFER,
78         //! Disable texture targets.
79         TTT_NONE
80     };
81 
82     /*!
83     \brief
84         Convenience function that creates the required objects to initialise the
85         CEGUI system.
86 
87         The created Renderer will use the current OpenGL ES viewport as it's
88         default surface size.
89 
90         This will create and initialise the following objects for you:
91         - CEGUI::OpenGLESRenderer
92         - CEGUI::DefaultResourceProvider
93         - CEGUI::System
94 
95     \param tt_type
96         Specifies one of the TextureTargetType enumerated values indicating the
97         desired TextureTarget type to be used.  Defaults to TTT_AUTO.
98 
99     \param abi
100         This must be set to CEGUI_VERSION_ABI
101 
102     \return
103         Reference to the CEGUI::OpenGLESRenderer object that was created.
104     */
105     static OpenGLESRenderer& bootstrapSystem(
106                                 const TextureTargetType tt_type = TTT_AUTO,
107                                 const int abi = CEGUI_VERSION_ABI);
108 
109     /*!
110     \brief
111         Convenience function that creates the required objects to initialise the
112         CEGUI system.
113 
114         The created Renderer will use /a display_size as the default surface
115         size.
116 
117         This will create and initialise the following objects for you:
118         - CEGUI::OpenGLESRenderer
119         - CEGUI::DefaultResourceProvider
120         - CEGUI::System
121 
122     \param display_size
123         Size object describing the initial display dimensions.
124 
125     \param tt_type
126         Specifies one of the TextureTargetType enumerated values indicating the
127         desired TextureTarget type to be used.  Defaults to TTT_AUTO.
128 
129     \param abi
130         This must be set to CEGUI_VERSION_ABI
131 
132     \return
133         Reference to the CEGUI::OpenGLESRenderer object that was created.
134     */
135     static OpenGLESRenderer& bootstrapSystem(
136                                 const Sizef& display_size,
137                                 const TextureTargetType tt_type = TTT_AUTO,
138                                 const int abi = CEGUI_VERSION_ABI);
139 
140     /*!
141     \brief
142         Convenience function to cleanup the CEGUI system and related objects
143         that were created by calling the bootstrapSystem function.
144 
145         This function will destroy the following objects for you:
146         - CEGUI::System
147         - CEGUI::DefaultResourceProvider
148         - CEGUI::OpenGLRenderer
149 
150     \note
151         If you did not initialise CEGUI by calling the bootstrapSystem function,
152         you should \e not call this, but rather delete any objects you created
153         manually.
154     */
155     static void destroySystem();
156 
157     /*!
158     \brief
159         Create an OpenGLESRenderer object.
160 
161     \param tt_type
162         Specifies one of the TextureTargetType enumerated values indicating the
163         desired TextureTarget type to be used.
164 
165     \param abi
166         This must be set to CEGUI_VERSION_ABI
167     */
168     static OpenGLESRenderer& create(const TextureTargetType tt_type = TTT_AUTO,
169                                     const int abi = CEGUI_VERSION_ABI);
170 
171     /*!
172     \brief
173         Create an OpenGLESRenderer object.
174 
175     \param display_size
176         Size object describing the initial display resolution.
177 
178     \param tt_type
179         Specifies one of the TextureTargetType enumerated values indicating the
180         desired TextureTarget type to be used.
181 
182     \param abi
183         This must be set to CEGUI_VERSION_ABI
184     */
185     static OpenGLESRenderer& create(const Sizef& display_size,
186                                     const TextureTargetType tt_type = TTT_AUTO,
187                                     const int abi = CEGUI_VERSION_ABI);
188 
189     /*!
190     \brief
191         Destroy an OpenGLESRenderer object.
192 
193     \param renderer
194         The OpenGLESRenderer object to be destroyed.
195     */
196     static void destroy(OpenGLESRenderer& renderer);
197 
198 	/*!
199     \brief
200         Check if provided extension is supported on current platform.
201 		Khronos reference implementation.
202 
203     \param extension
204         Provided extension string identification
205     */
206 	static bool isGLExtensionSupported( const char* extension );
207 
208     // implement Renderer interface
209     RenderTarget& getDefaultRenderTarget();
210     GeometryBuffer& createGeometryBuffer();
211     void destroyGeometryBuffer(const GeometryBuffer& buffer);
212     void destroyAllGeometryBuffers();
213     TextureTarget* createTextureTarget();
214     void destroyTextureTarget(TextureTarget* target);
215     void destroyAllTextureTargets();
216     Texture& createTexture(const String& name);
217     Texture& createTexture(const String& name,
218                            const String& filename,
219                            const String& resourceGroup);
220     Texture& createTexture(const String& name, const Sizef& size);
221     void destroyTexture(Texture& texture);
222     void destroyTexture(const String& name);
223     void destroyAllTextures();
224     Texture& getTexture(const String& name) const;
225     bool isTextureDefined(const String& name) const;
226     void beginRendering();
227     void endRendering();
228     void setDisplaySize(const Sizef& sz);
229     const Sizef& getDisplaySize() const;
230     const Vector2f& getDisplayDPI() const;
231     uint getMaxTextureSize() const;
232     const String& getIdentifierString() const;
233 
234     /*!
235     \brief
236         Create a texture that uses an existing OpenGLES texture with the specified
237         size.  Note that it is your responsibility to ensure that the OpenGLES
238         texture is valid and that the specified size is accurate.
239 
240     \param sz
241         Size object that describes the pixel size of the OpenGLES texture
242         identified by \a tex.
243 
244     \return
245         Texture object that wraps the OpenGLES texture \a tex, and whose size is
246         specified to be \a sz.
247     */
248     Texture& createTexture(const String& name, GLuint tex, const Sizef& sz);
249 
250     /*!
251     \brief
252         Tells the renderer to initialise some extra states beyond what it
253         directly needs itself for CEGUI.
254 
255         This option is useful in cases where you've made changes to the default
256         OpenGLES state and do not want to save/restore those between CEGUI
257         rendering calls.  Note that this option will not deal with every
258         possible state or extension - if you want a state added here, make a
259         request and we'll consider it ;)
260     */
261     void enableExtraStateSettings(bool setting);
262 
263     /*!
264     \brief
265         Grabs all the loaded textures from Texture RAM and stores them in a
266         local data buffer.  This function invalidates all textures, and
267         restoreTextures must be called before any CEGUI rendering is done for
268         predictable results.
269     */
270     void grabTextures();
271 
272     /*!
273     \brief
274         Restores all the loaded textures from the local data buffers previously
275         created by 'grabTextures'
276     */
277     void restoreTextures();
278 
279     /*!
280     \brief
281         Helper to return a valid texture size according to reported OpenGLES
282         capabilities.
283 
284     \param sz
285         Size object containing input size.
286 
287     \return
288         Size object containing - possibly different - output size.
289     */
290     Sizef getAdjustedTextureSize(const Sizef& sz) const;
291 
292     /*!
293     \brief
294         Utility function that will return \a f if it's a power of two, or the
295         next power of two up from \a f if it's not.
296     */
297     static float getNextPOTSize(const float f);
298 
299     /*!
300     \brief
301         Returns if the texture coordinate system is vertically flipped or not. The original of a
302         texture coordinate system is typically located either at the the top-left or the bottom-left.
303         CEGUI, Direct3D and most rendering engines assume it to be on the top-left. OpenGL assumes it to
304         be at the bottom left.
305 
306         This function is intended to be used when generating geometry for rendering the TextureTarget
307         onto another surface. It is also intended to be used when trying to use a custom texture (RTT)
308         inside CEGUI using the Image class, in order to determine the Image coordinates correctly.
309 
310     \return
311         - true if flipping is required: the texture coordinate origin is at the bottom left
312         - false if flipping is not required: the texture coordinate origin is at the top left
313     */
isTexCoordSystemFlipped()314     bool isTexCoordSystemFlipped() const { return true; }
315 
316 private:
317     /*!
318     \brief
319         Constructor for OpenGLES Renderer objects
320 
321     \param tt_type
322         Specifies one of the TextureTargetType enumerated values indicating the
323         desired TextureTarget type to be used.
324     */
325     OpenGLESRenderer(const TextureTargetType tt_type);
326 
327     /*!
328     \brief
329         Constructor for OpenGLES Renderer objects.
330 
331     \param display_size
332         Size object describing the initial display resolution.
333 
334     \param tt_type
335         Specifies one of the TextureTargetType enumerated values indicating the
336         desired TextureTarget type to be used.
337     */
338     OpenGLESRenderer(const Sizef& display_size, const TextureTargetType tt_type);
339 
340     /*!
341     \brief
342         Destructor for OpenGLESRenderer objects
343     */
344     virtual ~OpenGLESRenderer();
345 
346     //! init the extra GL states enabled via enableExtraStateSettings
347     void setupExtraStates();
348 
349     //! cleanup the extra GL states enabled via enableExtraStateSettings
350     void cleanupExtraStates();
351 
352     //! initialise OGLTextureTargetFactory that will generate TextureTargets
353     void initialiseTextureTargetFactory(const TextureTargetType tt_type);
354 
355     //! Log about the fact we destroyed a named texture.
356     void logTextureDestruction(const String& name);
357 
358 	//! Replacement for glPushAttrib =(
359 	struct RenderStates
360 	{
361 		GLboolean glScissorTest;
362 		GLboolean texturing;
363 		GLboolean blend;
364 		GLint arrayBuffer;
365 		GLint texture;
366 		GLint texEnvParam;
367 	} glPreRenderStates;
368 
369     //! String holding the renderer identification text.
370     static String d_rendererID;
371     //! What the renderer considers to be the current display size.
372     Sizef d_displaySize;
373     //! What the renderer considers to be the current display DPI resolution.
374     Vector2f d_displayDPI;
375     //! The default RenderTarget
376     RenderTarget* d_defaultTarget;
377     //! container type used to hold TextureTargets we create.
378     typedef std::vector<TextureTarget*> TextureTargetList;
379     //! Container used to track texture targets.
380     TextureTargetList d_textureTargets;
381     //! container type used to hold GeometryBuffers we create.
382     typedef std::vector<OpenGLESGeometryBuffer*> GeometryBufferList;
383     //! Container used to track geometry buffers.
384     GeometryBufferList d_geometryBuffers;
385     //! container type used to hold Textures we create.
386     typedef std::map<String, OpenGLESTexture*, StringFastLessCompare
387                      CEGUI_MAP_ALLOC(String, OpenGLESTexture*)> TextureMap;
388     //! Container used to track textures.
389     TextureMap d_textures;
390     //! What the renderer thinks the max texture size is.
391     uint d_maxTextureSize;
392     //! option of whether to initialise extra states that may not be at default
393     bool d_initExtraStates;
394     //! pointer to a helper that creates TextureTargets supported by the system.
395     OGLTextureTargetFactory* d_textureTargetFactory;
396   };
397 
398 } // End of  CEGUI namespace section
399 
400 #if defined(_MSC_VER)
401 #   pragma warning(pop)
402 #endif
403 
404 #endif // end of guard _CEGUIOpenGLESRenderer_h_
405 
406