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-2016, 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/core/material.h"
30 
31 #include "graphics/opengl/glframebuffer.h"
32 #include "graphics/opengl/glutil.h"
33 
34 #include "math/matrix.h"
35 
36 #include <string>
37 #include <vector>
38 #include <set>
39 #include <map>
40 
41 
42 // Graphics module namespace
43 namespace Gfx
44 {
45 
51 {
52  VBT_DISPLAY_LIST,
55 };
56 
58 {
59  SMS_NONE,
62 };
63 
75 class CGLDevice : public CDevice
76 {
77 public:
78  CGLDevice(const DeviceConfig &config);
79  virtual ~CGLDevice();
80 
81  void DebugHook() override;
82  void DebugLights() override;
83 
84  std::string GetName() override;
85 
86  bool Create() override;
87  void Destroy() override;
88 
89  void ConfigChanged(const DeviceConfig &newConfig) override;
90 
91  void BeginScene() override;
92  void EndScene() override;
93 
94  void Clear() override;
95 
96  void SetRenderMode(RenderMode mode) override;
97 
98  void SetTransform(TransformType type, const Math::Matrix &matrix) override;
99 
100  void SetMaterial(const Material &material) override;
101 
102  int GetMaxLightCount() override;
103  void SetLight(int index, const Light &light) override;
104  void SetLightEnabled(int index, bool enabled) override;
105 
106  Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
107  Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
108  Texture CreateDepthTexture(int width, int height, int depth) override;
109  void UpdateTexture(const Texture& texture, Math::IntPoint offset, ImageData* data, TexImgFormat format) override;
110  void DestroyTexture(const Texture &texture) override;
111  void DestroyAllTextures() override;
112 
113  int GetMaxTextureStageCount() override;
114  void SetTexture(int index, const Texture &texture) override;
115  void SetTexture(int index, unsigned int textureId) override;
116  void SetTextureEnabled(int index, bool enabled) override;
117 
118  void SetTextureStageParams(int index, const TextureStageParams &params) override;
119 
120  void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
121  void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override;
122 
123  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
124  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
125  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
126  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
127  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount) override;
128 
129  virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices,
130  int first[], int count[], int drawCount,
131  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
132  virtual void DrawPrimitives(PrimitiveType type, const VertexTex2 *vertices,
133  int first[], int count[], int drawCount,
134  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
135  virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
136  int first[], int count[], int drawCount) override;
137 
138  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
139  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
140  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
141  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
142  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
143  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
144  void DrawStaticBuffer(unsigned int bufferId) override;
145  void DestroyStaticBuffer(unsigned int bufferId) override;
146 
147  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
148 
149  void SetViewport(int x, int y, int width, int height) override;
150 
151  void SetRenderState(RenderState state, bool enabled) override;
152 
153  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
154 
155  void SetDepthTestFunc(CompFunc func) override;
156 
157  void SetDepthBias(float factor, float units) override;
158 
159  void SetAlphaTestFunc(CompFunc func, float refValue) override;
160 
161  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
162 
163  void SetClearColor(const Color &color) override;
164 
165  void SetGlobalAmbient(const Color &color) override;
166 
167  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
168 
169  void SetCullMode(CullMode mode) override;
170 
171  void SetShadeModel(ShadeModel model) override;
172 
173  void SetShadowColor(float value) override;
174 
175  void SetFillMode(FillMode mode) override;
176 
177  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
178 
179  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
180 
181  CFramebuffer* GetFramebuffer(std::string name) override;
182 
183  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
184 
185  void DeleteFramebuffer(std::string name) override;
186 
187  bool IsAnisotropySupported() override;
188  int GetMaxAnisotropyLevel() override;
189 
190  int GetMaxSamples() override;
191 
192  bool IsShadowMappingSupported() override;
193 
194  int GetMaxTextureSize() override;
195 
196  bool IsFramebufferSupported() override;
197 
198 private:
200  void UpdateModelviewMatrix();
202  void UpdateLightPosition(int index);
204  void UpdateLightPositions();
206  void UpdateTextureParams(int index);
207 
209  void EnableShadows();
211  void DisableShadows();
212 
213 private:
215  DeviceConfig m_config;
216 
218  Math::Matrix m_worldMat;
220  Math::Matrix m_viewMat;
222  Math::Matrix m_modelviewMat;
224  Math::Matrix m_projectionMat;
226  Math::Matrix m_combinedMatrix;
228  Math::Matrix m_shadowMatrix;
230  bool m_combinedMatrixOutdated = true;
231 
233  Material m_material;
234 
236  bool m_lighting = false;
238  std::vector<Light> m_lights;
240  std::vector<bool> m_lightsEnabled;
241 
243  std::vector<Texture> m_currentTextures;
245  std::vector<bool> m_texturesEnabled;
247  std::vector<TextureStageParams> m_textureStageParams;
249  std::vector<int> m_remap;
250 
252  std::set<Texture> m_allTextures;
254  GLuint m_whiteTexture = 0;
255 
257  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
258 
260  enum VertexType
261  {
262  VERTEX_TYPE_NORMAL,
263  VERTEX_TYPE_TEX2,
264  VERTEX_TYPE_COL,
265  };
266 
268  struct VboObjectInfo
269  {
270  PrimitiveType primitiveType = {};
271  unsigned int bufferId = 0;
272  VertexType vertexType = {};
273  int vertexCount = 0;
274  };
275 
278  ShadowMappingSupport m_shadowMappingSupport = SMS_NONE;
280  bool m_multiDrawArrays = false;
282  FramebufferSupport m_framebufferSupport = FBS_NONE;
284  VertexBufferType m_vertexBufferType = VBT_DISPLAY_LIST;
286  std::map<unsigned int, VboObjectInfo> m_vboObjects;
288  unsigned int m_lastVboId = 0;
289 
291  bool m_shadowMapping = false;
293  bool m_shadowQuality = true;
294 };
295 
296 
297 } // namespace Gfx
void SetDepthTestFunc(CompFunc func) override
Sets the function of depth test.
Definition: gldevice.cpp:2013
void SetViewport(int x, int y, int width, int height) override
Changes rendering viewport.
Definition: gldevice.cpp:1951
RenderMode
Render modes the graphics device can be in.
Definition: device.h:172
bool IsShadowMappingSupported() override
Checks if shadow mapping is supported.
Definition: gldevice.cpp:2165
int GetMaxTextureSize() override
Returns max texture size supported.
Definition: gldevice.cpp:2170
void SetTextureEnabled(int index, bool enabled) override
Enables/disables the given texture stage.
Definition: gldevice.cpp:962
Material struct.
int GetMaxAnisotropyLevel() override
Returns max anisotropy level supported.
Definition: gldevice.cpp:2155
CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params) override
Creates new framebuffer with given name or nullptr if it&#39;s not possible.
Definition: gldevice.cpp:2113
void DestroyStaticBuffer(unsigned int bufferId) override
Deletes a static buffer.
Definition: gldevice.cpp:1849
void SetColorMask(bool red, bool green, bool blue, bool alpha) override
Sets the color mask.
Definition: gldevice.cpp:2008
Vertex of a primitive.
Definition: vertex.h:52
Vertex with secondary texture coordinates.
Definition: vertex.h:113
ARB extension.
Definition: gldevice.h:61
FogMode
Type of fog calculation function.
Definition: device.h:218
void EndScene() override
Ends drawing the 3D scene.
Definition: gldevice.cpp:436
void SetRenderMode(RenderMode mode) override
Sets current rendering mode.
Definition: gldevice.cpp:452
4x4 matrix
Definition: matrix.h:65
void DestroyTexture(const Texture &texture) override
Deletes a given texture, freeing it from video memory.
Definition: gldevice.cpp:875
void DeleteFramebuffer(std::string name) override
Deletes framebuffer.
Definition: gldevice.cpp:2137
void SetCullMode(CullMode mode) override
Sets the current cull mode.
Definition: gldevice.cpp:2056
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&#39;t exist.
Definition: gldevice.cpp:2104
void SetTransform(TransformType type, const Math::Matrix &matrix) override
Sets the transform matrix of given type.
Definition: gldevice.cpp:457
void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override
Sets the texture coordinate generation mode for given texture unit.
Definition: gldevice.cpp:995
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:2085
void SetShadeModel(ShadeModel model) override
Sets the shade model.
Definition: gldevice.cpp:2065
FillMode
Polygon fill mode.
Definition: device.h:251
void SetTextureStageParams(int index, const TextureStageParams &params) override
Definition: gldevice.cpp:985
ShadowMappingSupport
Definition: gldevice.h:57
Implementation of CDevice interface in OpenGL.
Definition: gldevice.h:75
void SetRenderState(RenderState state, bool enabled) override
Enables/disables the given render state.
Definition: gldevice.cpp:1956
use display lists
Definition: gldevice.h:53
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override
Definition: gldevice.cpp:672
CompFunc
Type of function used to compare values.
Definition: device.h:183
void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override
Sets the blending functions for source and destination operations.
Definition: gldevice.cpp:2028
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:1524
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:510
std::string GetName() override
Returns a name of this device.
Definition: gldevice.cpp:161
void DebugHook() override
Provides a hook to debug graphics code (implementation-specific)
Definition: gldevice.cpp:54
Parameters for texture coordinate generation.
Definition: texture.h:234
No support for depth textures.
Definition: gldevice.h:60
void SetShadowColor(float value) override
Sets shadow color.
Definition: gldevice.cpp:2072
VertexBufferType
Specifies type of vertex buffer to use.
Definition: gldevice.h:50
TexImgFormat
Format of image data.
Definition: texture.h:42
General config for graphics device.
Definition: device.h:64
Properties of light in 3D scene.
Definition: light.h:54
void SetGlobalAmbient(const Color &color) override
Sets the global ambient color.
Definition: gldevice.cpp:2038
void SetLightEnabled(int index, bool enabled) override
Enables/disables the light at given index.
Definition: gldevice.cpp:656
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:2043
void SetDepthBias(float factor, float units) override
Sets the depth bias (constant value added to Z-coords)
Definition: gldevice.cpp:2018
bool IsFramebufferSupported() override
Checks if framebuffers are supported.
Definition: gldevice.cpp:2175
Parameters for texture creation.
Definition: texture.h:155
ShadeModel
Shade model used in rendering.
Definition: device.h:241
BlendFunc
Type of blending function.
Definition: device.h:199
std::unique_ptr< CFrameBufferPixels > GetFrameBufferPixels() const override
Returns the pixels of the entire screen.
Definition: gldevice.cpp:2099
bool IsAnisotropySupported() override
Checks if anisotropy is supported.
Definition: gldevice.cpp:2150
Matrix struct and related functions.
virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices, int first[], int count[], int drawCount, Color color=Color(1.0f, 1.0f, 1.0f, 1.0f)) override
Renders primitives composed of lists of vertices with single texture.
Definition: gldevice.cpp:1422
void UpdateTexture(const Texture &texture, Math::IntPoint offset, ImageData *data, TexImgFormat format) override
Updates a part of texture from raw image data.
Definition: gldevice.cpp:855
Image loaded from file.
Definition: image.h:54
int GetMaxLightCount() override
Returns the maximum number of lights available.
Definition: gldevice.cpp:519
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
void DebugLights() override
Displays light positions to aid in debuggings.
Definition: gldevice.cpp:61
PrimitiveType
Type of primitive to render.
Definition: device.h:265
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:1350
void SetFillMode(FillMode mode) override
Sets the current fill mode.
Definition: gldevice.cpp:2077
CullMode
Culling mode for polygons.
Definition: device.h:229
use core OpenGL 1.5 VBOs
Definition: gldevice.h:54
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:2033
int ComputeSphereVisibility(const Math::Vector &center, float radius) override
Definition: gldevice.cpp:1872
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:1656
RenderState
Render states that can be enabled/disabled.
Definition: device.h:155
void Clear() override
Clears the screen to blank.
Definition: gldevice.cpp:446
bool Create() override
Initializes the device, setting the initial state.
Definition: gldevice.cpp:166
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:524
3D (3x1) vector
Definition: vector.h:53
void ConfigChanged(const DeviceConfig &newConfig) override
Changes configuration.
Definition: gldevice.cpp:407
void BeginScene() override
Begins drawing the 3D scene.
Definition: gldevice.cpp:426
Abstract graphics device - CDevice class and related structs/enums.
2D Point with integer coords
Definition: intpoint.h:39
void SetAlphaTestFunc(CompFunc func, float refValue) override
Sets the alpha test function and reference value.
Definition: gldevice.cpp:2023
RGBA color.
Definition: color.h:39
void SetTexture(int index, const Texture &texture) override
Definition: gldevice.cpp:926
int GetMaxTextureStageCount() override
Returns the maximum number of multitexture stages.
Definition: gldevice.cpp:917
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:143
void Destroy() override
Destroys the device, releasing every acquired resource.
Definition: gldevice.cpp:385
void DrawStaticBuffer(unsigned int bufferId) override
Draws a static buffer.
Definition: gldevice.cpp:1764
void DestroyAllTextures() override
Deletes all textures created so far.
Definition: gldevice.cpp:892
int GetMaxSamples() override
Returns max samples supported.
Definition: gldevice.cpp:2160
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:1319
Texture CreateDepthTexture(int width, int height, int depth) override
Creates a depth texture with specific dimensions and depth.
Definition: gldevice.cpp:781
Abstract interface of graphics device.
Definition: device.h:323
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67