[英]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.