1 // Copyright (C) 2002-2012 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
5 #ifndef __C_VIDEO_OPEN_GL_H_INCLUDED__
\r
6 #define __C_VIDEO_OPEN_GL_H_INCLUDED__
\r
9 #include "SIrrCreationParameters.h"
\r
13 class CIrrDeviceWin32;
\r
14 class CIrrDeviceLinux;
\r
15 class CIrrDeviceSDL;
\r
16 class CIrrDeviceMacOSX;
\r
19 #ifdef _IRR_COMPILE_WITH_OPENGL_
\r
21 #include "IMaterialRendererServices.h"
\r
22 #include "CNullDriver.h"
\r
24 #include "COpenGLExtensionHandler.h"
\r
25 #include "IContextManager.h"
\r
32 class IContextManager;
\r
34 class COpenGLDriver : public CNullDriver, public IMaterialRendererServices, public COpenGLExtensionHandler
\r
37 // Information about state of fixed pipeline activity.
\r
38 enum E_OPENGL_FIXED_PIPELINE_STATE
\r
40 EOFPS_ENABLE = 0, // fixed pipeline.
\r
41 EOFPS_DISABLE, // programmable pipeline.
\r
42 EOFPS_ENABLE_TO_DISABLE, // switch from fixed to programmable pipeline.
\r
43 EOFPS_DISABLE_TO_ENABLE // switch from programmable to fixed pipeline.
\r
46 COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
51 virtual ~COpenGLDriver();
\r
53 virtual bool beginScene(u16 clearFlag, SColor clearColor = SColor(255,0,0,0), f32 clearDepth = 1.f, u8 clearStencil = 0,
\r
54 const SExposedVideoData& videoData = SExposedVideoData(), core::rect<s32>* sourceRect = 0) override;
\r
56 bool endScene() override;
\r
58 //! sets transformation
\r
59 void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat) override;
\r
62 struct SHWBufferLink_opengl : public SHWBufferLink
\r
64 SHWBufferLink_opengl(const scene::IMeshBuffer *_MeshBuffer): SHWBufferLink(_MeshBuffer), vbo_verticesID(0),vbo_indicesID(0){}
\r
66 GLuint vbo_verticesID; //tmp
\r
67 GLuint vbo_indicesID; //tmp
\r
69 GLuint vbo_verticesSize; //tmp
\r
70 GLuint vbo_indicesSize; //tmp
\r
73 //! updates hardware buffer if needed
\r
74 bool updateHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
76 //! Create hardware buffer from mesh
\r
77 SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb) override;
\r
79 //! Delete hardware buffer (only some drivers can)
\r
80 void deleteHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
82 //! Draw hardware buffer
\r
83 void drawHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
85 //! Create occlusion query.
\r
86 /** Use node for identification and mesh for occlusion test. */
\r
87 virtual void addOcclusionQuery(scene::ISceneNode* node,
\r
88 const scene::IMesh* mesh=0) override;
\r
90 //! Remove occlusion query.
\r
91 void removeOcclusionQuery(scene::ISceneNode* node) override;
\r
93 //! Run occlusion query. Draws mesh stored in query.
\r
94 /** If the mesh shall not be rendered visible, use
\r
95 overrideMaterial to disable the color and depth buffer. */
\r
96 void runOcclusionQuery(scene::ISceneNode* node, bool visible=false) override;
\r
98 //! Update occlusion query. Retrieves results from GPU.
\r
99 /** If the query shall not block, set the flag to false.
\r
100 Update might not occur in this case, though */
\r
101 void updateOcclusionQuery(scene::ISceneNode* node, bool block=true) override;
\r
103 //! Return query result.
\r
104 /** Return value is the number of visible pixels/fragments.
\r
105 The value is a safe approximation, i.e. can be larger then the
\r
106 actual value of pixels. */
\r
107 u32 getOcclusionQueryResult(scene::ISceneNode* node) const override;
\r
109 //! Create render target.
\r
110 IRenderTarget* addRenderTarget() override;
\r
112 //! draws a vertex primitive list
\r
113 virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
\r
114 const void* indexList, u32 primitiveCount,
\r
115 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType) override;
\r
117 //! draws a vertex primitive list in 2d
\r
118 virtual void draw2DVertexPrimitiveList(const void* vertices, u32 vertexCount,
\r
119 const void* indexList, u32 primitiveCount,
\r
120 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType) override;
\r
122 //! queries the features of the driver, returns true if feature is available
\r
123 bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const override
\r
125 return FeatureEnabled[feature] && COpenGLExtensionHandler::queryFeature(feature);
\r
128 //! Disable a feature of the driver.
\r
129 void disableFeature(E_VIDEO_DRIVER_FEATURE feature, bool flag=true) override;
\r
131 //! Sets a material. All 3d drawing functions draw geometry now
\r
132 //! using this material.
\r
133 //! \param material: Material to be used from now on.
\r
134 void setMaterial(const SMaterial& material) override;
\r
136 virtual void draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
\r
137 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
\r
138 SColor color = SColor(255, 255, 255, 255), bool useAlphaChannelOfTexture = false) override;
\r
140 virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
\r
141 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
\r
142 const video::SColor* const colors = 0, bool useAlphaChannelOfTexture = false) override;
\r
144 // Explicitly bring in base class methods, otherwise
\r
145 // this overload would hide them.
\r
146 using CNullDriver::draw2DImage;
\r
147 virtual void draw2DImage(const video::ITexture* texture, u32 layer, bool flip);
\r
149 //! draws a set of 2d images, using a color and the alpha channel of the
\r
150 //! texture if desired.
\r
151 void draw2DImageBatch(const video::ITexture* texture,
\r
152 const core::array<core::position2d<s32> >& positions,
\r
153 const core::array<core::rect<s32> >& sourceRects,
\r
154 const core::rect<s32>* clipRect,
\r
156 bool useAlphaChannelOfTexture) override;
\r
158 //! draws a set of 2d images, using a color and the alpha
\r
159 /** channel of the texture if desired. The images are drawn
\r
160 beginning at pos and concatenated in one line. All drawings
\r
161 are clipped against clipRect (if != 0).
\r
162 The subtextures are defined by the array of sourceRects
\r
163 and are chosen by the indices given.
\r
164 \param texture: Texture to be drawn.
\r
165 \param pos: Upper left 2d destination position where the image will be drawn.
\r
166 \param sourceRects: Source rectangles of the image.
\r
167 \param indices: List of indices which choose the actual rectangle used each time.
\r
168 \param clipRect: Pointer to rectangle on the screen where the image is clipped to.
\r
169 This pointer can be 0. Then the image is not clipped.
\r
170 \param color: Color with which the image is colored.
\r
171 Note that the alpha component is used: If alpha is other than 255, the image will be transparent.
\r
172 \param useAlphaChannelOfTexture: If true, the alpha channel of the texture is
\r
173 used to draw the image. */
\r
174 virtual void draw2DImageBatch(const video::ITexture* texture,
\r
175 const core::position2d<s32>& pos,
\r
176 const core::array<core::rect<s32> >& sourceRects,
\r
177 const core::array<s32>& indices,
\r
178 s32 kerningWidth=0,
\r
179 const core::rect<s32>* clipRect=0,
\r
180 SColor color=SColor(255,255,255,255),
\r
181 bool useAlphaChannelOfTexture=false) override;
\r
183 //! draw an 2d rectangle
\r
184 virtual void draw2DRectangle(SColor color, const core::rect<s32>& pos,
\r
185 const core::rect<s32>* clip = 0) override;
\r
187 //!Draws an 2d rectangle with a gradient.
\r
188 virtual void draw2DRectangle(const core::rect<s32>& pos,
\r
189 SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
\r
190 const core::rect<s32>* clip = 0) override;
\r
192 //! Draws a 2d line.
\r
193 virtual void draw2DLine(const core::position2d<s32>& start,
\r
194 const core::position2d<s32>& end,
\r
195 SColor color=SColor(255,255,255,255)) override;
\r
197 //! Draws a single pixel
\r
198 void drawPixel(u32 x, u32 y, const SColor & color) override;
\r
201 void draw3DBox( const core::aabbox3d<f32>& box, SColor color = SColor(255,255,255,255 ) ) override;
\r
203 //! Draws a 3d line.
\r
204 virtual void draw3DLine(const core::vector3df& start,
\r
205 const core::vector3df& end,
\r
206 SColor color = SColor(255,255,255,255)) override;
\r
208 //! \return Returns the name of the video driver. Example: In case of the Direct3D8
\r
209 //! driver, it would return "Direct3D8.1".
\r
210 const wchar_t* getName() const override;
\r
212 //! Sets the dynamic ambient light color. The default color is
\r
213 //! (0,0,0,0) which means it is dark.
\r
214 //! \param color: New color of the ambient light.
\r
215 void setAmbientLight(const SColorf& color) override;
\r
217 //! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
\r
218 //! this: First, draw all geometry. Then use this method, to draw the shadow
\r
219 //! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
\r
220 void drawStencilShadowVolume(const core::array<core::vector3df>& triangles, bool zfail, u32 debugDataVisible=0) override;
\r
222 //! Fills the stencil shadow with color. After the shadow volume has been drawn
\r
223 //! into the stencil buffer using IVideoDriver::drawStencilShadowVolume(), use this
\r
224 //! to draw the color of the shadow.
\r
225 virtual void drawStencilShadow(bool clearStencilBuffer=false,
\r
226 video::SColor leftUpEdge = video::SColor(0,0,0,0),
\r
227 video::SColor rightUpEdge = video::SColor(0,0,0,0),
\r
228 video::SColor leftDownEdge = video::SColor(0,0,0,0),
\r
229 video::SColor rightDownEdge = video::SColor(0,0,0,0)) override;
\r
231 //! sets a viewport
\r
232 void setViewPort(const core::rect<s32>& area) override;
\r
234 //! Sets the fog mode.
\r
235 virtual void setFog(SColor color, E_FOG_TYPE fogType, f32 start,
\r
236 f32 end, f32 density, bool pixelFog, bool rangeFog) override;
\r
238 //! Only used by the internal engine. Used to notify the driver that
\r
239 //! the window was resized.
\r
240 void OnResize(const core::dimension2d<u32>& size) override;
\r
242 //! Returns type of video driver
\r
243 E_DRIVER_TYPE getDriverType() const override;
\r
245 //! get color format of the current color buffer
\r
246 ECOLOR_FORMAT getColorFormat() const override;
\r
248 //! Returns the transformation set by setTransform
\r
249 const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const override;
\r
251 //! Can be called by an IMaterialRenderer to make its work easier.
\r
252 virtual void setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial,
\r
253 bool resetAllRenderstates) override;
\r
255 //! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.
\r
256 virtual void setTextureRenderStates(const SMaterial& material, bool resetAllRenderstates);
\r
258 //! Get a vertex shader constant index.
\r
259 s32 getVertexShaderConstantID(const c8* name) override;
\r
261 //! Get a pixel shader constant index.
\r
262 s32 getPixelShaderConstantID(const c8* name) override;
\r
264 //! Sets a vertex shader constant.
\r
265 void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1) override;
\r
267 //! Sets a pixel shader constant.
\r
268 void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1) override;
\r
270 //! Sets a constant for the vertex shader based on an index.
\r
271 bool setVertexShaderConstant(s32 index, const f32* floats, int count) override;
\r
273 //! Int interface for the above.
\r
274 bool setVertexShaderConstant(s32 index, const s32* ints, int count) override;
\r
276 //! Uint interface for the above.
\r
277 bool setVertexShaderConstant(s32 index, const u32* ints, int count) override;
\r
279 //! Sets a constant for the pixel shader based on an index.
\r
280 bool setPixelShaderConstant(s32 index, const f32* floats, int count) override;
\r
282 //! Int interface for the above.
\r
283 bool setPixelShaderConstant(s32 index, const s32* ints, int count) override;
\r
285 //! Uint interface for the above.
\r
286 bool setPixelShaderConstant(s32 index, const u32* ints, int count) override;
\r
288 //! disables all textures beginning with the optional fromStage parameter. Otherwise all texture stages are disabled.
\r
289 //! Returns whether disabling was successful or not.
\r
290 bool disableTextures(u32 fromStage=0);
\r
292 //! Adds a new material renderer to the VideoDriver, using
\r
293 //! extGLGetObjectParameteriv(shaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &status)
\r
294 //! pixel and/or vertex shaders to render geometry.
\r
295 virtual s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,
\r
296 IShaderConstantSetCallBack* callback, E_MATERIAL_TYPE baseMaterial, s32 userData) override;
\r
298 //! Adds a new material renderer to the VideoDriver, using GLSL to render geometry.
\r
299 virtual s32 addHighLevelShaderMaterial(
\r
300 const c8* vertexShaderProgram,
\r
301 const c8* vertexShaderEntryPointName,
\r
302 E_VERTEX_SHADER_TYPE vsCompileTarget,
\r
303 const c8* pixelShaderProgram,
\r
304 const c8* pixelShaderEntryPointName,
\r
305 E_PIXEL_SHADER_TYPE psCompileTarget,
\r
306 const c8* geometryShaderProgram,
\r
307 const c8* geometryShaderEntryPointName = "main",
\r
308 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,
\r
309 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
\r
310 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,
\r
311 u32 verticesOut = 0,
\r
312 IShaderConstantSetCallBack* callback = 0,
\r
313 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
\r
314 s32 userData = 0) override;
\r
316 //! Returns a pointer to the IVideoDriver interface. (Implementation for
\r
317 //! IMaterialRendererServices)
\r
318 IVideoDriver* getVideoDriver() override;
\r
320 //! Returns the maximum amount of primitives (mostly vertices) which
\r
321 //! the device is able to render with one drawIndexedTriangleList
\r
323 u32 getMaximalPrimitiveCount() const override;
\r
325 virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
\r
326 const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN) override;
\r
328 //! Creates a render target texture for a cubemap
\r
329 ITexture* addRenderTargetTextureCubemap(const irr::u32 sideLen,
\r
330 const io::path& name, const ECOLOR_FORMAT format) override;
\r
332 virtual bool setRenderTargetEx(IRenderTarget* target, u16 clearFlag, SColor clearColor = SColor(255,0,0,0),
\r
333 f32 clearDepth = 1.f, u8 clearStencil = 0) override;
\r
335 void clearBuffers(u16 flag, SColor color = SColor(255,0,0,0), f32 depth = 1.f, u8 stencil = 0) override;
\r
337 //! Returns an image created from the last rendered frame.
\r
338 IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER) override;
\r
340 //! checks if an OpenGL error has happened and prints it (+ some internal code which is usually the line number)
\r
341 //! for performance reasons only available in debug mode
\r
342 bool testGLError(int code=0);
\r
344 //! Set/unset a clipping plane.
\r
345 //! There are at least 6 clipping planes available for the user to set at will.
\r
346 //! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
\r
347 //! \param plane: The plane itself.
\r
348 //! \param enable: If true, enable the clipping plane else disable it.
\r
349 bool setClipPlane(u32 index, const core::plane3df& plane, bool enable=false) override;
\r
351 //! Enable/disable a clipping plane.
\r
352 //! There are at least 6 clipping planes available for the user to set at will.
\r
353 //! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
\r
354 //! \param enable: If true, enable the clipping plane else disable it.
\r
355 void enableClipPlane(u32 index, bool enable) override;
\r
357 //! Enable the 2d override material
\r
358 void enableMaterial2D(bool enable=true) override;
\r
360 //! Returns the graphics card vendor name.
\r
361 core::stringc getVendorInfo() override {return VendorName;}
\r
363 //! Returns the maximum texture size supported.
\r
364 core::dimension2du getMaxTextureSize() const override;
\r
366 //! Removes a texture from the texture cache and deletes it, freeing lot of memory.
\r
367 void removeTexture(ITexture* texture) override;
\r
369 //! Check if the driver supports creating textures with the given color format
\r
370 bool queryTextureFormat(ECOLOR_FORMAT format) const override;
\r
372 //! Used by some SceneNodes to check if a material should be rendered in the transparent render pass
\r
373 bool needsTransparentRenderPass(const irr::video::SMaterial& material) const override;
\r
375 //! Convert E_PRIMITIVE_TYPE to OpenGL equivalent
\r
376 GLenum primitiveTypeToGL(scene::E_PRIMITIVE_TYPE type) const;
\r
378 //! Convert E_BLEND_FACTOR to OpenGL equivalent
\r
379 GLenum getGLBlend(E_BLEND_FACTOR factor) const;
\r
381 //! Get ZBuffer bits.
\r
382 GLenum getZBufferBits() const;
\r
384 bool getColorFormatParameters(ECOLOR_FORMAT format, GLint& internalFormat, GLenum& pixelFormat,
\r
385 GLenum& pixelType, void(**converter)(const void*, s32, void*)) const;
\r
387 //! Return info about fixed pipeline state.
\r
388 E_OPENGL_FIXED_PIPELINE_STATE getFixedPipelineState() const;
\r
390 //! Set info about fixed pipeline state.
\r
391 void setFixedPipelineState(E_OPENGL_FIXED_PIPELINE_STATE state);
\r
393 //! Get current material.
\r
394 const SMaterial& getCurrentMaterial() const;
\r
396 COpenGLCacheHandler* getCacheHandler() const;
\r
400 bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
401 bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
403 void uploadClipPlane(u32 index);
\r
405 //! inits the parts of the open gl driver used on all platforms
\r
406 bool genericDriverInit();
\r
408 ITexture* createDeviceDependentTexture(const io::path& name, IImage* image) override;
\r
410 ITexture* createDeviceDependentTextureCubemap(const io::path& name, const core::array<IImage*>& image) override;
\r
412 //! creates a transposed matrix in supplied GLfloat array to pass to OpenGL
\r
413 inline void getGLMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
\r
414 inline void getGLTextureMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
\r
416 //! get native wrap mode value
\r
417 GLint getTextureWrapMode(const u8 clamp);
\r
419 //! sets the needed renderstates
\r
420 void setRenderStates3DMode();
\r
422 //! sets the needed renderstates
\r
423 void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
\r
425 void createMaterialRenderers();
\r
427 //! Assign a hardware light to the specified requested light, if any
\r
428 //! free hardware lights exist.
\r
429 //! \param[in] lightIndex: the index of the requesting light
\r
430 void assignHardwareLight(u32 lightIndex);
\r
432 //! helper function for render setup.
\r
433 void getColorBuffer(const void* vertices, u32 vertexCount, E_VERTEX_TYPE vType);
\r
435 //! helper function doing the actual rendering.
\r
436 void renderArray(const void* indexList, u32 primitiveCount,
\r
437 scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
\r
439 //! Same as `CacheHandler->setViewport`, but also sets `ViewPort`
\r
440 virtual void setViewPortRaw(u32 width, u32 height);
\r
442 COpenGLCacheHandler* CacheHandler;
\r
444 core::stringw Name;
\r
445 core::matrix4 Matrices[ETS_COUNT];
\r
446 core::array<u8> ColorBuffer;
\r
448 //! enumeration for rendering modes such as 2d and 3d for minizing the switching of renderStates.
\r
451 ERM_NONE = 0, // no render state has been set yet.
\r
452 ERM_2D, // 2d drawing rendermode
\r
453 ERM_3D // 3d rendering mode
\r
456 E_RENDER_MODE CurrentRenderMode;
\r
457 //! bool to make all renderstates reset if set to true.
\r
458 bool ResetRenderStates;
\r
459 bool Transformation3DChanged;
\r
462 SMaterial Material, LastMaterial;
\r
464 struct SUserClipPlane
\r
466 SUserClipPlane() : Enabled(false) {}
\r
467 core::plane3df Plane;
\r
470 core::array<SUserClipPlane> UserClipPlanes;
\r
472 core::stringc VendorName;
\r
474 core::matrix4 TextureFlipMatrix;
\r
476 //! Color buffer format
\r
477 ECOLOR_FORMAT ColorFormat;
\r
479 E_OPENGL_FIXED_PIPELINE_STATE FixedPipelineState;
\r
481 SIrrlichtCreationParameters Params;
\r
483 //! Built-in 2D quad for 2D rendering.
\r
484 S3DVertex Quad2DVertices[4];
\r
485 static const u16 Quad2DIndices[4];
\r
487 IContextManager* ContextManager;
\r
490 } // end namespace video
\r
491 } // end namespace irr
\r
494 #endif // _IRR_COMPILE_WITH_OPENGL_
\r