Нет текстуры, если несколько текстур в современном OpenGL - PullRequest
0 голосов
/ 03 марта 2019

Я использую C ++ и OpenGL 4.6 с SDL2 и glew.У меня есть класс Texture:

#include "stb_image/stb_image.h"
class Texture {
private:
    unsigned int m_RendererID;
    std::string m_FilePath;
    unsigned char *m_LocalBuffer;
    int m_Width, m_Height, m_BPP;
public:
    Texture(const std::string &path);
    ~Texture();

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

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

Texture::Texture(const std::string &path) 
    : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 
{
    stbi_set_flip_vertically_on_load(1); //Flip vertically because OpenGL renders texture in the opposite way
    m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);

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

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

    GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
    GLCall(glBindTexture(GL_TEXTURE_2D, 0));

    if (m_LocalBuffer) {
        stbi_image_free(m_LocalBuffer);
    }
}

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

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

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

Если я создаю одну текстуру, текстура показывает:

Texture texture("ship.png");
texture.Bind(0);
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);

Но если я создаю две текстуры, я получаю только черный экран:

Texture texture("ship.png");
texture.Bind(0);
Texture texture2("ship2.png");
texture2.Bind(1);
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);

Это должно работать.Я искал в Интернете в течение 2 часов, и я не могу найти причину, почему это не сработает.Не могли бы вы, ребята, помочь мне?

1 Ответ

0 голосов
/ 03 марта 2019

В конце следующих строк кода

 Texture texture("ship.png");
 texture.Bind(0);
 Texture texture2("ship2.png");
 texture2.Bind(1);

2-й объект текстуры ( "ship2.png" ) привязан к единице текстуры 1, но к единице текстуры 0 привязан объект текстуры по умолчанию (0).

Обратите внимание, когда вызывается Texture texture("ship.png");, создается объект текстуры и привязывается к текущей единице текстуры, после этого объект текстуры по умолчанию (0) привязан к текущему текстурному блоку:

 Texture::Texture(const std::string &path) {

     GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));

     // [...]

     GLCall(glBindTexture(GL_TEXTURE_2D, 0));
 }

Обратите внимание, что то, что вы называете "unbind" , ничего не отменяет.glBindTexture(GL_TEXTURE_2D, 0) привязывает объект текстуры по умолчанию (0) к текстурному блоку, который в данный момент активен.Нет необходимости делать это.


Просто измените порядок инструкций, чтобы решить вашу проблему.Сначала создайте и загрузите объекты текстуры.Затем привяжите объекты текстуры к текстурным единицам:

Texture texture("ship.png");
Texture texture2("ship2.png");

texture.Bind(0);
texture2.Bind(1);

shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);

В качестве альтернативы вы можете избежать использования текстурного блока 0. Используйте текстурный блок 0 только для создания текстурного объекта:

Texture::Texture(const std::string &path, unsigned int slot) {

    GLCall(glActiveTexture(GL_TEXTURE0));

    // [...]
}
Texture texture("ship.png");
texture.Bind(1);                     // <--- 1
Texture texture2("ship2.png");
texture2.Bind(2);                    // <--- 2
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 1); // <--- 1
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);

Или добавить параметр для текстурного блока в конструктор класса Texture:

Texture::Texture(const std::string &path, unsigned int slot) {

    GLCall(glGenTextures(1, &m_RendererID));

    GLCall(glActiveTexture(GL_TEXTURE0 + slot));
    GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));

    // [...]

    // GLCall(glBindTexture(GL_TEXTURE_2D, 0)); <--- delete
}
Texture texture("ship.png", 0);
Texture texture2("ship2.png", 1);
...