0
votes

I think I didn't quite get texture units and texture slots...

For some reason only one texture is rendering, I added even new texture slots for the same reason but seam to not work.

Main loop:

void Test::Play() const
{
    //Windows
    Window win;
    Renderer* renderer = new Renderer();

    //Sound
    Sound s;
    s.testSound();

    //Object 1 
    Texture tex("res/textures/Te.png");
    tex.BindTextureSlot();
    CubeObject* cubeObject = new CubeObject("res/shaders/Cube.shader");
    cubeObject->SetTexture("uniformTexture", 0);
    //Object 2
    Texture texture2("res/textures/SwordImage.png");
    texture2.BindTextureSlot();
    CubeObject* cubeObject2 = new CubeObject("res/shaders/Sword.shader");
    cubeObject2->SetTexture("SwordUniformTexture",1);
    //Gui
    Gui GUI;
    GUI.Init(win.getWindowsPointer());


    while (!win.windowShouldClose())
    {

        //Update
        glfwPollEvents();
        GUI.Update();
        //Input
        win.processInputForWindow();
        //Player Input
        playerInput(win.getWindowsPointer(), cubeObject);
        //Rendering commands here
        renderer->Clear();
        cubeObject->Render(renderer);
        cubeObject2->Render(renderer);
        GUI.Render();
        win.swapBuffers();
    }

    GUI.Shutdown();
    win.Shutdown();
    return ;
}

Shader and texture class code

#shader vertex
#version 450 core  

layout(location = 0) in vec3 position;  
layout(location = 1) in vec3 colorcoord;
layout(location = 2) in vec2 texcoord;
layout(location = 3) in vec3 normal;

out vec3 vertex_position;
out vec3 vertex_color;
out vec2 vertex_texcoord;
out vec3 vertex_normal;

//uniform mat4 u_MatrixViewProjection; 
uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_PerspectiveProjection;

void main()  
{  
    vertex_position = vec4(u_ModelMatrix * vec4(position, 1.f)).xyz;
    vertex_color = colorcoord;
    vertex_texcoord = texcoord;
    vertex_normal = mat3(u_ModelMatrix) * normal;

    gl_Position = u_PerspectiveProjection * u_ViewMatrix * u_ModelMatrix * vec4(position, 1.f);
};

#shader fragment
#version 450 core  

layout(location = 0) out vec4 color;

in vec3 vertex_position;
in vec3 vertex_color;
in vec2 vertex_texcoord;
in vec3 vertex_normal;

// IMPORTANT PART

uniform sampler2D uniformTexture;
uniform sampler2D SwordUniformTexture;

uniform vec3 lightPos;
uniform vec3 camPosition;

void main()
{
    vec3 lightColor = vec3(1.f, 1.f, 1.f);

    //Ambient light
    vec3 darkAmbientColor = vec3(0.1f, 0.1f, 0.1f);

    //Diffuse light
    vec3 positionToLightNormalizedDirectionalVec = normalize(lightPos - vertex_position);
    float diffuse = clamp(dot(positionToLightNormalizedDirectionalVec, vertex_normal), 0, 1);
    vec3 diffuseColor = lightColor * diffuse;

    //Specular light
    vec3 lightToPositionNormalizedDirectionVec = normalize(vertex_position -lightPos);
    vec3 reflectNormalizedDirectionVec = normalize(reflect(lightToPositionNormalizedDirectionVec, normalize(vertex_normal)));
    vec3 positionToViewDirection = normalize(camPosition- vertex_position);
    float specularConstat = pow(max(dot(positionToViewDirection, reflectNormalizedDirectionVec), 0), 30);//higher the pow higher the range
    vec3 specularLight = lightColor * specularConstat;

    //Error?
    //Texture light
    color = texture(SwordUniformTexture, vertex_texcoord) * vec4(vertex_color, 1.f) *
    (vec4(darkAmbientColor, 1.f)+vec4(diffuseColor,1.f)+vec4(specularLight,1.f)); //lights
    
};

And finally

static int textureSlots = 0;

Texture::Texture(const std::string& filepath):filepath(filepath),localBuffer(nullptr),width(0),height(0),bitsPerPixel(0)
{
    stbi_set_flip_vertically_on_load(true);
    localBuffer = stbi_load(filepath.c_str(),&width,&height,&bitsPerPixel,4);

    GLCall(glGenTextures(1, &rendererID));
    GLCall(glBindTexture(GL_TEXTURE_2D, rendererID));

    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); // min filter to be resampled down
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); // if we render texture area larger then texture pix size
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)); // horizontal wrap clamp to not extend to area
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)); // vertical wrap clamp to not extend to area

    GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, localBuffer)); // check everything ( levels , format, data )

    GLCall(glGenerateMipmap(GL_TEXTURE_2D));

    GLCall(glBindTexture(GL_TEXTURE_2D, 0));

    if (localBuffer) 
        stbi_image_free(localBuffer);
    
}

Texture::~Texture()
{
    GLCall(glDeleteTextures(1, &rendererID));
}

void Texture::BindTextureSlot()const
{
        
        GLCall(glActiveTexture(GL_TEXTURE0+textureSlots));
        GLCall(glBindTexture(GL_TEXTURE_2D, rendererID));
        textureSlots++;
}

I would like somehow to manage my shaders so each has one texture to be rendered without using extra slots but even this is not working, and that's crucial for continuation.

TextureLook

Re. "... for some reason only one texture is rendering": but you're only using one texture, right? Your shaders use SwordUniformTexture but not uniformTexture as far as I can see. - G.M.
Thank you for your reply, i should have posted another shader , there are two first one uses uniformTexture but second one not like it's shown . - Stefan Radosevic