2014-02-23 124 views
2

我正在使用Visual Studio 2013 Ultimate在C++中编写我自己的游戏库。 我已经设法为1纹理获得一些基本的绘图工作,但是当我添加另一个时,它似乎'覆盖'前一个。在OpenGL中绘制多个2D纹理

例如,如果我有纹理A和纹理B,B会绘制其中A应该是。我检查过我没有为每个纹理使用相同的纹理ID。

我看过OpenGL trying to Draw Multiple 2d Textures, only 1st one appears,但这并没有解决我的问题。

我对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()); 
} 

所以,仅仅是非常明确,行为我得到的是一个纹理随处可见,而不是所需的不同纹理。

回答

3

您打电话glBindTexture()glBegin/glEnd块内,这是无效的,除了产生一个错误之外没有任何效果。因此,真正绑定的是之前的最后一个绑定 - 很可能是加载纹理时的绑定操作,因此加载的最后一个是所有对象显示的绑定操作...

+0

谢谢!将glBindTexture移到glBegin调用之前解决了我的问题。 – OMGtechy