NPOT支持在OpenGL for R8G8B8纹理 [英] NPOT support in OpenGL for R8G8B8 texture

查看:159
本文介绍了NPOT支持在OpenGL for R8G8B8纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个示例应用程序使用glew和glut读取dds文件并显示它。我手动读取dds文件(R8G8B8中的NPOT(886 x 317)文件)并创建数据指针(unsigned char *)。

I have created a sample application using glew and glut which reads a dds file and displays it. I manually read the dds file (NPOT(886 x 317) file in R8G8B8) and creates the data pointer(unsigned char*).

/ p>

Then I prepared the texture using

void prepareTexture(int w, int h, unsigned char* data) {

    /* Create and load texture to OpenGL */
    glGenTextures(1, &textureID); /* Texture name generation */
    glBindTexture(GL_TEXTURE_2D, textureID); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
                w, h, 
                0, GL_RGB, GL_UNSIGNED_BYTE,
                data); 
    glGenerateMipmap(GL_TEXTURE_2D);
}

在上图中,第一个显示原始dds文件,第二个是我的渲染结果应用显然是错误的。如果我将图片尺寸调整为1024 x 512,则两张图片的外观都相同。

In the above figure, First one shows the original dds file and second one is the rendering result of my application which is obviously wrong. If I re-size the image to 1024 x 512, both images will look same.

OpenGL规范


-Power-Of-Two Textures -

对于所有纹理目标,纹理对二维力量的限制已被放宽,所以
是非
功率 - 可以指定两个纹理而不产生
错误。

I.3 Non-Power-Of-Two Textures

The restriction of textures to power-of-two dimensions has been relaxed for all texture targets, so that non-power-of-two textures may be specified without generating errors. Non-power-of-two textures was promoted from the ARB texture non power of two extension.

从ARB纹理
中提取非功率纹理我理解是从OpenGl 2.0,我们可以使用NPOT纹理和OpenGL将处理这个。

From which what I understand is from OpenGl 2.0 we can use NPOT textures and OpenGL will handle this.

我试图使用DevIL图像库加载dds文件,但最终以相同的结果。如果我将图像转换为RGBA,并将glTexImage2D的内部格式和格式更改为GL_RGBA,即使dds文件是NPOT,我也会得到正确的结果。

I tried using the DevIL image library to load the dds file but end up with same result. If I convert the image to a RGBA and and change the internal format and format of glTexImage2D to GL_RGBA I will get correct result even if the dds file is NPOT.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
                w, h, 
                0, GL_RGBA, GL_UNSIGNED_BYTE,
                data);

我试过在PC的应用程序与NVIDA卡和Radeon卡,他们都给出相同的结果。

I tried the application in PC's with NVIDA card and Radeon card and both of them are giving the same result.

我的示例源代码可以从链接

My sample source code can be downloaded from the link

任何人都可以告诉我我的申请有什么问题?

Can anybody tell me what is wrong with my application? Or OpenGL does not allow NPOT if the image is in R8G8B8.

推荐答案

这看起来像一个对齐方面的问题。如果图像在R8G8B8中,则OpenGL不允许NPOT。在 glTexImage2D()调用之前添加:

This looks like an alignment issue. Add this before the glTexImage2D() call:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

此值指定数据的行对齐,以字节为单位。默认值为4.

This value specifies the row alignment of your data in bytes. The default value is 4.

对于 GL_RGB ,每个像素的纹理宽度为886和3个字节,

With your texture width of 886 and 3 bytes per pixel for GL_RGB, each row is 886 * 3 = 2658 bytes, which is not a multiple of 4.

使用 UNPACK_ALIGNMENT 的值,可以得到的值是886 * 3 = 2658字节,不是4的倍数。默认情况下,大小将向上取整为4的下一个倍数,即2660.因此,将为每行读取2660个字节,这说明每行的递增移位。第一行是正确的,第二行是2字节关闭,第二行是4字节关闭,第三行是6字节关闭等。

With the UNPACK_ALIGNMENT value at the default, the size would be rounded up to the next multiple of 4, which is 2660. So 2660 bytes will be read for each row, which explains the increasing shift for each row. The first row would be correct, the second one 2 bytes off, the 2nd row 4 bytes off, the 3rd row 6 bytes off, etc.

这篇关于NPOT支持在OpenGL for R8G8B8纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆