Properly handle unlit object.

This commit is contained in:
Vincent Lejeune 2014-01-25 21:00:02 +01:00
parent 8d0bf49cbd
commit 2e994502e3
7 changed files with 93 additions and 10 deletions

View File

@ -0,0 +1,11 @@
#version 130
uniform sampler2D tex;
in vec2 uv;
out vec4 FragColor;
void main(void)
{
vec4 color = texture(tex, uv);
if (color.a < 0.5) discard;
FragColor = vec4(color.xyz, 1.);
}

View File

@ -653,18 +653,14 @@ void Material::setMaterialProperties(video::SMaterial *m, scene::IMeshBuffer* m
}
if (!m_lighting && irr_driver->isGLSL() && !m_alpha_blending && !m_add)
{
// we abuse alpha blender a little here : in the shader-based pipeline,
// transparent objects are rendered after lighting has been applied.
// Therefore, pretending the object is transparent will have the effect
// of making it unaffected by lights
m_alpha_blending = true;
m_disable_z_write = false;
}
int modes = 0;
if (!m_lighting && irr_driver->isGLSL() && !m_alpha_blending && !m_add)
{
m->MaterialType = irr_driver->getShader(ES_OBJECT_UNLIT);
modes++;
}
if (m_alpha_testing)
{
m->MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;

View File

@ -145,6 +145,8 @@ void Shaders::loadShaders()
m_shaders[ES_OBJECTPASS] = glslmat(dir + "objectpass.vert", dir + "objectpass.frag",
m_callbacks[ES_OBJECTPASS], EMT_SOLID);
m_shaders[ES_OBJECT_UNLIT] = glslmat(dir + "objectpass.vert", dir + "objectpass.frag",
m_callbacks[ES_OBJECTPASS], EMT_SOLID);
m_shaders[ES_OBJECTPASS_REF] = glslmat(dir + "objectpass.vert", dir + "objectpass_ref.frag",
m_callbacks[ES_OBJECTPASS], EMT_SOLID);
m_shaders[ES_OBJECTPASS_RIMLIT] = glslmat(dir + "objectpass_rimlit.vert", dir + "objectpass_rimlit.frag",
@ -245,6 +247,7 @@ void Shaders::loadShaders()
MeshShader::ObjectRimLimitShader::init();
MeshShader::UntexturedObjectShader::init();
MeshShader::ObjectRefPass2Shader::init();
MeshShader::ObjectUnlitShader::init();
MeshShader::SphereMapShader::init();
MeshShader::SplattingShader::init();
MeshShader::GrassPass1Shader::init();
@ -370,6 +373,27 @@ namespace MeshShader
glUniform3f(uniform_ambient, s.r, s.g, s.b);
}
GLuint ObjectUnlitShader::Program;
GLuint ObjectUnlitShader::attrib_position;
GLuint ObjectUnlitShader::attrib_texcoord;
GLuint ObjectUnlitShader::uniform_MVP;
GLuint ObjectUnlitShader::uniform_tex;
void ObjectUnlitShader::init()
{
Program = LoadProgram(file_manager->getAsset("shaders/object_pass2.vert").c_str(), file_manager->getAsset("shaders/object_unlit.frag").c_str());
attrib_position = glGetAttribLocation(Program, "Position");
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
uniform_MVP = glGetUniformLocation(Program, "ModelViewProjectionMatrix");
uniform_tex = glGetUniformLocation(Program, "tex");
}
void ObjectUnlitShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, unsigned TU_tex)
{
glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelViewProjectionMatrix.pointer());
glUniform1i(uniform_tex, TU_tex);
}
GLuint ObjectRimLimitShader::Program;
GLuint ObjectRimLimitShader::attrib_position;
GLuint ObjectRimLimitShader::attrib_texcoord;

View File

@ -81,6 +81,17 @@ public:
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, unsigned TU_DiffuseMap, unsigned TU_SpecularMap, unsigned TU_SSAO);
};
class ObjectUnlitShader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord;
static GLuint uniform_MVP, uniform_tex;
static void init();
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, unsigned TU_tex);
};
class ObjectRefPass2Shader
{
public:
@ -419,6 +430,7 @@ public:
ACT(ES_COLORIZE) \
ACT(ES_COLORIZE_REF) \
ACT(ES_GLOW) \
ACT(ES_OBJECT_UNLIT) \
ACT(ES_OBJECTPASS) \
ACT(ES_OBJECTPASS_REF) \
ACT(ES_SUNLIGHT) \

View File

@ -79,6 +79,8 @@ void STKAnimatedMesh::drawSolid(const GLMesh &mesh, video::E_MATERIAL_TYPE type)
drawObjectRefPass2(mesh, ModelViewProjectionMatrix);
else if (type == irr_driver->getShader(ES_OBJECTPASS_RIMLIT))
drawObjectRimLimit(mesh, ModelViewProjectionMatrix, TransposeInverseModelView);
else if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
drawObjectUnlit(mesh, ModelViewProjectionMatrix);
else
drawObjectPass2(mesh, ModelViewProjectionMatrix);
break;
@ -99,6 +101,8 @@ isObjectPass(video::E_MATERIAL_TYPE type)
return true;
if (type == irr_driver->getShader(ES_OBJECTPASS_RIMLIT))
return true;
if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
return true;
if (type == video::EMT_ONETEXTURE_BLEND)
return true;
return false;

View File

@ -508,6 +508,32 @@ void drawObjectRimLimit(const GLMesh &mesh, const core::matrix4 &ModelViewProjec
glDrawElements(ptype, count, itype, 0);
}
void drawObjectUnlit(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix)
{
GLenum ptype = mesh.PrimitiveType;
GLenum itype = mesh.IndexType;
size_t count = mesh.IndexCount;
setTexture(0, mesh.textures[0], GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
if (irr_driver->getLightViz())
{
GLint swizzleMask[] = { GL_ONE, GL_ONE, GL_ONE, GL_ALPHA };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
else
{
GLint swizzleMask[] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
glUseProgram(MeshShader::ObjectUnlitShader::Program);
MeshShader::ObjectUnlitShader::setUniforms(ModelViewProjectionMatrix, 0);
glBindVertexArray(mesh.vao_second_pass);
glDrawElements(ptype, count, itype, 0);
}
void drawObjectPass2(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix)
{
GLenum ptype = mesh.PrimitiveType;
@ -687,6 +713,8 @@ void STKMesh::drawSolid(const GLMesh &mesh, video::E_MATERIAL_TYPE type)
drawGrassPass2(mesh, ModelViewProjectionMatrix, windDir);
else if (type == irr_driver->getShader(ES_OBJECTPASS_RIMLIT))
drawObjectRimLimit(mesh, ModelViewProjectionMatrix, TransposeInverseModelView);
else if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
drawObjectUnlit(mesh, ModelViewProjectionMatrix);
else if (!mesh.textures[0])
drawUntexturedObject(mesh, ModelViewProjectionMatrix);
else
@ -720,6 +748,8 @@ static bool isObject(video::E_MATERIAL_TYPE type)
return true;
if (type == irr_driver->getShader(ES_BUBBLES))
return true;
if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
return true;
if (type == video::EMT_TRANSPARENT_ALPHA_CHANNEL)
return true;
if (type == video::EMT_ONETEXTURE_BLEND)
@ -783,6 +813,11 @@ void initvaostate(GLMesh &mesh, video::E_MATERIAL_TYPE type)
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
MeshShader::GrassPass2Shader::attrib_position, MeshShader::GrassPass2Shader::attrib_texcoord, -1, -1, -1, -1, MeshShader::GrassPass2Shader::attrib_color, mesh.Stride);
}
else if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
{
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
MeshShader::ObjectUnlitShader::attrib_position, MeshShader::ObjectUnlitShader::attrib_texcoord, -1, -1, -1, -1, -1, mesh.Stride);
}
else if (!mesh.textures[0])
{
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,

View File

@ -42,6 +42,7 @@ void drawSphereMap(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionM
void drawSplatting(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix);
void drawGrassPass2(const GLMesh &mesh, const core::matrix4 & ModelViewProjectionMatrix, core::vector3df windDir);
void drawObjectRimLimit(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeInverseModelView);
void drawObjectUnlit(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix);
// Forward pass (for transparents meshes)
void drawTransparentObject(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix);