Added OpenGL 3.3 graphics implementation

master
Tomasz Kapuściński 2015-05-19 14:29:31 +02:00
parent b2e781b1e9
commit 933609967a
13 changed files with 2570 additions and 216 deletions

View File

@ -110,6 +110,8 @@ set(BASE_SOURCES
graphics/engine/text.cpp
graphics/engine/water.cpp
graphics/opengl/gldevice.cpp
graphics/opengl/gl33device.cpp
graphics/opengl/glutil.cpp
object/auto/auto.cpp
object/auto/autobase.cpp
object/auto/autoconvert.cpp

View File

@ -36,6 +36,7 @@
#include "graphics/engine/modelmanager.h"
#include "graphics/core/nulldevice.h"
#include "graphics/opengl/gldevice.h"
#include "graphics/opengl/gl33device.h"
#include "object/robotmain.h"
#include "object/objman.h"
@ -162,6 +163,8 @@ CApplication::CApplication()
m_language = LANGUAGE_ENV;
m_lowCPU = true;
m_graphics = "opengl";
}
CApplication::~CApplication()
@ -224,7 +227,8 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
OPT_MOD,
OPT_VBO,
OPT_RESOLUTION,
OPT_HEADLESS
OPT_HEADLESS,
OPT_DEVICE
};
option options[] =
@ -242,6 +246,7 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
{ "vbo", required_argument, nullptr, OPT_VBO },
{ "resolution", required_argument, nullptr, OPT_RESOLUTION },
{ "headless", no_argument, nullptr, OPT_HEADLESS },
{ "graphics", required_argument, nullptr, OPT_DEVICE },
{ nullptr, 0, nullptr, 0}
};
@ -285,6 +290,7 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
GetLogger()->Message(" -vbo mode set OpenGL VBO mode (one of: auto, enable, disable)\n");
GetLogger()->Message(" -resolution WxH set resolution\n");
GetLogger()->Message(" -headless headless mode - disables graphics, sound and user interaction\n");
GetLogger()->Message(" -graphics changes graphics device (defaults to opengl)\n");
return PARSE_ARGS_HELP;
}
case OPT_DEBUG:
@ -404,6 +410,11 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
m_headless = true;
break;
}
case OPT_DEVICE:
{
m_graphics = optarg;
break;
}
default:
assert(false); // should never get here
}
@ -549,12 +560,24 @@ bool CApplication::Create()
// Don't generate joystick events
SDL_JoystickEventState(SDL_IGNORE);
if(!m_headless) {
if(!m_headless)
{
// The video is ready, we can create and initalize the graphics device
m_device = new Gfx::CGLDevice(m_deviceConfig);
} else {
if (m_graphics == "opengl")
m_device = new Gfx::CGLDevice(m_deviceConfig);
else if (m_graphics == "gl33")
m_device = new Gfx::CGL33Device(m_deviceConfig);
else
{
m_device = new Gfx::CNullDevice();
GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str());
}
}
else
{
m_device = new Gfx::CNullDevice();
}
if (! m_device->Create() )
{
m_errorMessage = std::string("Error in CDevice::Create()\n") + standardInfoMessage;

View File

@ -425,6 +425,9 @@ protected:
bool m_simulationSuspended;
//@}
//! Graphics device to use
std::string m_graphics;
//! Current mode of mouse
MouseMode m_mouseMode;

View File

@ -91,7 +91,8 @@ enum TransformType
{
TRANSFORM_WORLD,
TRANSFORM_VIEW,
TRANSFORM_PROJECTION
TRANSFORM_PROJECTION,
TRANSFORM_SHADOW
};
/**
@ -302,9 +303,6 @@ public:
//! Sets the texture coordinate generation mode for given texture unit
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) = 0;
//! Sets texture coordinate transform matrix
virtual void SetTextureMatrix(int index, Math::Matrix& matrix) = 0;
//! Renders primitive composed of vertices with single texture
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;

View File

@ -182,10 +182,6 @@ void CNullDevice::SetTextureCoordGeneration(int index, TextureGenerationParams &
{
}
void CNullDevice::SetTextureMatrix(int index, Math::Matrix& matrix)
{
}
TextureStageParams CNullDevice::GetTextureStageParams(int index)
{
return TextureStageParams();

View File

@ -83,7 +83,6 @@ public:
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT);
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params);
virtual void SetTextureMatrix(int index, Math::Matrix& matrix);
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f));

View File

@ -2212,7 +2212,7 @@ void CEngine::SetState(int state, const Color& color)
if (state & ENG_RSTATE_FOG)
m_device->SetRenderState(RENDER_STATE_FOG, true);
bool second = m_groundSpotVisible || m_dirty;
@ -3255,7 +3255,7 @@ void CEngine::Draw3DScene()
// Texture Unit 2
m_device->SetTextureEnabled(2, true);
m_device->SetTexture(2, m_shadowMap);
m_device->SetTextureMatrix(2, m_shadowTextureMat);
m_device->SetTransform(TRANSFORM_SHADOW, m_shadowTextureMat);
Math::Matrix identity;
identity.LoadIdentity();
@ -3338,7 +3338,7 @@ void CEngine::Draw3DScene()
// Texture Unit 2
m_device->SetTextureEnabled(2, true);
m_device->SetTexture(2, m_shadowMap);
m_device->SetTextureMatrix(2, m_shadowTextureMat);
m_device->SetTransform(TRANSFORM_SHADOW, m_shadowTextureMat);
Math::Matrix identity;
identity.LoadIdentity();
@ -3427,7 +3427,7 @@ void CEngine::Draw3DScene()
m_device->SetTexture(2, 0);
m_device->SetTextureEnabled(2, false);
m_device->SetTextureMatrix(2, identity);
m_device->SetTransform(TRANSFORM_SHADOW, identity);
TextureGenerationParams params;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,300 @@
/*
* 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
*/
/**
* \file graphics/opengl/gl33device.h
* \brief OpenGL 3.3 implementation - CGL33Device class
*/
#pragma once
#include "graphics/core/device.h"
#include "graphics/opengl/glutil.h"
#include "GL/glew.h"
#include <string>
#include <vector>
#include <set>
#include <map>
// Graphics module namespace
namespace Gfx {
/**
\class CGL33Device
\brief Implementation of CDevice interface in OpenGL 3.3
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.
*/
class CGL33Device : public CDevice
{
public:
CGL33Device(const GLDeviceConfig &config);
virtual ~CGL33Device();
virtual void DebugHook() OVERRIDE;
virtual void DebugLights() OVERRIDE;
virtual bool Create() OVERRIDE;
virtual void Destroy() OVERRIDE;
void ConfigChanged(const GLDeviceConfig &newConfig);
virtual void BeginScene() OVERRIDE;
virtual void EndScene() OVERRIDE;
virtual void Clear() OVERRIDE;
virtual void SetTransform(TransformType type, const Math::Matrix &matrix) OVERRIDE;
virtual void SetMaterial(const Material &material) OVERRIDE;
virtual int GetMaxLightCount() OVERRIDE;
virtual void SetLight(int index, const Light &light) OVERRIDE;
virtual void SetLightEnabled(int index, bool enabled) OVERRIDE;
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) OVERRIDE;
virtual Texture CreateTexture(ImageData *data, const TextureCreateParams &params) OVERRIDE;
virtual Texture CreateDepthTexture(int width, int height, int depth) OVERRIDE;
virtual void DestroyTexture(const Texture &texture) OVERRIDE;
virtual void DestroyAllTextures() OVERRIDE;
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;
virtual void SetTextureStageParams(int index, const TextureStageParams &params) OVERRIDE;
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) OVERRIDE;
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE;
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) OVERRIDE;
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;
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) OVERRIDE;
virtual void SetViewport(int x, int y, int width, int height) OVERRIDE;
virtual void SetRenderState(RenderState state, bool enabled) OVERRIDE;
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) OVERRIDE;
virtual void SetDepthTestFunc(CompFunc func) OVERRIDE;
virtual void SetDepthBias(float factor, float units) OVERRIDE;
virtual void SetAlphaTestFunc(CompFunc func, float refValue) OVERRIDE;
virtual void SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend) OVERRIDE;
virtual void SetClearColor(const Color &color) OVERRIDE;
virtual void SetGlobalAmbient(const Color &color) OVERRIDE;
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) OVERRIDE;
virtual void SetCullMode(CullMode mode) OVERRIDE;
virtual void SetShadeModel(ShadeModel model) OVERRIDE;
virtual void SetFillMode(FillMode mode) OVERRIDE;
virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
virtual void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) OVERRIDE;
virtual void* GetFrameBufferPixels() const OVERRIDE;
private:
//! Updates position for given light based on transformation matrices
void UpdateLightPosition(int index);
//! Updates the texture params for given texture stage
void UpdateTextureParams(int index);
//! Updates rendering mode
void UpdateRenderingMode();
private:
//! Current config
GLDeviceConfig m_config;
//! Current world matrix
Math::Matrix m_worldMat;
//! Current view matrix
Math::Matrix m_viewMat;
//! OpenGL modelview matrix = world matrix * view matrix
Math::Matrix m_modelviewMat;
//! Current projection matrix
Math::Matrix m_projectionMat;
//! The current material
Material m_material;
//! Whether lighting is enabled
bool m_lighting;
//! Current lights
std::vector<Light> m_lights;
//! Current lights enable status
std::vector<bool> m_lightsEnabled;
//! Current textures; \c NULL value means unassigned
std::vector<Texture> m_currentTextures;
//! Current texture stages enable status
std::vector<bool> m_texturesEnabled;
//! Current texture params
std::vector<TextureStageParams> m_textureStageParams;
//! Set of all created textures
std::set<Texture> m_allTextures;
//! Type of vertex structure
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
//! Info about static VBO buffers
struct VertexBufferInfo
{
PrimitiveType primitiveType;
GLuint vbo;
GLuint vao;
VertexType vertexType;
int vertexCount;
unsigned int size;
};
//! Detected capabilities
//! OpenGL version
int m_glMajor, m_glMinor;
//! Whether anisotropic filtering is available
bool m_anisotropyAvailable;
//! Maximum anisotropy level
int m_maxAnisotropy;
//! Map of saved VBO objects
std::map<unsigned int, VertexBufferInfo> m_vboObjects;
//! Last ID of VBO object
unsigned int m_lastVboId;
// Offscreen buffer
//! Framebuffer object
GLuint m_framebuffer;
//! Color renderbuffer
GLuint m_colorBuffer;
//! Depth renderbuffer
GLuint m_depthBuffer;
//! Maximum available renderbuffer size
int m_maxRenderbufferSize;
//! Shader program
GLuint m_shaderProgram;
//! Auxilliary vertex buffers for general rendering
unsigned int m_vertex;
unsigned int m_vertexTex2;
unsigned int m_vertexCol;
// Uniforms
//! Projection matrix
GLint uni_ProjectionMatrix;
//! View matrix
GLint uni_ViewMatrix;
//! Model matrix
GLint uni_ModelMatrix;
//! Shadow matrix
GLint uni_ShadowMatrix;
//! Normal matrix
GLint uni_NormalMatrix;
//! Primary texture sampler
GLint uni_PrimaryTexture;
//! Secondary texture sampler
GLint uni_SecondaryTexture;
//! Shadow texture sampler
GLint uni_ShadowTexture;
GLint uni_PrimaryTextureEnabled;
GLint uni_SecondaryTextureEnabled;
GLint uni_ShadowTextureEnabled;
// Fog parameters
//! true enables fog
GLint uni_FogEnabled;
//! Fog range
GLint uni_FogRange;
//! Fog color
GLint uni_FogColor;
// Alpha test parameters
//! true enables alpha test
GLint uni_AlphaTestEnabled;
//! Alpha test reference value
GLint uni_AlphaReference;
// Lighting parameters
GLint uni_SmoothShading;
//! true enables lighting
GLint uni_LightingEnabled;
//! Ambient color
GLint uni_AmbientColor;
//! Diffuse color
GLint uni_DiffuseColor;
//! Specular color
GLint uni_SpecularColor;
struct
{
//! true enables light
GLint Enabled;
//! Light type
GLint Type;
//! Position or direction vector
GLint Position;
//! Ambient color
GLint Ambient;
//! Diffuse color
GLint Diffuse;
//! Specular color
GLint Specular;
//! Attenuation
GLint Attenuation;
} uni_Light[8];
};
} // namespace Gfx

View File

@ -39,29 +39,6 @@
// Graphics module namespace
namespace Gfx {
GLDeviceConfig::GLDeviceConfig()
{
LoadDefault();
}
void GLDeviceConfig::LoadDefault()
{
DeviceConfig::LoadDefault();
hardwareAccel = true;
redSize = 8;
blueSize = 8;
greenSize = 8;
alphaSize = 8;
depthSize = 24;
vboMode = VBO_MODE_AUTO;
}
GLuint textureCoordinates[] = { GL_S, GL_T, GL_R, GL_Q };
GLuint textureCoordGen[] = { GL_TEXTURE_GEN_S, GL_TEXTURE_GEN_T, GL_TEXTURE_GEN_R, GL_TEXTURE_GEN_Q };
CGLDevice::CGLDevice(const GLDeviceConfig &config)
{
m_config = config;
@ -195,7 +172,7 @@ void CGLDevice::DebugLights()
bool CGLDevice::Create()
{
GetLogger()->Info("Creating CDevice\n");
GetLogger()->Info("Creating CDevice - OpenGL 1.4\n");
/*static*/ bool glewInited = false;
@ -416,6 +393,16 @@ void CGLDevice::SetTransform(TransformType type, const Math::Matrix &matrix)
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(m_projectionMat.Array());
}
else if (type == TRANSFORM_SHADOW)
{
if (!m_multitextureAvailable)
return;
Math::Matrix temp = matrix;
glActiveTexture(GL_TEXTURE2);
glMatrixMode(GL_TEXTURE);
glLoadMatrixf(temp.Array());
}
else
{
assert(false);
@ -951,47 +938,37 @@ void CGLDevice::SetTextureCoordGeneration(int index, TextureGenerationParams &pa
GLuint texCoordGen = textureCoordGen[i];
GLuint texCoord = textureCoordinates[i];
if (params.coords[i].mode == TEX_GEN_NONE)
switch (params.coords[i].mode)
{
case TEX_GEN_NONE:
glDisable(texCoordGen);
}
else
{
break;
case TEX_GEN_OBJECT_LINEAR:
glEnable(texCoordGen);
switch (params.coords[i].mode)
{
case TEX_GEN_OBJECT_LINEAR:
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(texCoord, GL_OBJECT_PLANE, params.coords[i].plane);
break;
case TEX_GEN_EYE_LINEAR:
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(texCoord, GL_EYE_PLANE, params.coords[i].plane);
break;
case TEX_GEN_SPHERE_MAP:
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
break;
case TEX_GEN_NORMAL_MAP:
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
break;
case TEX_GEN_REFLECTION_MAP:
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
break;
}
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(texCoord, GL_OBJECT_PLANE, params.coords[i].plane);
break;
case TEX_GEN_EYE_LINEAR:
glEnable(texCoordGen);
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(texCoord, GL_EYE_PLANE, params.coords[i].plane);
break;
case TEX_GEN_SPHERE_MAP:
glEnable(texCoordGen);
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
break;
case TEX_GEN_NORMAL_MAP:
glEnable(texCoordGen);
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
break;
case TEX_GEN_REFLECTION_MAP:
glEnable(texCoordGen);
glTexGeni(texCoord, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
break;
}
}
}
void CGLDevice::SetTextureMatrix(int index, Math::Matrix& matrix)
{
if (!m_multitextureAvailable && index != 0)
return;
glMatrixMode(GL_TEXTURE);
glLoadMatrixf(matrix.Array());
}
void CGLDevice::UpdateTextureParams(int index)
{
assert(index >= 0 && index < static_cast<int>( m_currentTextures.size() ));
@ -1200,21 +1177,6 @@ void CGLDevice::SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wr
else assert(false);
}
GLenum TranslateGfxPrimitive(PrimitiveType type)
{
GLenum flag = 0;
switch (type)
{
case PRIMITIVE_POINTS: flag = GL_POINTS; break;
case PRIMITIVE_LINES: flag = GL_LINES; break;
case PRIMITIVE_LINE_STRIP: flag = GL_LINE_STRIP; break;
case PRIMITIVE_TRIANGLES: flag = GL_TRIANGLES; break;
case PRIMITIVE_TRIANGLE_STRIP: flag = GL_TRIANGLE_STRIP; break;
default: assert(false); break;
}
return flag;
}
void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount,
Color color)
{
@ -1654,16 +1616,6 @@ void CGLDevice::DestroyStaticBuffer(unsigned int bufferId)
}
}
bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius)
{
float distance = originPlane + Math::DotProduct(normal, center);
if (distance < -radius)
return false;
return true;
}
/* Based on libwine's implementation */
int CGLDevice::ComputeSphereVisibility(const Math::Vector &center, float radius)
@ -1811,40 +1763,6 @@ void CGLDevice::SetRenderState(RenderState state, bool enabled)
glDisable(flag);
}
CompFunc TranslateGLCompFunc(GLenum flag)
{
switch (flag)
{
case GL_NEVER: return COMP_FUNC_NEVER;
case GL_LESS: return COMP_FUNC_LESS;
case GL_EQUAL: return COMP_FUNC_EQUAL;
case GL_NOTEQUAL: return COMP_FUNC_NOTEQUAL;
case GL_LEQUAL: return COMP_FUNC_LEQUAL;
case GL_GREATER: return COMP_FUNC_GREATER;
case GL_GEQUAL: return COMP_FUNC_GEQUAL;
case GL_ALWAYS: return COMP_FUNC_ALWAYS;
default: assert(false); break;
}
return COMP_FUNC_NEVER;
}
GLenum TranslateGfxCompFunc(CompFunc func)
{
switch (func)
{
case COMP_FUNC_NEVER: return GL_NEVER;
case COMP_FUNC_LESS: return GL_LESS;
case COMP_FUNC_EQUAL: return GL_EQUAL;
case COMP_FUNC_NOTEQUAL: return GL_NOTEQUAL;
case COMP_FUNC_LEQUAL: return GL_LEQUAL;
case COMP_FUNC_GREATER: return GL_GREATER;
case COMP_FUNC_GEQUAL: return GL_GEQUAL;
case COMP_FUNC_ALWAYS: return GL_ALWAYS;
default: assert(false); break;
}
return 0;
}
void CGLDevice::SetColorMask(bool red, bool green, bool blue, bool alpha)
{
glColorMask(red, green, blue, alpha);
@ -1865,47 +1783,6 @@ void CGLDevice::SetAlphaTestFunc(CompFunc func, float refValue)
glAlphaFunc(TranslateGfxCompFunc(func), refValue);
}
BlendFunc TranslateGLBlendFunc(GLenum flag)
{
switch (flag)
{
case GL_ZERO: return BLEND_ZERO;
case GL_ONE: return BLEND_ONE;
case GL_SRC_COLOR: return BLEND_SRC_COLOR;
case GL_ONE_MINUS_SRC_COLOR: return BLEND_INV_SRC_COLOR;
case GL_DST_COLOR: return BLEND_DST_COLOR;
case GL_ONE_MINUS_DST_COLOR: return BLEND_INV_DST_COLOR;
case GL_SRC_ALPHA: return BLEND_SRC_ALPHA;
case GL_ONE_MINUS_SRC_ALPHA: return BLEND_INV_SRC_ALPHA;
case GL_DST_ALPHA: return BLEND_DST_ALPHA;
case GL_ONE_MINUS_DST_ALPHA: return BLEND_INV_DST_ALPHA;
case GL_SRC_ALPHA_SATURATE: return BLEND_SRC_ALPHA_SATURATE;
default: assert(false); break;
}
return BLEND_ZERO;
}
GLenum TranslateGfxBlendFunc(BlendFunc func)
{
switch (func)
{
case BLEND_ZERO: return GL_ZERO;
case BLEND_ONE: return GL_ONE;
case BLEND_SRC_COLOR: return GL_SRC_COLOR;
case BLEND_INV_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR;
case BLEND_DST_COLOR: return GL_DST_COLOR;
case BLEND_INV_DST_COLOR: return GL_ONE_MINUS_DST_COLOR;
case BLEND_SRC_ALPHA: return GL_SRC_ALPHA;
case BLEND_INV_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA;
case BLEND_DST_ALPHA: return GL_DST_ALPHA;
case BLEND_INV_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA;
case BLEND_SRC_ALPHA_SATURATE: return GL_SRC_ALPHA_SATURATE;
default: assert(false); break;
}
return 0;
}
void CGLDevice::SetBlendFunc(BlendFunc srcBlend, BlendFunc dstBlend)
{
glBlendFunc(TranslateGfxBlendFunc(srcBlend), TranslateGfxBlendFunc(dstBlend));

View File

@ -26,6 +26,7 @@
#include "graphics/core/device.h"
#include "graphics/opengl/glutil.h"
#include "GL/glew.h"
#include <string>
@ -37,17 +38,6 @@
// Graphics module namespace
namespace Gfx {
/**
\enum VBOMode
\brief VBO autodetect/override
*/
enum VBOMode
{
VBO_MODE_ENABLE, //! < override: enable
VBO_MODE_DISABLE, //! < override: disable
VBO_MODE_AUTO //! < autodetect
};
/**
\enum VertexBufferType
\brief Specifies type of vertex buffer to use
@ -66,35 +56,6 @@ enum ShadowMappingSupport
SMS_CORE //! Core support
};
/**
\struct GLDeviceConfig
\brief Additional config with OpenGL-specific settings */
struct GLDeviceConfig : public DeviceConfig
{
//! Size of red channel in bits
int redSize;
//! Size of green channel in bits
int greenSize;
//! Size of blue channel in bits
int blueSize;
//! Size of alpha channel in bits
int alphaSize;
//! Color depth in bits
int depthSize;
//! Force hardware acceleration (video mode set will fail on lack of hw accel)
bool hardwareAccel;
//! VBO override/autodetect
VBOMode vboMode;
//! Constructor calls LoadDefaults()
GLDeviceConfig();
//! Loads the default values
void LoadDefault();
};
struct GLDevicePrivate;
/**
@ -153,7 +114,6 @@ public:
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT) OVERRIDE;
virtual void SetTextureCoordGeneration(int index, TextureGenerationParams &params) OVERRIDE;
virtual void SetTextureMatrix(int index, Math::Matrix& matrix) OVERRIDE;
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) OVERRIDE;

View File

@ -0,0 +1,151 @@
/*
* 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
*/
#include "graphics/opengl/glutil.h"
#include "graphics/core/device.h"
#include "GL/glew.h"
// Graphics module namespace
namespace Gfx {
GLDeviceConfig::GLDeviceConfig()
{
LoadDefault();
}
GLuint textureCoordinates[] = { GL_S, GL_T, GL_R, GL_Q };
GLuint textureCoordGen[] = { GL_TEXTURE_GEN_S, GL_TEXTURE_GEN_T, GL_TEXTURE_GEN_R, GL_TEXTURE_GEN_Q };
void GLDeviceConfig::LoadDefault()
{
DeviceConfig::LoadDefault();
hardwareAccel = true;
redSize = 8;
blueSize = 8;
greenSize = 8;
alphaSize = 8;
depthSize = 24;
vboMode = VBO_MODE_AUTO;
}
GLenum TranslateGfxPrimitive(PrimitiveType type)
{
GLenum flag = 0;
switch (type)
{
case PRIMITIVE_POINTS: flag = GL_POINTS; break;
case PRIMITIVE_LINES: flag = GL_LINES; break;
case PRIMITIVE_LINE_STRIP: flag = GL_LINE_STRIP; break;
case PRIMITIVE_TRIANGLES: flag = GL_TRIANGLES; break;
case PRIMITIVE_TRIANGLE_STRIP: flag = GL_TRIANGLE_STRIP; break;
default: assert(false); break;
}
return flag;
}
CompFunc TranslateGLCompFunc(GLenum flag)
{
switch (flag)
{
case GL_NEVER: return COMP_FUNC_NEVER;
case GL_LESS: return COMP_FUNC_LESS;
case GL_EQUAL: return COMP_FUNC_EQUAL;
case GL_NOTEQUAL: return COMP_FUNC_NOTEQUAL;
case GL_LEQUAL: return COMP_FUNC_LEQUAL;
case GL_GREATER: return COMP_FUNC_GREATER;
case GL_GEQUAL: return COMP_FUNC_GEQUAL;
case GL_ALWAYS: return COMP_FUNC_ALWAYS;
default: assert(false); break;
}
return COMP_FUNC_NEVER;
}
GLenum TranslateGfxCompFunc(CompFunc func)
{
switch (func)
{
case COMP_FUNC_NEVER: return GL_NEVER;
case COMP_FUNC_LESS: return GL_LESS;
case COMP_FUNC_EQUAL: return GL_EQUAL;
case COMP_FUNC_NOTEQUAL: return GL_NOTEQUAL;
case COMP_FUNC_LEQUAL: return GL_LEQUAL;
case COMP_FUNC_GREATER: return GL_GREATER;
case COMP_FUNC_GEQUAL: return GL_GEQUAL;
case COMP_FUNC_ALWAYS: return GL_ALWAYS;
default: assert(false); break;
}
return 0;
}
BlendFunc TranslateGLBlendFunc(GLenum flag)
{
switch (flag)
{
case GL_ZERO: return BLEND_ZERO;
case GL_ONE: return BLEND_ONE;
case GL_SRC_COLOR: return BLEND_SRC_COLOR;
case GL_ONE_MINUS_SRC_COLOR: return BLEND_INV_SRC_COLOR;
case GL_DST_COLOR: return BLEND_DST_COLOR;
case GL_ONE_MINUS_DST_COLOR: return BLEND_INV_DST_COLOR;
case GL_SRC_ALPHA: return BLEND_SRC_ALPHA;
case GL_ONE_MINUS_SRC_ALPHA: return BLEND_INV_SRC_ALPHA;
case GL_DST_ALPHA: return BLEND_DST_ALPHA;
case GL_ONE_MINUS_DST_ALPHA: return BLEND_INV_DST_ALPHA;
case GL_SRC_ALPHA_SATURATE: return BLEND_SRC_ALPHA_SATURATE;
default: assert(false); break;
}
return BLEND_ZERO;
}
GLenum TranslateGfxBlendFunc(BlendFunc func)
{
switch (func)
{
case BLEND_ZERO: return GL_ZERO;
case BLEND_ONE: return GL_ONE;
case BLEND_SRC_COLOR: return GL_SRC_COLOR;
case BLEND_INV_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR;
case BLEND_DST_COLOR: return GL_DST_COLOR;
case BLEND_INV_DST_COLOR: return GL_ONE_MINUS_DST_COLOR;
case BLEND_SRC_ALPHA: return GL_SRC_ALPHA;
case BLEND_INV_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA;
case BLEND_DST_ALPHA: return GL_DST_ALPHA;
case BLEND_INV_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA;
case BLEND_SRC_ALPHA_SATURATE: return GL_SRC_ALPHA_SATURATE;
default: assert(false); break;
}
return 0;
}
bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius)
{
float distance = originPlane + Math::DotProduct(normal, center);
if (distance < -radius)
return false;
return true;
}
}

View File

@ -0,0 +1,85 @@
/*
* 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
*/
#pragma once
#include "graphics/core/device.h"
#include "GL/glew.h"
// Graphics module namespace
namespace Gfx
{
GLuint textureCoordinates[];
GLuint textureCoordGen[];
/**
\enum VBOMode
\brief VBO autodetect/override
*/
enum VBOMode
{
VBO_MODE_ENABLE, //! < override: enable
VBO_MODE_DISABLE, //! < override: disable
VBO_MODE_AUTO //! < autodetect
};
/**
\struct GLDeviceConfig
\brief Additional config with OpenGL-specific settings */
struct GLDeviceConfig : public DeviceConfig
{
//! Size of red channel in bits
int redSize;
//! Size of green channel in bits
int greenSize;
//! Size of blue channel in bits
int blueSize;
//! Size of alpha channel in bits
int alphaSize;
//! Color depth in bits
int depthSize;
//! Force hardware acceleration (video mode set will fail on lack of hw accel)
bool hardwareAccel;
//! VBO override/autodetect
VBOMode vboMode;
//! Constructor calls LoadDefaults()
GLDeviceConfig();
//! Loads the default values
void LoadDefault();
};
//! Translate Gfx primitive type to OpenGL primitive type
GLenum TranslateGfxPrimitive(PrimitiveType type);
CompFunc TranslateGLCompFunc(GLenum flag);
GLenum TranslateGfxCompFunc(CompFunc func);
BlendFunc TranslateGLBlendFunc(GLenum flag);
GLenum TranslateGfxBlendFunc(BlendFunc func);
bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius);
} // namespace Gfx