Colobot
gl14device.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 CGL14Device : public CDevice
76 {
77 public:
78  CGL14Device(const DeviceConfig &config);
79  virtual ~CGL14Device();
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 
122  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
123  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
124  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
125  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
126  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount) override;
127 
128  virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices,
129  int first[], int count[], int drawCount,
130  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
131  virtual void DrawPrimitives(PrimitiveType type, const VertexTex2 *vertices,
132  int first[], int count[], int drawCount,
133  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
134  virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
135  int first[], int count[], int drawCount) override;
136 
137  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
138  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
139  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
140  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
141  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
142  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
143  void DrawStaticBuffer(unsigned int bufferId) override;
144  void DestroyStaticBuffer(unsigned int bufferId) override;
145 
146  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
147 
148  void SetViewport(int x, int y, int width, int height) override;
149 
150  void SetRenderState(RenderState state, bool enabled) override;
151 
152  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
153 
154  void SetDepthTestFunc(CompFunc func) override;
155 
156  void SetDepthBias(float factor, float units) override;
157 
158  void SetAlphaTestFunc(CompFunc func, float refValue) override;
159 
160  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
161 
162  void SetClearColor(const Color &color) override;
163 
164  void SetGlobalAmbient(const Color &color) override;
165 
166  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
167 
168  void SetCullMode(CullMode mode) override;
169 
170  void SetShadeModel(ShadeModel model) override;
171 
172  void SetShadowColor(float value) override;
173 
174  void SetFillMode(FillMode mode) override;
175 
176  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
177 
178  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
179 
180  CFramebuffer* GetFramebuffer(std::string name) override;
181 
182  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
183 
184  void DeleteFramebuffer(std::string name) override;
185 
186  bool IsAnisotropySupported() override;
187  int GetMaxAnisotropyLevel() override;
188 
189  int GetMaxSamples() override;
190 
191  bool IsShadowMappingSupported() override;
192 
193  int GetMaxTextureSize() override;
194 
195  bool IsFramebufferSupported() override;
196 
197 private:
199  void UpdateModelviewMatrix();
201  void UpdateLightPosition(int index);
203  void UpdateLightPositions();
205  void UpdateTextureParams(int index);
206 
208  void EnableShadows();
210  void DisableShadows();
211 
212 private:
214  DeviceConfig m_config;
215 
217  Math::Matrix m_worldMat;
219  Math::Matrix m_viewMat;
221  Math::Matrix m_modelviewMat;
223  Math::Matrix m_projectionMat;
225  Math::Matrix m_combinedMatrix;
227  Math::Matrix m_shadowMatrix;
229  bool m_combinedMatrixOutdated = true;
230 
232  Material m_material;
233 
235  bool m_lighting = false;
237  std::vector<Light> m_lights;
239  std::vector<bool> m_lightsEnabled;
240 
242  std::vector<Texture> m_currentTextures;
244  std::vector<bool> m_texturesEnabled;
246  std::vector<TextureStageParams> m_textureStageParams;
248  std::vector<int> m_remap;
249 
251  std::set<Texture> m_allTextures;
253  GLuint m_whiteTexture = 0;
254 
256  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
257 
259  enum VertexType
260  {
261  VERTEX_TYPE_NORMAL,
262  VERTEX_TYPE_TEX2,
263  VERTEX_TYPE_COL,
264  };
265 
267  struct VboObjectInfo
268  {
269  PrimitiveType primitiveType = {};
270  unsigned int bufferId = 0;
271  VertexType vertexType = {};
272  int vertexCount = 0;
273  };
274 
277  ShadowMappingSupport m_shadowMappingSupport = SMS_NONE;
279  bool m_multiDrawArrays = false;
281  FramebufferSupport m_framebufferSupport = FBS_NONE;
283  VertexBufferType m_vertexBufferType = VBT_DISPLAY_LIST;
285  std::map<unsigned int, VboObjectInfo> m_vboObjects;
287  unsigned int m_lastVboId = 0;
288 
290  bool m_shadowMapping = false;
292  bool m_shadowQuality = true;
293 };
294 
295 
296 } // namespace Gfx
void SetShadeModel(ShadeModel model) override
Sets the shade model.
Definition: gl14device.cpp:2025
bool IsShadowMappingSupported() override
Checks if shadow mapping is supported.
Definition: gl14device.cpp:2125
RenderMode
Render modes the graphics device can be in.
Definition: device.h:172
void Destroy() override
Destroys the device, releasing every acquired resource.
Definition: gl14device.cpp:385
std::string GetName() override
Returns a name of this device.
Definition: gl14device.cpp:161
Texture CreateDepthTexture(int width, int height, int depth) override
Creates a depth texture with specific dimensions and depth.
Definition: gl14device.cpp:781
bool IsAnisotropySupported() override
Checks if anisotropy is supported.
Definition: gl14device.cpp:2110
Material struct.
Implementation of CDevice interface in OpenGL.
Definition: gl14device.h:75
void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override
Sets only the texture wrap modes (for faster than thru stage params)
Definition: gl14device.cpp:1279
Vertex of a primitive.
Definition: vertex.h:52
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: gl14device.cpp:1616
Vertex with secondary texture coordinates.
Definition: vertex.h:113
void SetFillMode(FillMode mode) override
Sets the current fill mode.
Definition: gl14device.cpp:2037
void DebugLights() override
Displays light positions to aid in debuggings.
Definition: gl14device.cpp:61
bool Create() override
Initializes the device, setting the initial state.
Definition: gl14device.cpp:166
ARB extension.
Definition: gl14device.h:61
FogMode
Type of fog calculation function.
Definition: device.h:218
void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override
Sets the blending functions for source and destination operations.
Definition: gl14device.cpp:1988
void SetCullMode(CullMode mode) override
Sets the current cull mode.
Definition: gl14device.cpp:2016
4x4 matrix
Definition: matrix.h:65
bool IsFramebufferSupported() override
Checks if framebuffers are supported.
Definition: gl14device.cpp:2135
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:99
void DebugHook() override
Provides a hook to debug graphics code (implementation-specific)
Definition: gl14device.cpp:54
std::unique_ptr< CFrameBufferPixels > GetFrameBufferPixels() const override
Returns the pixels of the entire screen.
Definition: gl14device.cpp:2059
FillMode
Polygon fill mode.
Definition: device.h:251
void SetClearColor(const Color &color) override
Sets the clear color.
Definition: gl14device.cpp:1993
ShadowMappingSupport
Definition: gl14device.h:57
CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params) override
Creates new framebuffer with given name or nullptr if it&#39;s not possible.
Definition: gl14device.cpp:2073
use display lists
Definition: gl14device.h:53
void EndScene() override
Ends drawing the 3D scene.
Definition: gl14device.cpp:436
CompFunc
Type of function used to compare values.
Definition: device.h:183
void ConfigChanged(const DeviceConfig &newConfig) override
Changes configuration.
Definition: gl14device.cpp:407
void SetTextureEnabled(int index, bool enabled) override
Enables/disables the given texture stage.
Definition: gl14device.cpp:962
int GetMaxLightCount() override
Returns the maximum number of lights available.
Definition: gl14device.cpp:519
void SetTexture(int index, const Texture &texture) override
Definition: gl14device.cpp:926
Parameters for a texture unit.
Definition: texture.h:180
Material of a surface.
Definition: material.h:45
void CopyFramebufferToTexture(Texture &texture, int xOffset, int yOffset, int x, int y, int width, int height) override
Copies content of framebuffer to texture.
Definition: gl14device.cpp:2045
int ComputeSphereVisibility(const Math::Vector &center, float radius) override
Definition: gl14device.cpp:1832
void BeginScene() override
Begins drawing the 3D scene.
Definition: gl14device.cpp:426
No support for depth textures.
Definition: gl14device.h:60
VertexBufferType
Specifies type of vertex buffer to use.
Definition: gl14device.h:50
void SetColorMask(bool red, bool green, bool blue, bool alpha) override
Sets the color mask.
Definition: gl14device.cpp:1968
void SetShadowColor(float value) override
Sets shadow color.
Definition: gl14device.cpp:2032
TexImgFormat
Format of image data.
Definition: texture.h:42
General config for graphics device.
Definition: device.h:64
void SetRenderState(RenderState state, bool enabled) override
Enables/disables the given render state.
Definition: gl14device.cpp:1916
Properties of light in 3D scene.
Definition: light.h:54
void SetAlphaTestFunc(CompFunc func, float refValue) override
Sets the alpha test function and reference value.
Definition: gl14device.cpp:1983
void UpdateTexture(const Texture &texture, Math::IntPoint offset, ImageData *data, TexImgFormat format) override
Updates a part of texture from raw image data.
Definition: gl14device.cpp:855
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
Matrix struct and related functions.
void SetGlobalAmbient(const Color &color) override
Sets the global ambient color.
Definition: gl14device.cpp:1998
Image loaded from file.
Definition: image.h:54
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex *vertices, int vertexCount) override
Creates a static buffer composed of given primitives with single texture vertices.
Definition: gl14device.cpp:1484
int GetMaxTextureStageCount() override
Returns the maximum number of multitexture stages.
Definition: gl14device.cpp:917
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
PrimitiveType
Type of primitive to render.
Definition: device.h:265
CullMode
Culling mode for polygons.
Definition: device.h:229
use core OpenGL 1.5 VBOs
Definition: gl14device.h:54
Colored vertex.
Definition: vertex.h:84
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: gl14device.cpp:1310
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: gl14device.cpp:2003
void Clear() override
Clears the screen to blank.
Definition: gl14device.cpp:446
Namespace for (new) graphics code.
Definition: app.h:49
void DrawStaticBuffer(unsigned int bufferId) override
Draws a static buffer.
Definition: gl14device.cpp:1724
Implementation-specific image data.
Definition: image.h:41
void DestroyTexture(const Texture &texture) override
Deletes a given texture, freeing it from video memory.
Definition: gl14device.cpp:875
RenderState
Render states that can be enabled/disabled.
Definition: device.h:155
void SetDepthBias(float factor, float units) override
Sets the depth bias (constant value added to Z-coords)
Definition: gl14device.cpp:1978
Info about a texture.
Definition: texture.h:256
void DestroyAllTextures() override
Deletes all textures created so far.
Definition: gl14device.cpp:892
int GetMaxTextureSize() override
Returns max texture size supported.
Definition: gl14device.cpp:2130
void SetRenderMode(RenderMode mode) override
Sets current rendering mode.
Definition: gl14device.cpp:452
3D (3x1) vector
Definition: vector.h:53
void DeleteFramebuffer(std::string name) override
Deletes framebuffer.
Definition: gl14device.cpp:2097
CFramebuffer * GetFramebuffer(std::string name) override
Returns framebuffer with given name or nullptr if it doesn&#39;t exist.
Definition: gl14device.cpp:2064
Abstract graphics device - CDevice class and related structs/enums.
void DestroyStaticBuffer(unsigned int bufferId) override
Deletes a static buffer.
Definition: gl14device.cpp:1809
void SetTextureStageParams(int index, const TextureStageParams &params) override
Definition: gl14device.cpp:985
void SetMaterial(const Material &material) override
Sets the current material.
Definition: gl14device.cpp:510
2D Point with integer coords
Definition: intpoint.h:41
void SetDepthTestFunc(CompFunc func) override
Sets the function of depth test.
Definition: gl14device.cpp:1973
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override
Definition: gl14device.cpp:672
RGBA color.
Definition: color.h:39
void SetViewport(int x, int y, int width, int height) override
Changes rendering viewport.
Definition: gl14device.cpp:1911
void SetTransform(TransformType type, const Math::Matrix &matrix) override
Sets the transform matrix of given type.
Definition: gl14device.cpp:457
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:143
int GetMaxAnisotropyLevel() override
Returns max anisotropy level supported.
Definition: gl14device.cpp:2115
int GetMaxSamples() override
Returns max samples supported.
Definition: gl14device.cpp:2120
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: gl14device.cpp:1382
Abstract interface of graphics device.
Definition: device.h:323
void SetLightEnabled(int index, bool enabled) override
Enables/disables the light at given index.
Definition: gl14device.cpp:656
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67
void SetLight(int index, const Light &light) override
Sets the light at given index.
Definition: gl14device.cpp:524