Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gldevice.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include "graphics/core/device.h"
26 
27 #include <string>
28 #include <vector>
29 #include <set>
30 #include <map>
31 
32 
33 // Graphics module namespace
34 namespace Gfx {
35 
40 enum VBOMode
41 {
42  VBO_MODE_ENABLE,
45 };
46 
51 {
53  int redSize;
55  int greenSize;
57  int blueSize;
59  int alphaSize;
61  int depthSize;
62 
65 
68 
71 
73  void LoadDefault();
74 };
75 
76 struct GLDevicePrivate;
77 
89 class CGLDevice : public CDevice
90 {
91 public:
92  CGLDevice(const GLDeviceConfig &config);
93  virtual ~CGLDevice();
94 
95  virtual void DebugHook();
96  virtual void DebugLights();
97 
98  virtual bool Create();
99  virtual void Destroy();
100 
101  void ConfigChanged(const GLDeviceConfig &newConfig);
102 
103  void SetUseVbo(bool useVbo);
104  bool GetUseVbo();
105 
106  virtual void BeginScene();
107  virtual void EndScene();
108 
109  virtual void Clear();
110 
111  virtual void SetTransform(TransformType type, const Math::Matrix &matrix);
112  virtual const Math::Matrix& GetTransform(TransformType type);
113  virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix);
114 
115  virtual void SetMaterial(const Material &material);
116  virtual const Material& GetMaterial();
117 
118  virtual int GetMaxLightCount();
119  virtual void SetLight(int index, const Light &light);
120  virtual const Light& GetLight(int index);
121  virtual void SetLightEnabled(int index, bool enabled);
122  virtual bool GetLightEnabled(int index);
123 
124  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params);
125  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params);
126  virtual void DestroyTexture(const Texture &texture);
127  virtual void DestroyAllTextures();
128 
129  virtual int GetMaxTextureStageCount();
130  virtual void SetTexture(int index, const Texture &texture);
131  virtual void SetTexture(int index, unsigned int textureId);
132  virtual Texture GetTexture(int index);
133  virtual void SetTextureEnabled(int index, bool enabled);
134  virtual bool GetTextureEnabled(int index);
135 
136  virtual void SetTextureStageParams(int index, const TextureStageParams &params);
137  virtual TextureStageParams GetTextureStageParams(int index);
138 
139  virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT);
140 
141  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
142  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f));
143  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
144  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f));
145  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount);
146 
147  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
148  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount);
149  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount);
150  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
151  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount);
152  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount);
153  virtual void DrawStaticBuffer(unsigned int bufferId);
154  virtual void DestroyStaticBuffer(unsigned int bufferId);
155 
156  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius);
157 
158  virtual void SetRenderState(RenderState state, bool enabled);
159  virtual bool GetRenderState(RenderState state);
160 
161  virtual void SetDepthTestFunc(CompFunc func);
162  virtual CompFunc GetDepthTestFunc();
163 
164  virtual void SetDepthBias(float factor);
165  virtual float GetDepthBias();
166 
167  virtual void SetAlphaTestFunc(CompFunc func, float refValue);
168  virtual void GetAlphaTestFunc(CompFunc &func, float &refValue);
169 
170  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend);
171  virtual void GetBlendFunc(BlendFunc &srcBlend, BlendFunc &dstBlend);
172 
173  virtual void SetClearColor(const Color &color);
174  virtual Color GetClearColor();
175 
176  virtual void SetGlobalAmbient(const Color &color);
177  virtual Color GetGlobalAmbient();
178 
179  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density);
180  virtual void GetFogParams(FogMode &mode, Color &color, float &start, float &end, float &density);
181 
182  virtual void SetCullMode(CullMode mode);
183  virtual CullMode GetCullMode();
184 
185  virtual void SetShadeModel(ShadeModel model);
186  virtual ShadeModel GetShadeModel();
187 
188  virtual void SetFillMode(FillMode mode) ;
189  virtual FillMode GetFillMode();
190 
191 private:
193  void UpdateModelviewMatrix();
195  void UpdateLightPosition(int index);
197  void UpdateTextureParams(int index);
198 
199 private:
201  GLDeviceConfig m_config;
202 
204  Math::Matrix m_worldMat;
206  Math::Matrix m_viewMat;
208  Math::Matrix m_modelviewMat;
210  Math::Matrix m_projectionMat;
211 
213  Material m_material;
214 
216  bool m_lighting;
218  std::vector<Light> m_lights;
220  std::vector<bool> m_lightsEnabled;
221 
223  std::vector<Texture> m_currentTextures;
225  std::vector<bool> m_texturesEnabled;
227  std::vector<TextureStageParams> m_textureStageParams;
228 
230  std::set<Texture> m_allTextures;
231 
233  enum VertexType
234  {
235  VERTEX_TYPE_NORMAL,
236  VERTEX_TYPE_TEX2,
237  VERTEX_TYPE_COL,
238  };
239 
241  struct VboObjectInfo
242  {
243  PrimitiveType primitiveType;
244  unsigned int bufferId;
245  VertexType vertexType;
246  int vertexCount;
247  };
248 
250  bool m_multitextureAvailable;
252  bool m_vboAvailable;
254  std::map<unsigned int, VboObjectInfo> m_vboObjects;
256  unsigned int m_lastVboId;
257 };
258 
259 
260 } // namespace Gfx
261 
virtual const Math::Matrix & GetTransform(TransformType type)
Returns the current transform matrix of given type.
Definition: gldevice.cpp:342
Additional config with OpenGL-specific settings.
Definition: gldevice.h:50
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend)
Sets the blending functions for source and destination operations.
Definition: gldevice.cpp:1669
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density)
Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) ...
Definition: gldevice.cpp:1709
virtual int GetMaxTextureStageCount()
Returns the maximum number of multitexture stages.
Definition: gldevice.cpp:731
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex *vertices, int vertexCount)
Updates the static buffer composed of given primitives with single texture vertices.
Definition: gldevice.cpp:1209
virtual void SetShadeModel(ShadeModel model)
Sets the shade model.
Definition: gldevice.cpp:1758
virtual void SetGlobalAmbient(const Color &color)
Sets the global ambient color.
Definition: gldevice.cpp:1697
virtual CullMode GetCullMode()
Returns the current cull mode.
Definition: gldevice.cpp:1748
GLDeviceConfig()
Constructor calls LoadDefaults()
Definition: gldevice.cpp:38
virtual int GetMaxLightCount()
Returns the maximum number of lights available.
Definition: gldevice.cpp:410
Vertex of a primitive.
Definition: vertex.h:48
Vertex with secondary texture coordinates.
Definition: vertex.h:107
int depthSize
Color depth in bits.
Definition: gldevice.h:61
int redSize
Size of red channel in bits.
Definition: gldevice.h:53
virtual bool Create()
Initializes the device, setting the initial state.
Definition: gldevice.cpp:182
int blueSize
Size of blue channel in bits.
Definition: gldevice.h:57
FogMode
Type of fog calculation function.
Definition: device.h:149
virtual void DrawStaticBuffer(unsigned int bufferId)
Draws a static buffer.
Definition: gldevice.cpp:1290
virtual bool GetRenderState(RenderState state)
Returns the current setting of given render state.
Definition: gldevice.cpp:1532
4x4 matrix
Definition: matrix.h:63
virtual void SetLight(int index, const Light &light)
Sets the light at given index.
Definition: gldevice.cpp:415
virtual FillMode GetFillMode()
Returns the current fill mode.
Definition: gldevice.cpp:1783
virtual void SetLightEnabled(int index, bool enabled)
Enables/disables the light at given index.
Definition: gldevice.cpp:493
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:82
virtual void SetTexture(int index, const Texture &texture)
Definition: gldevice.cpp:740
virtual void DestroyStaticBuffer(unsigned int bufferId)
Deletes a static buffer.
Definition: gldevice.cpp:1380
FillMode
Polygon fill mode.
Definition: device.h:182
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius)
Definition: gldevice.cpp:1410
virtual void GetAlphaTestFunc(CompFunc &func, float &refValue)
Returns the current alpha test function and reference value.
Definition: gldevice.cpp:1619
virtual void SetAlphaTestFunc(CompFunc func, float refValue)
Sets the alpha test function and reference value.
Definition: gldevice.cpp:1614
Implementation of CDevice interface in OpenGL.
Definition: gldevice.h:89
CompFunc
Type of function used to compare values.
Definition: device.h:114
virtual Color GetGlobalAmbient()
Returns the global ambient color.
Definition: gldevice.cpp:1702
Parameters for a texture unit.
Definition: texture.h:165
Material of a surface.
Definition: material.h:41
virtual Texture GetTexture(int index)
Definition: gldevice.cpp:786
&lt; override: disable
Definition: gldevice.h:44
virtual bool GetLightEnabled(int index)
Returns the current enable state of light at given index.
Definition: gldevice.cpp:506
virtual TextureStageParams GetTextureStageParams(int index)
Returns the current params of texture stage with given index.
Definition: gldevice.cpp:1005
virtual void SetRenderState(RenderState state, bool enabled)
Enables/disables the given render state.
Definition: gldevice.cpp:1489
virtual float GetDepthBias()
Returns the current depth bias.
Definition: gldevice.cpp:1607
virtual void SetCullMode(CullMode mode)
Sets the current cull mode.
Definition: gldevice.cpp:1739
virtual const Light & GetLight(int index)
Returns the current light at given index.
Definition: gldevice.cpp:485
General config for graphics device.
Definition: device.h:51
Properties of light in 3D scene.
Definition: light.h:51
virtual void DebugHook()
Provides a hook to debug graphics code (implementation-specific)
Definition: gldevice.cpp:75
Parameters for texture creation.
Definition: texture.h:129
ShadeModel
Shade model used in rendering.
Definition: device.h:172
virtual void SetMaterial(const Material &material)
Sets the current material.
Definition: gldevice.cpp:396
BlendFunc
Type of blending function.
Definition: device.h:130
int alphaSize
Size of alpha channel in bits.
Definition: gldevice.h:59
&lt; override: enable
Definition: gldevice.h:43
virtual void GetFogParams(FogMode &mode, Color &color, float &start, float &end, float &density)
Returns the current fog parameters: mode, color, start distance, end distance and density (for exp mo...
Definition: gldevice.cpp:1722
virtual void SetTransform(TransformType type, const Math::Matrix &matrix)
Sets the transform matrix of given type.
Definition: gldevice.cpp:318
virtual void SetFillMode(FillMode mode)
Sets the current fill mode.
Definition: gldevice.cpp:1775
virtual const Material & GetMaterial()
Returns the current material.
Definition: gldevice.cpp:405
virtual void Clear()
Clears the screen to blank.
Definition: gldevice.cpp:312
virtual void SetClearColor(const Color &color)
Sets the clear color.
Definition: gldevice.cpp:1685
Image loaded from file.
Definition: image.h:54
virtual ShadeModel GetShadeModel()
Returns the current shade model.
Definition: gldevice.cpp:1765
PrimitiveType
Type of primitive to render.
Definition: device.h:196
CullMode
Culling mode for polygons.
Definition: device.h:160
Colored vertex.
Definition: vertex.h:80
virtual void DestroyTexture(const Texture &texture)
Deletes a given texture, freeing it from video memory.
Definition: gldevice.cpp:703
VBOMode
VBO autodetect/override.
Definition: gldevice.h:40
int greenSize
Size of green channel in bits.
Definition: gldevice.h:55
Implementation-specific image data.
Definition: image.h:39
virtual void GetBlendFunc(BlendFunc &srcBlend, BlendFunc &dstBlend)
Returns the current blending functions for source and destination operations.
Definition: gldevice.cpp:1674
virtual Color GetClearColor()
Returns the current clear color.
Definition: gldevice.cpp:1690
RenderState
Render states that can be enabled/disabled.
Definition: device.h:99
Info about a texture.
Definition: texture.h:212
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT)
Sets only the texture wrap modes (for faster than thru stage params)
Definition: gldevice.cpp:974
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex *vertices, int vertexCount)
Creates a static buffer composed of given primitives with single texture vertices.
Definition: gldevice.cpp:1107
virtual void Destroy()
Destroys the device, releasing every acquired resource.
Definition: gldevice.cpp:264
virtual void SetTextureEnabled(int index, bool enabled)
Enables/disables the given texture stage.
Definition: gldevice.cpp:793
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params)
Definition: gldevice.cpp:517
3D (3x1) vector
Definition: vector.h:49
Abstract graphics device - CDevice class and related structs/enums.
void LoadDefault()
Loads the default values.
Definition: gldevice.cpp:43
virtual bool GetTextureEnabled(int index)
Returns the current enable state of given texture stage.
Definition: gldevice.cpp:816
RGBA color.
Definition: color.h:35
virtual void EndScene()
Ends drawing the 3D scene.
Definition: gldevice.cpp:308
VBOMode vboMode
VBO override/autodetect.
Definition: gldevice.h:67
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount, Color color=Color(1.0f, 1.0f, 1.0f, 1.0f))
Renders primitive composed of vertices with single texture.
Definition: gldevice.cpp:1027
virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix)
Multiplies the current transform matrix of given type by given matrix.
Definition: gldevice.cpp:356
virtual void DebugLights()
Displays light positions to aid in debuggings.
Definition: gldevice.cpp:82
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:88
virtual void BeginScene()
Begins drawing the 3D scene.
Definition: gldevice.cpp:298
virtual void SetDepthBias(float factor)
Sets the depth bias (constant value added to Z-coords)
Definition: gldevice.cpp:1602
bool hardwareAccel
Force hardware acceleration (video mode set will fail on lack of hw accel)
Definition: gldevice.h:64
virtual void SetTextureStageParams(int index, const TextureStageParams &params)
Definition: gldevice.cpp:827
virtual CompFunc GetDepthTestFunc()
Returns the current function of depth test.
Definition: gldevice.cpp:1595
virtual void DestroyAllTextures()
Deletes all textures created so far.
Definition: gldevice.cpp:719
Abstract interface of graphics device.
Definition: device.h:235
virtual void SetDepthTestFunc(CompFunc func)
Sets the function of depth test.
Definition: gldevice.cpp:1590