1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in Irrlicht.h
5
#ifndef __C_VIDEO_OPEN_GL_H_INCLUDED__
6
#define __C_VIDEO_OPEN_GL_H_INCLUDED__
8
#include "IrrCompileConfig.h"
10
#include "SIrrCreationParameters.h"
14
class CIrrDeviceWin32;
15
class CIrrDeviceLinux;
17
class CIrrDeviceMacOSX;
20
#ifdef _IRR_COMPILE_WITH_OPENGL_
22
#include "CNullDriver.h"
23
#include "IMaterialRendererServices.h"
24
// also includes the OpenGL stuff
25
#include "COpenGLExtensionHandler.h"
34
class COpenGLDriver : public CNullDriver, public IMaterialRendererServices, public COpenGLExtensionHandler
36
friend class COpenGLTexture;
39
#ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
40
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceWin32* device);
41
//! inits the windows specific parts of the open gl driver
42
bool initDriver(CIrrDeviceWin32* device);
43
bool changeRenderContext(const SExposedVideoData& videoData, CIrrDeviceWin32* device);
46
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
47
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceLinux* device);
48
//! inits the GLX specific parts of the open gl driver
49
bool initDriver(CIrrDeviceLinux* device);
50
bool changeRenderContext(const SExposedVideoData& videoData, CIrrDeviceLinux* device);
53
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
54
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceSDL* device);
57
#ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
58
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceMacOSX *device);
61
//! generic version which overloads the unimplemented versions
62
bool changeRenderContext(const SExposedVideoData& videoData, void* device) {return false;}
65
virtual ~COpenGLDriver();
67
//! clears the zbuffer
68
virtual bool beginScene(bool backBuffer=true, bool zBuffer=true,
69
SColor color=SColor(255,0,0,0),
70
const SExposedVideoData& videoData=SExposedVideoData(),
71
core::rect<s32>* sourceRect=0);
73
//! presents the rendered scene on the screen, returns false if failed
74
virtual bool endScene();
76
//! sets transformation
77
virtual void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat);
80
struct SHWBufferLink_opengl : public SHWBufferLink
82
SHWBufferLink_opengl(const scene::IMeshBuffer *_MeshBuffer): SHWBufferLink(_MeshBuffer), vbo_verticesID(0),vbo_indicesID(0){}
84
GLuint vbo_verticesID; //tmp
85
GLuint vbo_indicesID; //tmp
87
GLuint vbo_verticesSize; //tmp
88
GLuint vbo_indicesSize; //tmp
91
//! updates hardware buffer if needed
92
virtual bool updateHardwareBuffer(SHWBufferLink *HWBuffer);
94
//! Create hardware buffer from mesh
95
virtual SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb);
97
//! Delete hardware buffer (only some drivers can)
98
virtual void deleteHardwareBuffer(SHWBufferLink *HWBuffer);
100
//! Draw hardware buffer
101
virtual void drawHardwareBuffer(SHWBufferLink *HWBuffer);
103
//! Create occlusion query.
104
/** Use node for identification and mesh for occlusion test. */
105
virtual void addOcclusionQuery(scene::ISceneNode* node,
106
const scene::IMesh* mesh=0);
108
//! Remove occlusion query.
109
virtual void removeOcclusionQuery(scene::ISceneNode* node);
111
//! Run occlusion query. Draws mesh stored in query.
112
/** If the mesh shall not be rendered visible, use
113
overrideMaterial to disable the color and depth buffer. */
114
virtual void runOcclusionQuery(scene::ISceneNode* node, bool visible=false);
116
//! Update occlusion query. Retrieves results from GPU.
117
/** If the query shall not block, set the flag to false.
118
Update might not occur in this case, though */
119
virtual void updateOcclusionQuery(scene::ISceneNode* node, bool block=true);
121
//! Return query result.
122
/** Return value is the number of visible pixels/fragments.
123
The value is a safe approximation, i.e. can be larger then the
124
actual value of pixels. */
125
virtual u32 getOcclusionQueryResult(scene::ISceneNode* node) const;
127
//! draws a vertex primitive list
128
virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
129
const void* indexList, u32 primitiveCount,
130
E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
132
//! draws a vertex primitive list in 2d
133
virtual void draw2DVertexPrimitiveList(const void* vertices, u32 vertexCount,
134
const void* indexList, u32 primitiveCount,
135
E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
137
//! queries the features of the driver, returns true if feature is available
138
virtual bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const
140
return FeatureEnabled[feature] && COpenGLExtensionHandler::queryFeature(feature);
143
//! Sets a material. All 3d drawing functions draw geometry now
144
//! using this material.
145
//! \param material: Material to be used from now on.
146
virtual void setMaterial(const SMaterial& material);
148
//! draws a set of 2d images, using a color and the alpha channel of the
149
//! texture if desired.
150
void draw2DImageBatch(const video::ITexture* texture,
151
const core::array<core::position2d<s32> >& positions,
152
const core::array<core::rect<s32> >& sourceRects,
153
const core::rect<s32>* clipRect,
155
bool useAlphaChannelOfTexture);
157
//! draws an 2d image, using a color (if color is other then Color(255,255,255,255)) and the alpha channel of the texture if wanted.
158
virtual void draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
159
const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
160
SColor color=SColor(255,255,255,255), bool useAlphaChannelOfTexture=false);
162
//! draws a set of 2d images, using a color and the alpha
163
/** channel of the texture if desired. The images are drawn
164
beginning at pos and concatenated in one line. All drawings
165
are clipped against clipRect (if != 0).
166
The subtextures are defined by the array of sourceRects
167
and are chosen by the indices given.
168
\param texture: Texture to be drawn.
169
\param pos: Upper left 2d destination position where the image will be drawn.
170
\param sourceRects: Source rectangles of the image.
171
\param indices: List of indices which choose the actual rectangle used each time.
172
\param clipRect: Pointer to rectangle on the screen where the image is clipped to.
173
This pointer can be 0. Then the image is not clipped.
174
\param color: Color with which the image is colored.
175
Note that the alpha component is used: If alpha is other than 255, the image will be transparent.
176
\param useAlphaChannelOfTexture: If true, the alpha channel of the texture is
177
used to draw the image. */
178
virtual void draw2DImage(const video::ITexture* texture,
179
const core::position2d<s32>& pos,
180
const core::array<core::rect<s32> >& sourceRects,
181
const core::array<s32>& indices,
182
const core::rect<s32>* clipRect=0,
183
SColor color=SColor(255,255,255,255),
184
bool useAlphaChannelOfTexture=false);
186
//! Draws a part of the texture into the rectangle.
187
virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
188
const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
189
const video::SColor* const colors=0, bool useAlphaChannelOfTexture=false);
191
//! draw an 2d rectangle
192
virtual void draw2DRectangle(SColor color, const core::rect<s32>& pos,
193
const core::rect<s32>* clip = 0);
195
//!Draws an 2d rectangle with a gradient.
196
virtual void draw2DRectangle(const core::rect<s32>& pos,
197
SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
198
const core::rect<s32>* clip = 0);
201
virtual void draw2DLine(const core::position2d<s32>& start,
202
const core::position2d<s32>& end,
203
SColor color=SColor(255,255,255,255));
205
//! Draws a single pixel
206
virtual void drawPixel(u32 x, u32 y, const SColor & color);
209
virtual void draw3DLine(const core::vector3df& start,
210
const core::vector3df& end,
211
SColor color = SColor(255,255,255,255));
213
//! \return Returns the name of the video driver. Example: In case of the Direct3D8
214
//! driver, it would return "Direct3D8.1".
215
virtual const wchar_t* getName() const;
217
//! deletes all dynamic lights there are
218
virtual void deleteAllDynamicLights();
220
//! adds a dynamic light, returning an index to the light
221
//! \param light: the light data to use to create the light
222
//! \return An index to the light, or -1 if an error occurs
223
virtual s32 addDynamicLight(const SLight& light);
225
//! Turns a dynamic light on or off
226
//! \param lightIndex: the index returned by addDynamicLight
227
//! \param turnOn: true to turn the light on, false to turn it off
228
virtual void turnLightOn(s32 lightIndex, bool turnOn);
230
//! returns the maximal amount of dynamic lights the device can handle
231
virtual u32 getMaximalDynamicLightAmount() const;
233
//! Sets the dynamic ambient light color. The default color is
234
//! (0,0,0,0) which means it is dark.
235
//! \param color: New color of the ambient light.
236
virtual void setAmbientLight(const SColorf& color);
238
//! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
239
//! this: First, draw all geometry. Then use this method, to draw the shadow
240
//! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
241
virtual void drawStencilShadowVolume(const core::vector3df* triangles, s32 count, bool zfail);
243
//! Fills the stencil shadow with color. After the shadow volume has been drawn
244
//! into the stencil buffer using IVideoDriver::drawStencilShadowVolume(), use this
245
//! to draw the color of the shadow.
246
virtual void drawStencilShadow(bool clearStencilBuffer=false,
247
video::SColor leftUpEdge = video::SColor(0,0,0,0),
248
video::SColor rightUpEdge = video::SColor(0,0,0,0),
249
video::SColor leftDownEdge = video::SColor(0,0,0,0),
250
video::SColor rightDownEdge = video::SColor(0,0,0,0));
253
virtual void setViewPort(const core::rect<s32>& area);
255
//! Sets the fog mode.
256
virtual void setFog(SColor color, E_FOG_TYPE fogType, f32 start,
257
f32 end, f32 density, bool pixelFog, bool rangeFog);
259
//! Only used by the internal engine. Used to notify the driver that
260
//! the window was resized.
261
virtual void OnResize(const core::dimension2d<u32>& size);
263
//! Returns type of video driver
264
virtual E_DRIVER_TYPE getDriverType() const;
266
//! get color format of the current color buffer
267
virtual ECOLOR_FORMAT getColorFormat() const;
269
//! Returns the transformation set by setTransform
270
virtual const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const;
272
//! Can be called by an IMaterialRenderer to make its work easier.
273
virtual void setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial,
274
bool resetAllRenderstates);
276
//! Sets a vertex shader constant.
277
virtual void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1);
279
//! Sets a pixel shader constant.
280
virtual void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1);
282
//! Sets a constant for the vertex shader based on a name.
283
virtual bool setVertexShaderConstant(const c8* name, const f32* floats, int count);
285
//! Sets a constant for the pixel shader based on a name.
286
virtual bool setPixelShaderConstant(const c8* name, const f32* floats, int count);
288
//! sets the current Texture
289
//! Returns whether setting was a success or not.
290
bool setActiveTexture(u32 stage, const video::ITexture* texture);
292
//! disables all textures beginning with the optional fromStage parameter. Otherwise all texture stages are disabled.
293
//! Returns whether disabling was successful or not.
294
bool disableTextures(u32 fromStage=0);
296
//! Adds a new material renderer to the VideoDriver, using
297
//! extGLGetObjectParameteriv(shaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &status)
298
//! pixel and/or vertex shaders to render geometry.
299
virtual s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,
300
IShaderConstantSetCallBack* callback, E_MATERIAL_TYPE baseMaterial, s32 userData);
302
//! Adds a new material renderer to the VideoDriver, using GLSL to render geometry.
303
virtual s32 addHighLevelShaderMaterial(
304
const c8* vertexShaderProgram,
305
const c8* vertexShaderEntryPointName,
306
E_VERTEX_SHADER_TYPE vsCompileTarget,
307
const c8* pixelShaderProgram,
308
const c8* pixelShaderEntryPointName,
309
E_PIXEL_SHADER_TYPE psCompileTarget,
310
const c8* geometryShaderProgram,
311
const c8* geometryShaderEntryPointName = "main",
312
E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,
313
scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
314
scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,
316
IShaderConstantSetCallBack* callback = 0,
317
E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
320
//! Returns a pointer to the IVideoDriver interface. (Implementation for
321
//! IMaterialRendererServices)
322
virtual IVideoDriver* getVideoDriver();
324
//! Returns the maximum amount of primitives (mostly vertices) which
325
//! the device is able to render with one drawIndexedTriangleList
327
virtual u32 getMaximalPrimitiveCount() const;
329
virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
330
const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN);
332
//! set or reset render target
333
virtual bool setRenderTarget(video::E_RENDER_TARGET target, bool clearTarget,
334
bool clearZBuffer, SColor color);
336
//! set or reset render target texture
337
virtual bool setRenderTarget(video::ITexture* texture, bool clearBackBuffer,
338
bool clearZBuffer, SColor color);
340
//! Sets multiple render targets
341
virtual bool setRenderTarget(const core::array<video::IRenderTarget>& texture,
342
bool clearBackBuffer=true, bool clearZBuffer=true, SColor color=SColor(0,0,0,0));
344
//! Clears the ZBuffer.
345
virtual void clearZBuffer();
347
//! Returns an image created from the last rendered frame.
348
virtual IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER);
350
//! checks if an OpenGL error has happend and prints it
351
//! for performance reasons only available in debug mode
354
//! Set/unset a clipping plane.
355
//! There are at least 6 clipping planes available for the user to set at will.
356
//! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
357
//! \param plane: The plane itself.
358
//! \param enable: If true, enable the clipping plane else disable it.
359
virtual bool setClipPlane(u32 index, const core::plane3df& plane, bool enable=false);
361
//! Enable/disable a clipping plane.
362
//! There are at least 6 clipping planes available for the user to set at will.
363
//! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
364
//! \param enable: If true, enable the clipping plane else disable it.
365
virtual void enableClipPlane(u32 index, bool enable);
367
//! Enable the 2d override material
368
virtual void enableMaterial2D(bool enable=true);
370
//! Returns the graphics card vendor name.
371
virtual core::stringc getVendorInfo() {return VendorName;}
373
//! Returns the maximum texture size supported.
374
virtual core::dimension2du getMaxTextureSize() const;
376
ITexture* createDepthTexture(ITexture* texture, bool shared=true);
377
void removeDepthTexture(ITexture* texture);
379
//! Convert E_PRIMITIVE_TYPE to OpenGL equivalent
380
GLenum primitiveTypeToGL(scene::E_PRIMITIVE_TYPE type) const;
382
//! Convert E_BLEND_FACTOR to OpenGL equivalent
383
GLenum getGLBlend(E_BLEND_FACTOR factor) const;
387
//! clears the zbuffer and color buffer
388
void clearBuffers(bool backBuffer, bool zBuffer, bool stencilBuffer, SColor color);
390
bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
391
bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
393
void uploadClipPlane(u32 index);
395
//! inits the parts of the open gl driver used on all platforms
396
bool genericDriverInit();
397
//! returns a device dependent texture from a software surface (IImage)
398
virtual video::ITexture* createDeviceDependentTexture(IImage* surface, const io::path& name, void* mipmapData);
400
//! creates a transposed matrix in supplied GLfloat array to pass to OpenGL
401
inline void getGLMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
402
inline void getGLTextureMatrix(GLfloat gl_matrix[16], const core::matrix4& m);
404
//! Set GL pipeline to desired texture wrap modes of the material
405
void setWrapMode(const SMaterial& material);
407
//! get native wrap mode value
408
GLint getTextureWrapMode(const u8 clamp);
410
//! sets the needed renderstates
411
void setRenderStates3DMode();
413
//! sets the needed renderstates
414
void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
416
// returns the current size of the screen or rendertarget
417
virtual const core::dimension2d<u32>& getCurrentRenderTargetSize() const;
419
void createMaterialRenderers();
421
//! Assign a hardware light to the specified requested light, if any
422
//! free hardware lights exist.
423
//! \param[in] lightIndex: the index of the requesting light
424
void assignHardwareLight(u32 lightIndex);
426
//! helper function for render setup.
427
void getColorBuffer(const void* vertices, u32 vertexCount, E_VERTEX_TYPE vType);
429
//! helper function doing the actual rendering.
430
void renderArray(const void* indexList, u32 primitiveCount,
431
scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
434
core::matrix4 Matrices[ETS_COUNT];
435
core::array<u8> ColorBuffer;
437
//! enumeration for rendering modes such as 2d and 3d for minizing the switching of renderStates.
440
ERM_NONE = 0, // no render state has been set yet.
441
ERM_2D, // 2d drawing rendermode
442
ERM_3D // 3d rendering mode
445
E_RENDER_MODE CurrentRenderMode;
446
//! bool to make all renderstates reset if set to true.
447
bool ResetRenderStates;
448
bool Transformation3DChanged;
451
SMaterial Material, LastMaterial;
452
COpenGLTexture* RenderTargetTexture;
453
core::array<video::IRenderTarget> MRTargets;
454
const ITexture* CurrentTexture[MATERIAL_MAX_TEXTURES];
455
core::array<ITexture*> DepthTextures;
456
struct SUserClipPlane
458
SUserClipPlane() : Enabled(false) {}
459
core::plane3df Plane;
462
core::array<SUserClipPlane> UserClipPlanes;
464
core::dimension2d<u32> CurrentRendertargetSize;
466
core::stringc VendorName;
468
core::matrix4 TextureFlipMatrix;
470
//! Color buffer format
471
ECOLOR_FORMAT ColorFormat;
473
//! Render target type for render operations
474
E_RENDER_TARGET CurrentTarget;
476
SIrrlichtCreationParameters Params;
478
//! All the lights that have been requested; a hardware limited
479
//! number of them will be used at once.
480
struct RequestedLight
482
RequestedLight(SLight const & lightData)
483
: LightData(lightData), HardwareLightIndex(-1), DesireToBeOn(true) { }
486
s32 HardwareLightIndex; // GL_LIGHT0 - GL_LIGHT7
489
core::array<RequestedLight> RequestedLights;
491
#ifdef _IRR_WINDOWS_API_
492
HDC HDc; // Private GDI Device Context
494
#ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
495
CIrrDeviceWin32 *Win32Device;
498
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
499
GLXDrawable Drawable;
501
CIrrDeviceLinux *X11Device;
503
#ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
504
CIrrDeviceMacOSX *OSXDevice;
506
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
507
CIrrDeviceSDL *SDLDevice;
510
E_DEVICE_TYPE DeviceType;
513
} // end namespace video
514
} // end namespace irr
517
#endif // _IRR_COMPILE_WITH_OPENGL_