c++ – Opengl. A single texture is applied to all my objects. glTextureStorage2D Error faced


I can’t seem to figure out why a single texture is applied to all my objects.
I have three objects – fence, ground and cone but they get weirdly placed textures or a single texture on all of them. This is my code below including fragment and vertex shadder. I am a beginner and I really need help.

GLuint texture[3];

Fence.LoadModel("O_Fence.obj");
            Ground.LoadModel("O_Ground.obj");
            O_Roof.LoadModel("O_Roof.obj");

            string names[] = {"wood.jpg","Green.jpg","cone.jpg"};
            glGenTextures(3, texture);
            for( string name :names)
            { stbi_set_flip_vertically_on_load(true);
            
            for (int i=0;i<3;i++)
             {
            
            int iWidth, iHeight, iChannels;
            unsigned char* iData = stbi_load(name.c_str(), &iWidth, &iHeight, &iChannels, 0);
            glBindTexture(GL_TEXTURE_2D, texture[i]); 
glTextureStorage2D(texture[i], 1, GL_RGB8, iWidth, iHeight);
            glTextureSubImage2D(texture[i], 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_MIPMAP_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);

In my render function :

glUseProgram(program);
    
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    GLint tex_location = glGetUniformLocation(program, "tex");
    glUniform1i(tex_location, 0);
    Ground.Draw();


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

    glActiveTexture(GL_TEXTURE0 + 2);
    glBindTexture(GL_TEXTURE_2D, texture[2]);
    GLint tex_location2 = glGetUniformLocation(program, "tex2");
    glUniform1i(tex_location2, 2);
    Fence.Draw();
    
    
    Fence.Draw();
    Ground.Draw();
    O_Roof.Draw();

My fsmodel1 :

out vec4 color;


in VS_OUT
{
    vec2 tc;
    vec3 normals;
} 

fs_in;


layout(binding=1)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);
}

My vsmodel1

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;
}



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email