I want to load .obj files with c++ and draw it's shapes and materiels to the scene with OpenGl. To do the loading I am using the tinyobjectloader by syoyo. The loading works very well and I am able to draw the obect with a static color or a static diffuse texture. So I am able to give all the vericies and texture coordinates into the shader using buffers. My Problem now is how to set the right color in the pixel shader. The shapes given by the loader class have a mesh and a meterial, which are defined as follows:
typedef struct {
std::string name;
float ambient[3];
float diffuse[3];
float specular[3];
float transmittance[3];
float emission[3];
float shininess;
float ior; // index of refraction
float dissolve; // 1 == opaque; 0 == fully transparent
// illumination model (see http://www.fileformat.info/format/material/)
int illum;
std::string ambient_texname;
std::string diffuse_texname;
std::string specular_texname;
std::string normal_texname;
std::map<std::string, std::string> unknown_parameter;
} material_t;
typedef struct
{
std::vector<float> positions;
std::vector<float> normals;
std::vector<float> texcoords;
std::vector<unsigned int> indices;
} mesh_t;
I would like to set all the colos and different textures as uniforms. For example, the diffuse color would be set like this:
GLuint diffColLocation = glGetUniformLocation(programmId, "diffuseColor");
glUniform3f(diffColLocation, this->material.diffuse[0], this->material.diffuse[1], this->material.diffuse[2]);
And the Textures like this:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->ambientTextureID);
glUniform1i(this->diffuseTextureUniformIndex, 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, this->diffuseTextureID);
glUniform1i(this->diffuseTextureUniformIndex, 0);
etc.
I was also able to load an object, where all shapes had a diffuse texture given. So no problem here. My problem ist, how to deal with unset textures. For example many objects don't have an specular texture set. Can I somehow create an default texture?
My Pixel shader looks like this at the moment:
#version 150 core
#extension GL_ARB_explicit_attrib_location: enable
// output to the (default) FBO in color buffer 0
layout (location = 0) out vec4 outColor;
in vec2 fragTextCoords;
uniform sampler2D ambientTex;
uniform sampler2D diffuseTex;
uniform sampler2D specularTex;
uniform vec3 ambientColor;
uniform vec3 diffuseColor;
uniform vec3 specularColor;
// main entry point for the fragment shader
void main() {
//vec3 tempColor ambientColor + diffuseTex * specularTex; //Is that correct?
//outcolor = tempColor * texture(diffuseTex, fragTextCoords); //Then this?
outcolor = texture(diffuseTex, fragTextCoords);
}
So only the texture is being shown and this only works if every shape has a diffuse texture. But how to I combine every color and texture together in such a way, that the object looks just like in my 3d software? What happens if one of the colors is null or when an sampler2D is null?