[英]Drawing multiple 2D textures in OpenGL
I am writing my own game library in C++ using Visual Studio 2013 Ultimate. 我正在使用Visual Studio 2013 Ultimate用C ++编写自己的游戏库。 I have managed to get some basic drawing working for 1 texture, but when I add another it appears to 'overwrite' the previous.
我已经设法得到一些适用于1种纹理的基本图形,但是当我添加另一种图形时,它似乎会“覆盖”以前的纹理。
For example, if I have texture A and texture B, B will get drawn where A should have been . 例如, 如果我有纹理A和纹理B,则B会在应有A的位置绘制 。 I have checked that I am not using the same texture ID for each texture.
我检查过我没有为每个纹理使用相同的纹理ID。
I have taken a look at OpenGL trying to Draw Multiple 2d Textures, only 1st one appears , but this did not solve my problem. 我看了一下OpenGL,尝试绘制多个2d纹理,只有第1个出现 ,但这并不能解决我的问题。
I am quite new to OpenGL and lower level graphics programming in general, could someone please explain to me what is wrong with my code? 我对OpenGL和较低级别的图形编程非常陌生,有人可以向我解释我的代码有什么问题吗?
Here is my texture loading code: 这是我的纹理加载代码:
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());
}
Here is my 'start draw' code 这是我的“开始抽奖”代码
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);
}
Here is my drawing code 这是我的绘图代码
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();
}
Here is my 'end draw' code 这是我的“结束抽奖”代码
void GraphicsAPIWrapper::endDraw()
{
glfwSwapBuffers(m_window->getWindow());
}
So, just to be extremely clear, the behavior I am getting is that one texture is getting drawn everywhere, rather than different textures as desired. 因此,非常明确地说,我得到的行为是一种纹理在各处绘制,而不是所需的不同纹理。
You are calling glBindTexture()
inside a glBegin/glEnd
-Block, which is invalid and will have no effect besides generating an error. 您正在
glBegin/glEnd
-Block内部调用glBindTexture()
,这是无效的,除了生成错误之外没有任何作用。 Hence, the one really bound is the one last bound before that - very likely to be the bind operation when you load your texture, so the last one loaded is the one shown for all objects... 因此,真正绑定的对象是在此之前的最后一个绑定-很有可能是在加载纹理时进行的绑定操作,因此最后加载的对象是为所有对象显示的绑定操作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.