Colobot
gl21device.h
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/glutil.h"
32 
33 #include "math/matrix.h"
34 
35 #include <string>
36 #include <vector>
37 #include <set>
38 #include <map>
39 
40 
41 // Graphics module namespace
42 namespace Gfx
43 {
44 
56 class CGL21Device : public CDevice
57 {
58 public:
59  CGL21Device(const DeviceConfig &config);
60  virtual ~CGL21Device();
61 
62  void DebugHook() override;
63  void DebugLights() override;
64 
65  std::string GetName() override;
66 
67  bool Create() override;
68  void Destroy() override;
69 
70  void ConfigChanged(const DeviceConfig &newConfig) override;
71 
72  void BeginScene() override;
73  void EndScene() override;
74 
75  void Clear() override;
76 
77  void SetRenderMode(RenderMode mode) override;
78 
79  void SetTransform(TransformType type, const Math::Matrix &matrix) override;
80 
81  void SetMaterial(const Material &material) override;
82 
83  int GetMaxLightCount() override;
84  void SetLight(int index, const Light &light) override;
85  void SetLightEnabled(int index, bool enabled) override;
86 
87  Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
88  Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
89  Texture CreateDepthTexture(int width, int height, int depth) override;
90  void UpdateTexture(const Texture& texture, Math::IntPoint offset, ImageData* data, TexImgFormat format) override;
91  void DestroyTexture(const Texture &texture) override;
92  void DestroyAllTextures() override;
93 
94  int GetMaxTextureStageCount() override;
95  void SetTexture(int index, const Texture &texture) override;
96  void SetTexture(int index, unsigned int textureId) override;
97  void SetTextureEnabled(int index, bool enabled) override;
98 
99  void SetTextureStageParams(int index, const TextureStageParams &params) override;
100 
101  void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
102  void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override;
103 
104  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
105  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
106  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
107  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
108  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount) override;
109 
110  virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices,
111  int first[], int count[], int drawCount,
112  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
113  virtual void DrawPrimitives(PrimitiveType type, const VertexTex2 *vertices,
114  int first[], int count[], int drawCount,
115  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
116  virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
117  int first[], int count[], int drawCount) override;
118 
119  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
120  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
121  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
122  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
123  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
124  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
125  void DrawStaticBuffer(unsigned int bufferId) override;
126  void DestroyStaticBuffer(unsigned int bufferId) override;
127 
128  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
129 
130  void SetViewport(int x, int y, int width, int height) override;
131 
132  void SetRenderState(RenderState state, bool enabled) override;
133 
134  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
135 
136  void SetDepthTestFunc(CompFunc func) override;
137 
138  void SetDepthBias(float factor, float units) override;
139 
140  void SetAlphaTestFunc(CompFunc func, float refValue) override;
141 
142  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
143 
144  void SetClearColor(const Color &color) override;
145 
146  void SetGlobalAmbient(const Color &color) override;
147 
148  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
149 
150  void SetCullMode(CullMode mode) override;
151 
152  void SetShadeModel(ShadeModel model) override;
153 
154  void SetShadowColor(float value) override;
155 
156  void SetFillMode(FillMode mode) override;
157 
158  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
159 
160  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
161 
162  CFramebuffer* GetFramebuffer(std::string name) override;
163 
164  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
165 
166  void DeleteFramebuffer(std::string name) override;
167 
168  bool IsAnisotropySupported() override;
169  int GetMaxAnisotropyLevel() override;
170 
171  int GetMaxSamples() override;
172 
173  bool IsShadowMappingSupported() override;
174 
175  int GetMaxTextureSize() override;
176 
177  bool IsFramebufferSupported() override;
178 
179 private:
181  void UpdateTextureParams(int index);
183  void UpdateTextureStatus();
185  inline void BindVBO(GLuint vbo);
186 
187 private:
189  DeviceConfig m_config;
190 
192  Math::Matrix m_worldMat;
194  Math::Matrix m_viewMat;
196  Math::Matrix m_modelviewMat;
198  Math::Matrix m_projectionMat;
200  Math::Matrix m_combinedMatrix;
201 
203  Material m_material;
204 
206  bool m_lighting = false;
208  std::vector<Light> m_lights;
210  std::vector<bool> m_lightsEnabled;
211 
213  std::vector<Texture> m_currentTextures;
215  std::vector<bool> m_texturesEnabled;
217  std::vector<TextureStageParams> m_textureStageParams;
218 
220  std::set<Texture> m_allTextures;
221 
223  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
224 
226  enum VertexType
227  {
228  VERTEX_TYPE_NORMAL,
229  VERTEX_TYPE_TEX2,
230  VERTEX_TYPE_COL,
231  };
232 
234  struct VboObjectInfo
235  {
236  PrimitiveType primitiveType = {};
237  unsigned int bufferId = 0;
238  VertexType vertexType = {};
239  int vertexCount = 0;
240  int size = 0;
241  };
242 
245  FramebufferSupport m_framebufferSupport = FBS_NONE;
247  std::map<unsigned int, VboObjectInfo> m_vboObjects;
249  unsigned int m_lastVboId = 0;
251  GLuint m_currentVBO = 0;
252 
254  GLuint m_normalProgram = 0;
256  GLuint m_interfaceProgram = 0;
258  GLuint m_shadowProgram = 0;
259 
261  UniformLocations m_uniforms[3];
263  int m_mode = 0;
264 };
265 
266 
267 } // namespace Gfx
RenderMode
Render modes the graphics device can be in.
Definition: device.h:172
void BeginScene() override
Begins drawing the 3D scene.
Definition: gl21device.cpp:610
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:1754
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override
Definition: gl21device.cpp:769
Implementation of CDevice interface in OpenGL.
Definition: gl21device.h:56
void Clear() override
Clears the screen to blank.
Definition: gl21device.cpp:622
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:1319
Material struct.
void SetShadeModel(ShadeModel model) override
Sets the shade model.
Definition: gl21device.cpp:1734
Vertex of a primitive.
Definition: vertex.h:52
Vertex with secondary texture coordinates.
Definition: vertex.h:113
void SetDepthBias(float factor, float units) override
Sets the depth bias (constant value added to Z-coords)
Definition: gl21device.cpp:1682
void SetGlobalAmbient(const Color &color) override
Sets the global ambient color.
Definition: gl21device.cpp:1702
int GetMaxAnisotropyLevel() override
Returns max anisotropy level supported.
Definition: gl21device.cpp:1824
int GetMaxTextureSize() override
Returns max texture size supported.
Definition: gl21device.cpp:1839
FogMode
Type of fog calculation function.
Definition: device.h:218
4x4 matrix
Definition: matrix.h:65
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:99
void SetColorMask(bool red, bool green, bool blue, bool alpha) override
Sets the color mask.
Definition: gl21device.cpp:1672
void SetTextureStageParams(int index, const TextureStageParams &params) override
Definition: gl21device.cpp:1051
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: gl21device.cpp:1241
FillMode
Polygon fill mode.
Definition: device.h:251
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:1707
std::string GetName() override
Returns a name of this device.
Definition: gl21device.cpp:166
void DestroyTexture(const Texture &texture) override
Deletes a given texture, freeing it from video memory.
Definition: gl21device.cpp:940
void SetLight(int index, const Light &light) override
Sets the light at given index.
Definition: gl21device.cpp:717
int ComputeSphereVisibility(const Math::Vector &center, float radius) override
Definition: gl21device.cpp:1543
void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override
Sets the texture coordinate generation mode for given texture unit.
Definition: gl21device.cpp:1061
void DrawStaticBuffer(unsigned int bufferId) override
Draws a static buffer.
Definition: gl21device.cpp:1457
CompFunc
Type of function used to compare values.
Definition: device.h:183
void SetLightEnabled(int index, bool enabled) override
Enables/disables the light at given index.
Definition: gl21device.cpp:756
Parameters for a texture unit.
Definition: texture.h:180
Material of a surface.
Definition: material.h:45
void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override
Sets the blending functions for source and destination operations.
Definition: gl21device.cpp:1692
int GetMaxTextureStageCount() override
Returns the maximum number of multitexture stages.
Definition: gl21device.cpp:969
void SetRenderMode(RenderMode mode) override
Sets current rendering mode.
Definition: gl21device.cpp:628
CFramebuffer * GetFramebuffer(std::string name) override
Returns framebuffer with given name or nullptr if it doesn&#39;t exist.
Definition: gl21device.cpp:1773
Parameters for texture coordinate generation.
Definition: texture.h:234
void SetRenderState(RenderState state, bool enabled) override
Enables/disables the given render state.
Definition: gl21device.cpp:1621
void ConfigChanged(const DeviceConfig &newConfig) override
Changes configuration.
Definition: gl21device.cpp:591
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 SetFillMode(FillMode mode) override
Sets the current fill mode.
Definition: gl21device.cpp:1746
void SetCullMode(CullMode mode) override
Sets the current cull mode.
Definition: gl21device.cpp:1725
void SetViewport(int x, int y, int width, int height) override
Changes rendering viewport.
Definition: gl21device.cpp:1616
void SetMaterial(const Material &material) override
Sets the current material.
Definition: gl21device.cpp:703
bool Create() override
Initializes the device, setting the initial state.
Definition: gl21device.cpp:171
Parameters for texture creation.
Definition: texture.h:155
Definition: glutil.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: gl21device.cpp:1768
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:1379
CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params) override
Creates new framebuffer with given name or nullptr if it&#39;s not possible.
Definition: gl21device.cpp:1782
Matrix struct and related functions.
int GetMaxSamples() override
Returns max samples supported.
Definition: gl21device.cpp:1829
Image loaded from file.
Definition: image.h:54
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
PrimitiveType
Type of primitive to render.
Definition: device.h:265
bool IsAnisotropySupported() override
Checks if anisotropy is supported.
Definition: gl21device.cpp:1819
bool IsShadowMappingSupported() override
Checks if shadow mapping is supported.
Definition: gl21device.cpp:1834
CullMode
Culling mode for polygons.
Definition: device.h:229
Colored vertex.
Definition: vertex.h:84
Namespace for (new) graphics code.
Definition: app.h:49
Implementation-specific image data.
Definition: image.h:41
int GetMaxLightCount() override
Returns the maximum number of lights available.
Definition: gl21device.cpp:712
void DeleteFramebuffer(std::string name) override
Deletes framebuffer.
Definition: gl21device.cpp:1806
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:1132
RenderState
Render states that can be enabled/disabled.
Definition: device.h:155
Info about a texture.
Definition: texture.h:256
void SetTexture(int index, const Texture &texture) override
Definition: gl21device.cpp:978
void SetTextureEnabled(int index, bool enabled) override
Enables/disables the given texture stage.
Definition: gl21device.cpp:1016
bool IsFramebufferSupported() override
Checks if framebuffers are supported.
Definition: gl21device.cpp:1844
void Destroy() override
Destroys the device, releasing every acquired resource.
Definition: gl21device.cpp:566
3D (3x1) vector
Definition: vector.h:53
void DestroyStaticBuffer(unsigned int bufferId) override
Deletes a static buffer.
Definition: gl21device.cpp:1527
void SetShadowColor(float value) override
Sets shadow color.
Definition: gl21device.cpp:1741
Texture CreateDepthTexture(int width, int height, int depth) override
Creates a depth texture with specific dimensions and depth.
Definition: gl21device.cpp:876
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:1163
Abstract graphics device - CDevice class and related structs/enums.
void SetTransform(TransformType type, const Math::Matrix &matrix) override
Sets the transform matrix of given type.
Definition: gl21device.cpp:654
2D Point with integer coords
Definition: intpoint.h:39
RGBA color.
Definition: color.h:39
void DebugHook() override
Provides a hook to debug graphics code (implementation-specific)
Definition: gl21device.cpp:58
void UpdateTexture(const Texture &texture, Math::IntPoint offset, ImageData *data, TexImgFormat format) override
Updates a part of texture from raw image data.
Definition: gl21device.cpp:922
void EndScene() override
Ends drawing the 3D scene.
Definition: gl21device.cpp:615
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:143
void SetAlphaTestFunc(CompFunc func, float refValue) override
Sets the alpha test function and reference value.
Definition: gl21device.cpp:1687
void SetClearColor(const Color &color) override
Sets the clear color.
Definition: gl21device.cpp:1697
void SetDepthTestFunc(CompFunc func) override
Sets the function of depth test.
Definition: gl21device.cpp:1677
Abstract interface of graphics device.
Definition: device.h:323
void DestroyAllTextures() override
Deletes all textures created so far.
Definition: gl21device.cpp:957
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67
void DebugLights() override
Displays light positions to aid in debuggings.
Definition: gl21device.cpp:65