1
\$\begingroup\$

I am lerning opengl programming from http: //learnopengl.com .And i can load any model which i want.But if i want to load two models with different shaders,a problem occurs. For example: First i draw two models with same shader and there is no problem.

Model1.Draw(shader);
Model2.Draw(shader);

Then if i do this:

Model1.Draw(shader);
Model2.Draw(otherShader);

The first model is not drown.Only the second model is drown. Here is my Model.h code and Mesh.h code:

Model.h: Model.h

Mesh.h Mesh.h

And if you want, these are my vertex and fragment shaders:

Shaders: http: //pastebin.com/d0v3Y4vU

Thank you for help!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The first line in the following code block is unnecessary:

glUniform1i( glGetUniformLocation( shader.getProgram(), ("material." + name + number ).c_str( ) ), i );
glActiveTexture( GL_TEXTURE0 + i );
glBindTexture( GL_TEXTURE_2D, this->textures[i].id );

So remove it, and change the following GLSL code:

struct Material
{
    sampler2D texture_diffuse1;
    sampler2D texture_specular1;
    float shininess;
};

To:

uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;

Texture samplers must be defined as a uniform. Though I've never attempted that sort of GLSL code before, I'm surprised it even compiles.

You are confusing concepts here. Typically a non texture material would look like this:

struct Material
{
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    float shininess;
};

So, if using a mix of textures and values:

uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;
uniform float shininess;

Then bind the two textures, and the uniform for shininess separately. Keep your code simple, it really is the best way.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .