Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am beginner in Opengl.
All my objects get the same texture.
I want them to have three different textures, wood.jpg and Green.jpg and cookie.jpg.
What can I do to fix this.
This is in my code.

Mesh cube;
Mesh Fence;
Mesh GM;

   Ground.LoadModel("O_Ground.obj");
    string name = "Green.jpg";

    glGenTextures(2, texture);
    stbi_set_flip_vertically_on_load(true);
    int iWidth, iHeight, iChannels;
    unsigned char* iData = stbi_load(name.c_str(), &iWidth, &iHeight, &iChannels, 0);
    glBindTexture(GL_TEXTURE_2D, texture[0]); // more than 1 texture use 1, 2, 3,
    glTextureStorage2D(texture[0], 1, GL_RGB8, iWidth, iHeight);
    glTextureSubImage2D(texture[0], 0, 0, 0, iWidth, iHeight, GL_RGB, GL_UNSIGNED_BYTE, iData);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glGenerateMipmap(GL_TEXTURE_2D);

Fence.LoadModel("O_Fence.obj");

string name1 = "wood.jpg";
stbi_set_flip_vertically_on_load(true);
int iWidth1, iHeight1, iChannels1;
unsigned char* iData1 = stbi_load(name1.c_str(), &iWidth1, &iHeight1, &iChannels1, 0);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTextureStorage2D(texture[1], 1, GL_RGB8, iWidth1, iHeight1);
glTextureSubImage2D(texture[1], 0, 0, 0, iWidth1, iHeight1, GL_RGB, GL_UNSIGNED_BYTE, iData1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);



program = glCreateProgram();
string vs_text2 = readShader("vs_model1.glsl"); const char* vs_source2 = vs_text2.c_str();
GLuint vs2 = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs2, 1, &vs_source2, NULL);
glCompileShader(vs2);
checkErrorShader(vs2);
glAttachShader(program, vs2);

string fs_text2 = readShader("fs_model1.glsl"); const char* fs_source2 = fs_text2.c_str();
GLuint fs2 = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs2, 1, &fs_source2, NULL);
glCompileShader(fs2);
checkErrorShader(fs2);
glAttachShader(program, fs2);

glLinkProgram(program);
glUseProgram(program);

// Start from the centre
modelPosition = glm::vec3(0.0f, -0.7f, 0.0f);
modelRotation = glm::vec3(0.0f, 0.0f, 0.0f);

modelPosition = glm::vec3(0.0f, -0.5f, 0.0f);
modelRotation = glm::vec3(0.0f, 0.0f, 0.0f);

modelPosition = glm::vec3(0.0f, -0.7f, 0.0f);
modelRotation = glm::vec3(0.0f, 0.0f, 0.0f);

modelPosition = glm::vec3(0.0f, -0.7f, 0.0f);
modelRotation = glm::vec3(0.0f, 0.0f, 0.0f);

//A few optimizations.
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

// Calculate proj_matrix for the first time.
aspect = (float)windowWidth / (float)windowHeight;
proj_matrix = glm::perspective(glm::radians(fovy), aspect, 0.1f, 1000.0f);


In my render function I have
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
GLint tex_location = glGetUniformLocation(program, "texture[0]");
glUniform1i(tex_location, 0);
Ground.Draw();

glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture[1]);
GLint tex_location1 = glGetUniformLocation(program, "texture[1]");
glUniform1i(tex_location1, 1);
Fence.Draw();





For fs_model1.gsls
 out vec4 color;


in VS_OUT
{
    vec2 tc;
    vec3 normals;
} 

fs_in;


layout(binding=0)uniform sampler2D tex;
  

void main(void)
{
color= texture(tex, fs_in.tc);
}



Previously had this included :
```
layout(binding=0) uniform sampler2D tex;
layout(binding=1) uniform sampler2D tex1;
layout(binding=2) uniform sampler2D tex2;

void main(void)
{
color= texture(tex, fs_in.tc);
color= texture(tex1, fs_in.tc);
color= texture(tex2, fs_in.tc);
}
```

For vs_model1.glsl

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 tc;
layout (location = 2) in vec3 normals;

out VS_OUT
{
    vec2 tc;
    vec3 normals;
} vs_out;

uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 proj_matrix;

void main(void)
{
    gl_Position = proj_matrix * view_matrix * model_matrix * vec4(position, 1.0);

    vs_out.tc = tc;
    vs_out.normals = normals;
}


What I have tried:

I am not able to figure out the issue. All my objects get the last texture.
It would be helpful if someone could edit the code in areas of change because I find it hard to understannd through just text. Thank you.
Posted
Updated 18-Nov-21 0:29am
v3

1 solution

I believe the problem is you are calling glGenTextures(3, texture); three times. That should be called only once. glBindTexture should still be called for each texture.
 
Share this answer
 
Comments
candijen 18-Nov-21 6:27am    
@RickYork I have updated my code and made a few changes because I was trying to figure it out myself, do you mind taking a look at the changes? Now all the objects have Green.jpg on them and if I add layout binding in the fragment shader as I did previously. I get wood.jpg on all objects.
Also thank you for responding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900