Colobot
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-2015, 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 
29 #include "graphics/opengl/glframebuffer.h"
30 #include "graphics/opengl/glutil.h"
31 
32 #include <map>
33 #include <memory>
34 #include <set>
35 #include <string>
36 #include <vector>
37 
38 
39 // Graphics module namespace
40 namespace Gfx
41 {
42 
54 class CGL33Device : public CDevice
55 {
56 public:
57  CGL33Device(const DeviceConfig &config);
58  virtual ~CGL33Device();
59 
60  void DebugHook() override;
61  void DebugLights() override;
62 
63  bool Create() override;
64  void Destroy() override;
65 
66  void ConfigChanged(const DeviceConfig &newConfig) override;
67 
68  void BeginScene() override;
69  void EndScene() override;
70 
71  void Clear() override;
72 
73  void SetTransform(TransformType type, const Math::Matrix &matrix) override;
74 
75  void SetMaterial(const Material &material) override;
76 
77  int GetMaxLightCount() override;
78  void SetLight(int index, const Light &light) override;
79  void SetLightEnabled(int index, bool enabled) override;
80 
81  Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
82  Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
83  Texture CreateDepthTexture(int width, int height, int depth) override;
84  void DestroyTexture(const Texture &texture) override;
85  void DestroyAllTextures() override;
86 
87  int GetMaxTextureStageCount() override;
88  void SetTexture(int index, const Texture &texture) override;
89  void SetTexture(int index, unsigned int textureId) override;
90  void SetTextureEnabled(int index, bool enabled) override;
91 
92  void SetTextureStageParams(int index, const TextureStageParams &params) override;
93 
94  void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
95  void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override;
96 
97  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
98  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
99  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
100  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
101  void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) override;
102 
103  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
104  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
105  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
106  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
107  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
108  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
109  void DrawStaticBuffer(unsigned int bufferId) override;
110  void DestroyStaticBuffer(unsigned int bufferId) override;
111 
112  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
113 
114  void SetViewport(int x, int y, int width, int height) override;
115 
116  void SetRenderState(RenderState state, bool enabled) override;
117 
118  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
119 
120  void SetDepthTestFunc(CompFunc func) override;
121 
122  void SetDepthBias(float factor, float units) override;
123 
124  void SetAlphaTestFunc(CompFunc func, float refValue) override;
125 
126  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
127 
128  void SetClearColor(const Color &color) override;
129 
130  void SetGlobalAmbient(const Color &color) override;
131 
132  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
133 
134  void SetCullMode(CullMode mode) override;
135 
136  void SetShadeModel(ShadeModel model) override;
137 
138  void SetShadowColor(float value) override;
139 
140  void SetFillMode(FillMode mode) override;
141 
142  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
143 
144  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
145 
146  CFramebuffer* GetFramebuffer(std::string name) override;
147 
148  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
149 
150  void DeleteFramebuffer(std::string name) override;
151 
152  bool IsAnisotropySupported() override;
153  int GetMaxAnisotropyLevel() override;
154 
155  int GetMaxSamples() override;
156 
157  bool IsShadowMappingSupported() override;
158 
159  int GetMaxTextureSize() override;
160 
161  bool IsFramebufferSupported() override;
162 
163 private:
165  void UpdateLightPosition(int index);
167  void UpdateTextureParams(int index);
169  void UpdateRenderingMode();
170 
172  inline void BindVBO(GLuint vbo);
174  inline void BindVAO(GLuint vao);
175 
176 private:
178  DeviceConfig m_config;
179 
181  Math::Matrix m_worldMat;
183  Math::Matrix m_viewMat;
185  Math::Matrix m_modelviewMat;
187  Math::Matrix m_projectionMat;
188 
190  Material m_material;
191 
193  bool m_lighting = false;
195  std::vector<Light> m_lights;
197  std::vector<bool> m_lightsEnabled;
198 
200  std::vector<Texture> m_currentTextures;
202  std::vector<bool> m_texturesEnabled;
204  std::vector<TextureStageParams> m_textureStageParams;
205 
207  std::set<Texture> m_allTextures;
208 
210  enum VertexType
211  {
212  VERTEX_TYPE_NORMAL,
213  VERTEX_TYPE_TEX2,
214  VERTEX_TYPE_COL,
215  };
216 
218  struct VertexBufferInfo
219  {
220  PrimitiveType primitiveType = {};
221  GLuint vbo = 0;
222  GLuint vao = 0;
223  VertexType vertexType = {};
224  int vertexCount = 0;
225  unsigned int size = 0;
226  };
227 
230  int m_glMajor = 1, m_glMinor = 1;
232  bool m_anisotropyAvailable = false;
234  int m_maxAnisotropy = 1;
236  int m_maxSamples = 1;
238  std::map<unsigned int, VertexBufferInfo> m_vboObjects;
240  unsigned int m_lastVboId = 0;
242  GLuint m_currentVBO = 0;
244  GLuint m_currentVAO = 0;
245 
247  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
248 
250  GLuint m_shaderProgram = 0;
252  bool m_perPixelLighting = false;
253 
255  unsigned int m_vertex = 0;
256  unsigned int m_vertexTex2 = 0;
257  unsigned int m_vertexCol = 0;
258 
259  // Uniforms
261  GLint uni_ProjectionMatrix = 0;
263  GLint uni_ViewMatrix = 0;
265  GLint uni_ModelMatrix = 0;
267  GLint uni_ShadowMatrix = 0;
269  GLint uni_NormalMatrix = 0;
270 
272  GLint uni_PrimaryTexture = 0;
274  GLint uni_SecondaryTexture = 0;
276  GLint uni_ShadowTexture = 0;
277 
278  GLint uni_PrimaryTextureEnabled = 0;
279  GLint uni_SecondaryTextureEnabled = 0;
280  GLint uni_ShadowTextureEnabled = 0;
281 
282  // Fog parameters
284  GLint uni_FogEnabled = 0;
286  GLint uni_FogRange = 0;
288  GLint uni_FogColor = 0;
289 
290  // Alpha test parameters
292  GLint uni_AlphaTestEnabled = 0;
294  GLint uni_AlphaReference = 0;
295 
297  GLint uni_ShadowColor = 0;
298 
299  // Lighting parameters
300  GLint uni_SmoothShading = 0;
302  GLint uni_LightingEnabled = 0;
304  GLint uni_AmbientColor = 0;
306  GLint uni_DiffuseColor = 0;
308  GLint uni_SpecularColor = 0;
309 
310  struct LightUniforms
311  {
313  GLint Enabled = 0;
315  GLint Type = 0;
317  GLint Position = 0;
319  GLint Ambient = 0;
321  GLint Diffuse = 0;
323  GLint Specular = 0;
325  GLint Attenuation = 0;
326  };
327 
328  LightUniforms uni_Light[8];
329 };
330 
331 } // namespace Gfx
void DeleteFramebuffer(std::string name) override
Deletes framebuffer.
Definition: gl33device.cpp:1912
void DrawStaticBuffer(unsigned int bufferId) override
Draws a static buffer.
Definition: gl33device.cpp:1618
void DestroyAllTextures() override
Deletes all textures created so far.
Definition: gl33device.cpp:843
void BeginScene() override
Begins drawing the 3D scene.
Definition: gl33device.cpp:428
bool IsShadowMappingSupported() override
Checks if shadow mapping is supported.
Definition: gl33device.cpp:1968
int ComputeSphereVisibility(const Math::Vector &center, float radius) override
Definition: gl33device.cpp:1658
void SetFillMode(FillMode mode) override
Sets the current fill mode.
Definition: gl33device.cpp:1861
void Destroy() override
Destroys the device, releasing every acquired resource.
Definition: gl33device.cpp:394
Vertex of a primitive.
Definition: vertex.h:52
std::unique_ptr< CFrameBufferPixels > GetFrameBufferPixels() const override
Returns the pixels of the entire screen.
Definition: gl33device.cpp:1882
Vertex with secondary texture coordinates.
Definition: vertex.h:113
void ConfigChanged(const DeviceConfig &newConfig) override
Changes configuration.
Definition: gl33device.cpp:418
FogMode
Type of fog calculation function.
Definition: device.h:163
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override
Definition: gl33device.cpp:581
void SetLight(int index, const Light &light) override
Sets the light at given index.
Definition: gl33device.cpp:500
void SetColorMask(bool red, bool green, bool blue, bool alpha) override
Sets the color mask.
Definition: gl33device.cpp:1787
4x4 matrix
Definition: matrix.h:65
void SetDepthBias(float factor, float units) override
Sets the depth bias (constant value added to Z-coords)
Definition: gl33device.cpp:1797
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:99
int GetMaxLightCount() override
Returns the maximum number of lights available.
Definition: gl33device.cpp:495
FillMode
Polygon fill mode.
Definition: device.h:196
int GetMaxTextureSize() override
Returns max texture size supported.
Definition: gl33device.cpp:1973
void SetTexture(int index, const Texture &texture) override
Definition: gl33device.cpp:864
void SetClearColor(const Color &color) override
Sets the clear color.
Definition: gl33device.cpp:1812
void DebugHook() override
Provides a hook to debug graphics code (implementation-specific)
Definition: gl33device.cpp:55
void SetDepthTestFunc(CompFunc func) override
Sets the function of depth test.
Definition: gl33device.cpp:1792
void SetTextureEnabled(int index, bool enabled) override
Enables/disables the given texture stage.
Definition: gl33device.cpp:898
CompFunc
Type of function used to compare values.
Definition: device.h:128
void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override
Sets the blending functions for source and destination operations.
Definition: gl33device.cpp:1807
CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params) override
Creates new framebuffer with given name or nullptr if it's not possible.
Definition: gl33device.cpp:1896
Parameters for a texture unit.
Definition: texture.h:180
Material of a surface.
Definition: material.h:45
void DebugLights() override
Displays light positions to aid in debuggings.
Definition: gl33device.cpp:62
bool IsAnisotropySupported() override
Checks if anisotropy is supported.
Definition: gl33device.cpp:1953
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:1138
bool IsFramebufferSupported() override
Checks if framebuffers are supported.
Definition: gl33device.cpp:1980
Parameters for texture coordinate generation.
Definition: texture.h:234
void SetTransform(TransformType type, const Math::Matrix &matrix) override
Sets the transform matrix of given type.
Definition: gl33device.cpp:447
General config for graphics device.
Definition: device.h:55
Properties of light in 3D scene.
Definition: light.h:54
void SetLightEnabled(int index, bool enabled) override
Enables/disables the light at given index.
Definition: gl33device.cpp:568
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:1447
Parameters for texture creation.
Definition: texture.h:155
ShadeModel
Shade model used in rendering.
Definition: device.h:186
BlendFunc
Type of blending function.
Definition: device.h:144
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:1822
int GetMaxSamples() override
Returns max samples supported.
Definition: gl33device.cpp:1963
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:1314
bool Create() override
Initializes the device, setting the initial state.
Definition: gl33device.cpp:163
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:1169
void SetGlobalAmbient(const Color &color) override
Sets the global ambient color.
Definition: gl33device.cpp:1817
int GetMaxTextureStageCount() override
Returns the maximum number of multitexture stages.
Definition: gl33device.cpp:855
Image loaded from file.
Definition: image.h:54
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override
Sets the texture coordinate generation mode for given texture unit.
Definition: gl33device.cpp:926
PrimitiveType
Type of primitive to render.
Definition: device.h:210
void DestroyStaticBuffer(unsigned int bufferId) override
Deletes a static buffer.
Definition: gl33device.cpp:1634
CullMode
Culling mode for polygons.
Definition: device.h:174
Colored vertex.
Definition: vertex.h:84
Namespace for (new) graphics code.
Definition: app.h:49
void SetMaterial(const Material &material) override
Sets the current material.
Definition: gl33device.cpp:486
Implementation-specific image data.
Definition: image.h:41
void EndScene() override
Ends drawing the 3D scene.
Definition: gl33device.cpp:437
RenderState
Render states that can be enabled/disabled.
Definition: device.h:112
void SetTextureStageParams(int index, const TextureStageParams &params) override
Definition: gl33device.cpp:916
void SetRenderState(RenderState state, bool enabled) override
Enables/disables the given render state.
Definition: gl33device.cpp:1742
void SetCullMode(CullMode mode) override
Sets the current cull mode.
Definition: gl33device.cpp:1842
Info about a texture.
Definition: texture.h:256
Texture CreateDepthTexture(int width, int height, int depth) override
Creates a depth texture with specific dimensions and depth.
Definition: gl33device.cpp:781
void SetViewport(int x, int y, int width, int height) override
Changes rendering viewport.
Definition: gl33device.cpp:1737
3D (3x1) vector
Definition: vector.h:53
Abstract graphics device - CDevice class and related structs/enums.
void SetAlphaTestFunc(CompFunc func, float refValue) override
Sets the alpha test function and reference value.
Definition: gl33device.cpp:1802
RGBA color.
Definition: color.h:39
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:100
void SetShadeModel(ShadeModel model) override
Sets the shade model.
Definition: gl33device.cpp:1851
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:1869
void DestroyTexture(const Texture &texture) override
Deletes a given texture, freeing it from video memory.
Definition: gl33device.cpp:826
void SetShadowColor(float value) override
Sets shadow color.
Definition: gl33device.cpp:1856
Implementation of CDevice interface in OpenGL 3.3.
Definition: gl33device.h:54
Abstract interface of graphics device.
Definition: device.h:268
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67
void Clear() override
Clears the screen to blank.
Definition: gl33device.cpp:441
int GetMaxAnisotropyLevel() override
Returns max anisotropy level supported.
Definition: gl33device.cpp:1958
CFramebuffer * GetFramebuffer(std::string name) override
Returns framebuffer with given name or nullptr if it doesn't exist.
Definition: gl33device.cpp:1887