Removed material, lighting, fog and shadowing code from CDevice

dev
Tomasz Kapuściński 2022-02-02 19:09:38 +01:00
parent 7d9badb542
commit 0908e10ff6
10 changed files with 40 additions and 604 deletions

View File

@ -135,7 +135,6 @@ enum TextureUnit
{
TEXTURE_PRIMARY = 0,
TEXTURE_SECONDARY = 1,
TEXTURE_SHADOW = 2,
};
/**
@ -147,7 +146,6 @@ enum TransformType
TRANSFORM_WORLD,
TRANSFORM_VIEW,
TRANSFORM_PROJECTION,
TRANSFORM_SHADOW
};
/**
@ -156,14 +154,11 @@ enum TransformType
*/
enum RenderState
{
RENDER_STATE_LIGHTING,
RENDER_STATE_BLENDING,
RENDER_STATE_FOG,
RENDER_STATE_DEPTH_TEST,
RENDER_STATE_DEPTH_WRITE,
RENDER_STATE_ALPHA_TEST,
RENDER_STATE_CULLING,
RENDER_STATE_SHADOW_MAPPING,
};
/**
@ -412,12 +407,6 @@ public:
return m_capabilities;
}
//! Provides a hook to debug graphics code (implementation-specific)
virtual void DebugHook() = 0;
//! Displays light positions to aid in debuggings
virtual void DebugLights() = 0;
//! Returns a name of this device
virtual std::string GetName() = 0;
@ -454,16 +443,6 @@ public:
//! Sets the transform matrix of given type
virtual void SetTransform(TransformType type, const glm::mat4 &matrix) = 0;
//! Sets the current material
virtual void SetMaterial(const Material &material) = 0;
//! Returns the maximum number of lights available
virtual int GetMaxLightCount() = 0;
//! Sets the light at given index
virtual void SetLight(int index, const Light &light) = 0;
//! Enables/disables the light at given index
virtual void SetLightEnabled(int index, bool enabled) = 0;
//! Creates a texture from image; the image can be safely removed after that
virtual Texture CreateTexture(CImage *image, const TextureCreateParams &params) = 0;
//! Creates a texture from raw image data; image data can be freed after that
@ -529,18 +508,9 @@ public:
//! Sets the clear color
virtual void SetClearColor(const Color &color) = 0;
//! Sets the global ambient color
virtual void SetGlobalAmbient(const Color &color) = 0;
//! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models)
virtual void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) = 0;
//! Sets the current cull mode
virtual void SetCullMode(CullMode mode) = 0;
//! Sets shadow color
virtual void SetShadowColor(float value) = 0;
//! Sets the current fill mode
virtual void SetFillMode(FillMode mode) = 0;

View File

@ -32,36 +32,10 @@
namespace Gfx
{
/**
* \struct Material
* \brief Material of a surface
*
* This structure was created as analog to DirectX's D3DMATERIAL.
*
* It contains values of 3 material colors: diffuse, ambient and specular.
* In D3DMATERIAL there are other fields, but they are not used
* by the graphics engine.
*/
//! Remains of the legacy material structure, to be reused
struct Material
{
//! Diffuse color
Color diffuse;
//! Ambient color
Color ambient;
//! Specular color
Color specular;
bool operator==(const Material &mat) const
{
return diffuse == mat.diffuse && ambient == mat.ambient && specular == mat.specular;
}
bool operator!=(const Material &mat) const
{
return ! operator==(mat);
}
};
} // namespace Gfx

View File

@ -1931,7 +1931,6 @@ void CEngine::SetState(int state, const Color& color)
if (state & ENG_RSTATE_TTEXTURE_BLACK) // transparent black texture?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -1950,7 +1949,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TTEXTURE_WHITE) // transparent white texture?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -1969,7 +1967,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TCOLOR_BLACK) // transparent black color?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -1980,7 +1977,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TCOLOR_WHITE) // transparent white color?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -1991,7 +1987,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TDIFFUSE) // diffuse color as transparent?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -2008,7 +2003,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_OPAQUE_TEXTURE) // opaque texture ?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(RENDER_STATE_BLENDING, false);
@ -2018,7 +2012,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_OPAQUE_COLOR) // opaque color ?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(RENDER_STATE_BLENDING, false);
@ -2027,7 +2020,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TEXT) // font rendering?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -2042,7 +2034,6 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetRenderState(RENDER_STATE_BLENDING, true);
m_device->SetBlendFunc(BLEND_SRC_ALPHA, BLEND_INV_SRC_ALPHA);
m_device->SetRenderState(RENDER_STATE_FOG, true);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, true);
@ -2062,7 +2053,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TTEXTURE_ALPHA) // texture with alpha channel?
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, true);
@ -2085,7 +2075,6 @@ void CEngine::SetState(int state, const Color& color)
}
else if (state & ENG_RSTATE_TCOLOR_ALPHA)
{
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_ALPHA_TEST, false);
@ -2100,7 +2089,6 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetRenderState(RENDER_STATE_BLENDING, false);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true);
m_device->SetRenderState(RENDER_STATE_FOG, true);
TextureStageParams params;
params.colorOperation = TEX_MIX_OPER_DEFAULT; // default modulate
@ -2110,10 +2098,6 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetTextureStageParams(0, params);
}
if (state & ENG_RSTATE_FOG)
m_device->SetRenderState(RENDER_STATE_FOG, true);
bool second = m_dirty;
// TODO: I'm pretty sure this is reversed and should be m_dirty instead of !m_dirty ~krzys_h
@ -2164,22 +2148,11 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetRenderState(RENDER_STATE_CULLING, true);
m_device->SetCullMode(CULL_CCW);
}
if (state & ENG_RSTATE_LIGHT)
m_device->SetGlobalAmbient(Color(1.0f, 1.0f, 1.0f, 1.0f));
else
m_device->SetGlobalAmbient(m_ambientColor[m_rankView]);
// In interface mode, disable lighting
if (m_interfaceMode)
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
}
void CEngine::SetMaterial(const Material& mat)
{
m_lastMaterial = mat;
m_device->SetMaterial(mat);
}
void CEngine::SetViewParams(const glm::vec3 &eyePt, const glm::vec3 &lookatPt, const glm::vec3 &upVec)
@ -3246,7 +3219,6 @@ void CEngine::Draw3DScene()
float fogStart = m_deepView[m_rankView] * m_fogStart[m_rankView] * m_clippingDistance;
float fogEnd = m_deepView[m_rankView] * m_clippingDistance;
m_device->SetFogParams(FOG_LINEAR, m_fogColor[m_rankView], fogStart, fogEnd, 1.0f);
m_device->SetTransform(TRANSFORM_PROJECTION, m_matProj);
m_device->SetTransform(TRANSFORM_VIEW, m_matView);
@ -3516,9 +3488,6 @@ void CEngine::Draw3DScene()
m_lightMan->UpdateDeviceLights(ENG_OBJTYPE_TERRAIN);
if (m_debugLights)
m_device->DebugLights();
if (m_debugDumpLights)
{
m_debugDumpLights = false;
@ -3546,8 +3515,6 @@ void CEngine::Draw3DScene()
CProfiler::StopPerformanceCounter(PCNT_RENDER_WATER);
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
RenderPendingDebugDraws();
if (m_debugGoto)
@ -3575,8 +3542,6 @@ void CEngine::Draw3DScene()
particleRenderer->End();
m_device->SetRenderState(RENDER_STATE_LIGHTING, true);
m_lightning->Draw(); // draws lightning
DrawForegroundImage(); // draws the foreground
@ -4089,8 +4054,6 @@ void CEngine::DrawInterface()
m_device->Restore();
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false);
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
m_device->SetRenderState(RENDER_STATE_FOG, false);
SetInterfaceCoordinates();
@ -4213,8 +4176,6 @@ void CEngine::DrawInterface()
particleRenderer->End();
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false);
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
m_device->SetRenderState(RENDER_STATE_FOG, false);
m_device->Restore();
@ -4548,17 +4509,10 @@ void CEngine::UpdateGroundSpotTextures()
void CEngine::DrawShadowSpots()
{
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
glm::mat4 matrix = glm::mat4(1.0f);
m_device->SetTransform(TRANSFORM_WORLD, matrix);
Material material;
material.diffuse = Color(1.0f, 1.0f, 1.0f);
material.ambient = Color(0.5f, 0.5f, 0.5f);
SetMaterial(material);
// TODO: create a separate texture
SetTexture("textures/effect03.png");
@ -4742,7 +4696,6 @@ void CEngine::DrawShadowSpots()
}
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true);
m_device->SetRenderState(RENDER_STATE_LIGHTING, true);
}
void CEngine::DrawBackground()
@ -5096,11 +5049,6 @@ void CEngine::DrawMouse()
SetWindowCoordinates();
Material material;
material.diffuse = Color(1.0f, 1.0f, 1.0f);
material.ambient = Color(0.5f, 0.5f, 0.5f);
m_device->SetMaterial(material);
m_device->SetTexture(0, m_miceTexture);
SetUITexture(m_miceTexture);

View File

@ -89,7 +89,7 @@ CLightManager::~CLightManager()
void CLightManager::SetDevice(CDevice* device)
{
m_device = device;
m_lightMap = std::vector<int>(m_device->GetMaxLightCount(), -1);
m_lightMap = std::vector<int>(8, -1);
}
void CLightManager::DebugDumpLights()
@ -484,12 +484,12 @@ void CLightManager::UpdateDeviceLights(EngineObjectType type)
if (rank != -1)
{
Light light = m_dynLights[rank].light;
m_device->SetLight(i, light);
m_device->SetLightEnabled(i, true);
//m_device->SetLight(i, light);
//m_device->SetLightEnabled(i, true);
}
else
{
m_device->SetLightEnabled(i, false);
//m_device->SetLightEnabled(i, false);
}
}
}

View File

@ -18,7 +18,6 @@
*/
#include "graphics/model/model_input.h"
#include "graphics/core/material.h"
#include "common/ioutils.h"
#include "common/logger.h"
@ -37,6 +36,27 @@
namespace Gfx
{
//! Legacy material structure
struct LegacyMaterial
{
//! Diffuse color
Color diffuse;
//! Ambient color
Color ambient;
//! Specular color
Color specular;
bool operator==(const LegacyMaterial& mat) const
{
return diffuse == mat.diffuse && ambient == mat.ambient && specular == mat.specular;
}
bool operator!=(const LegacyMaterial& mat) const
{
return !operator==(mat);
}
};
// Private functions
namespace ModelInput
{
@ -58,12 +78,12 @@ namespace ModelInput
Vertex3D ReadBinaryVertex(std::istream& stream);
Vertex3D ReadBinaryVertexTex2(std::istream& stream);
Material ReadBinaryMaterial(std::istream& stream);
LegacyMaterial ReadBinaryMaterial(std::istream& stream);
std::string ReadLineString(std::istream& stream, const std::string& expectedPrefix);
void ReadValuePrefix(std::istream& stream, const std::string& expectedPrefix);
Vertex3D ParseVertexTex2(const std::string& text);
Material ParseMaterial(const std::string& text);
LegacyMaterial ParseMaterial(const std::string& text);
glm::vec3 ParseVector(const std::string& text);
ModelCrashSphere ParseCrashSphere(const std::string& text);
ModelShadowSpot ParseShadowSpot(const std::string& text);
@ -364,7 +384,7 @@ CModelMesh ModelInput::ReadTextMesh(std::istream& stream)
t.p3 = ParseVertexTex2(p3Text);
std::string matText = ReadLineString(stream, "mat");
Material mat = ParseMaterial(matText);
LegacyMaterial mat = ParseMaterial(matText);
auto diffuse = Gfx::ColorToIntColor(mat.diffuse);
glm::u8vec4 color = { diffuse.r, diffuse.g, diffuse.b, 255 };
@ -677,9 +697,9 @@ Vertex3D ModelInput::ReadBinaryVertexTex2(std::istream& stream)
return vertex;
}
Material ModelInput::ReadBinaryMaterial(std::istream& stream)
LegacyMaterial ModelInput::ReadBinaryMaterial(std::istream& stream)
{
Material material;
LegacyMaterial material;
material.diffuse.r = ReadBinaryFloat(stream);
material.diffuse.g = ReadBinaryFloat(stream);
@ -766,9 +786,9 @@ Vertex3D ModelInput::ParseVertexTex2(const std::string& text)
return vertex;
}
Material ModelInput::ParseMaterial(const std::string& text)
LegacyMaterial ModelInput::ParseMaterial(const std::string& text)
{
Material material;
LegacyMaterial material;
std::stringstream stream;
stream.str(text);

View File

@ -102,113 +102,6 @@ CGL33Device::~CGL33Device()
{
}
void CGL33Device::DebugHook()
{
/* This function is only called here, so it can be used
* as a breakpoint when debugging using gDEBugger */
glColor3i(0, 0, 0);
}
void CGL33Device::DebugLights()
{
Gfx::ColorHSV color(0.0, 1.0, 1.0);
glLineWidth(3.0f);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDisable(GL_BLEND);
glm::mat4 saveWorldMat = m_worldMat;
glm::mat4 identity = glm::mat4(1.0f);
SetTransform(TRANSFORM_WORLD, identity);
for (int i = 0; i < static_cast<int>( m_lights.size() ); ++i)
{
color.h = static_cast<float>(i) / static_cast<float>(m_lights.size());
if (m_lightsEnabled[i])
{
const Light& l = m_lights[i];
if (l.type == LIGHT_DIRECTIONAL)
{
Gfx::VertexCol v[2];
v[0].coord = -glm::normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i);
v[0].color = HSV2RGB(color);
v[1].coord = glm::normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i);
v[1].color = HSV2RGB(color);
while (v[0].coord.y < 60.0f && v[0].coord.y < 60.0f)
{
v[0].coord.y += 10.0f;
v[1].coord.y += 10.0f;
}
DrawPrimitive(PrimitiveType::LINES, v, 2);
v[0].coord = v[1].coord + glm::normalize(v[0].coord - v[1].coord) * 50.0f;
glLineWidth(10.0f);
DrawPrimitive(PrimitiveType::LINES, v, 2);
glLineWidth(3.0f);
}
else if (l.type == LIGHT_POINT)
{
Gfx::VertexCol v[8];
for (int i = 0; i < 8; ++i)
v[i].color = HSV2RGB(color);
v[0].coord = l.position + glm::vec3(-1.0f, -1.0f, -1.0f) * 4.0f;
v[1].coord = l.position + glm::vec3( 1.0f, -1.0f, -1.0f) * 4.0f;
v[2].coord = l.position + glm::vec3( 1.0f, 1.0f, -1.0f) * 4.0f;
v[3].coord = l.position + glm::vec3(-1.0f, 1.0f, -1.0f) * 4.0f;
v[4].coord = l.position + glm::vec3(-1.0f, -1.0f, -1.0f) * 4.0f;
DrawPrimitive(PrimitiveType::LINE_STRIP, v, 5);
v[0].coord = l.position + glm::vec3(-1.0f, -1.0f, 1.0f) * 4.0f;
v[1].coord = l.position + glm::vec3( 1.0f, -1.0f, 1.0f) * 4.0f;
v[2].coord = l.position + glm::vec3( 1.0f, 1.0f, 1.0f) * 4.0f;
v[3].coord = l.position + glm::vec3(-1.0f, 1.0f, 1.0f) * 4.0f;
v[4].coord = l.position + glm::vec3(-1.0f, -1.0f, 1.0f) * 4.0f;
DrawPrimitive(PrimitiveType::LINE_STRIP, v, 5);
v[0].coord = l.position + glm::vec3(-1.0f, -1.0f, -1.0f) * 4.0f;
v[1].coord = l.position + glm::vec3(-1.0f, -1.0f, 1.0f) * 4.0f;
v[2].coord = l.position + glm::vec3( 1.0f, -1.0f, -1.0f) * 4.0f;
v[3].coord = l.position + glm::vec3( 1.0f, -1.0f, 1.0f) * 4.0f;
v[4].coord = l.position + glm::vec3( 1.0f, 1.0f, -1.0f) * 4.0f;
v[5].coord = l.position + glm::vec3( 1.0f, 1.0f, 1.0f) * 4.0f;
v[6].coord = l.position + glm::vec3(-1.0f, 1.0f, -1.0f) * 4.0f;
v[7].coord = l.position + glm::vec3(-1.0f, 1.0f, 1.0f) * 4.0f;
DrawPrimitive(PrimitiveType::LINES, v, 8);
}
else if (l.type == LIGHT_SPOT)
{
Gfx::VertexCol v[5];
for (int i = 0; i < 5; ++i)
v[i].color = HSV2RGB(color);
v[0].coord = l.position + glm::vec3(-1.0f, 0.0f, -1.0f) * 4.0f;
v[1].coord = l.position + glm::vec3( 1.0f, 0.0f, -1.0f) * 4.0f;
v[2].coord = l.position + glm::vec3( 1.0f, 0.0f, 1.0f) * 4.0f;
v[3].coord = l.position + glm::vec3(-1.0f, 0.0f, 1.0f) * 4.0f;
v[4].coord = l.position + glm::vec3(-1.0f, 0.0f, -1.0f) * 4.0f;
DrawPrimitive(PrimitiveType::LINE_STRIP, v, 5);
v[0].coord = l.position;
v[1].coord = l.position + glm::normalize(l.direction) * 100.0f;
glEnable(GL_LINE_STIPPLE);
glLineStipple(3.0, 0xFF);
DrawPrimitive(PrimitiveType::LINES, v, 2);
glDisable(GL_LINE_STIPPLE);
}
}
}
glLineWidth(1.0f);
glEnable(GL_LIGHTING);
glDepthMask(GL_TRUE);
glEnable(GL_BLEND);
SetTransform(TRANSFORM_WORLD, saveWorldMat);
}
std::string CGL33Device::GetName()
{
return std::string("OpenGL 3.3");
@ -282,9 +175,6 @@ bool CGL33Device::Create()
// this is set in shader
m_capabilities.maxLights = 4;
m_lights = std::vector<Light>(m_capabilities.maxLights, Light());
m_lightsEnabled = std::vector<bool>(m_capabilities.maxLights, false);
int maxTextures = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextures);
GetLogger()->Info("Maximum texture image units: %d\n", maxTextures);
@ -353,51 +243,14 @@ bool CGL33Device::Create()
uni.primaryTexture = glGetUniformLocation(m_normalProgram, "uni_PrimaryTexture");
uni.secondaryTexture = glGetUniformLocation(m_normalProgram, "uni_SecondaryTexture");
uni.shadowTexture = glGetUniformLocation(m_normalProgram, "uni_ShadowTexture");
uni.textureEnabled[0] = glGetUniformLocation(m_normalProgram, "uni_PrimaryTextureEnabled");
uni.textureEnabled[1] = glGetUniformLocation(m_normalProgram, "uni_SecondaryTextureEnabled");
uni.textureEnabled[2] = glGetUniformLocation(m_normalProgram, "uni_ShadowTextureEnabled");
uni.fogEnabled = glGetUniformLocation(m_normalProgram, "uni_FogEnabled");
uni.fogRange = glGetUniformLocation(m_normalProgram, "uni_FogRange");
uni.fogColor = glGetUniformLocation(m_normalProgram, "uni_FogColor");
uni.alphaTestEnabled = glGetUniformLocation(m_normalProgram, "uni_AlphaTestEnabled");
uni.alphaReference = glGetUniformLocation(m_normalProgram, "uni_AlphaReference");
uni.shadowColor = glGetUniformLocation(m_normalProgram, "uni_ShadowColor");
uni.lightCount = glGetUniformLocation(m_normalProgram, "uni_LightCount");
uni.ambientColor = glGetUniformLocation(m_normalProgram, "uni_AmbientColor");
uni.diffuseColor = glGetUniformLocation(m_normalProgram, "uni_DiffuseColor");
uni.specularColor = glGetUniformLocation(m_normalProgram, "uni_SpecularColor");
GLchar name[64];
for (int i = 0; i < m_capabilities.maxLights; i++)
{
LightLocations &light = uni.lights[i];
sprintf(name, "uni_Light[%d].Enabled", i);
light.enabled = glGetUniformLocation(m_normalProgram, name);
sprintf(name, "uni_Light[%d].Position", i);
light.position = glGetUniformLocation(m_normalProgram, name);
sprintf(name, "uni_Light[%d].Ambient", i);
light.ambient = glGetUniformLocation(m_normalProgram, name);
sprintf(name, "uni_Light[%d].Diffuse", i);
light.diffuse = glGetUniformLocation(m_normalProgram, name);
sprintf(name, "uni_Light[%d].Specular", i);
light.specular = glGetUniformLocation(m_normalProgram, name);
sprintf(name, "uni_Light[%d].Attenuation", i);
light.attenuation = glGetUniformLocation(m_normalProgram, name);
}
// Set default uniform values
glm::mat4 matrix = glm::mat4(1.0f);
@ -410,26 +263,13 @@ bool CGL33Device::Create()
glUniform1i(uni.primaryTexture, 0);
glUniform1i(uni.secondaryTexture, 1);
glUniform1i(uni.shadowTexture, 2);
glUniform1i(uni.textureEnabled[0], 0);
glUniform1i(uni.textureEnabled[1], 0);
glUniform1i(uni.textureEnabled[2], 0);
glUniform4f(uni.ambientColor, 0.4f, 0.4f, 0.4f, 1.0f);
glUniform4f(uni.diffuseColor, 0.8f, 0.8f, 0.8f, 1.0f);
glUniform4f(uni.specularColor, 0.3f, 0.3f, 0.3f, 1.0f);
glUniform1i(uni.fogEnabled, 0);
glUniform2f(uni.fogRange, 100.0f, 200.0f);
glUniform4f(uni.fogColor, 0.8f, 0.8f, 0.8f, 1.0f);
glUniform1f(uni.shadowColor, 0.5f);
glUniform1i(uni.alphaTestEnabled, 0);
glUniform1f(uni.alphaReference, 0.5f);
glUniform1i(uni.lightCount, 0);
}
m_uiRenderer = std::make_unique<CGL33UIRenderer>(this);
@ -489,9 +329,6 @@ void CGL33Device::Destroy()
m_vboMemory -= m_dynamicBuffer.size;
m_lights.clear();
m_lightsEnabled.clear();
m_currentTextures.clear();
m_texturesEnabled.clear();
m_textureStageParams.clear();
@ -513,8 +350,6 @@ void CGL33Device::ConfigChanged(const DeviceConfig& newConfig)
m_config = newConfig;
// Reset state
m_lighting = false;
glViewport(0, 0, m_config.size.x, m_config.size.y);
// create default framebuffer object
@ -627,51 +462,12 @@ void CGL33Device::SetTransform(TransformType type, const glm::mat4 &matrix)
glUniformMatrix4fv(m_uniforms.projectionMatrix, 1, GL_FALSE, glm::value_ptr(m_projectionMat));
}
else if (type == TRANSFORM_SHADOW)
{
glm::mat4 temp = matrix;
glUniformMatrix4fv(m_uniforms.shadowMatrix, 1, GL_FALSE, glm::value_ptr(temp));
}
else
{
assert(false);
}
}
void CGL33Device::SetMaterial(const Material &material)
{
m_material = material;
glUniform4fv(m_uniforms.ambientColor, 1, m_material.ambient.Array());
glUniform4fv(m_uniforms.diffuseColor, 1, m_material.diffuse.Array());
glUniform4fv(m_uniforms.specularColor, 1, m_material.specular.Array());
}
int CGL33Device::GetMaxLightCount()
{
return m_capabilities.maxLights;
}
void CGL33Device::SetLight(int index, const Light &light)
{
assert(index >= 0);
assert(index < static_cast<int>( m_lights.size() ));
m_lights[index] = light;
m_updateLights = true;
}
void CGL33Device::SetLightEnabled(int index, bool enabled)
{
assert(index >= 0);
assert(index < static_cast<int>( m_lights.size() ));
m_lightsEnabled[index] = enabled;
m_updateLights = true;
}
/** If image is invalid, returns invalid texture.
Otherwise, returns pointer to new Texture struct.
This struct must not be deleted in other way than through DeleteTexture() */
@ -991,8 +787,6 @@ void CGL33Device::SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode
void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount, Color color)
{
if (m_updateLights) UpdateLights();
unsigned int size = vertexCount * sizeof(Vertex);
DynamicBuffer& buffer = m_dynamicBuffer;
@ -1030,8 +824,6 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int
void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount)
{
if (m_updateLights) UpdateLights();
unsigned int size = vertexCount * sizeof(VertexCol);
DynamicBuffer& buffer = m_dynamicBuffer;
@ -1068,8 +860,6 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, i
void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex3D* vertices, int vertexCount)
{
if (m_updateLights) UpdateLights();
unsigned int size = vertexCount * sizeof(Vertex3D);
DynamicBuffer& buffer = m_dynamicBuffer;
@ -1110,8 +900,6 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex3D* vertices, in
void CGL33Device::DrawPrimitives(PrimitiveType type, const Vertex *vertices,
int first[], int count[], int drawCount, Color color)
{
if (m_updateLights) UpdateLights();
int vertexCount = 0;
for (int i = 0; i < drawCount; i++)
@ -1160,8 +948,6 @@ void CGL33Device::DrawPrimitives(PrimitiveType type, const Vertex *vertices,
void CGL33Device::DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
int first[], int count[], int drawCount)
{
if (m_updateLights) UpdateLights();
int vertexCount = 0;
for (int i = 0; i < drawCount; i++)
@ -1239,34 +1025,12 @@ void CGL33Device::SetRenderState(RenderState state, bool enabled)
glDepthMask(enabled ? GL_TRUE : GL_FALSE);
return;
}
else if (state == RENDER_STATE_LIGHTING)
{
m_lighting = enabled;
m_updateLights = true;
//glUniform1i(m_uniforms.lightingEnabled, enabled ? 1 : 0);
return;
}
else if (state == RENDER_STATE_FOG)
{
glUniform1i(m_uniforms.fogEnabled, enabled ? 1 : 0);
return;
}
else if (state == RENDER_STATE_ALPHA_TEST)
{
glUniform1i(m_uniforms.alphaTestEnabled, enabled ? 1 : 0);
return;
}
else if (state == RENDER_STATE_SHADOW_MAPPING)
{
SetTextureEnabled(TEXTURE_SHADOW, enabled);
return;
}
GLenum flag = 0;
@ -1304,31 +1068,6 @@ void CGL33Device::SetClearColor(const Color &color)
glClearColor(color.r, color.g, color.b, color.a);
}
void CGL33Device::SetGlobalAmbient(const Color &color)
{
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color.Array());
}
void CGL33Device::SetFogParams(FogMode mode, const Color &color, float start, float end, float density)
{
// TODO: reimplement
glUniform2f(m_uniforms.fogRange, start, end);
glUniform4f(m_uniforms.fogColor, color.r, color.g, color.b, color.a);
/*
if (mode == FOG_LINEAR) glFogi(GL_FOG_MODE, GL_LINEAR);
else if (mode == FOG_EXP) glFogi(GL_FOG_MODE, GL_EXP);
else if (mode == FOG_EXP2) glFogi(GL_FOG_MODE, GL_EXP2);
else assert(false);
glFogf(GL_FOG_START, start);
glFogf(GL_FOG_END, end);
glFogf(GL_FOG_DENSITY, density);
glFogfv(GL_FOG_COLOR, color.Array());
// */
}
void CGL33Device::SetCullMode(CullMode mode)
{
// Cull clockwise back faces, so front face is the opposite
@ -1338,11 +1077,6 @@ void CGL33Device::SetCullMode(CullMode mode)
else assert(false);
}
void CGL33Device::SetShadowColor(float value)
{
glUniform1f(m_uniforms.shadowColor, value);
}
void CGL33Device::SetFillMode(FillMode mode)
{
if (mode == FillMode::POINT) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
@ -1409,48 +1143,6 @@ inline void CGL33Device::UpdateTextureState(int index)
glUniform1i(m_uniforms.textureEnabled[index], enabled ? 1 : 0);
}
void CGL33Device::UpdateLights()
{
m_updateLights = false;
// If not in normal rendering mode, return immediately
if (m_mode != 0) return;
// Lighting enabled
if (m_lighting)
{
int index = 0;
// Iterate all lights
for (unsigned int i = 0; i < m_lights.size(); i++)
{
// If disabled, ignore and continue
if (!m_lightsEnabled[i]) continue;
// If not directional, ignore and continue
if (m_lights[i].type != LIGHT_DIRECTIONAL) continue;
Light &light = m_lights[i];
LightLocations &uni = m_uniforms.lights[index];
glUniform4fv(uni.ambient, 1, light.ambient.Array());
glUniform4fv(uni.diffuse, 1, light.diffuse.Array());
glUniform4fv(uni.specular, 1, light.specular.Array());
glUniform4f(uni.position, -light.direction.x, -light.direction.y, -light.direction.z, 0.0f);
index++;
}
glUniform1i(m_uniforms.lightCount, index);
}
// Lighting disabled
else
{
glUniform1i(m_uniforms.lightCount, 0);
}
}
inline void CGL33Device::BindVBO(GLuint vbo)
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);

View File

@ -101,9 +101,6 @@ public:
CGL33Device(const DeviceConfig &config);
virtual ~CGL33Device();
void DebugHook() override;
void DebugLights() override;
std::string GetName() override;
bool Create() override;
@ -126,12 +123,6 @@ public:
void SetTransform(TransformType type, const glm::mat4 &matrix) override;
void SetMaterial(const Material &material) override;
int GetMaxLightCount() override;
void SetLight(int index, const Light &light) override;
void SetLightEnabled(int index, bool enabled) override;
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
Texture CreateDepthTexture(int width, int height, int depth) override;
@ -175,14 +166,8 @@ public:
void SetClearColor(const Color &color) override;
void SetGlobalAmbient(const Color &color) override;
void SetFogParams(FogMode mode, const Color &color, float start, float end, float density) override;
void SetCullMode(CullMode mode) override;
void SetShadowColor(float value) override;
void SetFillMode(FillMode mode) override;
void CopyFramebufferToTexture(Texture& texture, int xOffset, int yOffset, int x, int y, int width, int height) override;
@ -211,8 +196,6 @@ private:
void UpdateTextureParams(int index);
//! Updates texture state
inline void UpdateTextureState(int index);
//! Update light parameters
void UpdateLights();
//! Binds VBO
inline void BindVBO(GLuint vbo);
@ -229,29 +212,17 @@ private:
DeviceConfig m_config;
//! Current world matrix
glm::mat4 m_worldMat;
glm::mat4 m_worldMat = glm::mat4(1.0f);
//! Current view matrix
glm::mat4 m_viewMat;
glm::mat4 m_viewMat = glm::mat4(1.0f);
//! OpenGL modelview matrix = world matrix * view matrix
glm::mat4 m_modelviewMat;
glm::mat4 m_modelviewMat = glm::mat4(1.0f);
//! Current projection matrix
glm::mat4 m_projectionMat;
glm::mat4 m_projectionMat = glm::mat4(1.0f);
//! Combined world-view-projection matrix
glm::mat4 m_combinedMatrix;
glm::mat4 m_combinedMatrix = glm::mat4(1.0f);
//! true means combined matrix is outdated
bool m_combinedMatrixOutdated = true;
//! The current material
Material m_material;
//! Whether lighting is enabled
bool m_lighting = false;
//! true means that light update is needed
bool m_updateLights = false;
//! Current lights
std::vector<Light> m_lights;
//! Current lights enable status
std::vector<bool> m_lightsEnabled;
bool m_combinedMatrixOutdated = false;
//! Current textures; \c nullptr value means unassigned
std::vector<Texture> m_currentTextures;
@ -286,8 +257,6 @@ private:
DynamicBuffer m_dynamicBuffer;
//! Current mode
unsigned int m_mode = 0;
//! Uniform locations for all modes
UniformLocations m_uniforms;

View File

@ -135,30 +135,6 @@ private:
std::unique_ptr<CGLFrameBufferPixels> GetGLFrameBufferPixels(const glm::ivec2& size);
struct LightLocations
{
//! true enables light
GLint enabled = -1;
//! Light type
GLint type = -1;
//! Position or direction vector
GLint position = -1;
//! Ambient color
GLint ambient = -1;
//! Diffuse color
GLint diffuse = -1;
//! Specular color
GLint specular = -1;
//! Attenuation
GLint attenuation = -1;
//! Spot light direction
GLint spotDirection = -1;
//! Spot light exponent
GLint spotExponent = -1;
//! Spot light cutoff
GLint spotCutoff = -1;
};
struct UniformLocations
{
// Uniforms
@ -179,8 +155,6 @@ struct UniformLocations
GLint primaryTexture = -1;
//! Secondary texture sampler
GLint secondaryTexture = -1;
//! Shadow texture sampler
GLint shadowTexture = -1;
//! true enables texture
GLint textureEnabled[3] = {};
@ -190,29 +164,6 @@ struct UniformLocations
GLint alphaTestEnabled = -1;
//! Alpha test reference value
GLint alphaReference = -1;
//! true enables fog
GLint fogEnabled = -1;
//! Fog range
GLint fogRange = -1;
//! Fog color
GLint fogColor = -1;
//! Shadow color
GLint shadowColor = -1;
//! Shadow texel size
GLint shadowTexelSize = -1;
// Number of enabled lights
GLint lightCount = -1;
//! Ambient color
GLint ambientColor = -1;
//! Diffuse color
GLint diffuseColor = -1;
//! Specular color
GLint specularColor = -1;
LightLocations lights[8] = {};
};
} // namespace Gfx

View File

@ -20,50 +20,21 @@
// FRAGMENT SHADER - NORMAL MODE
#version 330 core
#define CONFIG_QUALITY_SHADOWS 1
uniform sampler2D uni_PrimaryTexture;
uniform sampler2D uni_SecondaryTexture;
uniform sampler2DShadow uni_ShadowTexture;
uniform bool uni_PrimaryTextureEnabled;
uniform bool uni_SecondaryTextureEnabled;
uniform bool uni_ShadowTextureEnabled;
uniform bool uni_FogEnabled;
uniform vec2 uni_FogRange;
uniform vec4 uni_FogColor;
uniform float uni_ShadowColor;
uniform bool uni_AlphaTestEnabled;
uniform float uni_AlphaReference;
struct LightParams
{
vec4 Position;
vec4 Ambient;
vec4 Diffuse;
vec4 Specular;
};
uniform vec4 uni_AmbientColor;
uniform vec4 uni_DiffuseColor;
uniform vec4 uni_SpecularColor;
uniform int uni_LightCount;
uniform LightParams uni_Light[4];
in VertexData
{
vec4 Color;
vec2 TexCoord0;
vec2 TexCoord1;
vec3 Normal;
vec4 ShadowCoord;
vec4 LightColor;
float Distance;
vec3 CameraDirection;
} data;
out vec4 out_FragColor;
@ -72,49 +43,6 @@ void main()
{
vec4 color = data.Color;
if (uni_LightCount > 0)
{
vec4 ambient = vec4(0.0f);
vec4 diffuse = vec4(0.0f);
vec4 specular = vec4(0.0f);
vec3 normal = normalize(data.Normal);
vec3 camera = normalize(data.CameraDirection);
for (int i = 0; i < uni_LightCount; i++)
{
vec3 lightDirection = uni_Light[i].Position.xyz;
vec3 reflectAxis = normalize(normalize(lightDirection) + camera);
ambient += uni_Light[i].Ambient;
diffuse += clamp(dot(normal, lightDirection), 0.0f, 1.0f)
* uni_Light[i].Diffuse;
specular += pow(clamp(dot(normal, reflectAxis), 0.0f, 1.0f), 10.0f)
* uni_Light[i].Specular;
}
float shadow = 1.0f;
if (uni_ShadowTextureEnabled)
{
float value = texture(uni_ShadowTexture, data.ShadowCoord.xyz);
#ifdef CONFIG_QUALITY_SHADOWS
value += textureOffset(uni_ShadowTexture, data.ShadowCoord.xyz, ivec2( 1, 0))
+ textureOffset(uni_ShadowTexture, data.ShadowCoord.xyz, ivec2(-1, 0))
+ textureOffset(uni_ShadowTexture, data.ShadowCoord.xyz, ivec2( 0, 1))
+ textureOffset(uni_ShadowTexture, data.ShadowCoord.xyz, ivec2( 0,-1));
value = value * (1.0f / 5.0f);
#endif
shadow = mix(uni_ShadowColor, 1.0f, value);
}
vec4 result = uni_AmbientColor * ambient
+ uni_DiffuseColor * diffuse * shadow
+ uni_SpecularColor * specular * shadow;
color = vec4(min(vec3(1.0f), result.rgb), 1.0f);
}
if (uni_PrimaryTextureEnabled)
{
color = color * texture(uni_PrimaryTexture, data.TexCoord0);
@ -125,13 +53,6 @@ void main()
color = color * texture(uni_SecondaryTexture, data.TexCoord1);
}
if (uni_FogEnabled)
{
float interpolate = (data.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)

View File

@ -23,7 +23,6 @@
uniform mat4 uni_ProjectionMatrix;
uniform mat4 uni_ViewMatrix;
uniform mat4 uni_ModelMatrix;
uniform mat4 uni_ShadowMatrix;
uniform mat4 uni_NormalMatrix;
uniform vec3 uni_CameraPosition;
@ -39,10 +38,6 @@ out VertexData
vec2 TexCoord0;
vec2 TexCoord1;
vec3 Normal;
vec4 ShadowCoord;
vec4 LightColor;
float Distance;
vec3 CameraDirection;
} data;
void main()
@ -50,13 +45,9 @@ 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.Normal = normalize((uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz);
data.ShadowCoord = vec4(shadowCoord.xyz / shadowCoord.w, 1.0f);
data.Distance = abs(eyeSpace.z);
data.CameraDirection = uni_CameraPosition - position.xyz;
}