简体   繁体   中英

Modern OpenGL , getting a white texture on an object

im trying to load a texture to an object in Modern NVIDIA openGL

i have a square that i want to load the texture to , i use stb_image as my loader

here is the main code :

float position1[16]{
    0.0f , 0.0f, 0.0f , 0.0f,
    0.8f , 0.0f, 1.0f , 0.0f,
    0.0f , 0.8f, 0.0f , 1.0f,
    0.8f , 0.8f, 1.0f , 1.0f ,
};

unsigned int Indexs[6]{
    0 , 1 , 2 ,
    2 , 1 , 3

};
VertexBuffer buffer(position1, sizeof(position1));
IndexBuffer Index(Indexs, 6);
Texture Tex("Smile.png");
Tex.Bind();
buffer.Bind();
Index.Bind();
Shader BasicShader("basic.shader");

Renderer renderer; 
glfwSwapInterval(0);


//! MAIN LOOP !!!


BasicShader.Uniform1i("u_Texture ", 0);
BasicShader.Uniform4f("u_Color", 0.0f, 1.0f, 1.0f, 1.0f );
while (!glfwWindowShouldClose(window) )

{
    //! RENDER
    renderer.Clear();
    glEnable(GL_TEXTURE_2D);
    BasicShader.Uniform1i("u_Texture ", 0);
    BasicShader.Uniform4f("u_Color", 0.f, 1.0f, 1.0f, 1.0f);
    Tex.Bind();
    BasicShader.Bind();
    renderer.Draw(buffer, Index, Layout::D4Vertex, BasicShader);



    //! FRAME

    glfwSwapBuffers(window);

    //! EVENTS
    glfwPollEvents();
}

glfwTerminate();
return 0;
};

here is my shader its of course being compiled the right way as expected with no errors :

#shader vertex
#version 410 core

layout(location = 0 ) in vec4 position;
layout(location = 1) in vec2 texCoord;

out vec2  v_TexCoord;

void main()
{
    gl_Position = position;
    v_TexCoord = texCoord;
};


#shader fragment
#version 410 core

layout(location = 0 ) out vec4 color;

uniform vec4 u_Color;
uniform sampler2D u_Texture;

in vec2 v_TexCoord;

void main()
{
    vec4 texColor = texture2D(u_Texture, v_TexCoord);
    color = texColor;
};

here is my draw call :

void Renderer::Draw(VertexBuffer& vb,  IndexBuffer& iv, 
                    Layout layout,Shader& shader) 
{
    BufferLayout bufferLayout;
    shader.Bind();
    vb.Bind();
    iv.Bind();
    bufferLayout.SetLayout(layout);
    glDrawElements(GL_TRIANGLES, iv.GetCount(), GL_UNSIGNED_INT, nullptr);
}

this is how i set the buffer layout:

void BufferLayout::SetLayout(Layout l)
{
    switch (l) {
        case Layout::NONE :
            glEnableVertexAttribArray(0);
        case Layout::D2Vertex :
            glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 
         2,0);
        case Layout::D4Vertex:
            glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 
        4,0);
    }
}

here is my texture class (handels loading parameters and binding ) :

#include "Texture.h"
#include "stb_image.h";
#include "Renderer.h"

Texture::Texture(string  path)
    : m_RendererID(0) , m_BPP(0) , m_Height (0) , m_Width (0), 
      m_LocalBuffer(nullptr) , m_FilePath(path)
{
    stbi_set_flip_vertically_on_load(1);
    m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
    //seems like the image is there
    glEnable(GL_TEXTURE_2D);

    glGenTextures(1, & m_RendererID);
    glBindTexture(GL_TEXTURE_2D, m_RendererID);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    GLenum temp = glGetError();
    if (temp != GL_NO_ERROR)  throw std::invalid_argument("OPENGL ERROR");
    // I Get  no erros
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, 
                 GL_UNSIGNED_BYTE, m_LocalBuffer);
}

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

void Texture::Bind(unsigned int slot)
{
    glActiveTexture(GL_TEXTURE0 + slot);
    glBindTexture(GL_TEXTURE_2D, m_RendererID); 
}

void Texture::Unbind()
{
    glBindTexture(GL_TEXTURE_2D, 0);
}

and my texture.h file :

#pragma once
#include "Renderer.h"

class Texture {
    private :
    unsigned int  m_RendererID;
    string m_FilePath;
    unsigned char * m_LocalBuffer;
    int m_Width, m_Height, m_BPP;

    public:
    Texture(string  path);
    ~Texture();

    void Bind(unsigned int slot = 0);
    void Unbind();

    inline int GetWidth()const { return m_Width; }
    inline int GetHeight()const { return m_Height; }

};

basicaly im pretty sure the problem must be somewhere in the code i have posted and all the other code works as expected .

Can anyone spot the problem ? The result should be a square with the texture "Smile.png" on it and i get a white texture . I do have the file "Smile.png" in my project directory and it seems to me like stb_image can find and load it .

*no erros from glGetError();

In the code sample, the setup code for attribut 1 ( texCoord ) is missing. For each attribute used by a shader, a buffer has to be bound, the attribute has to be enabled and the relation between attribute and shader has to be specified.

As said, this is done for attribute 0 but not for attribute 1. The code in the SetLayout method could look somehow like this:

//Attribute 0: uses first two floats
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);

//Attribute 1: uses 3rd and 4th float
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(2 * sizeof(float)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM