簡體   English   中英

現代 OpenGL,在對象上獲得白色紋理

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

我正在嘗試將紋理加載到現代 NVIDIA openGL 中的對象

我有一個正方形,我想將紋理加載到其中,我使用 stb_image 作為我的加載器

這是主要代碼:

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

這是我的着色器,它當然按預期以正確的方式編譯,沒有錯誤:

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

這是我的抽獎電話:

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

這就是我設置緩沖區布局的方式:

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

這是我的紋理類(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);
}

和我的 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; }

};

基本上我很確定問題一定在我發布的代碼中的某個地方,並且所有其他代碼都按預期工作。

任何人都可以發現問題嗎? 結果應該是一個帶有紋理“Smile.png”的正方形,我得到一個白色紋理。 我的項目目錄中有文件“Smile.png”,在我看來,stb_image 可以找到並加載它。

*沒有來自glGetError();

在代碼示例中,缺少屬性 1 ( texCoord ) 的設置代碼。 對於着色器使用的每個屬性,必須綁定一個緩沖區,必須啟用該屬性,並且必須指定屬性和着色器之間的關系。

如上所述,這是為屬性 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