2014-10-14 13:11:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
|
|
|
|
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
|
|
|
|
|
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see http://gnu.org/licenses
|
|
|
|
|
*/
|
2012-06-24 13:41:56 +00:00
|
|
|
|
|
2012-08-11 16:39:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file graphics/opengl/gldevice.h
|
2012-09-19 21:50:28 +00:00
|
|
|
|
* \brief OpenGL implementation - CGLDevice class
|
2012-08-11 16:39:16 +00:00
|
|
|
|
*/
|
2012-06-24 13:41:56 +00:00
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-07-26 20:26:19 +00:00
|
|
|
|
#include "graphics/core/device.h"
|
2015-05-19 12:29:31 +00:00
|
|
|
|
#include "graphics/opengl/glutil.h"
|
2015-06-21 16:48:31 +00:00
|
|
|
|
#include "graphics/opengl/glframebuffer.h"
|
2012-06-24 13:41:56 +00:00
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
|
#include <string>
|
2012-06-30 23:37:30 +00:00
|
|
|
|
#include <vector>
|
2012-07-01 20:59:22 +00:00
|
|
|
|
#include <set>
|
2012-12-14 20:30:35 +00:00
|
|
|
|
#include <map>
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
|
// Graphics module namespace
|
2012-06-24 13:41:56 +00:00
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2014-10-23 22:29:26 +00:00
|
|
|
|
/**
|
|
|
|
|
\enum VertexBufferType
|
|
|
|
|
\brief Specifies type of vertex buffer to use
|
|
|
|
|
*/
|
|
|
|
|
enum VertexBufferType
|
|
|
|
|
{
|
|
|
|
|
VBT_DISPLAY_LIST, //! use display lists
|
|
|
|
|
VBT_VBO_CORE, //! use core OpenGL 1.5 VBOs
|
|
|
|
|
VBT_VBO_ARB //! use ARB extension VBOs
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-11 13:21:17 +00:00
|
|
|
|
enum ShadowMappingSupport
|
|
|
|
|
{
|
|
|
|
|
SMS_NONE, //! No support for depth textures
|
|
|
|
|
SMS_ARB, //! ARB extension
|
|
|
|
|
SMS_CORE //! Core support
|
|
|
|
|
};
|
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
|
struct GLDevicePrivate;
|
|
|
|
|
|
2012-06-30 23:37:30 +00:00
|
|
|
|
/**
|
|
|
|
|
\class CGLDevice
|
|
|
|
|
\brief Implementation of CDevice interface in OpenGL
|
|
|
|
|
|
|
|
|
|
Provides the concrete implementation of 3D device in OpenGL.
|
|
|
|
|
|
|
|
|
|
This class should be initialized (by calling Initialize() ) only after
|
|
|
|
|
setting the video mode by CApplication, once the OpenGL context is defined.
|
|
|
|
|
Because of that, CGLDeviceConfig is outside the CDevice class and must be set
|
|
|
|
|
in CApplication.
|
|
|
|
|
*/
|
2012-09-19 21:50:28 +00:00
|
|
|
|
class CGLDevice : public CDevice
|
2012-06-24 13:41:56 +00:00
|
|
|
|
{
|
2012-06-30 23:37:30 +00:00
|
|
|
|
public:
|
2015-06-16 13:31:59 +00:00
|
|
|
|
CGLDevice(const DeviceConfig &config);
|
2012-06-30 23:37:30 +00:00
|
|
|
|
virtual ~CGLDevice();
|
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void DebugHook() override;
|
|
|
|
|
virtual void DebugLights() override;
|
2012-08-12 08:45:04 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual bool Create() override;
|
|
|
|
|
virtual void Destroy() override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void ConfigChanged(const DeviceConfig &newConfig) override;
|
2012-12-14 20:30:35 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void BeginScene() override;
|
|
|
|
|
virtual void EndScene() override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void Clear() override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetTransform(TransformType type, const Math::Matrix &matrix) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetMaterial(const Material &material) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual int GetMaxLightCount() override;
|
|
|
|
|
virtual void SetLight(int index, const Light &light) override;
|
|
|
|
|
virtual void SetLightEnabled(int index, bool enabled) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual Texture CreateTexture(CImage *image, const TextureCreateParams ¶ms) override;
|
|
|
|
|
virtual Texture CreateTexture(ImageData *data, const TextureCreateParams ¶ms) override;
|
|
|
|
|
virtual Texture CreateDepthTexture(int width, int height, int depth) override;
|
|
|
|
|
virtual void DestroyTexture(const Texture &texture) override;
|
|
|
|
|
virtual void DestroyAllTextures() override;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual int GetMaxTextureStageCount() override;
|
|
|
|
|
virtual void SetTexture(int index, const Texture &texture) override;
|
|
|
|
|
virtual void SetTexture(int index, unsigned int textureId) override;
|
|
|
|
|
virtual void SetTextureEnabled(int index, bool enabled) override;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetTextureStageParams(int index, const TextureStageParams ¶ms) override;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) override;
|
|
|
|
|
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams ¶ms) override;
|
2012-09-29 08:40:11 +00:00
|
|
|
|
|
2012-09-30 08:56:35 +00:00
|
|
|
|
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
|
2015-06-25 22:24:32 +00:00
|
|
|
|
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
|
2012-09-30 08:56:35 +00:00
|
|
|
|
virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
|
2015-06-25 22:24:32 +00:00
|
|
|
|
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) override;
|
|
|
|
|
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
|
|
|
|
|
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
|
|
|
|
|
virtual unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
|
|
|
|
|
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override;
|
|
|
|
|
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override;
|
|
|
|
|
virtual void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override;
|
|
|
|
|
virtual void DrawStaticBuffer(unsigned int bufferId) override;
|
|
|
|
|
virtual void DestroyStaticBuffer(unsigned int bufferId) override;
|
2012-12-14 20:30:35 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetViewport(int x, int y, int width, int height) override;
|
2015-05-11 13:21:17 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetRenderState(RenderState state, bool enabled) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
|
2015-05-11 13:21:17 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetDepthTestFunc(CompFunc func) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetDepthBias(float factor, float units) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetAlphaTestFunc(CompFunc func, float refValue) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetClearColor(const Color &color) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetGlobalAmbient(const Color &color) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetCullMode(CullMode mode) override;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetShadeModel(ShadeModel model) override;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetShadowColor(float value) override;
|
2015-05-27 19:58:32 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void SetFillMode(FillMode mode) override;
|
2015-05-11 13:21:17 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void* GetFrameBufferPixels() const override;
|
2014-06-23 22:35:05 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual CFramebuffer* GetFramebuffer(std::string name) override;
|
2015-06-21 16:48:31 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual CFramebuffer* CreateFramebuffer(std::string name, const FramebufferParams& params) override;
|
2015-06-21 16:48:31 +00:00
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
|
virtual void DeleteFramebuffer(std::string name) override;
|
2015-06-21 16:48:31 +00:00
|
|
|
|
|
2012-06-30 23:37:30 +00:00
|
|
|
|
private:
|
2012-07-24 22:27:01 +00:00
|
|
|
|
//! Updates internal modelview matrix
|
2012-07-22 20:05:12 +00:00
|
|
|
|
void UpdateModelviewMatrix();
|
2012-07-26 17:05:09 +00:00
|
|
|
|
//! Updates position for given light based on transformation matrices
|
|
|
|
|
void UpdateLightPosition(int index);
|
2013-05-26 09:34:53 +00:00
|
|
|
|
//! Updates the texture params for given texture stage
|
|
|
|
|
void UpdateTextureParams(int index);
|
2012-07-22 20:05:12 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2012-07-29 13:09:53 +00:00
|
|
|
|
//! Current config
|
2015-06-16 13:31:59 +00:00
|
|
|
|
DeviceConfig m_config;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2012-06-30 23:37:30 +00:00
|
|
|
|
//! Current world matrix
|
|
|
|
|
Math::Matrix m_worldMat;
|
|
|
|
|
//! Current view matrix
|
|
|
|
|
Math::Matrix m_viewMat;
|
2012-07-01 20:59:22 +00:00
|
|
|
|
//! OpenGL modelview matrix = world matrix * view matrix
|
|
|
|
|
Math::Matrix m_modelviewMat;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
//! Current projection matrix
|
|
|
|
|
Math::Matrix m_projectionMat;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2012-06-30 23:37:30 +00:00
|
|
|
|
//! The current material
|
2012-09-19 21:50:28 +00:00
|
|
|
|
Material m_material;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2012-07-26 17:05:09 +00:00
|
|
|
|
//! Whether lighting is enabled
|
|
|
|
|
bool m_lighting;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
//! Current lights
|
2012-09-19 21:50:28 +00:00
|
|
|
|
std::vector<Light> m_lights;
|
2012-06-30 23:37:30 +00:00
|
|
|
|
//! Current lights enable status
|
|
|
|
|
std::vector<bool> m_lightsEnabled;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
|
|
|
|
//! Current textures; \c NULL value means unassigned
|
2012-09-19 21:50:28 +00:00
|
|
|
|
std::vector<Texture> m_currentTextures;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
//! Current texture stages enable status
|
|
|
|
|
std::vector<bool> m_texturesEnabled;
|
|
|
|
|
//! Current texture params
|
2012-09-19 21:50:28 +00:00
|
|
|
|
std::vector<TextureStageParams> m_textureStageParams;
|
2012-07-03 22:04:53 +00:00
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
|
//! Set of all created textures
|
2012-09-19 21:50:28 +00:00
|
|
|
|
std::set<Texture> m_allTextures;
|
2012-12-14 20:30:35 +00:00
|
|
|
|
|
2015-06-21 16:48:31 +00:00
|
|
|
|
//! Map of framebuffers
|
|
|
|
|
std::map<std::string, CFramebuffer*> m_framebuffers;
|
|
|
|
|
|
2012-12-14 20:30:35 +00:00
|
|
|
|
//! Type of vertex structure
|
|
|
|
|
enum VertexType
|
|
|
|
|
{
|
|
|
|
|
VERTEX_TYPE_NORMAL,
|
|
|
|
|
VERTEX_TYPE_TEX2,
|
|
|
|
|
VERTEX_TYPE_COL,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//! Info about static VBO buffers
|
|
|
|
|
struct VboObjectInfo
|
|
|
|
|
{
|
|
|
|
|
PrimitiveType primitiveType;
|
|
|
|
|
unsigned int bufferId;
|
|
|
|
|
VertexType vertexType;
|
|
|
|
|
int vertexCount;
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-11 13:21:17 +00:00
|
|
|
|
//! Detected capabilities
|
|
|
|
|
//! OpenGL version
|
|
|
|
|
int m_glMajor, m_glMinor;
|
|
|
|
|
//! Depth texture support
|
|
|
|
|
ShadowMappingSupport m_shadowMappingSupport;
|
2015-05-21 14:47:24 +00:00
|
|
|
|
//! Shadow ambient support
|
|
|
|
|
bool m_shadowAmbientSupported;
|
2013-01-05 22:03:06 +00:00
|
|
|
|
//! Whether to use multitexturing
|
|
|
|
|
bool m_multitextureAvailable;
|
2012-12-14 20:30:35 +00:00
|
|
|
|
//! Whether to use VBOs or display lists
|
2013-01-05 22:03:06 +00:00
|
|
|
|
bool m_vboAvailable;
|
2015-05-06 15:55:10 +00:00
|
|
|
|
//! Whether anisotropic filtering is available
|
|
|
|
|
bool m_anisotropyAvailable;
|
|
|
|
|
//! Maximum anisotropy level
|
|
|
|
|
int m_maxAnisotropy;
|
2015-06-21 16:48:31 +00:00
|
|
|
|
//! Framebuffer support
|
|
|
|
|
FramebufferSupport m_framebufferSupport;
|
2014-10-23 22:29:26 +00:00
|
|
|
|
//! Which vertex buffer type to use
|
|
|
|
|
VertexBufferType m_vertexBufferType;
|
2012-12-14 20:30:35 +00:00
|
|
|
|
//! Map of saved VBO objects
|
|
|
|
|
std::map<unsigned int, VboObjectInfo> m_vboObjects;
|
|
|
|
|
//! Last ID of VBO object
|
|
|
|
|
unsigned int m_lastVboId;
|
2012-06-24 13:41:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
|
|
|
|
|
|
} // namespace Gfx
|
2013-05-26 15:47:54 +00:00
|
|
|
|
|