我正在努力使用openGL渲染纹理。我是那里的一部分,卡住了。
我的目标是得到这张照片:https://i.stack.imgur.com/67lqK.jpg
这就是我所在的地方:https://i.stack.imgur.com/qBkwQ.png
以前有人见过这个问题吗?
if (tObject == 0) // We don't yet have an OpenGL texture target
{
// This code counts the number of images and if there are none simply
// returns without doing anything
int nImages = 0;
while (tName[nImages][0] != ' ' && nImages < MAX_IMAGES)
nImages++;
if (nImages < 1)
return;
// To Do
//
// Generate a texture object and place the object's value in the "tObject"
// member, then bind the object to the 2D texture target
glGenTextures(nImages, &tObject);
glBindTexture(GL_TEXTURE_2D, tObject);
for (int nImage = 0; nImage < nImages; nImage++)
{
// This code loads the texture using the windows library's "BitmapFile" object
BitmapFile texture;
if (!texture.Read(tName[nImage]))
complain("Couldn't read texture %s", tName);
GLuint srcFormat, targFormat;
// To Do
//
// First decide which format the texture is. If the texture has 4 bytes
// per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
// then it is an RGB image. Notice though that the byte order for the BitmatFile
// object is reversed, so you need to take that into account in the "source" format
if( texture.BytesPerPixel() == 3 )
{
srcFormat = GL_BGR;
targFormat = GL_RGB;
}
else
{
srcFormat = GL_BGRA;
targFormat = GL_RGBA;
}
// Then you need to set the unpack alignment to tell OpenGL about the structure
// of the data in the image and send the data to OpenGL. If there are multiple files
// then we are manually creating a mipmap here and you will use the "level" parameter
// of glTexImage2D to tell OpenGL which mipmap level is being set. The levels are
// set in the same order as they are stored in the image list.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if( nImages > 1 )
{
glGenerateMipmap(GL_TEXTURE_2D);
}
glTexImage2D(GL_TEXTURE_2D, nImage, targFormat, texture.Width(), texture.Height(), 0, srcFormat, GL_UNSIGNED_BYTE, texture.ImageData());
}
// Finally, if there is only one image, you need to tell OpenGL to generate a mipmap
if( nImages == 1)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
}
// Here you need to bind the texture to the 2D texture target and set the texture parameters
// You need to set the wrap mode, the minification and magnification filters.
glBindTexture(GL_TEXTURE_2D, tObject);
glTexParameteri(tObject, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tObject, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// To Do
//
// For advanced antialiasing set the number of anisotropic samples
GLERR;
我不明白你用来调用glGenerateMipmap (...)
的逻辑。要glTexImage2D (...)
的第二个参数是纹理 LOD - glGenerateMipmap
将生成从 LOD 0 开始的整个 mip 金字塔。实质上,通过执行此操作,您可以使glTexImage2D (...)
除该循环的第一次和最后一次迭代之外的每个调用都无效。看起来您要么想要一个数组纹理,要么每个图像都应该是一个单独的纹理。
事实上,glGenTextures (...)
并不像你想象的那样工作。如果数组> 1,您应该传递一个数组nImages
。该数组将保存nImages
多个纹理对象名称。您绑定每个图像并将图像数据分别上传到 LOD 0,然后您可以生成 mipmap。
下面解决了我刚才提到的所有内容:
GLuint* tObjects = NULL;
if (tObjects == NULL) // We don't yet have any OpenGL textures
{
// This code counts the number of images and if there are none simply
// returns without doing anything
int nImages = 0;
while (tName[nImages][0] != ' ' && nImages < MAX_IMAGES)
nImages++;
if (nImages < 1)
return;
tObjects = new GLuint [nImages];
// To Do
//
// Generate multiple texture objects and place the object's values in the "tObjects"
// member, then bind the object to the 2D texture target
glGenTextures (nImages, tObjects);
for (int nImage = 0; nImage < nImages; nImage++)
{
glBindTexture(GL_TEXTURE_2D, tObjects [nImage]);
// This code loads the texture using the windows library's "BitmapFile" object
BitmapFile texture;
if (!texture.Read(tName[nImage]))
complain("Couldn't read texture %s", tName);
GLuint srcFormat, targFormat;
// To Do
//
// First decide which format the texture is. If the texture has 4 bytes
// per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
// then it is an RGB image. Notice though that the byte order for the BitmatFile
// object is reversed, so you need to take that into account in the "source" format
if( texture.BytesPerPixel() == 3 )
{
srcFormat = GL_BGR;
targFormat = GL_RGB;
}
else
{
srcFormat = GL_BGRA;
targFormat = GL_RGBA;
}
// Then you need to set the unpack alignment to tell OpenGL about the structure
// of the data in the image and send the data to OpenGL. If there are multiple files
// then we are manually creating a mipmap here and you will use the "level" parameter
// of glTexImage2D to tell OpenGL which mipmap level is being set. The levels are
// set in the same order as they are stored in the image list.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, targFormat, texture.Width(), texture.Height(), 0, srcFormat, GL_UNSIGNED_BYTE, texture.ImageData());
glGenerateMipmap (GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
}