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-2015, 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"
29 #include "graphics/core/light.h"
30 #include "graphics/core/material.h"
31 #include "graphics/core/texture.h"
32 #include "graphics/core/vertex.h"
33 
34 #include "math/intpoint.h"
35 #include "math/matrix.h"
36 
37 #include <memory>
38 #include <string>
39 
40 
41 class CImage;
42 struct ImageData;
43 
44 
45 // Graphics module namespace
46 namespace Gfx
47 {
48 
56 {
60  int bpp = 32;
62  bool fullScreen = false;
64  bool resizeable = true;
66  bool doubleBuf = true;
68  bool noFrame = false;
69 
71  int redSize = 8;
73  int greenSize = 8;
75  int blueSize = 8;
77  int alphaSize = 8;
79  int depthSize = 24;
81  int stencilSize = 8;
82 
84  bool hardwareAccel = true;
85 
87  void LoadDefault()
88  {
89  *this = DeviceConfig();
90  }
91 };
92 
93 
101 {
102  TRANSFORM_WORLD,
103  TRANSFORM_VIEW,
104  TRANSFORM_PROJECTION,
105  TRANSFORM_SHADOW
106 };
107 
113 {
114  RENDER_STATE_LIGHTING,
115  RENDER_STATE_BLENDING,
116  RENDER_STATE_FOG,
117  RENDER_STATE_DEPTH_TEST,
118  RENDER_STATE_DEPTH_WRITE,
119  RENDER_STATE_ALPHA_TEST,
120  RENDER_STATE_CULLING,
121  RENDER_STATE_DEPTH_BIAS,
122 };
123 
129 {
130  COMP_FUNC_NEVER,
131  COMP_FUNC_LESS,
132  COMP_FUNC_EQUAL,
133  COMP_FUNC_NOTEQUAL,
134  COMP_FUNC_LEQUAL,
135  COMP_FUNC_GREATER,
136  COMP_FUNC_GEQUAL,
137  COMP_FUNC_ALWAYS
138 };
139 
145 {
146  BLEND_ZERO,
147  BLEND_ONE,
148  BLEND_SRC_COLOR,
149  BLEND_INV_SRC_COLOR,
150  BLEND_DST_COLOR,
151  BLEND_INV_DST_COLOR,
152  BLEND_SRC_ALPHA,
153  BLEND_INV_SRC_ALPHA,
154  BLEND_DST_ALPHA,
155  BLEND_INV_DST_ALPHA,
156  BLEND_SRC_ALPHA_SATURATE
157 };
158 
164 {
165  FOG_LINEAR,
166  FOG_EXP,
167  FOG_EXP2
168 };
169 
175 {
180 };
181 
187 {
188  SHADE_FLAT,
189  SHADE_SMOOTH
190 };
191 
197 {
204 };
205 
211 {
212  PRIMITIVE_POINTS,
213  PRIMITIVE_LINES,
214  PRIMITIVE_LINE_STRIP,
215  PRIMITIVE_TRIANGLES,
216  PRIMITIVE_TRIANGLE_STRIP
217 };
218 
226 {
227  FRUSTUM_PLANE_LEFT = 0x01,
228  FRUSTUM_PLANE_RIGHT = 0x02,
229  FRUSTUM_PLANE_TOP = 0x04,
230  FRUSTUM_PLANE_BOTTOM = 0x08,
231  FRUSTUM_PLANE_FRONT = 0x10,
232  FRUSTUM_PLANE_BACK = 0x20,
233  FRUSTUM_PLANE_ALL = FRUSTUM_PLANE_LEFT | FRUSTUM_PLANE_RIGHT |
234  FRUSTUM_PLANE_TOP | FRUSTUM_PLANE_BOTTOM |
235  FRUSTUM_PLANE_FRONT | FRUSTUM_PLANE_BACK
236 };
237 
243 {
244  RENDER_TARGET_COLOR,
245  RENDER_TARGET_DEPTH,
246  RENDER_TARGET_STENCIL
247 };
248 
250 {
251 public:
252  virtual ~CFrameBufferPixels() {}
253 
254  virtual void* GetPixelsData() = 0;
255 };
256 
268 class CDevice
269 {
270 public:
271  virtual ~CDevice() {}
272 
274  virtual void DebugHook() = 0;
275 
277  virtual void DebugLights() = 0;
278 
280  virtual bool Create() = 0;
282  virtual void Destroy() = 0;
283 
285  virtual void ConfigChanged(const DeviceConfig &newConfig) = 0;
286 
288  virtual void BeginScene() = 0;
290  virtual void EndScene() = 0;
291 
293  virtual void Clear() = 0;
294 
296  virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0;
297 
299  virtual void SetMaterial(const Material &material) = 0;
300 
302  virtual int GetMaxLightCount() = 0;
304  virtual void SetLight(int index, const Light &light) = 0;
306  virtual void SetLightEnabled(int index, bool enabled) = 0;
307 
309  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) = 0;
311  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) = 0;
313  virtual Texture CreateDepthTexture(int width, int height, int depth) = 0;
315  virtual void DestroyTexture(const Texture &texture) = 0;
317  virtual void DestroyAllTextures() = 0;
318 
320  virtual int GetMaxTextureStageCount() = 0;
322  virtual void SetTexture(int index, const Texture &texture) = 0;
324  virtual void SetTexture(int index, unsigned int textureId) = 0;
326  virtual void SetTextureEnabled(int index, bool enabled) = 0;
327 
329  virtual void SetTextureStageParams(int index, const TextureStageParams &params) = 0;
330 
332  virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) = 0;
333 
335  virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) = 0;
336 
338  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
339  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
341  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
342  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
344  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) = 0;
345 
347  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
348 
350  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
351 
353  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
354 
356  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
357 
359  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
360 
362  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
363 
365  virtual void DrawStaticBuffer(unsigned int bufferId) = 0;
366 
368  virtual void DestroyStaticBuffer(unsigned int bufferId) = 0;
369 
372  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0;
373 
375  virtual void SetViewport(int x, int y, int width, int height) = 0;
376 
378  virtual void SetRenderState(RenderState state, bool enabled) = 0;
379 
381  virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0;
382 
384  virtual void SetDepthTestFunc(CompFunc func) = 0;
385 
387  virtual void SetDepthBias(float factor, float units) = 0;
388 
390  virtual void SetAlphaTestFunc(CompFunc func, float refValue) = 0;
391 
393  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) = 0;
394 
396  virtual void SetClearColor(const Color &color) = 0;
397 
399  virtual void SetGlobalAmbient(const Color &color) = 0;
400 
402  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) = 0;
403 
405  virtual void SetCullMode(CullMode mode) = 0;
406 
408  virtual void SetShadeModel(ShadeModel model) = 0;
409 
411  virtual void SetShadowColor(float value) = 0;
412 
414  virtual void SetFillMode(FillMode mode) = 0;
415 
417  virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) = 0;
418 
420  virtual std::unique_ptr<CFrameBufferPixels> GetFrameBufferPixels() const = 0;
421 
423  virtual CFramebuffer* GetFramebuffer(std::string name) = 0;
424 
426  virtual CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) = 0;
427 
429  virtual void DeleteFramebuffer(std::string name) = 0;
430 
432  virtual bool IsAnisotropySupported() = 0;
433 
435  virtual int GetMaxAnisotropyLevel() = 0;
436 
438  virtual int GetMaxSamples() = 0;
439 
441  virtual bool IsShadowMappingSupported() = 0;
442 
444  virtual int GetMaxTextureSize() = 0;
445 
447  virtual bool IsFramebufferSupported() = 0;
448 };
449 
450 
451 } // namespace Gfx
Abstract representation of framebuffer and offscreen buffers.
Draw only points.
Definition: device.h:199
void LoadDefault()
Loads the default values.
Definition: device.h:87
virtual void ConfigChanged(const DeviceConfig &newConfig)=0
Changes configuration.
Material struct.
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount, Color color=Color(1.0f, 1.0f, 1.0f, 1.0f))=0
Renders primitive composed of vertices with single texture.
virtual bool IsAnisotropySupported()=0
Checks if anisotropy is supported.
virtual void Clear()=0
Clears the screen to blank.
bool noFrame
No window frame (also set with full screen)
Definition: device.h:68
Vertex of a primitive.
Definition: vertex.h:52
virtual void SetShadeModel(ShadeModel model)=0
Sets the shade model.
bool fullScreen
Full screen.
Definition: device.h:62
Vertex with secondary texture coordinates.
Definition: vertex.h:113
virtual void SetLight(int index, const Light &light)=0
Sets the light at given index.
FogMode
Type of fog calculation function.
Definition: device.h:163
int greenSize
Size of green channel in bits.
Definition: device.h:73
virtual int GetMaxTextureSize()=0
Returns max texture size supported.
4x4 matrix
Definition: matrix.h:65
virtual void DestroyTexture(const Texture &texture)=0
Deletes a given texture, freeing it from video memory.
TexWrapMode
Wrapping mode for texture coords.
Definition: texture.h:99
Math::IntPoint size
Screen size.
Definition: device.h:58
virtual void SetRenderState(RenderState state, bool enabled)=0
Enables/disables the given render state.
virtual void DestroyAllTextures()=0
Deletes all textures created so far.
FillMode
Polygon fill mode.
Definition: device.h:196
virtual void SetDepthTestFunc(CompFunc func)=0
Sets the function of depth test.
Definition: device.h:249
virtual void SetFillMode(FillMode mode)=0
Sets the current fill mode.
Texture struct and related enums.
CompFunc
Type of function used to compare values.
Definition: device.h:128
virtual void DestroyStaticBuffer(unsigned int bufferId)=0
Deletes a static buffer.
Parameters for a texture unit.
Definition: texture.h:180
Light struct and related enums.
Material of a surface.
Definition: material.h:45
int depthSize
Color depth in bits.
Definition: device.h:79
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius)=0
int bpp
Bits per pixel.
Definition: device.h:60
virtual void CopyFramebufferToTexture(Texture &texture, int xOffset, int yOffset, int x, int y, int width, int height)=0
Copies content of framebuffer to texture.
virtual void SetAlphaTestFunc(CompFunc func, float refValue)=0
Sets the alpha test function and reference value.
virtual void DeleteFramebuffer(std::string name)=0
Deletes framebuffer.
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex *vertices, int vertexCount)=0
Creates a static buffer composed of given primitives with single texture vertices.
virtual void DebugLights()=0
Displays light positions to aid in debuggings.
Parameters for texture coordinate generation.
Definition: texture.h:234
virtual Texture CreateDepthTexture(int width, int height, int depth)=0
Creates a depth texture with specific dimensions and depth.
bool resizeable
Resizeable window.
Definition: device.h:64
FrustumPlane
Planes of frustum space.
Definition: device.h:225
General config for graphics device.
Definition: device.h:55
Properties of light in 3D scene.
Definition: light.h:54
virtual void SetCullMode(CullMode mode)=0
Sets the current cull mode.
virtual CFramebuffer * GetFramebuffer(std::string name)=0
Returns framebuffer with given name or nullptr if it doesn't exist.
virtual void SetGlobalAmbient(const Color &color)=0
Sets the global ambient color.
virtual bool IsShadowMappingSupported()=0
Checks if shadow mapping is supported.
Color structs and related functions.
virtual CFramebuffer * CreateFramebuffer(std::string name, const FramebufferParams &params)=0
Creates new framebuffer with given name or nullptr if it's not possible.
virtual void Destroy()=0
Destroys the device, releasing every acquired resource.
Parameters for texture creation.
Definition: texture.h:155
ShadeModel
Shade model used in rendering.
Definition: device.h:186
virtual void EndScene()=0
Ends drawing the 3D scene.
BlendFunc
Type of blending function.
Definition: device.h:144
virtual void SetDepthBias(float factor, float units)=0
Sets the depth bias (constant value added to Z-coords)
Vertex structs.
int alphaSize
Size of alpha channel in bits.
Definition: device.h:77
Matrix struct and related functions.
int blueSize
Size of blue channel in bits.
Definition: device.h:75
virtual int GetMaxAnisotropyLevel()=0
Returns max anisotropy level supported.
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params)=0
Sets the texture coordinate generation mode for given texture unit.
virtual void BeginScene()=0
Begins drawing the 3D scene.
virtual bool IsFramebufferSupported()=0
Checks if framebuffers are supported.
Image loaded from file.
Definition: image.h:54
Cull clockwise faces.
Definition: device.h:177
Contains parameters for new framebuffer.
Definition: framebuffer.h:34
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density)=0
Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) ...
virtual void SetTransform(TransformType type, const Math::Matrix &matrix)=0
Sets the transform matrix of given type.
PrimitiveType
Type of primitive to render.
Definition: device.h:210
virtual void DrawStaticBuffer(unsigned int bufferId)=0
Draws a static buffer.
Draw full polygons.
Definition: device.h:203
CullMode
Culling mode for polygons.
Definition: device.h:174
Colored vertex.
Definition: vertex.h:84
virtual void SetMaterial(const Material &material)=0
Sets the current material.
int stencilSize
Stencil depth in bits.
Definition: device.h:81
virtual void SetTextureEnabled(int index, bool enabled)=0
Enables/disables the given texture stage.
int redSize
Size of red channel in bits.
Definition: device.h:71
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha)=0
Sets the color mask.
virtual void SetViewport(int x, int y, int width, int height)=0
Changes rendering viewport.
Namespace for (new) graphics code.
Definition: app.h:49
Implementation-specific image data.
Definition: image.h:41
Cull counter-clockwise faces.
Definition: device.h:179
virtual void DebugHook()=0
Provides a hook to debug graphics code (implementation-specific)
RenderState
Render states that can be enabled/disabled.
Definition: device.h:112
virtual bool Create()=0
Initializes the device, setting the initial state.
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex *vertices, int vertexCount)=0
Updates the static buffer composed of given primitives with single texture vertices.
virtual void SetShadowColor(float value)=0
Sets shadow color.
virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT)=0
Sets only the texture wrap modes (for faster than thru stage params)
Info about a texture.
Definition: texture.h:256
virtual std::unique_ptr< CFrameBufferPixels > GetFrameBufferPixels() const =0
Returns the pixels of the entire screen.
bool doubleBuf
Double buffering.
Definition: device.h:66
virtual void SetTextureStageParams(int index, const TextureStageParams &params)=0
Sets the params for texture stage with given index.
3D (3x1) vector
Definition: vector.h:53
bool hardwareAccel
Force hardware acceleration (video mode set will fail on lack of hw accel)
Definition: device.h:84
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend)=0
Sets the blending functions for source and destination operations.
2D Point with integer coords
Definition: intpoint.h:39
RGBA color.
Definition: color.h:39
virtual void SetLightEnabled(int index, bool enabled)=0
Enables/disables the light at given index.
IntPoint struct.
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params)=0
Creates a texture from image; the image can be safely removed after that.
TransformType
Type of transformation in rendering pipeline.
Definition: device.h:100
virtual int GetMaxLightCount()=0
Returns the maximum number of lights available.
virtual void SetClearColor(const Color &color)=0
Sets the clear color.
virtual int GetMaxTextureStageCount()=0
Returns the maximum number of multitexture stages.
Draw only lines.
Definition: device.h:201
virtual void SetTexture(int index, const Texture &texture)=0
Sets the texture at given texture stage.
virtual int GetMaxSamples()=0
Returns max samples supported.
Abstract interface of graphics device.
Definition: device.h:268
Abstract interface of default framebuffer and offscreen framebuffers.
Definition: framebuffer.h:67
RenderTarget
Render targets for rendering to textures.
Definition: device.h:242