Fixups in MTL loader.

Add specular lighting.
This commit is contained in:
Themaister 2013-05-12 13:20:33 +02:00
parent 54e6aa5e3e
commit d663d5af9a
8 changed files with 46 additions and 12 deletions

View File

@ -228,22 +228,37 @@ static void init_mesh(const string& path)
"varying vec4 vPos;\n"
"uniform sampler2D sDiffuse;\n"
"uniform sampler2D sAmbient;\n"
"uniform vec3 uLightDir;\n"
"uniform vec3 uLightAmbient;\n"
"uniform vec3 uMTLAmbient;\n"
"uniform float uMTLAlphaMod;\n"
"uniform vec3 uMTLDiffuse;\n"
"uniform vec3 uMTLSpecular;\n"
"uniform float uMTLSpecularPower;\n"
"void main() {\n"
" vec4 colorDiffuse = texture2D(sDiffuse, vTex);\n"
" if (colorDiffuse.a < 0.5) discard;\n"
" vec4 colorDiffuseFull = texture2D(sDiffuse, vTex);\n"
" vec4 colorAmbientFull = texture2D(sAmbient, vTex);\n"
" if (colorDiffuseFull.a < 0.5)\n"
" discard;\n"
" vec3 colorDiffuse = mix(uMTLDiffuse, colorDiffuseFull.rgb, vec3(colorDiffuseFull.a));\n"
" vec3 colorAmbient = mix(uMTLAmbient, colorAmbientFull.rgb, vec3(colorAmbientFull.a));\n"
" vec3 normal = normalize(vNormal.xyz);\n"
" float directivity = dot(uLightDir, -normal);\n"
" vec3 diffuse = colorDiffuse.rgb * (clamp(directivity, 0.0, 1.0) + uLightAmbient * uMTLAmbient);\n"
" vec3 diffuse = colorDiffuse * clamp(directivity, 0.0, 1.0);\n"
" vec3 ambient = colorAmbient * uLightAmbient;\n"
" gl_FragColor = vec4(diffuse, uMTLAlphaMod * colorDiffuse.a);\n"
" vec3 modelToFace = normalize(-vPos.xyz);\n"
" float specularity = pow(clamp(dot(modelToFace, reflect(uLightDir, normal)), 0.0, 1.0), uMTLSpecularPower);\n"
" vec3 specular = uMTLSpecular * specularity;\n"
" gl_FragColor = vec4(diffuse + ambient + specular, uMTLAlphaMod * colorDiffuseFull.a);\n"
"}";
shared_ptr<Shader> shader(new Shader(vertex_shader, fragment_shader));

View File

@ -85,6 +85,7 @@ namespace GL
shader->use();
SYM(glUniform1i)(shader->uniform("sDiffuse"), 0);
SYM(glUniform1i)(shader->uniform("sAmbient"), 0);
SYM(glUniformMatrix4fv)(shader->uniform("uModel"),
1, GL_FALSE, value_ptr(model));
@ -97,6 +98,8 @@ namespace GL
1, value_ptr(material.diffuse));
SYM(glUniform3fv)(shader->uniform("uMTLSpecular"),
1, value_ptr(material.specular));
SYM(glUniform1f)(shader->uniform("uMTLSpecularPower"),
material.specular_power);
SYM(glUniform1f)(shader->uniform("uMTLAlphaMod"),
material.alpha_mod);

View File

@ -27,12 +27,14 @@ namespace GL
ambient(1, 1, 1),
diffuse(1, 1, 1),
specular(0, 0, 0),
specular_power(60.0),
alpha_mod(1.0f)
{}
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
float specular_power;
float alpha_mod;
std1::shared_ptr<Texture> diffuse_map;
};

View File

@ -121,7 +121,7 @@ namespace OBJ
for (string line; getline(file, line); )
{
line = line.substr(0, line.find_first_of('\r'));
line = String::strip(line);
size_t split_point = line.find_first_of(' ');
string type = line.substr(0, split_point);
@ -141,8 +141,12 @@ namespace OBJ
current.diffuse = parse_line<vec3>(data);
else if (type == "Ks")
current.specular = parse_line<vec3>(data);
else if (type == "Tr" || type == "d")
else if (type == "Ns")
current.specular_power = String::stof(data);
else if (type == "d")
current.alpha_mod = String::stof(data);
else if (type == "Tr")
current.alpha_mod = 1.0f - String::stof(data);
else if (type == "map_Kd")
{
string diffuse_path = Path::join(Path::basedir(path), data);
@ -176,7 +180,7 @@ namespace OBJ
for (string line; getline(file, line); )
{
line = line.substr(0, line.find_first_of('\r'));
line = String::strip(line);
size_t split_point = line.find_first_of(' ');
string type = line.substr(0, split_point);
@ -226,7 +230,7 @@ namespace OBJ
current_material = materials[data];
}
else if (type == "mtllib")
materials = parse_mtllib(Path::join(Path::basedir(path), data + ".mtl"));
materials = parse_mtllib(Path::join(Path::basedir(path), data));
}
if (vertices.size())

View File

@ -30,7 +30,7 @@ namespace GL
SYM(glGetProgramInfoLog)(prog, len, &out_len, &buf[0]);
char err_str[256];
snprintf(err_str, sizeof(err_str), "Link error: %s", &buf[0]);
snprintf(err_str, sizeof(err_str), "Link error: %s\n", &buf[0]);
retro_stderr(err_str);
}
}
@ -58,7 +58,7 @@ namespace GL
SYM(glGetShaderInfoLog)(shader, len, &out_len, &buf[0]);
char err_str[256];
snprintf(err_str, sizeof(err_str), "Shader error: %s", &buf[0]);
snprintf(err_str, sizeof(err_str), "Shader error: %s\n", &buf[0]);
retro_stderr(err_str);
}

View File

@ -1,4 +1,4 @@
mtllib simple
mtllib simple.mtl
v -1.0 -1.0 0.0
v 1.0 -1.0 0.0

View File

@ -45,7 +45,7 @@ namespace GL
else
{
char err_str[256];
snprintf(err_str, sizeof(err_str), "Failed to load image: %s", path.c_str());
snprintf(err_str, sizeof(err_str), "Failed to load image: %s\n", path.c_str());
retro_stderr(err_str);
}
}

View File

@ -56,6 +56,16 @@ namespace String
{
return static_cast<float>(std::strtod(str.c_str(), NULL));
}
inline std::string strip(const std::string& str)
{
size_t first_non_space = str.find_first_not_of(" \t\r");
if (first_non_space == std::string::npos)
return "";
return str.substr(first_non_space,
str.find_last_not_of(" \t\r") - first_non_space + 1);
}
}
#endif