Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gl33device.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "graphics/core/device.h"
28 #include "graphics/opengl/glutil.h"
29 
30 #include <string>
31 #include <vector>
32 #include <set>
33 #include <map>
34 
35 
36 // Graphics module namespace
37 namespace Gfx {
38 
50 class CGL33Device : public CDevice
51 {
52 public:
53  CGL33Device(const GLDeviceConfig &config);
54  virtual ~CGL33Device();
55 
56  virtual void DebugHook() OVERRIDE;
57  virtual void DebugLights() OVERRIDE;
58 
59  virtual bool Create() OVERRIDE;
60  virtual void Destroy() OVERRIDE;
61 
62  void ConfigChanged(const GLDeviceConfig &newConfig);
63 
64  virtual void BeginScene() OVERRIDE;
65  virtual void EndScene() OVERRIDE;
66 
67  virtual void Clear() OVERRIDE;
68 
69  virtual void SetTransform(TransformType type, const Math::Matrix &matrix) OVERRIDE;
70 
71  virtual void SetMaterial(const Material &material) OVERRIDE;
72 
73  virtual int GetMaxLightCount() OVERRIDE;
74  virtual void SetLight(int index, const Light &light) OVERRIDE;
75  virtual void SetLightEnabled(int index, bool enabled) OVERRIDE;
76 
77  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) OVERRIDE;
78  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) OVERRIDE;
79  virtual Texture CreateDepthTexture(int width, int height, int depth) OVERRIDE;
80  virtual void DestroyTexture(const Texture &texture) OVERRIDE;
81  virtual void DestroyAllTextures() OVERRIDE;
82 
83  virtual int GetMaxTextureStageCount() OVERRIDE;
84  virtual void SetTexture(int index, const Texture &texture) OVERRIDE;
85  virtual void SetTexture(int index, unsigned int textureId) OVERRIDE;
86  virtual void SetTextureEnabled(int index, bool enabled) OVERRIDE;
87 
88  virtual void SetTextureStageParams(int index, const TextureStageParams &params) OVERRIDE;
89 
90  virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) OVERRIDE;
91  virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE;
92 
93  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
94  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
95  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
96  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
97  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) OVERRIDE;
98 
99  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) OVERRIDE;
100  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) OVERRIDE;
101  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) OVERRIDE;
102  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) OVERRIDE;
103  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) OVERRIDE;
104  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) OVERRIDE;
105  virtual void DrawStaticBuffer(unsigned int bufferId) OVERRIDE;
106  virtual void DestroyStaticBuffer(unsigned int bufferId) OVERRIDE;
107 
108  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) OVERRIDE;
109 
110  virtual void SetViewport(int x, int y, int width, int height) OVERRIDE;
111 
112  virtual void SetRenderState(RenderState state, bool enabled) OVERRIDE;
113 
114  virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) OVERRIDE;
115 
116  virtual void SetDepthTestFunc(CompFunc func) OVERRIDE;
117 
118  virtual void SetDepthBias(float factor, float units) OVERRIDE;
119 
120  virtual void SetAlphaTestFunc(CompFunc func, float refValue) OVERRIDE;
121 
122  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) OVERRIDE;
123 
124  virtual void SetClearColor(const Color &color) OVERRIDE;
125 
126  virtual void SetGlobalAmbient(const Color &color) OVERRIDE;
127 
128  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) OVERRIDE;
129 
130  virtual void SetCullMode(CullMode mode) OVERRIDE;
131 
132  virtual void SetShadeModel(ShadeModel model) OVERRIDE;
133 
134  virtual void SetShadowColor(float value) OVERRIDE;
135 
136  virtual void SetFillMode(FillMode mode) OVERRIDE;
137 
138  virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
139 
140  virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE;
141 
142  virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE;
143 
144  virtual void* GetFrameBufferPixels() const OVERRIDE;
145 
146 private:
148  void UpdateLightPosition(int index);
150  void UpdateTextureParams(int index);
152  void UpdateRenderingMode();
153 
155  inline void BindVBO(GLuint vbo);
157  inline void BindVAO(GLuint vao);
158 
159 private:
161  GLDeviceConfig m_config;
162 
164  Math::Matrix m_worldMat;
166  Math::Matrix m_viewMat;
168  Math::Matrix m_modelviewMat;
170  Math::Matrix m_projectionMat;
171 
173  Material m_material;
174 
176  bool m_lighting;
178  std::vector<Light> m_lights;
180  std::vector<bool> m_lightsEnabled;
181 
183  std::vector<Texture> m_currentTextures;
185  std::vector<bool> m_texturesEnabled;
187  std::vector<TextureStageParams> m_textureStageParams;
188 
190  std::set<Texture> m_allTextures;
191 
193  enum VertexType
194  {
195  VERTEX_TYPE_NORMAL,
196  VERTEX_TYPE_TEX2,
197  VERTEX_TYPE_COL,
198  };
199 
201  struct VertexBufferInfo
202  {
203  PrimitiveType primitiveType;
204  GLuint vbo;
205  GLuint vao;
206  VertexType vertexType;
207  int vertexCount;
208  unsigned int size;
209  };
210 
213  int m_glMajor, m_glMinor;
215  bool m_anisotropyAvailable;
217  int m_maxAnisotropy;
219  std::map<unsigned int, VertexBufferInfo> m_vboObjects;
221  unsigned int m_lastVboId;
223  GLuint m_currentVBO;
225  GLuint m_currentVAO;
226 
227  // Offscreen buffer
229  GLuint m_framebuffer;
231  GLuint m_colorBuffer;
233  GLuint m_depthBuffer;
235  int m_maxRenderbufferSize;
237  bool m_offscreenRenderingEnabled;
238 
240  GLuint m_shaderProgram;
242  bool m_perPixelLighting;
243 
245  unsigned int m_vertex;
246  unsigned int m_vertexTex2;
247  unsigned int m_vertexCol;
248 
249  // Uniforms
251  GLint uni_ProjectionMatrix;
253  GLint uni_ViewMatrix;
255  GLint uni_ModelMatrix;
257  GLint uni_ShadowMatrix;
259  GLint uni_NormalMatrix;
260 
262  GLint uni_PrimaryTexture;
264  GLint uni_SecondaryTexture;
266  GLint uni_ShadowTexture;
267 
268  GLint uni_PrimaryTextureEnabled;
269  GLint uni_SecondaryTextureEnabled;
270  GLint uni_ShadowTextureEnabled;
271 
272  // Fog parameters
274  GLint uni_FogEnabled;
276  GLint uni_FogRange;
278  GLint uni_FogColor;
279 
280  // Alpha test parameters
282  GLint uni_AlphaTestEnabled;
284  GLint uni_AlphaReference;
285 
287  GLint uni_ShadowColor;
288 
289  // Lighting parameters
290  GLint uni_SmoothShading;
292  GLint uni_LightingEnabled;
294  GLint uni_AmbientColor;
296  GLint uni_DiffuseColor;
298  GLint uni_SpecularColor;
299 
300  struct
301  {
303  GLint Enabled;
305  GLint Type;
307  GLint Position;
309  GLint Ambient;
311  GLint Diffuse;
313  GLint Specular;
315  GLint Attenuation;
316  } uni_Light[8];
317 };
318 
319 } // namespace Gfx
GLint Diffuse
Diffuse color.
Definition: gl33device.h:311
Additional config with OpenGL-specific settings.
Definition: glutil.h:48
virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE
Sets render target to texture.
Definition: gl33device.cpp:1897
virtual void DebugHook() OVERRIDE
Provides a hook to debug graphics code (implementation-specific)
Definition: gl33device.cpp:63
virtual void SetDepthBias(float factor, float units) OVERRIDE
Sets the depth bias (constant value added to Z-coords)
Definition: gl33device.cpp:1791
Vertex of a primitive.
Definition: vertex.h:51
Vertex with secondary texture coordinates.
Definition: vertex.h:112
virtual void SetLight(int index, const Light &light) OVERRIDE
Sets the light at given index.
Definition: gl33device.cpp:491
virtual void SetDepthTestFunc(CompFunc func) OVERRIDE
Sets the function of depth test.
Definition: gl33device.cpp:1786
virtual int GetMaxLightCount() OVERRIDE
Returns the maximum number of lights available.
Definition: gl33device.cpp:486
FogMode
Type of fog calculation function.
Definition: device.h:154
virtual void SetAlphaTestFunc(CompFunc func, float refValue) OVERRIDE
Sets the alpha test function and reference value.
Definition: gl33device.cpp:1796
virtual void Destroy() OVERRIDE
Destroys the device, releasing every acquired resource.
Definition: gl33device.cpp:385
virtual void SetFillMode(FillMode mode) OVERRIDE
Sets the current fill mode.
Definition: gl33device.cpp:1855
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) OVERRIDE
Sets the blending functions for source and destination operations.
Definition: gl33device.cpp:1801
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount, Color color=Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE
Renders primitive composed of vertices with single texture.
Definition: gl33device.cpp:1156
virtual void DestroyStaticBuffer(unsigned int bufferId) OVERRIDE
Deletes a static buffer.
Definition: gl33device.cpp:1615
4x4 matrix
Definition: matrix.h:66
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:98
virtual void SetViewport(int x, int y, int width, int height) OVERRIDE
Changes rendering viewport.
Definition: gl33device.cpp:1718
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) OVERRIDE
Definition: gl33device.cpp:572
FillMode
Polygon fill mode.
Definition: device.h:187
virtual void BeginScene() OVERRIDE
Begins drawing the 3D scene.
Definition: gl33device.cpp:421
virtual void SetMaterial(const Material &material) OVERRIDE
Sets the current material.
Definition: gl33device.cpp:477
GLint Ambient
Ambient color.
Definition: gl33device.h:309
CompFunc
Type of function used to compare values.
Definition: device.h:119
virtual void InitOffscreenBuffer(int width, int height) OVERRIDE
Initializes offscreen buffer.
Definition: gl33device.cpp:1863
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex *vertices, int vertexCount) OVERRIDE
Updates the static buffer composed of given primitives with single texture vertices.
Definition: gl33device.cpp:1434
Parameters for a texture unit.
Definition: texture.h:187
virtual Texture CreateDepthTexture(int width, int height, int depth) OVERRIDE
Creates a depth texture with specific dimensions and depth.
Definition: gl33device.cpp:769
Material of a surface.
Definition: material.h:44
GLint Position
Position or direction vector.
Definition: gl33device.h:307
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) OVERRIDE
Sets only the texture wrap modes (for faster than thru stage params)
Definition: gl33device.cpp:1125
virtual void EndScene() OVERRIDE
Ends drawing the 3D scene.
Definition: gl33device.cpp:430
virtual void SetShadowColor(float value) OVERRIDE
Sets shadow color.
Definition: gl33device.cpp:1850
Parameters for texture coordinate generation.
Definition: texture.h:253
virtual void SetCullMode(CullMode mode) OVERRIDE
Sets the current cull mode.
Definition: gl33device.cpp:1836
virtual void SetClearColor(const Color &color) OVERRIDE
Sets the clear color.
Definition: gl33device.cpp:1806
Properties of light in 3D scene.
Definition: light.h:53
virtual void SetTexture(int index, const Texture &texture) OVERRIDE
Definition: gl33device.cpp:851
virtual void CopyFramebufferToTexture(Texture &texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE
Copies content of framebuffer to texture.
Definition: gl33device.cpp:1933
virtual void Clear() OVERRIDE
Clears the screen to blank.
Definition: gl33device.cpp:434
virtual void DestroyAllTextures() OVERRIDE
Deletes all textures created so far.
Definition: gl33device.cpp:830
virtual void SetShadeModel(ShadeModel model) OVERRIDE
Sets the shade model.
Definition: gl33device.cpp:1845
Parameters for texture creation.
Definition: texture.h:154
virtual void SetGlobalAmbient(const Color &color) OVERRIDE
Sets the global ambient color.
Definition: gl33device.cpp:1811
ShadeModel
Shade model used in rendering.
Definition: device.h:177
BlendFunc
Type of blending function.
Definition: device.h:135
virtual void SetTextureStageParams(int index, const TextureStageParams &params) OVERRIDE
Definition: gl33device.cpp:903
virtual int GetMaxTextureStageCount() OVERRIDE
Returns the maximum number of multitexture stages.
Definition: gl33device.cpp:842
virtual void * GetFrameBufferPixels() const OVERRIDE
Returns the pixels of the entire screen.
Definition: gl33device.cpp:1946
virtual void DrawStaticBuffer(unsigned int bufferId) OVERRIDE
Draws a static buffer.
Definition: gl33device.cpp:1599
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) OVERRIDE
Sets the color mask.
Definition: gl33device.cpp:1781
Image loaded from file.
Definition: image.h:57
PrimitiveType
Type of primitive to render.
Definition: device.h:201
CullMode
Culling mode for polygons.
Definition: device.h:165
Colored vertex.
Definition: vertex.h:83
virtual void SetTextureEnabled(int index, bool enabled) OVERRIDE
Enables/disables the given texture stage.
Definition: gl33device.cpp:885
Namespace for (new) graphics code.
Definition: app.h:49
virtual bool Create() OVERRIDE
Initializes the device, setting the initial state.
Definition: gl33device.cpp:171
Implementation-specific image data.
Definition: image.h:42
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex *vertices, int vertexCount) OVERRIDE
Creates a static buffer composed of given primitives with single texture vertices.
Definition: gl33device.cpp:1301
RenderState
Render states that can be enabled/disabled.
Definition: device.h:102
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) OVERRIDE
Definition: gl33device.cpp:1639
Info about a texture.
Definition: texture.h:282
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) OVERRIDE
Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) ...
Definition: gl33device.cpp:1816
3D (3x1) vector
Definition: vector.h:52
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE
Sets the texture coordinate generation mode for given texture unit.
Definition: gl33device.cpp:913
Abstract graphics device - CDevice class and related structs/enums.
RGBA color.
Definition: color.h:38
GLint Specular
Specular color.
Definition: gl33device.h:313
virtual void DebugLights() OVERRIDE
Displays light positions to aid in debuggings.
Definition: gl33device.cpp:70
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:90
virtual void SetRenderState(RenderState state, bool enabled) OVERRIDE
Enables/disables the given render state.
Definition: gl33device.cpp:1723
virtual void SetLightEnabled(int index, bool enabled) OVERRIDE
Enables/disables the light at given index.
Definition: gl33device.cpp:559
virtual void SetTransform(TransformType type, const Math::Matrix &matrix) OVERRIDE
Sets the transform matrix of given type.
Definition: gl33device.cpp:440
Implementation of CDevice interface in OpenGL 3.3.
Definition: gl33device.h:50
GLint Attenuation
Attenuation.
Definition: gl33device.h:315
Abstract interface of graphics device.
Definition: device.h:251
GLint Enabled
true enables light
Definition: gl33device.h:303
virtual void DestroyTexture(const Texture &texture) OVERRIDE
Deletes a given texture, freeing it from video memory.
Definition: gl33device.cpp:814
GLint Type
Light type.
Definition: gl33device.h:305
RenderTarget
Render targets for rendering to textures.
Definition: device.h:233