Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.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 
28 #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 <string>
38 
39 
40 class CImage;
41 struct ImageData;
42 
43 
44 // Graphics module namespace
45 namespace Gfx {
46 
54 {
58  int bpp;
60  bool fullScreen;
62  bool resizeable;
64  bool doubleBuf;
66  bool noFrame;
67 
70 
72  inline void LoadDefault()
73  {
74  size = Math::IntPoint(800, 600);
75  bpp = 32;
76  fullScreen = false;
77  resizeable = true;
78  doubleBuf = true;
79  noFrame = false;
80  }
81 };
82 
83 
91 {
92  TRANSFORM_WORLD,
93  TRANSFORM_VIEW,
94  TRANSFORM_PROJECTION,
95  TRANSFORM_SHADOW
96 };
97 
103 {
104  RENDER_STATE_LIGHTING,
105  RENDER_STATE_BLENDING,
106  RENDER_STATE_FOG,
107  RENDER_STATE_DEPTH_TEST,
108  RENDER_STATE_DEPTH_WRITE,
109  RENDER_STATE_ALPHA_TEST,
110  RENDER_STATE_CULLING,
111  RENDER_STATE_DEPTH_BIAS,
112  RENDER_STATE_OFFSCREEN_RENDERING
113 };
114 
120 {
121  COMP_FUNC_NEVER,
122  COMP_FUNC_LESS,
123  COMP_FUNC_EQUAL,
124  COMP_FUNC_NOTEQUAL,
125  COMP_FUNC_LEQUAL,
126  COMP_FUNC_GREATER,
127  COMP_FUNC_GEQUAL,
128  COMP_FUNC_ALWAYS
129 };
130 
136 {
137  BLEND_ZERO,
138  BLEND_ONE,
139  BLEND_SRC_COLOR,
140  BLEND_INV_SRC_COLOR,
141  BLEND_DST_COLOR,
142  BLEND_INV_DST_COLOR,
143  BLEND_SRC_ALPHA,
144  BLEND_INV_SRC_ALPHA,
145  BLEND_DST_ALPHA,
146  BLEND_INV_DST_ALPHA,
147  BLEND_SRC_ALPHA_SATURATE
148 };
149 
155 {
156  FOG_LINEAR,
157  FOG_EXP,
158  FOG_EXP2
159 };
160 
166 {
171 };
172 
178 {
179  SHADE_FLAT,
180  SHADE_SMOOTH
181 };
182 
188 {
195 };
196 
202 {
203  PRIMITIVE_POINTS,
204  PRIMITIVE_LINES,
205  PRIMITIVE_LINE_STRIP,
206  PRIMITIVE_TRIANGLES,
207  PRIMITIVE_TRIANGLE_STRIP
208 };
209 
217 {
218  FRUSTUM_PLANE_LEFT = 0x01,
219  FRUSTUM_PLANE_RIGHT = 0x02,
220  FRUSTUM_PLANE_TOP = 0x04,
221  FRUSTUM_PLANE_BOTTOM = 0x08,
222  FRUSTUM_PLANE_FRONT = 0x10,
223  FRUSTUM_PLANE_BACK = 0x20,
224  FRUSTUM_PLANE_ALL = FRUSTUM_PLANE_LEFT | FRUSTUM_PLANE_RIGHT |
225  FRUSTUM_PLANE_TOP | FRUSTUM_PLANE_BOTTOM |
226  FRUSTUM_PLANE_FRONT | FRUSTUM_PLANE_BACK
227 };
228 
234 {
235  RENDER_TARGET_COLOR,
236  RENDER_TARGET_DEPTH,
237  RENDER_TARGET_STENCIL
238 };
239 
251 class CDevice
252 {
253 public:
254  virtual ~CDevice() {}
255 
257  virtual void DebugHook() = 0;
258 
260  virtual void DebugLights() = 0;
261 
263  virtual bool Create() = 0;
265  virtual void Destroy() = 0;
266 
268  virtual void BeginScene() = 0;
270  virtual void EndScene() = 0;
271 
273  virtual void Clear() = 0;
274 
276  virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0;
277 
279  virtual void SetMaterial(const Material &material) = 0;
280 
282  virtual int GetMaxLightCount() = 0;
284  virtual void SetLight(int index, const Light &light) = 0;
286  virtual void SetLightEnabled(int index, bool enabled) = 0;
287 
289  virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) = 0;
291  virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) = 0;
293  virtual Texture CreateDepthTexture(int width, int height, int depth) = 0;
295  virtual void DestroyTexture(const Texture &texture) = 0;
297  virtual void DestroyAllTextures() = 0;
298 
300  virtual int GetMaxTextureStageCount() = 0;
302  virtual void SetTexture(int index, const Texture &texture) = 0;
304  virtual void SetTexture(int index, unsigned int textureId) = 0;
306  virtual void SetTextureEnabled(int index, bool enabled) = 0;
307 
309  virtual void SetTextureStageParams(int index, const TextureStageParams &params) = 0;
310 
312  virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) = 0;
313 
315  virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) = 0;
316 
318  virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
319  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
321  virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
322  Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
324  virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) = 0;
325 
327  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
328 
330  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
331 
333  virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
334 
336  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) = 0;
337 
339  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) = 0;
340 
342  virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) = 0;
343 
345  virtual void DrawStaticBuffer(unsigned int bufferId) = 0;
346 
348  virtual void DestroyStaticBuffer(unsigned int bufferId) = 0;
349 
352  virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0;
353 
355  virtual void SetViewport(int x, int y, int width, int height) = 0;
356 
358  virtual void SetRenderState(RenderState state, bool enabled) = 0;
359 
361  virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0;
362 
364  virtual void SetDepthTestFunc(CompFunc func) = 0;
365 
367  virtual void SetDepthBias(float factor, float units) = 0;
368 
370  virtual void SetAlphaTestFunc(CompFunc func, float refValue) = 0;
371 
373  virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) = 0;
374 
376  virtual void SetClearColor(const Color &color) = 0;
377 
379  virtual void SetGlobalAmbient(const Color &color) = 0;
380 
382  virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) = 0;
383 
385  virtual void SetCullMode(CullMode mode) = 0;
386 
388  virtual void SetShadeModel(ShadeModel model) = 0;
389 
391  virtual void SetShadowColor(float value) = 0;
392 
394  virtual void SetFillMode(FillMode mode) = 0;
395 
397  virtual void InitOffscreenBuffer(int width, int height) = 0;
398 
400  virtual void SetRenderTexture(RenderTarget target, int texture) = 0;
401 
403  virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) = 0;
404 
406  virtual void* GetFrameBufferPixels() const = 0;
407 };
408 
409 
410 } // namespace Gfx
411 
Draw only points.
Definition: device.h:190
void LoadDefault()
Loads the default values.
Definition: device.h:72
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 void Clear()=0
Clears the screen to blank.
bool noFrame
No window frame (also set with full screen)
Definition: device.h:66
Vertex of a primitive.
Definition: vertex.h:51
virtual void SetShadeModel(ShadeModel model)=0
Sets the shade model.
bool fullScreen
Full screen.
Definition: device.h:60
Vertex with secondary texture coordinates.
Definition: vertex.h:112
virtual void SetLight(int index, const Light &light)=0
Sets the light at given index.
FogMode
Type of fog calculation function.
Definition: device.h:154
4x4 matrix
Definition: matrix.h:66
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:98
Math::IntPoint size
Screen size.
Definition: device.h:56
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:187
virtual void SetDepthTestFunc(CompFunc func)=0
Sets the function of depth test.
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:119
virtual void DestroyStaticBuffer(unsigned int bufferId)=0
Deletes a static buffer.
Parameters for a texture unit.
Definition: texture.h:187
Light struct and related enums.
Material of a surface.
Definition: material.h:44
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius)=0
int bpp
Bits per pixel.
Definition: device.h:58
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.
DeviceConfig()
Constructor calls LoadDefault()
Definition: device.h:69
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:253
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:62
virtual void InitOffscreenBuffer(int width, int height)=0
Initializes offscreen buffer.
FrustumPlane
Planes of frustum space.
Definition: device.h:216
General config for graphics device.
Definition: device.h:53
Properties of light in 3D scene.
Definition: light.h:53
virtual void SetCullMode(CullMode mode)=0
Sets the current cull mode.
virtual void SetRenderTexture(RenderTarget target, int texture)=0
Sets render target to texture.
virtual void SetGlobalAmbient(const Color &color)=0
Sets the global ambient color.
Color structs and related functions.
virtual void Destroy()=0
Destroys the device, releasing every acquired resource.
Parameters for texture creation.
Definition: texture.h:154
ShadeModel
Shade model used in rendering.
Definition: device.h:177
virtual void EndScene()=0
Ends drawing the 3D scene.
BlendFunc
Type of blending function.
Definition: device.h:135
virtual void SetDepthBias(float factor, float units)=0
Sets the depth bias (constant value added to Z-coords)
Vertex structs.
Matrix struct and related functions.
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 void * GetFrameBufferPixels() const =0
Returns the pixels of the entire screen.
Image loaded from file.
Definition: image.h:57
Cull clockwise faces.
Definition: device.h:168
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:201
virtual void DrawStaticBuffer(unsigned int bufferId)=0
Draws a static buffer.
Draw full polygons.
Definition: device.h:194
CullMode
Culling mode for polygons.
Definition: device.h:165
Colored vertex.
Definition: vertex.h:83
virtual void SetMaterial(const Material &material)=0
Sets the current material.
virtual void SetTextureEnabled(int index, bool enabled)=0
Enables/disables the given texture stage.
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:42
Cull counter-clockwise faces.
Definition: device.h:170
virtual void DebugHook()=0
Provides a hook to debug graphics code (implementation-specific)
RenderState
Render states that can be enabled/disabled.
Definition: device.h:102
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:282
bool doubleBuf
Double buffering.
Definition: device.h:64
virtual void SetTextureStageParams(int index, const TextureStageParams &params)=0
Sets the params for texture stage with given index.
3D (3x1) vector
Definition: vector.h:52
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:38
RGBA color.
Definition: color.h:38
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:90
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:192
virtual void SetTexture(int index, const Texture &texture)=0
Sets the texture at given texture stage.
Abstract interface of graphics device.
Definition: device.h:251
RenderTarget
Render targets for rendering to textures.
Definition: device.h:233