2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUiEffects_RendererEffectRenderer.h
19 * @brief This is the header file for the EffectRenderer class
22 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_
23 #define _FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_
29 #include <unique_ptr.h>
31 #include <FGraphicsOpengl2.h>
32 #include <FGraphics.h>
33 #include <FUiControl.h>
35 #include "FUiEffects_RendererViewport.h"
36 #include "FUiEffects_RuntimeRenderDataScene.h"
37 #include "FUiEffects_RendererRendererObject.h"
38 #include "FUiEffects_RendererPointLight.h"
39 #include "FUiEffects_RendererSpotLight.h"
40 #include "FUiEffects_RendererDirectionalLight.h"
41 #include "FUiEffects_RendererLightingParameters.h"
42 #include "FUiEffects_RendererMemoryTexture2DProperty.h"
44 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRender.h>
45 #include <renderer/engine-model/FUiEffects_RendererEngineModelCamera.h>
46 #include <renderer/engine-model/FUiEffects_RendererEngineModelNode.h>
47 #include <renderer/engine-model/FUiEffects_RendererEngineModelGroup.h>
48 #include <renderer/engine-model/FUiEffects_RendererEngineModelStringShaderProperty.h>
49 #include <renderer/engine-model/FUiEffects_RendererEngineModelShaderHolderProgramProperty.h>
51 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
54 extern const EGLint RENDERER_EGL_DEFAULT_CONFIG_ATTRIBS[];
57 * @class EffectRenderer
58 * @brief This class for model visualization
73 ~EffectRenderer(void);
76 * Initializes OpenGL for this device
78 * @param [in] config Specifies attributes required to match by configs
79 * @return bool true on success, otherwise false
81 static bool InitOpenGL(const EGLint* pConfig = RENDERER_EGL_DEFAULT_CONFIG_ATTRIBS);
84 * Closes OpenGL for this device
88 static void CloseOpenGL(void);
93 * @return bool true on success, otherwise false
95 static bool CheckOpenGlesInitialized(void);
98 * Creates OpenGL surface
100 * @param [in] wnd Specifies the native window
101 * @return EGLSurface Result of eglCreateWindowSurface (EGL_NO_SURFACE if failed)
103 static EGLSurface CreateSurface(EGLNativeWindowType wnd);
106 * Deletes OpenGL surface
108 * @param [in] surface Surface to delete
111 static void DeleteSurface(EGLSurface surface);
114 * Creates OpenGL context
116 * @param [in] ShareContext Sharable context
117 * @return EGLContext Result of eglCreateContext (EGL_NO_CONTEXT if failed)
119 static EGLContext CreateContext(EGLContext shareContext = EGL_NO_CONTEXT);
122 * Deletes OpenGL context
124 * @param [in] context Surface to delete
127 static void DeleteContext(EGLContext context);
130 * Initializes renderer from data: scene graph, objects etc. Clears previous content.
132 * @param [in] _renderDataCollection Array of data pointers
135 * @remark It saves pointer to EffectRenderer::renderDataCollection for further updates (see EffectRenderer::Update). @n
136 * @remark So it's assumed that content of _renderDataCollection (pointers values and their order in array) @n
137 * @remark is constant through all the life cycle of the renderer
139 void Construct(Tizen::Ui::Effects::_Runtime::RenderDataScene &renderDataCollection);
142 * Updates renderer objects
146 * @remark The source for updates is EffectRenderer::renderDataCollection. See comments for EffectRenderer::Construct
151 * Initialize OpenGL context state (Clear color etc)
155 void InitializeContext(void) const;
165 * Sets borders of visible scene (in model (world) space) for coordinates calculation
167 * @param [in] worldLeft, worldRight, worldBottom, worldTop - coordinates of world sides related to screen
168 * @param [in] worldZ - Z level of screen viewport
169 * @param [in] worldNearDist, worldFarDist - distances to camera clipping planes in world coordinates
170 * @param [in] FOV_Y_deg - Full angle, in degrees, in the vertical direction
171 * @param [in] perspectiveProjection - true = use perspective projection, false = use orthographic projection
173 * @return bool true on valid arguments and success execution
176 bool SetWorld(float worldLeft, float worldRight, float worldBottom, float worldTop, float worldZ, float worldNearDist, float worldFarDist, float FOV_Y_deg, bool perspectiveProjection);
179 * Transforms window coordinates to model space
181 * @param [in] p point on screen
183 * @return Vec3f Point in model space
184 * @see SetScreen, SetWorld
186 Tizen::Ui::Effects::_Utils::Vec3f TransformScreenToWorld(Tizen::Ui::Effects::_Utils::Vec2f p) const;
189 * Sets image for texture
191 * @param [in] bitmapID id of image
192 * @param [in] bitmap Image bitmap
194 * @return bool True on success, otherwise false
196 bool SetBitmap(long bitmapID, Tizen::Graphics::Bitmap &bitmap);
199 * Sets image for texture
201 * @param [in] ptr pointer to bitmap data
202 * @param [in] width width in pixels
203 * @param [in] height height in pixels
205 * @return bool True on success, otherwise false
207 * @remark If ptr = null empty texture of width*height size will be allocated in memory for faster response on next call to this method
209 bool SetBitmap(long bitmapID, const void* ptr, size_t width, size_t height);
212 * Sets borders of native screen (in pixels) for coordinates calculation
216 void SetScreen(const Tizen::Ui::Control& control);
219 * Tracks screen size changes
223 void TrackScreenSizeChanges(const Tizen::Ui::Control& control);
226 * Prebuild OpenGL data caches for fast response on Draw() method
233 * Frees OpenGL related data
242 * Hidden copy constructor
244 EffectRenderer(const EffectRenderer &rhs);
247 * Hidden assignment operator
249 EffectRenderer& operator=(const EffectRenderer &rhs);
252 * Initializes renderer lighting data: point, spot and directional lights, ambient parameters, etc.
254 * @param [in] _renderDataCollection Array of data pointers
257 * @remark It saves pointer to EffectRenderer::renderDataCollection for further updates (see EffectRenderer::Update). @n
258 * @remark So it's assumed that content of _renderDataCollection (pointers values and their order in array) @n
259 * @remark is constant through all the life cycle of the renderer
261 void ConstructLighting(void);
264 * Updates scene lighting
268 void UpdateLighting(void);
271 * Initializes renderer object from given data
273 * @param [in] index object index in EffectRenderer::objects
274 * @param [in] rdata Data for initialization
277 * @remark This method is for internal use only
279 void InitRObjectFromData(Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type index, Tizen::Ui::Effects::_Runtime::RenderDataSurface &rdata);
282 * Updates renderer object
284 * @param [in] index object index in EffectRenderer::objects
285 * @param [in] rdata Data for update
288 * @remark This method is for internal use only
290 void UpdateRObject(Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type index, Tizen::Ui::Effects::_Runtime::RenderDataSurface &rdata);
293 * Looks for texture in EffectRenderer::textures by its id.
295 * @param [in] bitmapID ID of texture
296 * @return MemoryTexture2DPropertyPtr Texture object
298 * @remark If ID not found method creates and returns empty texture for further usage filled with some noticeable color (violet [255 0 128])
300 MemoryTexture2DPropertyPtr FindTexture(long bitmapID);
303 * Sets borders of native screen (in pixels) for coordinates calculation
305 * @param [in] screenLeft, screenRight, screenBottom, screenTop - coordinates of screen sides in pixels. @n
306 * Used for screen-to-world transformation in touch events handlers. For example commonly screenTop = 0, screenBottom = window height
310 void SetScreen(float screenLeft, float screenRight, float screenBottom, float screenTop, int displayWidth, int displayHeight);
313 * (Re)Creates shader considering scene lighting parameters and count of different light types
320 * Makes this renderer context current
324 void MakeCurrent(void);
327 * Restores initial OpenGL context saved in MakeCurrent method
331 void RestorePreviousContext(void);
335 EGLSurface surface; /**< OpenGL surface */
338 typedef std::map<long, MemoryTexture2DPropertyPtr> TexturesMap;
339 typedef std::map<std::string, PointLightPtr> PointLightsList;
340 typedef std::map<std::string, SpotLightPtr> SpotLightsList;
341 typedef std::map<std::string, DirectionalLightPtr> DirectionalLightsList;
343 static EGLDisplay __eglDisplay;
344 static EGLConfig __eglConfig;
345 static EGLint __glMajorVer;
346 static EGLint __glMinorVer;
347 static bool __OpenGLInitialized;
348 EGLContext __context, __previousContext;
349 bool __previousContextSaved;
352 bool __perspectiveProjection;
354 TexturesMap __textures;
355 std::vector<RendererObjectPtr> __objects;
356 Tizen::Ui::Effects::_Runtime::RenderDataScene* __renderDataScene;
357 Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection* __pRenderDataSurfaceCollection;
360 GraphicsEngine::RenderPtr __renderer;
361 EngineModel::GroupPtr __root;
362 EngineModel::CameraPtr __camera;
363 EngineModel::ShaderHolderProgramPropertyPtr __program;
365 Tizen::Graphics::Dimension __dimensionPreviousControl;
368 LightingParameters __lightingParameters;
369 PointLightsList __pointLights;
370 SpotLightsList __spotLights;
371 DirectionalLightsList __directionalLights;
376 typedef std::unique_ptr<EffectRenderer> EffectRendererUniquePtr;
378 } } } } // Tizen::Ui::Effects::_EffectRenderer
380 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_