Colobot
gldevice.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 <string>
33 #include <vector>
34 #include <set>
35 #include <map>
36 
37 
38 // Graphics module namespace
39 namespace Gfx
40 {
41 
47 {
48  VBT_DISPLAY_LIST,
51 };
52 
54 {
55  SMS_NONE,
58 };
59 
60 struct GLDevicePrivate;
61 
73 class CGLDevice : public CDevice
74 {
75 public:
76  CGLDevice(const DeviceConfig &config);
77  virtual ~CGLDevice();
78 
79  void DebugHook() override;
80  void DebugLights() override;
81 
82  bool Create() override;
83  void Destroy() override;
84 
85  void ConfigChanged(const DeviceConfig &newConfig) override;
86 
87  void BeginScene() override;
88  void EndScene() override;
89 
90  void Clear() override;
91 
92  void SetTransform(TransformType type, const Math::Matrix &matrix) override;
93 
94  void SetMaterial(const Material &material) override;
95 
96  int GetMaxLightCount() override;
97  void SetLight(int index, const Light &light) override;
98  void SetLightEnabled(int index, bool enabled) override;
99 
100  Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
101  Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
102  Texture CreateDepthTexture(int width, int height, int depth) override;
103  void DestroyTexture(const Texture &texture) override;
104  void DestroyAllTextures() override;
105 
106  int GetMaxTextureStageCount() override;
107  void SetTexture(int index, const Texture &texture) override;
108  void SetTexture(int index, unsigned int textureId) override;
109  void SetTextureEnabled(int index, bool enabled) override;
110 
111  void SetTextureStageParams(int index, const TextureStageParams &params) override;
112 
113  void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
114  void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override;
115 
116  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
117  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
118  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
119  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
120  void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) override;
121 
122  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
123  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
124  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
125  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
126  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
127  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
128  void DrawStaticBuffer(unsigned int bufferId) override;
129  void DestroyStaticBuffer(unsigned int bufferId) override;
130 
131  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
132 
133  void SetViewport(int x, int y, int width, int height) override;
134 
135  void SetRenderState(RenderState state, bool enabled) override;
136 
137  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
138 
139  void SetDepthTestFunc(CompFunc func) override;
140 
141  void SetDepthBias(float factor, float units) override;
142 
143  void SetAlphaTestFunc(CompFunc func, float refValue) override;
144 
145  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
146 
147  void SetClearColor(const Color &color) override;
148 
149  void SetGlobalAmbient(const Color &color) override;
150 
151  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
152 
153  void SetCullMode(CullMode mode) override;
154 
155  void SetShadeModel(ShadeModel model) override;
156 
157  void SetShadowColor(float value) override;
158 
159  void SetFillMode(FillMode mode) override;
160 
161  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
162 
163  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
164 
165  CFramebuffer* GetFramebuffer(std::string name) override;
166 
167  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
168 
169  void DeleteFramebuffer(std::string name) override;
170 
171  bool IsAnisotropySupported() override;
172  int GetMaxAnisotropyLevel() override;
173 
174  int GetMaxSamples() override;
175 
176  bool IsShadowMappingSupported() override;
177 
178  int GetMaxTextureSize() override;
179 
180  bool IsFramebufferSupported() override;
181 
182 private:
184  void UpdateModelviewMatrix();
186  void UpdateLightPosition(int index);
188  void UpdateTextureParams(int index);
189 
190 private:
192  DeviceConfig m_config;
193 
195  Math::Matrix m_worldMat;
197  Math::Matrix m_viewMat;
199  Math::Matrix m_modelviewMat;
201  Math::Matrix m_projectionMat;
202 
204  Material m_material;
205 
207  bool m_lighting = false;
209  std::vector<Light> m_lights;
211  std::vector<bool> m_lightsEnabled;
212 
214  std::vector<Texture> m_currentTextures;
216  std::vector<bool> m_texturesEnabled;
218  std::vector<TextureStageParams> m_textureStageParams;
219 
221  std::set<Texture> m_allTextures;
222 
224  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
225 
227  enum VertexType
228  {
229  VERTEX_TYPE_NORMAL,
230  VERTEX_TYPE_TEX2,
231  VERTEX_TYPE_COL,
232  };
233 
235  struct VboObjectInfo
236  {
237  PrimitiveType primitiveType = {};
238  unsigned int bufferId = 0;
239  VertexType vertexType = {};
240  int vertexCount = 0;
241  };
242 
245  int m_glMajor = 1, m_glMinor = 1;
247  ShadowMappingSupport m_shadowMappingSupport = SMS_NONE;
249  bool m_shadowAmbientSupported = false;
251  bool m_multitextureAvailable = false;
253  bool m_vboAvailable = false;
255  bool m_anisotropyAvailable = false;
257  int m_maxAnisotropy = 1;
259  int m_maxSamples = 1;
261  FramebufferSupport m_framebufferSupport = FBS_NONE;
263  VertexBufferType m_vertexBufferType = VBT_DISPLAY_LIST;
265  std::map<unsigned int, VboObjectInfo> m_vboObjects;
267  unsigned int m_lastVboId = 0;
268 };
269 
270 
271 } // namespace Gfx
void SetDepthTestFunc(CompFunc func) override
Sets the function of depth test.
Definition: gldevice.cpp:1748
void SetViewport(int x, int y, int width, int height) override
Changes rendering viewport.
Definition: gldevice.cpp:1694
bool IsShadowMappingSupported() override
Checks if shadow mapping is supported.
Definition: gldevice.cpp:1901
int GetMaxTextureSize() override
Returns max texture size supported.
Definition: gldevice.cpp:1906
void SetTextureEnabled(int index, bool enabled) override
Enables/disables the given texture stage.
Definition: gldevice.cpp:885
int GetMaxAnisotropyLevel() override
Returns max anisotropy level supported.
Definition: gldevice.cpp:1891
CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params) override
Creates new framebuffer with given name or nullptr if it's not possible.
Definition: gldevice.cpp:1849
void DestroyStaticBuffer(unsigned int bufferId) override
Deletes a static buffer.
Definition: gldevice.cpp:1592
void SetColorMask(bool red, bool green, bool blue, bool alpha) override
Sets the color mask.
Definition: gldevice.cpp:1743
Vertex of a primitive.
Definition: vertex.h:52
Vertex with secondary texture coordinates.
Definition: vertex.h:113
ARB extension.
Definition: gldevice.h:57
FogMode
Type of fog calculation function.
Definition: device.h:163
void EndScene() override
Ends drawing the 3D scene.
Definition: gldevice.cpp:354
4x4 matrix
Definition: matrix.h:65
void DestroyTexture(const Texture &texture) override
Deletes a given texture, freeing it from video memory.
Definition: gldevice.cpp:803
void DeleteFramebuffer(std::string name) override
Deletes framebuffer.
Definition: gldevice.cpp:1873
void SetCullMode(CullMode mode) override
Sets the current cull mode.
Definition: gldevice.cpp:1791
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:99
CFramebuffer * GetFramebuffer(std::string name) override
Returns framebuffer with given name or nullptr if it doesn't exist.
Definition: gldevice.cpp:1840
void SetTransform(TransformType type, const Math::Matrix &matrix) override
Sets the transform matrix of given type.
Definition: gldevice.cpp:364
void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override
Sets the texture coordinate generation mode for given texture unit.
Definition: gldevice.cpp:922
void CopyFramebufferToTexture(Texture &texture, int xOffset, int yOffset, int x, int y, int width, int height) override
Copies content of framebuffer to texture.
Definition: gldevice.cpp:1820
void SetShadeModel(ShadeModel model) override
Sets the shade model.
Definition: gldevice.cpp:1800
FillMode
Polygon fill mode.
Definition: device.h:196
void SetTextureStageParams(int index, const TextureStageParams &params) override
Definition: gldevice.cpp:912
ShadowMappingSupport
Definition: gldevice.h:53
Implementation of CDevice interface in OpenGL.
Definition: gldevice.h:73
void SetRenderState(RenderState state, bool enabled) override
Enables/disables the given render state.
Definition: gldevice.cpp:1699
use display lists
Definition: gldevice.h:49
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override
Definition: gldevice.cpp:514
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: gldevice.cpp:1763
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex *vertices, int vertexCount) override
Creates a static buffer composed of given primitives with single texture vertices.
Definition: gldevice.cpp:1254
Parameters for a texture unit.
Definition: texture.h:180
Material of a surface.
Definition: material.h:45
void SetMaterial(const Material &material) override
Sets the current material.
Definition: gldevice.cpp:414
void DebugHook() override
Provides a hook to debug graphics code (implementation-specific)
Definition: gldevice.cpp:52
Parameters for texture coordinate generation.
Definition: texture.h:234
No support for depth textures.
Definition: gldevice.h:56
void SetShadowColor(float value) override
Sets shadow color.
Definition: gldevice.cpp:1807
VertexBufferType
Specifies type of vertex buffer to use.
Definition: gldevice.h:46
General config for graphics device.
Definition: device.h:55
Properties of light in 3D scene.
Definition: light.h:54
void SetGlobalAmbient(const Color &color) override
Sets the global ambient color.
Definition: gldevice.cpp:1773
void SetLightEnabled(int index, bool enabled) override
Enables/disables the light at given index.
Definition: gldevice.cpp:498
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: gldevice.cpp:1778
void SetDepthBias(float factor, float units) override
Sets the depth bias (constant value added to Z-coords)
Definition: gldevice.cpp:1753
bool IsFramebufferSupported() override
Checks if framebuffers are supported.
Definition: gldevice.cpp:1913
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
std::unique_ptr< CFrameBufferPixels > GetFrameBufferPixels() const override
Returns the pixels of the entire screen.
Definition: gldevice.cpp:1835
bool IsAnisotropySupported() override
Checks if anisotropy is supported.
Definition: gldevice.cpp:1886
Image loaded from file.
Definition: image.h:54
int GetMaxLightCount() override
Returns the maximum number of lights available.
Definition: gldevice.cpp:423
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
void DebugLights() override
Displays light positions to aid in debuggings.
Definition: gldevice.cpp:59
PrimitiveType
Type of primitive to render.
Definition: device.h:210
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: gldevice.cpp:1174
void SetFillMode(FillMode mode) override
Sets the current fill mode.
Definition: gldevice.cpp:1812
CullMode
Culling mode for polygons.
Definition: device.h:174
use core OpenGL 1.5 VBOs
Definition: gldevice.h:50
Colored vertex.
Definition: vertex.h:84
Namespace for (new) graphics code.
Definition: app.h:49
void SetClearColor(const Color &color) override
Sets the clear color.
Definition: gldevice.cpp:1768
int ComputeSphereVisibility(const Math::Vector &center, float radius) override
Definition: gldevice.cpp:1615
Implementation-specific image data.
Definition: image.h:41
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: gldevice.cpp:1386
RenderState
Render states that can be enabled/disabled.
Definition: device.h:112
void Clear() override
Clears the screen to blank.
Definition: gldevice.cpp:358
bool Create() override
Initializes the device, setting the initial state.
Definition: gldevice.cpp:159
Info about a texture.
Definition: texture.h:256
void SetLight(int index, const Light &light) override
Sets the light at given index.
Definition: gldevice.cpp:428
3D (3x1) vector
Definition: vector.h:53
void ConfigChanged(const DeviceConfig &newConfig) override
Changes configuration.
Definition: gldevice.cpp:334
void BeginScene() override
Begins drawing the 3D scene.
Definition: gldevice.cpp:344
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: gldevice.cpp:1758
RGBA color.
Definition: color.h:39
void SetTexture(int index, const Texture &texture) override
Definition: gldevice.cpp:841
int GetMaxTextureStageCount() override
Returns the maximum number of multitexture stages.
Definition: gldevice.cpp:832
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:100
void Destroy() override
Destroys the device, releasing every acquired resource.
Definition: gldevice.cpp:314
void DrawStaticBuffer(unsigned int bufferId) override
Draws a static buffer.
Definition: gldevice.cpp:1494
void DestroyAllTextures() override
Deletes all textures created so far.
Definition: gldevice.cpp:820
int GetMaxSamples() override
Returns max samples supported.
Definition: gldevice.cpp:1896
void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override
Sets only the texture wrap modes (for faster than thru stage params)
Definition: gldevice.cpp:1139
Texture CreateDepthTexture(int width, int height, int depth) override
Creates a depth texture with specific dimensions and depth.
Definition: gldevice.cpp:723
Abstract interface of graphics device.
Definition: device.h:268
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67