繁体   English   中英

在OpenGL中绘制多个2D纹理

[英]Drawing multiple 2D textures in OpenGL

我正在使用Visual Studio 2013 Ultimate用C ++编写自己的游戏库。 我已经设法得到一些适用于1种纹理的基本图形,但是当我添加另一种图形时,它似乎会“覆盖”以前的纹理。

例如, 如果我有纹理A和纹理B,则B会在应有A的位置绘制 我检查过我没有为每个纹理使用相同的纹理ID。

我看了一下OpenGL,尝试绘制多个2d纹理,只有第1个出现 ,但这并不能解决我的问题。

我对OpenGL和较低级别的图形编程非常陌生,有人可以向我解释我的代码有什么问题吗?


这是我的纹理加载代码:

Texture::Texture(const std::string& filePath)
{
    m_pixelData = PixelData(stbi_load(filePath.c_str(),
                                      &m_size.x,
                                      &m_size.y,
                                      &m_numberOfImageComponents,
                                      0 /* number of requested image components if non-zero */));

    // generate 1 texture name and store it in m_textureName
    glGenTextures(1, &m_textureID);

    // make this the active texture
    glBindTexture(GL_TEXTURE_2D, m_textureID);

    // specifies how the data to be uploaded is aligned
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    // set texture parameters (need to look up the details of what's going on here)
    // TODO work out what this does
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // TODO work out what this does
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    GLenum imageFormat;

    // Work out image format
    // TODO add proper error handling
    switch(m_numberOfImageComponents)
    {
    case 4:
        imageFormat = GL_RGBA;
        break;
    case 3:
        imageFormat = GL_RGB;
        break;
    default:
        DDGL_LOG(ERROR, "No image formats with " + std::to_string(m_numberOfImageComponents) + "components known");
        throw;
    }

    // upload the texture to VRAM
    glTexImage2D(GL_TEXTURE_2D,
                0,
                imageFormat,
                m_size.x,
                m_size.y,
                0,
                imageFormat,
                GL_UNSIGNED_BYTE,
                m_pixelData.get());
}

这是我的“开始抽奖”代码

void GraphicsAPIWrapper::startDraw()
{
    // TODO does this need to be called every frame?
    glMatrixMode(GL_PROJECTION); // select the matrix
    glLoadIdentity(); //reset the matrix

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

这是我的绘图代码

void GraphicsAPIWrapper::draw(std::shared_ptr<IDrawableGameObject> drawableObject)
{
        glPushMatrix();

        // convert pixel coordinates to decimals
        const Vector2F floatWindowSize          ((float) getWindowSize().x,                             (float) getWindowSize().y);
        const Vector2F floatObjectSize          ((float) drawableObject->getSize().x,                   (float) drawableObject->getSize().y);
        const Vector2F relativeObjectSize       (floatObjectSize.x / floatWindowSize.x,                 floatObjectSize.y / floatWindowSize.y);
        const Vector2F relativeObjectPosition   (drawableObject->getPosition().x / floatWindowSize.x,   drawableObject->getPosition().y / floatWindowSize.y);

        // transformations
        glTranslatef(relativeObjectPosition.x, relativeObjectPosition.y, 0.0f);
        glRotatef(drawableObject->getRotation(), 0.0f, 0.0f, 1.0f);

        // TODO should this be triangles or quads? I've been told triangles are generally higher performance
        // right now QUADS are simpler though
        glBegin(GL_QUADS);

        glBindTexture(GL_TEXTURE_2D, drawableObject->getTextureID());

        glTexCoord2f(0.0f, 1.0f); glVertex3f(-relativeObjectSize.x, -relativeObjectSize.y,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(relativeObjectSize.x,  -relativeObjectSize.y,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(relativeObjectSize.x,  relativeObjectSize.y,   1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-relativeObjectSize.x, relativeObjectSize.y,   1.0f);

        glEnd();

        glPopMatrix();
}

这是我的“结束抽奖”代码

void GraphicsAPIWrapper::endDraw()
{
    glfwSwapBuffers(m_window->getWindow());
}

因此,非常明确地说,我得到的行为是一种纹理在各处绘制,而不是所需的不同纹理。

您正在glBegin/glEnd -Block内部调用glBindTexture() ,这是无效的,除了生成错误之外没有任何作用。 因此,真正绑定的对象是在此之前的最后一个绑定-很有可能是在加载纹理时进行的绑定操作,因此最后加载的对象是为所有对象显示的绑定操作...

暂无
暂无

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

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