Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gl21device.h
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://epsitec.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 
39 struct GLDevicePrivate;
40 
52 class CGL21Device : public CDevice
53 {
54 public:
55  CGL21Device(const GLDeviceConfig &config);
56  virtual ~CGL21Device();
57 
58  virtual void DebugHook() OVERRIDE;
59  virtual void DebugLights() OVERRIDE;
60 
61  virtual bool Create() OVERRIDE;
62  virtual void Destroy() OVERRIDE;
63 
64  void ConfigChanged(const GLDeviceConfig &newConfig);
65 
66  virtual void BeginScene() OVERRIDE;
67  virtual void EndScene() OVERRIDE;
68 
69  virtual void Clear() OVERRIDE;
70 
71  virtual void SetTransform(TransformType type, const Math::Matrix &matrix) OVERRIDE;
72 
73  virtual void SetMaterial(const Material &material) OVERRIDE;
74 
75  virtual int GetMaxLightCount() OVERRIDE;
76  virtual void SetLight(int index, const Light &light) OVERRIDE;
77  virtual void SetLightEnabled(int index, bool enabled) OVERRIDE;
78 
79  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) OVERRIDE;
80  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) OVERRIDE;
81  virtual Texture CreateDepthTexture(int width, int height, int depth) OVERRIDE;
82  virtual void DestroyTexture(const Texture &texture) OVERRIDE;
83  virtual void DestroyAllTextures() OVERRIDE;
84 
85  virtual int GetMaxTextureStageCount() OVERRIDE;
86  virtual void SetTexture(int index, const Texture &texture) OVERRIDE;
87  virtual void SetTexture(int index, unsigned int textureId) OVERRIDE;
88  virtual void SetTextureEnabled(int index, bool enabled) OVERRIDE;
89 
90  virtual void SetTextureStageParams(int index, const TextureStageParams &params) OVERRIDE;
91 
92  virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) OVERRIDE;
93  virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE;
94 
95  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
96  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
97  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
98  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
99  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) OVERRIDE;
100 
101  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) OVERRIDE;
102  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) OVERRIDE;
103  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) OVERRIDE;
104  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) OVERRIDE;
105  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) OVERRIDE;
106  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) OVERRIDE;
107  virtual void DrawStaticBuffer(unsigned int bufferId) OVERRIDE;
108  virtual void DestroyStaticBuffer(unsigned int bufferId) OVERRIDE;
109 
110  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) OVERRIDE;
111 
112  virtual void SetViewport(int x, int y, int width, int height) OVERRIDE;
113 
114  virtual void SetRenderState(RenderState state, bool enabled) OVERRIDE;
115 
116  virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) OVERRIDE;
117 
118  virtual void SetDepthTestFunc(CompFunc func) OVERRIDE;
119 
120  virtual void SetDepthBias(float factor, float units) OVERRIDE;
121 
122  virtual void SetAlphaTestFunc(CompFunc func, float refValue) OVERRIDE;
123 
124  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) OVERRIDE;
125 
126  virtual void SetClearColor(const Color &color) OVERRIDE;
127 
128  virtual void SetGlobalAmbient(const Color &color) OVERRIDE;
129 
130  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) OVERRIDE;
131 
132  virtual void SetCullMode(CullMode mode) OVERRIDE;
133 
134  virtual void SetShadeModel(ShadeModel model) OVERRIDE;
135 
136  virtual void SetShadowColor(float value) OVERRIDE;
137 
138  virtual void SetFillMode(FillMode mode) OVERRIDE;
139 
140  virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
141 
142  virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE;
143 
144  virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE;
145 
146  virtual void* GetFrameBufferPixels() const OVERRIDE;
147 
148 private:
150  void UpdateLightPosition(int index);
152  void UpdateTextureParams(int index);
154  void UpdateTextureStatus();
156  inline void BindVBO(GLuint vbo);
157 
158 private:
160  GLDeviceConfig m_config;
161 
163  Math::Matrix m_worldMat;
165  Math::Matrix m_viewMat;
167  Math::Matrix m_modelviewMat;
169  Math::Matrix m_projectionMat;
170 
172  Material m_material;
173 
175  bool m_lighting;
177  std::vector<Light> m_lights;
179  std::vector<bool> m_lightsEnabled;
180 
182  std::vector<Texture> m_currentTextures;
184  std::vector<bool> m_texturesEnabled;
186  std::vector<TextureStageParams> m_textureStageParams;
187 
189  std::set<Texture> m_allTextures;
190 
192  enum VertexType
193  {
194  VERTEX_TYPE_NORMAL,
195  VERTEX_TYPE_TEX2,
196  VERTEX_TYPE_COL,
197  };
198 
200  struct VboObjectInfo
201  {
202  PrimitiveType primitiveType;
203  unsigned int bufferId;
204  VertexType vertexType;
205  int vertexCount;
206  int size;
207  };
208 
211  int m_glMajor, m_glMinor;
213  bool m_anisotropyAvailable;
215  int m_maxAnisotropy;
217  bool m_framebufferObject;
219  std::map<unsigned int, VboObjectInfo> m_vboObjects;
221  unsigned int m_lastVboId;
223  GLuint m_currentVBO;
224 
225  // Offscreen buffer
227  GLuint m_framebuffer;
229  GLuint m_colorBuffer;
231  GLuint m_depthBuffer;
233  int m_maxRenderbufferSize;
235  bool m_offscreenRenderingEnabled;
236 
238  bool m_perPixelLighting;
239 
241  GLuint m_program;
242 
243  // Uniforms
245  GLint uni_ProjectionMatrix;
247  GLint uni_ViewMatrix;
249  GLint uni_ModelMatrix;
251  GLint uni_ShadowMatrix;
253  GLint uni_NormalMatrix;
254 
256  GLint uni_PrimaryTexture;
258  GLint uni_SecondaryTexture;
260  GLint uni_ShadowTexture;
261 
263  GLint uni_TextureEnabled[3];
264 
265  // Alpha test parameters
267  GLint uni_AlphaTestEnabled;
269  GLint uni_AlphaReference;
270 
272  GLint uni_FogEnabled;
274  GLint uni_FogRange;
276  GLint uni_FogColor;
277 
279  GLint uni_ShadowColor;
280 
282  GLint uni_LightingEnabled;
284  GLint uni_LightEnabled[8];
285 };
286 
287 
288 } // namespace Gfx
virtual int GetMaxLightCount() OVERRIDE
Returns the maximum number of lights available.
Definition: gl21device.cpp:455
Additional config with OpenGL-specific settings.
Definition: glutil.h:48
Implementation of CDevice interface in OpenGL.
Definition: gl21device.h:52
virtual void BeginScene() OVERRIDE
Begins drawing the 3D scene.
Definition: gl21device.cpp:391
virtual void SetMaterial(const Material &material) OVERRIDE
Sets the current material.
Definition: gl21device.cpp:446
virtual void SetGlobalAmbient(const Color &color) OVERRIDE
Sets the global ambient color.
Definition: gl21device.cpp:1644
Vertex of a primitive.
Definition: vertex.h:51
virtual void DrawStaticBuffer(unsigned int bufferId) OVERRIDE
Draws a static buffer.
Definition: gl21device.cpp:1369
virtual void SetFillMode(FillMode mode) OVERRIDE
Sets the current fill mode.
Definition: gl21device.cpp:1688
Vertex with secondary texture coordinates.
Definition: vertex.h:112
virtual void SetTextureEnabled(int index, bool enabled) OVERRIDE
Enables/disables the given texture stage.
Definition: gl21device.cpp:860
virtual void DebugHook() OVERRIDE
Provides a hook to debug graphics code (implementation-specific)
Definition: gl21device.cpp:64
FogMode
Type of fog calculation function.
Definition: device.h:154
virtual void SetDepthTestFunc(CompFunc func) OVERRIDE
Sets the function of depth test.
Definition: gl21device.cpp:1619
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: gl21device.cpp:1121
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: gl21device.cpp:1152
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE
Sets the texture coordinate generation mode for given texture unit.
Definition: gl21device.cpp:914
4x4 matrix
Definition: matrix.h:66
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:98
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) OVERRIDE
Sets the color mask.
Definition: gl21device.cpp:1614
virtual void SetTexture(int index, const Texture &texture) OVERRIDE
Definition: gl21device.cpp:820
virtual void SetTextureStageParams(int index, const TextureStageParams &params) OVERRIDE
Definition: gl21device.cpp:904
FillMode
Polygon fill mode.
Definition: device.h:187
CompFunc
Type of function used to compare values.
Definition: device.h:119
virtual void Clear() OVERRIDE
Clears the screen to blank.
Definition: gl21device.cpp:400
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) OVERRIDE
Definition: gl21device.cpp:1459
Parameters for a texture unit.
Definition: texture.h:187
Material of a surface.
Definition: material.h:44
virtual void SetClearColor(const Color &color) OVERRIDE
Sets the clear color.
Definition: gl21device.cpp:1639
virtual void SetRenderTexture(RenderTarget target, int texture) OVERRIDE
Sets render target to texture.
Definition: gl21device.cpp:1732
virtual void SetRenderState(RenderState state, bool enabled) OVERRIDE
Enables/disables the given render state.
Definition: gl21device.cpp:1543
virtual void SetShadowColor(float value) OVERRIDE
Sets shadow color.
Definition: gl21device.cpp:1683
virtual void DestroyStaticBuffer(unsigned int bufferId) OVERRIDE
Deletes a static buffer.
Definition: gl21device.cpp:1443
virtual bool Create() OVERRIDE
Initializes the device, setting the initial state.
Definition: gl21device.cpp:172
Parameters for texture coordinate generation.
Definition: texture.h:253
virtual Texture CreateDepthTexture(int width, int height, int depth) OVERRIDE
Creates a depth texture with specific dimensions and depth.
Definition: gl21device.cpp:737
Properties of light in 3D scene.
Definition: light.h:53
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: gl21device.cpp:1231
virtual void DebugLights() OVERRIDE
Displays light positions to aid in debuggings.
Definition: gl21device.cpp:71
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: gl21device.cpp:1649
virtual void EndScene() OVERRIDE
Ends drawing the 3D scene.
Definition: gl21device.cpp:396
virtual void Destroy() OVERRIDE
Destroys the device, releasing every acquired resource.
Definition: gl21device.cpp:364
Parameters for texture creation.
Definition: texture.h:154
ShadeModel
Shade model used in rendering.
Definition: device.h:177
BlendFunc
Type of blending function.
Definition: device.h:135
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) OVERRIDE
Definition: gl21device.cpp:534
virtual void SetLightEnabled(int index, bool enabled) OVERRIDE
Enables/disables the light at given index.
Definition: gl21device.cpp:521
virtual int GetMaxTextureStageCount() OVERRIDE
Returns the maximum number of multitexture stages.
Definition: gl21device.cpp:811
Image loaded from file.
Definition: image.h:57
PrimitiveType
Type of primitive to render.
Definition: device.h:201
virtual void SetCullMode(CullMode mode) OVERRIDE
Sets the current cull mode.
Definition: gl21device.cpp:1667
CullMode
Culling mode for polygons.
Definition: device.h:165
Colored vertex.
Definition: vertex.h:83
virtual void SetTransform(TransformType type, const Math::Matrix &matrix) OVERRIDE
Sets the transform matrix of given type.
Definition: gl21device.cpp:406
Namespace for (new) graphics code.
Definition: app.h:49
Implementation-specific image data.
Definition: image.h:42
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: gl21device.cpp:1291
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) OVERRIDE
Sets the blending functions for source and destination operations.
Definition: gl21device.cpp:1634
RenderState
Render states that can be enabled/disabled.
Definition: device.h:102
Info about a texture.
Definition: texture.h:282
virtual void SetViewport(int x, int y, int width, int height) OVERRIDE
Changes rendering viewport.
Definition: gl21device.cpp:1538
3D (3x1) vector
Definition: vector.h:52
Abstract graphics device - CDevice class and related structs/enums.
virtual void * GetFrameBufferPixels() const OVERRIDE
Returns the pixels of the entire screen.
Definition: gl21device.cpp:1783
virtual void DestroyTexture(const Texture &texture) OVERRIDE
Deletes a given texture, freeing it from video memory.
Definition: gl21device.cpp:783
RGBA color.
Definition: color.h:38
virtual void SetAlphaTestFunc(CompFunc func, float refValue) OVERRIDE
Sets the alpha test function and reference value.
Definition: gl21device.cpp:1629
virtual void SetDepthBias(float factor, float units) OVERRIDE
Sets the depth bias (constant value added to Z-coords)
Definition: gl21device.cpp:1624
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:90
virtual void DestroyAllTextures() OVERRIDE
Deletes all textures created so far.
Definition: gl21device.cpp:799
virtual void SetShadeModel(ShadeModel model) OVERRIDE
Sets the shade model.
Definition: gl21device.cpp:1676
virtual void SetLight(int index, const Light &light) OVERRIDE
Sets the light at given index.
Definition: gl21device.cpp:460
Abstract interface of graphics device.
Definition: device.h:251
virtual void InitOffscreenBuffer(int width, int height) OVERRIDE
Initializes offscreen buffer.
Definition: gl21device.cpp:1696
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: gl21device.cpp:1769
RenderTarget
Render targets for rendering to textures.
Definition: device.h:233