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
8 #include "IrrCompileConfig.h"
\r
10 #include "SIrrCreationParameters.h"
\r
14 class CIrrDeviceWin32;
\r
15 class CIrrDeviceLinux;
\r
16 class CIrrDeviceSDL;
\r
17 class CIrrDeviceMacOSX;
\r
20 #ifdef _IRR_COMPILE_WITH_OPENGL_
\r
22 #include "IMaterialRendererServices.h"
\r
23 #include "CNullDriver.h"
\r
25 #include "COpenGLExtensionHandler.h"
\r
26 #include "IContextManager.h"
\r
33 class IContextManager;
\r
35 class COpenGLDriver : public CNullDriver, public IMaterialRendererServices, public COpenGLExtensionHandler
\r
38 // Information about state of fixed pipeline activity.
\r
39 enum E_OPENGL_FIXED_PIPELINE_STATE
\r
41 EOFPS_ENABLE = 0, // fixed pipeline.
\r
42 EOFPS_DISABLE, // programmable pipeline.
\r
43 EOFPS_ENABLE_TO_DISABLE, // switch from fixed to programmable pipeline.
\r
44 EOFPS_DISABLE_TO_ENABLE // switch from programmable to fixed pipeline.
\r
47 COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
52 virtual ~COpenGLDriver();
\r
54 virtual bool beginScene(u16 clearFlag, SColor clearColor = SColor(255,0,0,0), f32 clearDepth = 1.f, u8 clearStencil = 0,
\r
55 const SExposedVideoData& videoData = SExposedVideoData(), core::rect<s32>* sourceRect = 0) override;
\r
57 bool endScene() override;
\r
59 //! sets transformation
\r
60 void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat) override;
\r
63 struct SHWBufferLink_opengl : public SHWBufferLink
\r
65 SHWBufferLink_opengl(const scene::IMeshBuffer *_MeshBuffer): SHWBufferLink(_MeshBuffer), vbo_verticesID(0),vbo_indicesID(0){}
\r
67 GLuint vbo_verticesID; //tmp
\r
68 GLuint vbo_indicesID; //tmp
\r
70 GLuint vbo_verticesSize; //tmp
\r
71 GLuint vbo_indicesSize; //tmp
\r
74 //! updates hardware buffer if needed
\r
75 bool updateHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
77 //! Create hardware buffer from mesh
\r
78 SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb) override;
\r
80 //! Delete hardware buffer (only some drivers can)
\r
81 void deleteHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
83 //! Draw hardware buffer
\r
84 void drawHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
86 //! Create occlusion query.
\r
87 /** Use node for identification and mesh for occlusion test. */
\r
88 virtual void addOcclusionQuery(scene::ISceneNode* node,
\r
89 const scene::IMesh* mesh=0) override;
\r
91 //! Remove occlusion query.
\r
92 void removeOcclusionQuery(scene::ISceneNode* node) override;
\r
94 //! Run occlusion query. Draws mesh stored in query.
\r
95 /** If the mesh shall not be rendered visible, use
\r
96 overrideMaterial to disable the color and depth buffer. */
\r
97 void runOcclusionQuery(scene::ISceneNode* node, bool visible=false) override;
\r
99 //! Update occlusion query. Retrieves results from GPU.
\r
100 /** If the query shall not block, set the flag to false.
\r
101 Update might not occur in this case, though */
\r
102 void updateOcclusionQuery(scene::ISceneNode* node, bool block=true) override;
\r
104 //! Return query result.
\r
105 /** Return value is the number of visible pixels/fragments.
\r
106 The value is a safe approximation, i.e. can be larger then the
\r
107 actual value of pixels. */
\r
108 u32 getOcclusionQueryResult(scene::ISceneNode* node) const override;
\r
110 //! Create render target.
\r
111 IRenderTarget* addRenderTarget() override;
\r
113 //! draws a vertex primitive list
\r
114 virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
\r
115 const void* indexList, u32 primitiveCount,
\r
116 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType) override;
\r
118 //! draws a vertex primitive list in 2d
\r
119 virtual void draw2DVertexPrimitiveList(const void* vertices, u32 vertexCount,
\r
120 const void* indexList, u32 primitiveCount,
\r
121 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType) override;
\r
123 //! queries the features of the driver, returns true if feature is available
\r
124 bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const override
\r
126 return FeatureEnabled[feature] && COpenGLExtensionHandler::queryFeature(feature);
\r
129 //! Disable a feature of the driver.
\r
130 void disableFeature(E_VIDEO_DRIVER_FEATURE feature, bool flag=true) override;
\r
132 //! Sets a material. All 3d drawing functions draw geometry now
\r
133 //! using this material.
\r
134 //! \param material: Material to be used from now on.
\r
135 void setMaterial(const SMaterial& material) override;
\r
137 virtual void draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
\r
138 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
\r
139 SColor color = SColor(255, 255, 255, 255), bool useAlphaChannelOfTexture = false) override;
\r
141 virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
\r
142 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
\r
143 const video::SColor* const colors = 0, bool useAlphaChannelOfTexture = false) override;
\r
145 // Explicitly bring in base class methods, otherwise
\r
146 // this overload would hide them.
\r
147 using CNullDriver::draw2DImage;
\r
148 virtual void draw2DImage(const video::ITexture* texture, u32 layer, bool flip);
\r
150 //! draws a set of 2d images, using a color and the alpha channel of the
\r
151 //! texture if desired.
\r
152 void draw2DImageBatch(const video::ITexture* texture,
\r
153 const core::array<core::position2d<s32> >& positions,
\r
154 const core::array<core::rect<s32> >& sourceRects,
\r
155 const core::rect<s32>* clipRect,
\r
157 bool useAlphaChannelOfTexture) override;
\r
159 //! draws a set of 2d images, using a color and the alpha
\r
160 /** channel of the texture if desired. The images are drawn
\r
161 beginning at pos and concatenated in one line. All drawings
\r
162 are clipped against clipRect (if != 0).
\r
163 The subtextures are defined by the array of sourceRects
\r
164 and are chosen by the indices given.
\r
165 \param texture: Texture to be drawn.
\r
166 \param pos: Upper left 2d destination position where the image will be drawn.
\r
167 \param sourceRects: Source rectangles of the image.
\r
168 \param indices: List of indices which choose the actual rectangle used each time.
\r
169 \param clipRect: Pointer to rectangle on the screen where the image is clipped to.
\r
170 This pointer can be 0. Then the image is not clipped.
\r
171 \param color: Color with which the image is colored.
\r
172 Note that the alpha component is used: If alpha is other than 255, the image will be transparent.
\r
173 \param useAlphaChannelOfTexture: If true, the alpha channel of the texture is
\r
174 used to draw the image. */
\r
175 virtual void draw2DImageBatch(const video::ITexture* texture,
\r
176 const core::position2d<s32>& pos,
\r
177 const core::array<core::rect<s32> >& sourceRects,
\r
178 const core::array<s32>& indices,
\r
179 s32 kerningWidth=0,
\r
180 const core::rect<s32>* clipRect=0,
\r
181 SColor color=SColor(255,255,255,255),
\r
182 bool useAlphaChannelOfTexture=false) override;
\r
184 //! draw an 2d rectangle
\r
185 virtual void draw2DRectangle(SColor color, const core::rect<s32>& pos,
\r
186 const core::rect<s32>* clip = 0) override;
\r
188 //!Draws an 2d rectangle with a gradient.
\r
189 virtual void draw2DRectangle(const core::rect<s32>& pos,
\r
190 SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
\r
191 const core::rect<s32>* clip = 0) override;
\r
193 //! Draws a 2d line.
\r
194 virtual void draw2DLine(const core::position2d<s32>& start,
\r
195 const core::position2d<s32>& end,
\r
196 SColor color=SColor(255,255,255,255)) override;
\r
198 //! Draws a single pixel
\r
199 void drawPixel(u32 x, u32 y, const SColor & color) override;
\r
202 void draw3DBox( const core::aabbox3d<f32>& box, SColor color = SColor(255,255,255,255 ) ) override;
\r
204 //! Draws a 3d line.
\r
205 virtual void draw3DLine(const core::vector3df& start,
\r
206 const core::vector3df& end,
\r
207 SColor color = SColor(255,255,255,255)) override;
\r
209 //! \return Returns the name of the video driver. Example: In case of the Direct3D8
\r
210 //! driver, it would return "Direct3D8.1".
\r
211 const wchar_t* getName() const override;
\r
213 //! Sets the dynamic ambient light color. The default color is
\r
214 //! (0,0,0,0) which means it is dark.
\r
215 //! \param color: New color of the ambient light.
\r
216 void setAmbientLight(const SColorf& color) override;
\r
218 //! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
\r
219 //! this: First, draw all geometry. Then use this method, to draw the shadow
\r
220 //! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
\r
221 void drawStencilShadowVolume(const core::array<core::vector3df>& triangles, bool zfail, u32 debugDataVisible=0) override;
\r
223 //! Fills the stencil shadow with color. After the shadow volume has been drawn
\r
224 //! into the stencil buffer using IVideoDriver::drawStencilShadowVolume(), use this
\r
225 //! to draw the color of the shadow.
\r
226 virtual void drawStencilShadow(bool clearStencilBuffer=false,
\r
227 video::SColor leftUpEdge = video::SColor(0,0,0,0),
\r
228 video::SColor rightUpEdge = video::SColor(0,0,0,0),
\r
229 video::SColor leftDownEdge = video::SColor(0,0,0,0),
\r
230 video::SColor rightDownEdge = video::SColor(0,0,0,0)) override;
\r
232 //! sets a viewport
\r
233 void setViewPort(const core::rect<s32>& area) override;
\r
235 //! Sets the fog mode.
\r
236 virtual void setFog(SColor color, E_FOG_TYPE fogType, f32 start,
\r
237 f32 end, f32 density, bool pixelFog, bool rangeFog) override;
\r
239 //! Only used by the internal engine. Used to notify the driver that
\r
240 //! the window was resized.
\r
241 void OnResize(const core::dimension2d<u32>& size) override;
\r
243 //! Returns type of video driver
\r
244 E_DRIVER_TYPE getDriverType() const override;
\r
246 //! get color format of the current color buffer
\r
247 ECOLOR_FORMAT getColorFormat() const override;
\r
249 //! Returns the transformation set by setTransform
\r
250 const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const override;
\r
252 //! Can be called by an IMaterialRenderer to make its work easier.
\r
253 virtual void setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial,
\r
254 bool resetAllRenderstates) override;
\r
256 //! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.
\r
257 virtual void setTextureRenderStates(const SMaterial& material, bool resetAllRenderstates);
\r
259 //! Get a vertex shader constant index.
\r
260 s32 getVertexShaderConstantID(const c8* name) override;
\r
262 //! Get a pixel shader constant index.
\r
263 s32 getPixelShaderConstantID(const c8* name) override;
\r
265 //! Sets a vertex shader constant.
\r
266 void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1) override;
\r
268 //! Sets a pixel shader constant.
\r
269 void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1) override;
\r
271 //! Sets a constant for the vertex shader based on an index.
\r
272 bool setVertexShaderConstant(s32 index, const f32* floats, int count) override;
\r
274 //! Int interface for the above.
\r
275 bool setVertexShaderConstant(s32 index, const s32* ints, int count) override;
\r
277 //! Uint interface for the above.
\r
278 bool setVertexShaderConstant(s32 index, const u32* ints, int count) override;
\r
280 //! Sets a constant for the pixel shader based on an index.
\r
281 bool setPixelShaderConstant(s32 index, const f32* floats, int count) override;
\r
283 //! Int interface for the above.
\r
284 bool setPixelShaderConstant(s32 index, const s32* ints, int count) override;
\r
286 //! Uint interface for the above.
\r
287 bool setPixelShaderConstant(s32 index, const u32* ints, int count) override;
\r
289 //! disables all textures beginning with the optional fromStage parameter. Otherwise all texture stages are disabled.
\r
290 //! Returns whether disabling was successful or not.
\r
291 bool disableTextures(u32 fromStage=0);
\r
293 //! Adds a new material renderer to the VideoDriver, using
\r
294 //! extGLGetObjectParameteriv(shaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &status)
\r
295 //! pixel and/or vertex shaders to render geometry.
\r
296 virtual s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,
\r
297 IShaderConstantSetCallBack* callback, E_MATERIAL_TYPE baseMaterial, s32 userData) override;
\r
299 //! Adds a new material renderer to the VideoDriver, using GLSL to render geometry.
\r
300 virtual s32 addHighLevelShaderMaterial(
\r
301 const c8* vertexShaderProgram,
\r
302 const c8* vertexShaderEntryPointName,
\r
303 E_VERTEX_SHADER_TYPE vsCompileTarget,
\r
304 const c8* pixelShaderProgram,
\r
305 const c8* pixelShaderEntryPointName,
\r
306 E_PIXEL_SHADER_TYPE psCompileTarget,
\r
307 const c8* geometryShaderProgram,
\r
308 const c8* geometryShaderEntryPointName = "main",
\r
309 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,
\r
310 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
\r
311 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,
\r
312 u32 verticesOut = 0,
\r
313 IShaderConstantSetCallBack* callback = 0,
\r
314 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
\r
315 s32 userData = 0) override;
\r
317 //! Returns a pointer to the IVideoDriver interface. (Implementation for
\r
318 //! IMaterialRendererServices)
\r
319 IVideoDriver* getVideoDriver() override;
\r
321 //! Returns the maximum amount of primitives (mostly vertices) which
\r
322 //! the device is able to render with one drawIndexedTriangleList
\r
324 u32 getMaximalPrimitiveCount() const override;
\r
326 virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
\r
327 const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN) override;
\r
329 //! Creates a render target texture for a cubemap
\r
330 ITexture* addRenderTargetTextureCubemap(const irr::u32 sideLen,
\r
331 const io::path& name, const ECOLOR_FORMAT format) override;
\r
333 virtual bool setRenderTargetEx(IRenderTarget* target, u16 clearFlag, SColor clearColor = SColor(255,0,0,0),
\r
334 f32 clearDepth = 1.f, u8 clearStencil = 0) override;
\r
336 void clearBuffers(u16 flag, SColor color = SColor(255,0,0,0), f32 depth = 1.f, u8 stencil = 0) override;
\r
338 //! Returns an image created from the last rendered frame.
\r
339 IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER) override;
\r
341 //! checks if an OpenGL error has happened and prints it (+ some internal code which is usually the line number)
\r
342 //! for performance reasons only available in debug mode
\r
343 bool testGLError(int code=0);
\r
345 //! Set/unset a clipping plane.
\r
346 //! There are at least 6 clipping planes available for the user to set at will.
\r
347 //! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
\r
348 //! \param plane: The plane itself.
\r
349 //! \param enable: If true, enable the clipping plane else disable it.
\r
350 bool setClipPlane(u32 index, const core::plane3df& plane, bool enable=false) override;
\r
352 //! Enable/disable a clipping plane.
\r
353 //! There are at least 6 clipping planes available for the user to set at will.
\r
354 //! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
\r
355 //! \param enable: If true, enable the clipping plane else disable it.
\r
356 void enableClipPlane(u32 index, bool enable) override;
\r
358 //! Enable the 2d override material
\r
359 void enableMaterial2D(bool enable=true) override;
\r
361 //! Returns the graphics card vendor name.
\r
362 core::stringc getVendorInfo() override {return VendorName;}
\r
364 //! Returns the maximum texture size supported.
\r
365 core::dimension2du getMaxTextureSize() const override;
\r
367 //! Removes a texture from the texture cache and deletes it, freeing lot of memory.
\r
368 void removeTexture(ITexture* texture) override;
\r
370 //! Check if the driver supports creating textures with the given color format
\r
371 bool queryTextureFormat(ECOLOR_FORMAT format) const override;
\r
373 //! Used by some SceneNodes to check if a material should be rendered in the transparent render pass
\r
374 bool needsTransparentRenderPass(const irr::video::SMaterial& material) const override;
\r
376 //! Convert E_PRIMITIVE_TYPE to OpenGL equivalent
\r
377 GLenum primitiveTypeToGL(scene::E_PRIMITIVE_TYPE type) const;
\r
379 //! Convert E_BLEND_FACTOR to OpenGL equivalent
\r
380 GLenum getGLBlend(E_BLEND_FACTOR factor) const;
\r
382 //! Get ZBuffer bits.
\r
383 GLenum getZBufferBits() const;
\r
385 bool getColorFormatParameters(ECOLOR_FORMAT format, GLint& internalFormat, GLenum& pixelFormat,
\r
386 GLenum& pixelType, void(**converter)(const void*, s32, void*)) const;
\r
388 //! Return info about fixed pipeline state.
\r
389 E_OPENGL_FIXED_PIPELINE_STATE getFixedPipelineState() const;
\r
391 //! Set info about fixed pipeline state.
\r
392 void setFixedPipelineState(E_OPENGL_FIXED_PIPELINE_STATE state);
\r
394 //! Get current material.
\r
395 const SMaterial& getCurrentMaterial() const;
\r
397 COpenGLCacheHandler* getCacheHandler() const;
\r
401 bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
402 bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
404 void uploadClipPlane(u32 index);
\r
406 //! inits the parts of the open gl driver used on all platforms
\r
407 bool genericDriverInit();
\r
409 ITexture* createDeviceDependentTexture(const io::path& name, IImage* image) override;
\r
411 ITexture* createDeviceDependentTextureCubemap(const io::path& name, const core::array<IImage*>& image) override;
\r
413 //! creates a transposed matrix in supplied GLfloat array to pass to OpenGL
\r
414 inline void getGLMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
\r
415 inline void getGLTextureMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
\r
417 //! get native wrap mode value
\r
418 GLint getTextureWrapMode(const u8 clamp);
\r
420 //! sets the needed renderstates
\r
421 void setRenderStates3DMode();
\r
423 //! sets the needed renderstates
\r
424 void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
\r
426 void createMaterialRenderers();
\r
428 //! Assign a hardware light to the specified requested light, if any
\r
429 //! free hardware lights exist.
\r
430 //! \param[in] lightIndex: the index of the requesting light
\r
431 void assignHardwareLight(u32 lightIndex);
\r
433 //! helper function for render setup.
\r
434 void getColorBuffer(const void* vertices, u32 vertexCount, E_VERTEX_TYPE vType);
\r
436 //! helper function doing the actual rendering.
\r
437 void renderArray(const void* indexList, u32 primitiveCount,
\r
438 scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
\r
440 //! Same as `CacheHandler->setViewport`, but also sets `ViewPort`
\r
441 virtual void setViewPortRaw(u32 width, u32 height);
\r
443 COpenGLCacheHandler* CacheHandler;
\r
445 core::stringw Name;
\r
446 core::matrix4 Matrices[ETS_COUNT];
\r
447 core::array<u8> ColorBuffer;
\r
449 //! enumeration for rendering modes such as 2d and 3d for minizing the switching of renderStates.
\r
452 ERM_NONE = 0, // no render state has been set yet.
\r
453 ERM_2D, // 2d drawing rendermode
\r
454 ERM_3D // 3d rendering mode
\r
457 E_RENDER_MODE CurrentRenderMode;
\r
458 //! bool to make all renderstates reset if set to true.
\r
459 bool ResetRenderStates;
\r
460 bool Transformation3DChanged;
\r
463 SMaterial Material, LastMaterial;
\r
465 struct SUserClipPlane
\r
467 SUserClipPlane() : Enabled(false) {}
\r
468 core::plane3df Plane;
\r
471 core::array<SUserClipPlane> UserClipPlanes;
\r
473 core::stringc VendorName;
\r
475 core::matrix4 TextureFlipMatrix;
\r
477 //! Color buffer format
\r
478 ECOLOR_FORMAT ColorFormat;
\r
480 E_OPENGL_FIXED_PIPELINE_STATE FixedPipelineState;
\r
482 SIrrlichtCreationParameters Params;
\r
484 //! Built-in 2D quad for 2D rendering.
\r
485 S3DVertex Quad2DVertices[4];
\r
486 static const u16 Quad2DIndices[4];
\r
488 IContextManager* ContextManager;
\r
491 } // end namespace video
\r
492 } // end namespace irr
\r
495 #endif // _IRR_COMPILE_WITH_OPENGL_
\r