简体   繁体   English

现代 OpenGL,在对象上获得白色纹理

[英]Modern OpenGL , getting a white texture on an object

im trying to load a texture to an object in Modern NVIDIA openGL我正在尝试将纹理加载到现代 NVIDIA openGL 中的对象

i have a square that i want to load the texture to , i use stb_image as my loader我有一个正方形,我想将纹理加载到其中,我使用 stb_image 作为我的加载器

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 ) :这是我的纹理类(handels 加载参数和绑定):

#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 :和我的 texture.h 文件:

#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 .结果应该是一个带有纹理“Smile.png”的正方形,我得到一个白色纹理。 I do have the file "Smile.png" in my project directory and it seems to me like stb_image can find and load it .我的项目目录中有文件“Smile.png”,在我看来,stb_image 可以找到并加载它。

*no erros from glGetError(); *没有来自glGetError();

In the code sample, the setup code for attribut 1 ( texCoord ) is missing.在代码示例中,缺少属性 1 ( texCoord ) 的设置代码。 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:如上所述,这是为属性 0 完成的,而不是为属性 1 完成的。 SetLayout方法中的代码可能看起来像这样:

//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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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