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-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 <map>
37 #include <memory>
38 #include <set>
39 #include <string>
40 #include <vector>
41 
42 
43 // Graphics module namespace
44 namespace Gfx
45 {
46 
49 {
51  GLuint vao = 0;
53  GLuint vbo = 0;
55  unsigned int size = 0;
57  unsigned int offset = 0;
58 };
59 
71 class CGL33Device : public CDevice
72 {
73 public:
74  CGL33Device(const DeviceConfig &config);
75  virtual ~CGL33Device();
76 
77  void DebugHook() override;
78  void DebugLights() override;
79 
80  std::string GetName() 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 SetRenderMode(RenderMode mode) override;
93 
94  void SetTransform(TransformType type, const Math::Matrix &matrix) override;
95 
96  void SetMaterial(const Material &material) override;
97 
98  int GetMaxLightCount() override;
99  void SetLight(int index, const Light &light) override;
100  void SetLightEnabled(int index, bool enabled) override;
101 
102  Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
103  Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
104  Texture CreateDepthTexture(int width, int height, int depth) override;
105  void UpdateTexture(const Texture& texture, Math::IntPoint offset, ImageData* data, TexImgFormat format) override;
106  void DestroyTexture(const Texture &texture) override;
107  void DestroyAllTextures() override;
108 
109  int GetMaxTextureStageCount() override;
110  void SetTexture(int index, const Texture &texture) override;
111  void SetTexture(int index, unsigned int textureId) override;
112  void SetTextureEnabled(int index, bool enabled) override;
113 
114  void SetTextureStageParams(int index, const TextureStageParams &params) override;
115 
116  void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
117  void SetTextureCoordGeneration(int index, TextureGenerationParams &params) override;
118 
119  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
120  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
121  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
122  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
123  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) override;
124 
125  virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices,
126  int first[], int count[], int drawCount,
127  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
128  virtual void DrawPrimitives(PrimitiveType type, const VertexTex2 *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 VertexCol *vertices,
132  int first[], int count[], int drawCount) override;
133 
134  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
135  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
136  unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
137  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
138  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
139  void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
140  void DrawStaticBuffer(unsigned int bufferId) override;
141  void DestroyStaticBuffer(unsigned int bufferId) override;
142 
143  int ComputeSphereVisibility(const Math::Vector &center, float radius) override;
144 
145  void SetViewport(int x, int y, int width, int height) override;
146 
147  void SetRenderState(RenderState state, bool enabled) override;
148 
149  void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
150 
151  void SetDepthTestFunc(CompFunc func) override;
152 
153  void SetDepthBias(float factor, float units) override;
154 
155  void SetAlphaTestFunc(CompFunc func, float refValue) override;
156 
157  void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
158 
159  void SetClearColor(const Color &color) override;
160 
161  void SetGlobalAmbient(const Color &color) override;
162 
163  void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
164 
165  void SetCullMode(CullMode mode) override;
166 
167  void SetShadeModel(ShadeModel model) override;
168 
169  void SetShadowColor(float value) override;
170 
171  void SetFillMode(FillMode mode) override;
172 
173  void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
174 
175  std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const override;
176 
177  CFramebuffer* GetFramebuffer(std::string name) override;
178 
179  CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
180 
181  void DeleteFramebuffer(std::string name) override;
182 
183  bool IsAnisotropySupported() override;
184  int GetMaxAnisotropyLevel() override;
185 
186  int GetMaxSamples() override;
187 
188  bool IsShadowMappingSupported() override;
189 
190  int GetMaxTextureSize() override;
191 
192  bool IsFramebufferSupported() override;
193 
194 private:
196  void UpdateTextureParams(int index);
198  void UpdateRenderingMode();
199 
201  inline void BindVBO(GLuint vbo);
203  inline void BindVAO(GLuint vao);
204 
206  unsigned int UploadVertexData(DynamicBuffer& buffer, void* data, unsigned int size);
207 
208 private:
210  DeviceConfig m_config;
211 
213  Math::Matrix m_worldMat;
215  Math::Matrix m_viewMat;
217  Math::Matrix m_modelviewMat;
219  Math::Matrix m_projectionMat;
221  Math::Matrix m_combinedMatrix;
223  bool m_combinedMatrixOutdated = true;
224 
226  Material m_material;
227 
229  bool m_lighting = false;
231  std::vector<Light> m_lights;
233  std::vector<bool> m_lightsEnabled;
234 
236  std::vector<Texture> m_currentTextures;
238  std::vector<bool> m_texturesEnabled;
240  std::vector<TextureStageParams> m_textureStageParams;
241 
243  std::set<Texture> m_allTextures;
244 
246  enum VertexType
247  {
248  VERTEX_TYPE_NORMAL,
249  VERTEX_TYPE_TEX2,
250  VERTEX_TYPE_COL,
251  };
252 
254  struct VertexBufferInfo
255  {
256  PrimitiveType primitiveType = {};
257  GLuint vbo = 0;
258  GLuint vao = 0;
259  VertexType vertexType = {};
260  int vertexCount = 0;
261  unsigned int size = 0;
262  };
263 
266  std::map<unsigned int, VertexBufferInfo> m_vboObjects;
268  unsigned int m_lastVboId = 0;
270  GLuint m_currentVBO = 0;
272  GLuint m_currentVAO = 0;
273 
275  unsigned long m_vboMemory = 0;
276 
278  std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
279 
281  GLuint m_normalProgram = 0;
283  GLuint m_interfaceProgram = 0;
285  GLuint m_shadowProgram = 0;
286 
287  DynamicBuffer m_dynamicBuffers[3];
288 
290  unsigned int m_mode = 0;
292  UniformLocations m_uniforms[3];
294  UniformLocations* m_uni = nullptr;
295 };
296 
297 } // namespace Gfx
RenderMode
Render modes the graphics device can be in.
Definition: device.h:172
Material struct.
Vertex of a primitive.
Definition: vertex.h:52
Vertex with secondary texture coordinates.
Definition: vertex.h:113
unsigned int offset
Dynamic buffer offset.
Definition: gl33device.h:57
GLuint vao
Auxiliary VAO for rendering primitives with DrawPrimitive*.
Definition: gl33device.h:51
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
FillMode
Polygon fill mode.
Definition: device.h:251
CompFunc
Type of function used to compare values.
Definition: device.h:183
Parameters for a texture unit.
Definition: texture.h:180
Material of a surface.
Definition: material.h:45
Parameters for texture coordinate generation.
Definition: texture.h:234
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
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
Matrix struct and related functions.
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
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
RenderState
Render states that can be enabled/disabled.
Definition: device.h:155
Info about a texture.
Definition: texture.h:256
unsigned int size
Dynamic buffer size.
Definition: gl33device.h:55
3D (3x1) vector
Definition: vector.h:53
GLuint vbo
Dynamic buffer.
Definition: gl33device.h:53
Abstract graphics device - CDevice class and related structs/enums.
2D Point with integer coords
Definition: intpoint.h:39
RGBA color.
Definition: color.h:39
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:143
Struct for dynamic buffers.
Definition: gl33device.h:48
Implementation of CDevice interface in OpenGL 3.3.
Definition: gl33device.h:71
Abstract interface of graphics device.
Definition: device.h:323
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67