Added OpenGL 2.1 engine

master
Tomasz Kapuściński 2015-05-27 22:12:02 +02:00
parent 08bf066f78
commit e1a0f5b1c7
9 changed files with 2372 additions and 37 deletions

View File

@ -112,6 +112,7 @@ set(BASE_SOURCES
graphics/engine/text.cpp
graphics/engine/water.cpp
graphics/opengl/gldevice.cpp
graphics/opengl/gl21device.cpp
graphics/opengl/gl33device.cpp
graphics/opengl/glutil.cpp
object/auto/auto.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/gl21device.h"
#include "graphics/opengl/gl33device.h"
#include "object/robotmain.h"
@ -565,6 +566,8 @@ bool CApplication::Create()
// The video is ready, we can create and initalize the graphics device
if (m_graphics == "opengl")
m_device = new Gfx::CGLDevice(m_deviceConfig);
else if (m_graphics == "gl21")
m_device = new Gfx::CGL21Device(m_deviceConfig);
else if (m_graphics == "gl33")
m_device = new Gfx::CGL33Device(m_deviceConfig);
else

View File

@ -176,12 +176,12 @@ CEngine::CEngine(CApplication *app)
if (CProfile::GetInstance().GetIntProperty("Setup", "MipmapLevel", value))
{
m_textureMipmapLevel = value;
SetTextureMipmapLevel(value);
}
if (CProfile::GetInstance().GetIntProperty("Setup", "Anisotropy", value))
{
m_textureAnisotropy = value;
SetTextureAnisotropyLevel(value);
}
if (CProfile::GetInstance().GetIntProperty("Setup", "ShadowMapping", value))
@ -191,6 +191,12 @@ CEngine::CEngine(CApplication *app)
m_qualityShadows = (value > 2);
}
float shadowColor;
if (CProfile::GetInstance().GetFloatProperty("Setup", "ShadowColor", shadowColor))
{
SetShadowColor(shadowColor);
}
m_defaultTexParams.format = TEX_IMG_AUTO;
m_defaultTexParams.mipmap = mipmaps;
m_defaultTexParams.filter = filter;
@ -2698,6 +2704,16 @@ bool CEngine::GetShadow()
return m_shadowVisible;
}
void CEngine::SetShadowColor(float value)
{
m_shadowColor = value;
}
bool CEngine::GetShadowColor()
{
return m_shadowColor;
}
void CEngine::SetDirty(bool mode)
{
m_dirty = mode;
@ -3553,25 +3569,23 @@ void CEngine::RenderShadowMap()
// recompute matrices
Math::Vector worldUp(0.0f, 1.0f, 0.0f);
Math::Vector lightDir = Math::Vector(1.0f, 1.0f, -1.0f);
Math::Vector lightDir = Math::Vector(1.0f, 2.0f, -1.0f);
Math::Vector dir = m_lookatPt - m_eyePt;
dir.y = 0.0f;
dir.Normalize();
float dist = 50.0f * (log(m_shadowMap.size.x) / log(2.0f) - 6.5f);
float depth = 800.0f;
Math::Vector lightOffset = Math::Vector(1.0f, 0.0f, -1.0f);
lightOffset.Normalize();
Math::Vector pos = m_lookatPt + 0.5f * dist * dir + 0.5f * dist * Math::Vector(1.0f, 0.0f, -1.0f);
float scale = log(m_shadowMap.size.x) / log(2.0f) - 6.5f;
float dist = 100.0f * scale;
float depth = 2000.0f;
Math::Vector lightPos = pos + Math::Vector(0.0f, 30.0f, 0.0f);
Math::Vector lookAt = lightPos + lightDir;
Math::Vector pos = m_lookatPt;// +0.25f * dist * dir;// +0.25f * dist * lightOffset;
lightPos.x = round(lightPos.x);
lightPos.y = round(lightPos.y);
lightPos.z = round(lightPos.z);
Math::Vector lightPos = m_lookatPt + Math::Vector(0.0f, -200.0f, 0.0f);
lookAt.x = round(lookAt.x);
lookAt.y = round(lookAt.y);
lookAt.z = round(lookAt.z);
Math::Vector lookAt = lightPos - lightDir;
Math::LoadOrthoProjectionMatrix(m_shadowProjMat, -dist, dist, -dist, dist, -depth, depth);
Math::LoadViewMatrix(m_shadowViewMat, lightPos, lookAt, worldUp);
@ -3582,6 +3596,11 @@ void CEngine::RenderShadowMap()
m_device->SetTransform(TRANSFORM_PROJECTION, m_shadowProjMat);
m_device->SetTransform(TRANSFORM_VIEW, m_shadowViewMat);
Math::Matrix scaleMat;
Math::LoadScaleMatrix(scaleMat, Math::Vector(1.0f, 1.0f, -1.0f));
m_shadowViewMat = Math::MultiplyMatrices(scaleMat, m_shadowViewMat);
m_device->SetTexture(0, 0);
m_device->SetTexture(1, 0);
@ -3596,10 +3615,6 @@ void CEngine::RenderShadowMap()
m_device->SetTransform(TRANSFORM_WORLD, m_objects[objRank].transform);
// TODO: check proper object filtering
//if (!IsVisible(objRank))
// continue;
int baseObjRank = m_objects[objRank].baseObjRank;
if (baseObjRank == -1)
continue;
@ -3610,8 +3625,6 @@ void CEngine::RenderShadowMap()
if (!p1.used)
continue;
//m_lightMan->UpdateDeviceLights(m_objects[objRank].type);
for (int l2 = 0; l2 < static_cast<int>(p1.next.size()); l2++)
{
EngineBaseObjTexTier& p2 = p1.next[l2];
@ -3629,9 +3642,6 @@ void CEngine::RenderShadowMap()
{
EngineBaseObjDataTier& p4 = p3.next[l4];
if (m_objects[objRank].transparency != 0.0f) // transparent ?
continue;
DrawObject(p4);
}
}
@ -3669,6 +3679,8 @@ void CEngine::UseShadowMapping(bool enable)
if (enable) // Enable shadow mapping
{
m_device->SetShadowColor(m_shadowColor);
if (m_qualityShadows)
{
if (m_device->GetMaxTextureStageCount() < 6)

View File

@ -1140,6 +1140,12 @@ public:
bool GetShadowMapping();
//@}
//@{
//! Management of shadow mapping
void SetShadowColor(float value);
bool GetShadowColor();
//@}
//@{
//! Management mode of toto
void SetTotoMode(bool present);
@ -1463,6 +1469,8 @@ protected:
bool m_offscreenShadowRendering;
//! true enables higher quality shadows
bool m_qualityShadows;
//! Shadow color
float m_shadowColor;
//! Map of loaded textures (by name)
std::map<std::string, Texture> m_texNameMap;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,283 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.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/gldevice.h
* \brief OpenGL implementation - CGL21Device class
*/
#pragma once
#include "graphics/core/device.h"
#include "graphics/opengl/glutil.h"
#include <string>
#include <vector>
#include <set>
#include <map>
// Graphics module namespace
namespace Gfx {
struct GLDevicePrivate;
/**
\class CGL21Device
\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, CGL21DeviceConfig is outside the CDevice class and must be set
in CApplication.
*/
class CGL21Device : public CDevice
{
public:
CGL21Device(const GLDeviceConfig &config);
virtual ~CGL21Device();
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 SetShadowColor(float value) OVERRIDE;
virtual void SetFillMode(FillMode mode) OVERRIDE;
virtual void InitOffscreenBuffer(int width, int height) OVERRIDE;
virtual void SetRenderTexture(RenderTarget target, int texture) 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 texture status
void UpdateTextureStatus();
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 VboObjectInfo
{
PrimitiveType primitiveType;
unsigned int bufferId;
VertexType vertexType;
int vertexCount;
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;
//! Whether offscreen rendering is available
bool m_framebufferObject;
//! Map of saved VBO objects
std::map<unsigned int, VboObjectInfo> 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;
//! true if offscreen rendering enabled
bool m_offscreenRenderingEnabled;
//! Shader program
GLuint m_shaderProgram;
//! true enables per-pixel lighting
bool m_perPixelLighting;
// 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;
//! true enables texture
GLint uni_TextureEnabled[3];
// Alpha test parameters
//! true enables alpha test
GLint uni_AlphaTestEnabled;
//! Alpha test reference value
GLint uni_AlphaReference;
//! true enables fog
GLint uni_FogEnabled;
//! Fog range
GLint uni_FogRange;
//! Fog color
GLint uni_FogColor;
//! Shadow color
GLint uni_ShadowColor;
//! true enables lighting
GLint uni_LightingEnabled;
//! true enables light source
GLint uni_LightEnabled[8];
};
} // namespace Gfx

View File

@ -0,0 +1,72 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.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
*/
#version 120
uniform sampler2D uni_PrimaryTexture;
uniform sampler2D uni_SecondaryTexture;
uniform sampler2DShadow uni_ShadowTexture;
uniform bool uni_TextureEnabled[3];
uniform bool uni_AlphaTestEnabled;
uniform float uni_AlphaReference;
uniform bool uni_FogEnabled;
uniform vec2 uni_FogRange;
uniform vec4 uni_FogColor;
uniform float uni_ShadowColor;
varying float pass_Distance;
void main()
{
vec4 color = gl_Color;
if (uni_TextureEnabled[0])
{
color = color * texture2D(uni_PrimaryTexture, gl_TexCoord[0].st);
}
if (uni_TextureEnabled[1])
{
color = color * texture2D(uni_SecondaryTexture, gl_TexCoord[1].st);
}
if (uni_TextureEnabled[2])
{
color = color * mix(uni_ShadowColor, 1.0f, shadow2D(uni_ShadowTexture, gl_TexCoord[2].xyz).x);
}
if (uni_FogEnabled)
{
float interpolate = (pass_Distance - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x);
color = mix(color, uni_FogColor, clamp(interpolate, 0.0f, 1.0f));
}
if (uni_AlphaTestEnabled)
{
if(color.a < uni_AlphaReference)
discard;
}
gl_FragColor = color;
}

View File

@ -0,0 +1,96 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.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
*/
#version 120
uniform mat4 uni_ProjectionMatrix;
uniform mat4 uni_ViewMatrix;
uniform mat4 uni_ModelMatrix;
uniform mat4 uni_ShadowMatrix;
uniform mat4 uni_NormalMatrix;
uniform bool uni_LightingEnabled;
uniform bool uni_LightEnabled[8];
varying float pass_Distance;
void main()
{
vec4 position = uni_ModelMatrix * gl_Vertex;
vec4 eyeSpace = uni_ViewMatrix * position;
vec4 shadowCoord = uni_ShadowMatrix * position;
gl_Position = uni_ProjectionMatrix * eyeSpace;
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
gl_TexCoord[2] = vec4(shadowCoord.xyz / shadowCoord.w, 1.0f);
pass_Distance = abs(eyeSpace.z / eyeSpace.w);
if (uni_LightingEnabled)
{
vec4 color;
vec4 ambient = vec4(0.0f);
vec4 diffuse = vec4(0.0f);
vec4 specular = vec4(0.0f);
vec3 normal = normalize((uni_NormalMatrix * vec4(gl_Normal, 0.0f)).xyz);
for(int i=0; i<8; i++)
{
if(uni_LightEnabled[i])
{
vec3 lightDirection = vec3(0.0f);
float atten;
// Directional light
if(gl_LightSource[i].position.w == 0.0f)
{
lightDirection = gl_LightSource[i].position.xyz;
atten = 1.0f;
}
// Point light
else
{
vec3 lightDirection = normalize(gl_LightSource[i].position.xyz - position.xyz);
float dist = distance(gl_LightSource[i].position.xyz, position.xyz);
atten = 1.0f / (gl_LightSource[i].constantAttenuation
+ gl_LightSource[i].linearAttenuation * dist
+ gl_LightSource[i].quadraticAttenuation * dist * dist);
}
vec3 reflectDirection = -reflect(lightDirection, normal);
ambient += gl_LightSource[i].ambient;
diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * gl_LightSource[i].diffuse;
specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * gl_LightSource[i].specular;
}
}
vec4 result = gl_FrontMaterial.ambient * ambient
+ gl_FrontMaterial.diffuse * diffuse
+ gl_FrontMaterial.specular * specular;
color.rgb = min(vec3(1.0f), result.rgb);
color.a = 1.0f; //min(1.0f, 1.0f);
gl_FrontColor = color;
}
}

View File

@ -65,30 +65,31 @@ void main()
vec4 position = uni_ModelMatrix * in_VertexCoord;
vec4 eyeSpace = uni_ViewMatrix * position;
gl_Position = uni_ProjectionMatrix * eyeSpace;
vec4 shadowCoord = uni_ShadowMatrix * position;
data.Color = in_Color;
data.TexCoord0 = in_TexCoord0;
data.TexCoord1 = in_TexCoord1;
data.ShadowCoord = uni_ShadowMatrix * position;
data.ShadowCoord = vec4(shadowCoord.xyz / shadowCoord.w, 1.0f);
data.Distance = abs(eyeSpace.z);
vec4 color = in_Color;
if (uni_LightingEnabled)
{
vec4 ambient = vec4(0.0f);
vec4 diffuse = vec4(0.0f);
vec4 specular = vec4(0.0f);
vec3 normal = normalize((uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz);
for(int i=0; i<8; i++)
{
if(uni_Light[i].Enabled)
{
vec3 normal = (uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz;
vec3 lightDirection = vec3(0.0f);
float atten;
// Directional light
if(uni_Light[i].Position[3] == 0.0f)
{
@ -100,27 +101,27 @@ void main()
{
vec3 lightDirection = normalize(uni_Light[i].Position.xyz - position.xyz);
float dist = distance(uni_Light[i].Position.xyz, position.xyz);
atten = 1.0f / (uni_Light[i].Attenuation.x
+ uni_Light[i].Attenuation.y * dist
+ uni_Light[i].Attenuation.z * dist * dist);
}
vec3 reflectDirection = -reflect(lightDirection, normal);
ambient += uni_Light[i].Ambient;
diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * uni_Light[i].Diffuse;
specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * uni_Light[i].Specular;
}
}
vec4 result = uni_AmbientColor * ambient
+ uni_DiffuseColor * diffuse
+ uni_SpecularColor * specular;
color.rgb = min(vec3(1.0f), result.rgb);
color.a = 1.0f; //min(1.0f, 1.0f);
data.Color = color;
}
}