Colobot
device.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/color.h"
28 #include "graphics/core/texture.h"
29 
30 #include "math/intpoint.h"
31 
32 #include <memory>
33 #include <string>
34 
35 
36 class CImage;
37 struct ImageData;
38 
39 namespace Math
40 {
41 struct Matrix;
42 struct Vector;
43 } // namespace Math
44 
45 
46 // Graphics module namespace
47 namespace Gfx
48 {
49 
50 class CFramebuffer;
51 struct FramebufferParams;
52 struct Light;
53 struct Material;
54 struct Vertex;
55 struct VertexCol;
56 struct VertexTex2;
57 
65 {
67  Math::IntPoint size = Math::IntPoint(800, 600);
69  int bpp = 32;
71  bool fullScreen = false;
73  bool resizeable = true;
75  bool doubleBuf = true;
77  bool noFrame = false;
78 
80  int redSize = 8;
82  int greenSize = 8;
84  int blueSize = 8;
86  int alphaSize = 8;
88  int depthSize = 24;
90  int stencilSize = 8;
91 
93  bool hardwareAccel = true;
94 
96  void LoadDefault()
97  {
98  *this = DeviceConfig();
99  }
100 };
101 
107 {
108  bool multitexturingSupported = false;
109  int maxTextures = 1;
110  int maxTextureSize = 1024;
111 
112  int maxLights = 8;
113 
114  bool shadowMappingSupported = false;
115 
116  bool framebufferSupported = false;
117  int maxRenderbufferSize = 0;
118 
119  bool anisotropySupported = false;
120  int maxAnisotropy = 1;
121 
122  bool multisamplingSupported = false;
123  int maxSamples = 1;
124 };
125 
133 {
134  TEXTURE_PRIMARY = 0,
135  TEXTURE_SECONDARY = 1,
136  TEXTURE_SHADOW = 2,
137 };
138 
144 {
145  TRANSFORM_WORLD,
146  TRANSFORM_VIEW,
147  TRANSFORM_PROJECTION,
148  TRANSFORM_SHADOW
149 };
150 
156 {
157  RENDER_STATE_LIGHTING,
158  RENDER_STATE_BLENDING,
159  RENDER_STATE_FOG,
160  RENDER_STATE_DEPTH_TEST,
161  RENDER_STATE_DEPTH_WRITE,
162  RENDER_STATE_ALPHA_TEST,
163  RENDER_STATE_CULLING,
164  RENDER_STATE_DEPTH_BIAS,
165  RENDER_STATE_SHADOW_MAPPING,
166 };
167 
173 {
174  RENDER_MODE_NORMAL,
175  RENDER_MODE_INTERFACE,
176  RENDER_MODE_SHADOW,
177 };
178 
184 {
185  COMP_FUNC_NEVER,
186  COMP_FUNC_LESS,
187  COMP_FUNC_EQUAL,
188  COMP_FUNC_NOTEQUAL,
189  COMP_FUNC_LEQUAL,
190  COMP_FUNC_GREATER,
191  COMP_FUNC_GEQUAL,
192  COMP_FUNC_ALWAYS
193 };
194 
200 {
201  BLEND_ZERO,
202  BLEND_ONE,
203  BLEND_SRC_COLOR,
204  BLEND_INV_SRC_COLOR,
205  BLEND_DST_COLOR,
206  BLEND_INV_DST_COLOR,
207  BLEND_SRC_ALPHA,
208  BLEND_INV_SRC_ALPHA,
209  BLEND_DST_ALPHA,
210  BLEND_INV_DST_ALPHA,
211  BLEND_SRC_ALPHA_SATURATE
212 };
213 
219 {
220  FOG_LINEAR,
221  FOG_EXP,
222  FOG_EXP2
223 };
224 
230 {
235 };
236 
242 {
243  SHADE_FLAT,
244  SHADE_SMOOTH
245 };
246 
252 {
259 };
260 
266 {
267  PRIMITIVE_POINTS,
268  PRIMITIVE_LINES,
269  PRIMITIVE_LINE_STRIP,
270  PRIMITIVE_TRIANGLES,
271  PRIMITIVE_TRIANGLE_STRIP
272 };
273 
281 {
282  FRUSTUM_PLANE_LEFT = 0x01,
283  FRUSTUM_PLANE_RIGHT = 0x02,
284  FRUSTUM_PLANE_TOP = 0x04,
285  FRUSTUM_PLANE_BOTTOM = 0x08,
286  FRUSTUM_PLANE_FRONT = 0x10,
287  FRUSTUM_PLANE_BACK = 0x20,
288  FRUSTUM_PLANE_ALL = FRUSTUM_PLANE_LEFT | FRUSTUM_PLANE_RIGHT |
289  FRUSTUM_PLANE_TOP | FRUSTUM_PLANE_BOTTOM |
290  FRUSTUM_PLANE_FRONT | FRUSTUM_PLANE_BACK
291 };
292 
298 {
299  RENDER_TARGET_COLOR,
300  RENDER_TARGET_DEPTH,
301  RENDER_TARGET_STENCIL
302 };
303 
305 {
306 public:
307  virtual ~CFrameBufferPixels() {}
308 
309  virtual void* GetPixelsData() = 0;
310 };
311 
323 class CDevice
324 {
325 protected:
326  std::string m_errorMessage;
327 
331 
332 public:
333  virtual ~CDevice() {}
334 
336  inline std::string GetError()
337  {
338  return m_errorMessage;
339  }
340 
343  {
344  return m_capabilities;
345  }
346 
348  virtual void DebugHook() = 0;
349 
351  virtual void DebugLights() = 0;
352 
354  virtual std::string GetName() = 0;
355 
357  virtual bool Create() = 0;
359  virtual void Destroy() = 0;
360 
362  virtual void ConfigChanged(const DeviceConfig &newConfig) = 0;
363 
365  virtual void BeginScene() = 0;
367  virtual void EndScene() = 0;
368 
370  virtual void Clear() = 0;
371 
373  virtual void SetRenderMode(RenderMode mode) = 0;
374 
376  virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0;
377 
379  virtual void SetMaterial(const Material &material) = 0;
380 
382  virtual int GetMaxLightCount() = 0;
384  virtual void SetLight(int index, const Light &light) = 0;
386  virtual void SetLightEnabled(int index, bool enabled) = 0;
387 
389  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) = 0;
391  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) = 0;
393  virtual Texture CreateDepthTexture(int width, int height, int depth) = 0;
395  virtual void UpdateTexture(const Texture& texture, Math::IntPoint offset, ImageData* data, TexImgFormat format) = 0;
397  virtual void DestroyTexture(const Texture &texture) = 0;
399  virtual void DestroyAllTextures() = 0;
400 
402  virtual int GetMaxTextureStageCount() = 0;
404  virtual void SetTexture(int index, const Texture &texture) = 0;
406  virtual void SetTexture(int index, unsigned int textureId) = 0;
408  virtual void SetTextureEnabled(int index, bool enabled) = 0;
409 
411  virtual void SetTextureStageParams(int index, const TextureStageParams &params) = 0;
412 
414  virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) = 0;
415 
417  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
418  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
420  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
421  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
423  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) = 0;
424 
426  virtual void DrawPrimitives(PrimitiveType type, const Vertex *vertices,
427  int first[], int count[], int drawCount,
428  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
430  virtual void DrawPrimitives(PrimitiveType type, const VertexTex2 *vertices,
431  int first[], int count[], int drawCount,
432  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
434  virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
435  int first[], int count[], int drawCount) = 0;
436 
438  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
439 
441  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
442 
444  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
445 
447  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
448 
450  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
451 
453  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
454 
456  virtual void DrawStaticBuffer(unsigned int bufferId) = 0;
457 
459  virtual void DestroyStaticBuffer(unsigned int bufferId) = 0;
460 
463  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0;
464 
466  virtual void SetViewport(int x, int y, int width, int height) = 0;
467 
469  virtual void SetRenderState(RenderState state, bool enabled) = 0;
470 
472  virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0;
473 
475  virtual void SetDepthTestFunc(CompFunc func) = 0;
476 
478  virtual void SetDepthBias(float factor, float units) = 0;
479 
481  virtual void SetAlphaTestFunc(CompFunc func, float refValue) = 0;
482 
484  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) = 0;
485 
487  virtual void SetClearColor(const Color &color) = 0;
488 
490  virtual void SetGlobalAmbient(const Color &color) = 0;
491 
493  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) = 0;
494 
496  virtual void SetCullMode(CullMode mode) = 0;
497 
499  virtual void SetShadeModel(ShadeModel model) = 0;
500 
502  virtual void SetShadowColor(float value) = 0;
503 
505  virtual void SetFillMode(FillMode mode) = 0;
506 
508  virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) = 0;
509 
511  virtual std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const = 0;
512 
514  virtual CFramebuffer* GetFramebuffer(std::string name) = 0;
515 
517  virtual CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) = 0;
518 
520  virtual void DeleteFramebuffer(std::string name) = 0;
521 
523  virtual bool IsAnisotropySupported() = 0;
524 
526  virtual int GetMaxAnisotropyLevel() = 0;
527 
529  virtual int GetMaxSamples() = 0;
530 
532  virtual bool IsShadowMappingSupported() = 0;
533 
535  virtual int GetMaxTextureSize() = 0;
536 
538  virtual bool IsFramebufferSupported() = 0;
539 };
540 
541 
542 } // namespace Gfx
RenderMode
Render modes the graphics device can be in.
Definition: device.h:172
Draw only points.
Definition: device.h:254
void LoadDefault()
Loads the default values.
Definition: device.h:96
Vertex of a primitive.
Definition: vertex.h:52
Vertex with secondary texture coordinates.
Definition: vertex.h:113
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
This structs contains various capabilities of graphics device.
Definition: device.h:106
FillMode
Polygon fill mode.
Definition: device.h:251
Definition: device.h:304
Texture struct and related enums.
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
FrustumPlane
Planes of frustum space.
Definition: device.h:280
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
Color structs and related functions.
Namespace for (new) math code.
Definition: device.h:39
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
Image loaded from file.
Definition: image.h:54
Cull clockwise faces.
Definition: device.h:232
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
PrimitiveType
Type of primitive to render.
Definition: device.h:265
Draw full polygons.
Definition: device.h:258
const DeviceCapabilities & GetCapabilities()
Returns device capabilities.
Definition: device.h:342
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
Cull counter-clockwise faces.
Definition: device.h:234
RenderState
Render states that can be enabled/disabled.
Definition: device.h:155
Info about a texture.
Definition: texture.h:256
DeviceCapabilities m_capabilities
Definition: device.h:330
3D (3x1) vector
Definition: vector.h:53
TextureUnit
Texture unit values for binding textures.
Definition: device.h:132
2D Point with integer coords
Definition: intpoint.h:41
RGBA color.
Definition: color.h:39
IntPoint struct.
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:143
Draw only lines.
Definition: device.h:256
Abstract interface of graphics device.
Definition: device.h:323
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67
std::string GetError()
Returns last error message or empty string.
Definition: device.h:336
RenderTarget
Render targets for rendering to textures.
Definition: device.h:297