Implemented per-vertex lighting and set it as default in OpenGL 3.3 engine

master
Tomasz Kapuściński 2015-05-22 18:53:51 +02:00
parent 8abdd4b740
commit fa67e815b8
6 changed files with 335 additions and 81 deletions

View File

@ -23,6 +23,7 @@
#include "common/config.h"
#include "common/image.h"
#include "common/logger.h"
#include "common/profile.h"
#include "math/geometry.h"
@ -50,6 +51,8 @@ CGL33Device::CGL33Device(const GLDeviceConfig &config)
m_colorBuffer = 0;
m_depthBuffer = 0;
m_offscreenRenderingEnabled = false;
m_perPixelLighting = false;
}
@ -243,15 +246,33 @@ bool CGL33Device::Create()
GetLogger()->Info("CDevice created successfully\n");
int value;
if (CProfile::GetInstance().GetIntProperty("Setup", "PerPixelLighting", value))
{
m_perPixelLighting = value > 0;
}
if (m_perPixelLighting)
CLogger::GetInstance().Info("Using per-pixel lighting\n");
else
CLogger::GetInstance().Info("Using per-vertex lighting\n");
// Create shader program
GLchar source[65536];
const GLchar *sources[] = { source };
PHYSFS_file *file = PHYSFS_openRead("shaders/vertex_shader_33.glsl");
char filename[64];
if (m_perPixelLighting)
sprintf(filename, "shaders/vertex_shader_33_perpixel.glsl");
else
sprintf(filename, "shaders/vertex_shader_33_pervertex.glsl");
PHYSFS_file *file = PHYSFS_openRead(filename);
if (file == nullptr)
{
CLogger::GetInstance().Error("Cannot read vertex shader code file!\n");
assert(false);
return false;
}
int length = PHYSFS_read(file, source, 1, 65536);
@ -277,14 +298,19 @@ bool CGL33Device::Create()
GetLogger()->Error("Vertex shader compilation error occured!\n%s\n", message);
delete[] message;
assert(false);
return false;
}
file = PHYSFS_openRead("shaders/fragment_shader_33.glsl");
if (m_perPixelLighting)
sprintf(filename, "shaders/fragment_shader_33_perpixel.glsl");
else
sprintf(filename, "shaders/fragment_shader_33_pervertex.glsl");
file = PHYSFS_openRead(filename);
if (file == nullptr)
{
CLogger::GetInstance().Error("Cannot read fragment shader code file!\n");
assert(false);
return false;
}
length = PHYSFS_read(file, source, 1, 65536);
@ -309,7 +335,7 @@ bool CGL33Device::Create()
GetLogger()->Error("Fragment shader compilation error occured!\n%s\n", message);
delete[] message;
assert(false);
return false;
}
m_shaderProgram = glCreateProgram();
@ -334,7 +360,7 @@ bool CGL33Device::Create()
GetLogger()->Error("Shader program linking error occured!\n%s\n", message);
delete[] message;
assert(false);
return false;
}
glDeleteShader(vertexShader);
@ -1218,7 +1244,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int
}
else
{
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
info.size = size;
// Vertex coordinate
@ -1269,7 +1295,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices,
}
else
{
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
info.size = size;
// Vertex coordinate
@ -1320,7 +1346,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, i
}
else
{
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
info.size = size;
// Vertex coordinate
@ -1498,16 +1524,17 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
return;
VertexBufferInfo& info = (*it).second;
unsigned int size = vertexCount * sizeof(Vertex);
bool changed = (info.vertexType != VERTEX_TYPE_NORMAL) || (size > info.size);
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL;
info.vertexCount = vertexCount;
glBindVertexArray(info.vao);
glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
unsigned int size = vertexCount * sizeof(Vertex);
if (info.size < size)
{
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@ -1518,28 +1545,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
}
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
if (changed) // Update vertex array bindings
{
glBindVertexArray(info.vao);
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texCoord)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texCoord)));
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount)
@ -1549,15 +1582,16 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
return;
VertexBufferInfo& info = (*it).second;
unsigned int size = vertexCount * sizeof(VertexTex2);
bool changed = (info.vertexType != VERTEX_TYPE_TEX2) || (size > info.size);
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
glBindVertexArray(info.vao);
glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
unsigned int size = vertexCount * sizeof(VertexTex2);
if (info.size < size)
{
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@ -1568,28 +1602,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
}
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
if (changed) // Update vertex array bindings
{
glBindVertexArray(info.vao);
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, normal)));
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, normal)));
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Texture coordinate 1
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord)));
// Texture coordinate 1
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
@ -1599,15 +1639,17 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
return;
VertexBufferInfo& info = (*it).second;
unsigned int size = vertexCount * sizeof(VertexCol);
bool changed = (info.vertexType != VERTEX_TYPE_TEX2) || (size > info.size);
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
glBindVertexArray(info.vao);
glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
unsigned int size = vertexCount * sizeof(VertexCol);
if (info.size < size)
{
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@ -1618,28 +1660,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
}
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
if (changed) // Update vertex array bindings
{
glBindVertexArray(info.vao);
// Normal
glDisableVertexAttribArray(1);
glVertexAttrib3f(1, 0.0f, 0.0f, 1.0f);
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
// Color
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, color)));
// Normal
glDisableVertexAttribArray(1);
glVertexAttrib3f(1, 0.0f, 0.0f, 1.0f);
// Texture coordinate 0
glDisableVertexAttribArray(3);
glVertexAttrib2f(3, 0.0f, 0.0f);
// Color
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, color)));
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
// Texture coordinate 0
glDisableVertexAttribArray(3);
glVertexAttrib2f(3, 0.0f, 0.0f);
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void CGL33Device::DrawStaticBuffer(unsigned int bufferId)

View File

@ -227,6 +227,8 @@ private:
//! Shader program
GLuint m_shaderProgram;
//! true enables per-pixel lighting
bool m_perPixelLighting;
//! Auxilliary vertex buffers for general rendering
unsigned int m_vertex;

View File

@ -1,7 +1,7 @@
/*
* 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
* 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
@ -57,8 +57,7 @@ uniform bool uni_SmoothShading;
in VertexData
{
vec3 NormalSmooth;
flat vec3 NormalFlat;
vec3 Normal;
vec4 Color;
vec2 TexCoord0;
vec2 TexCoord1;
@ -83,10 +82,7 @@ void main()
{
if(uni_Light[i].Enabled)
{
ambient += uni_Light[i].Ambient;
vec3 normal = (uni_SmoothShading ? data.NormalSmooth : data.NormalFlat);
normal = (gl_FrontFacing ? normal : -normal);
vec3 normal = (gl_FrontFacing ? data.Normal : -data.Normal);
vec3 lightDirection = vec3(0.0f);
float atten;
@ -110,6 +106,7 @@ void main()
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;
}

View File

@ -0,0 +1,83 @@
/*
* 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
*/
// FRAGMENT SHADER - PER-VERTEX LIGHTING
#version 330
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 bool uni_AlphaTestEnabled;
uniform float uni_AlphaReference;
in VertexData
{
vec4 Color;
vec2 TexCoord0;
vec2 TexCoord1;
vec4 ShadowCoord;
vec4 LightColor;
float Distance;
} data;
out vec4 out_FragColor;
void main()
{
vec4 color = data.Color;
if (uni_PrimaryTextureEnabled)
{
color = color * texture(uni_PrimaryTexture, data.TexCoord0);
}
if (uni_SecondaryTextureEnabled)
{
color = color * texture(uni_SecondaryTexture, data.TexCoord1);
}
if (uni_ShadowTextureEnabled)
{
color = color * (0.35f + 0.65f * texture(uni_ShadowTexture, data.ShadowCoord.xyz));
}
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)
discard;
}
out_FragColor = color;
}

View File

@ -1,7 +1,7 @@
/*
* 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
* 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
@ -34,8 +34,7 @@ layout(location = 4) in vec2 in_TexCoord1;
out VertexData
{
vec3 NormalSmooth;
flat vec3 NormalFlat;
vec3 Normal;
vec4 Color;
vec2 TexCoord0;
vec2 TexCoord1;
@ -53,8 +52,7 @@ void main()
vec3 normal = normalize((uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz);
data.Color = in_Color;
data.NormalSmooth = normal;
data.NormalFlat = normal;
data.Normal = normal;
data.TexCoord0 = in_TexCoord0;
data.TexCoord1 = in_TexCoord1;
data.ShadowCoord = uni_ShadowMatrix * position;

View File

@ -0,0 +1,126 @@
/*
* 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
*/
// VERTEX SHADER - PER-VERTEX LIGHTING
#version 330
struct LightParams
{
bool Enabled;
vec4 Position;
vec4 Ambient;
vec4 Diffuse;
vec4 Specular;
float Shininess;
vec3 Attenuation;
};
uniform vec4 uni_AmbientColor;
uniform vec4 uni_DiffuseColor;
uniform vec4 uni_SpecularColor;
uniform bool uni_LightingEnabled;
uniform LightParams uni_Light[8];
uniform mat4 uni_ProjectionMatrix;
uniform mat4 uni_ViewMatrix;
uniform mat4 uni_ModelMatrix;
uniform mat4 uni_ShadowMatrix;
uniform mat4 uni_NormalMatrix;
layout(location = 0) in vec4 in_VertexCoord;
layout(location = 1) in vec3 in_Normal;
layout(location = 2) in vec4 in_Color;
layout(location = 3) in vec2 in_TexCoord0;
layout(location = 4) in vec2 in_TexCoord1;
out VertexData
{
vec4 Color;
vec2 TexCoord0;
vec2 TexCoord1;
vec4 ShadowCoord;
vec4 LightColor;
float Distance;
} data;
void main()
{
vec4 position = uni_ModelMatrix * in_VertexCoord;
vec4 eyeSpace = uni_ViewMatrix * position;
gl_Position = uni_ProjectionMatrix * eyeSpace;
data.Color = in_Color;
data.TexCoord0 = in_TexCoord0;
data.TexCoord1 = in_TexCoord1;
data.ShadowCoord = uni_ShadowMatrix * position;
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);
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)
{
lightDirection = uni_Light[i].Position.xyz;
atten = 1.0f;
}
// Point light
else
{
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;
}
}