Fix normals for normal object.

This commit is contained in:
vlj 2014-03-29 01:22:08 +01:00
parent 9406d69a72
commit ca1a1e37d9
4 changed files with 49 additions and 41 deletions

View File

@ -13,42 +13,8 @@ in vec4 Color;
out vec3 nor; out vec3 nor;
out vec2 uv; out vec2 uv;
mat4 getMatrixFromRotation(vec3 rotation) mat4 getWorldMatrix(vec3 translation, vec3 rotation);
{ mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation);
// from irrlicht
float cr = cos( rotation.z );
float sr = sin( rotation.z );
float cp = cos( rotation.x );
float sp = sin( rotation.x );
float cy = cos( rotation.y );
float sy = sin( rotation.y );
float srsp = sr*sp;
float crsp = cr*sp;
return mat4(
vec4(cp * cy, srsp * cy - cr * sy, crsp * cy + sr * sy, 0.),
vec4(cp * sy, srsp * sy + cr * cy, crsp * sy - sr * cy, 0.),
vec4(-sp, sr * cp, cr * cp, 0.),
vec4(0., 0., 0., 1.));
}
mat4 getWorldMatrix(vec3 translation, vec3 rotation)
{
mat4 result = getMatrixFromRotation(rotation);
// translation
result[3].xyz += translation;
return result;
}
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation)
{
mat4 result = transpose(getMatrixFromRotation(rotation));
// FIXME: it's wrong but the fourth column is not used
result[3].xyz -= translation;
return result;
}
void main() void main()
{ {

View File

@ -1,5 +1,5 @@
uniform mat4 ViewProjectionMatrix; uniform mat4 ViewProjectionMatrix;
uniform mat4 ViewMatrix; uniform mat4 InverseViewMatrix;
in vec3 Origin; in vec3 Origin;
in vec3 Orientation; in vec3 Orientation;
@ -11,11 +11,13 @@ in vec2 Texcoord;
out vec3 nor; out vec3 nor;
out vec2 uv; out vec2 uv;
mat4 getWorldMatrix(vec3 translation, vec3 rotation);
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation);
void main(void) void main(void)
{ {
mat4 ModelMatrix = mat4(1.); mat4 ModelMatrix = getWorldMatrix(Origin, Orientation);
ModelMatrix[3].xyz += Origin; mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation) * InverseViewMatrix);
mat4 TransposeInverseModelView = transpose(inverse(ViewMatrix * ModelMatrix));
gl_Position = ViewProjectionMatrix * ModelMatrix * vec4(Position, 1.); gl_Position = ViewProjectionMatrix * ModelMatrix * vec4(Position, 1.);
nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz; nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz;
uv = Texcoord; uv = Texcoord;

View File

@ -0,0 +1,36 @@
mat4 getMatrixFromRotation(vec3 rotation)
{
// from irrlicht
float cr = cos( rotation.z );
float sr = sin( rotation.z );
float cp = cos( rotation.x );
float sp = sin( rotation.x );
float cy = cos( rotation.y );
float sy = sin( rotation.y );
float srsp = sr*sp;
float crsp = cr*sp;
return mat4(
vec4(cp * cy, srsp * cy - cr * sy, crsp * cy + sr * sy, 0.),
vec4(cp * sy, srsp * sy + cr * cy, crsp * sy - sr * cy, 0.),
vec4(-sp, sr * cp, cr * cp, 0.),
vec4(0., 0., 0., 1.));
}
mat4 getWorldMatrix(vec3 translation, vec3 rotation)
{
mat4 result = getMatrixFromRotation(rotation);
// translation
result[3].xyz += translation;
return result;
}
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation)
{
mat4 result = transpose(getMatrixFromRotation(rotation));
// FIXME: it's wrong but the fourth column is not used
result[3].xyz -= translation;
return result;
}

View File

@ -514,6 +514,7 @@ namespace MeshShader
void InstancedObjectPass1Shader::init() void InstancedObjectPass1Shader::init()
{ {
Program = LoadProgram( Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(),
GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass1.frag").c_str()); GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass1.frag").c_str());
@ -521,7 +522,7 @@ namespace MeshShader
attrib_position = glGetAttribLocation(Program, "Position"); attrib_position = glGetAttribLocation(Program, "Position");
attrib_normal = glGetAttribLocation(Program, "Normal"); attrib_normal = glGetAttribLocation(Program, "Normal");
uniform_MP = glGetUniformLocation(Program, "ViewProjectionMatrix"); uniform_MP = glGetUniformLocation(Program, "ViewProjectionMatrix");
uniform_VM = glGetUniformLocation(Program, "ViewMatrix"); uniform_VM = glGetUniformLocation(Program, "InverseViewMatrix");
} }
void InstancedObjectPass1Shader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &ViewMatrix) void InstancedObjectPass1Shader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &ViewMatrix)
@ -545,6 +546,7 @@ namespace MeshShader
void InstancedGrassPass1Shader::init() void InstancedGrassPass1Shader::init()
{ {
Program = LoadProgram( Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(),
GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str()); GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str());
@ -627,6 +629,7 @@ namespace MeshShader
void InstancedObjectPass2Shader::init() void InstancedObjectPass2Shader::init()
{ {
Program = LoadProgram( Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(),
GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass2.frag").c_str()); GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass2.frag").c_str());
@ -926,6 +929,7 @@ namespace MeshShader
void InstancedGrassPass2Shader::init() void InstancedGrassPass2Shader::init()
{ {
Program = LoadProgram( Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(),
GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass2.frag").c_str()); GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass2.frag").c_str());