我做错了什么,无法在 OpenGL 中正确渲染图像/纹理?

发布于 2024-09-25 22:55:48 字数 1340 浏览 2 评论 0原文

我正在尝试将图像渲染到窗口。超级简单(或者我是这么认为的)。无论我做什么,我总是得到这个紫色方块。我的代码的一些花絮:

// load image from file, called one time on load:
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

RgbImage theTexMap( filename );

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData() );

// render quad and texture, called inside glutDisplayFunc callback
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, -50.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-50.0, 50.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(50.0, 50.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(50.0, -50.0, 0.0);
glEnd();

glFlush();
glDisable(GL_TEXTURE_2D);

我删除了很多代码,因为我试图扩展第三方库(ARToolkit)的示例。那里的代码比这里显示的需要多得多。

对于我可能犯的错误有什么想法吗?它会显示四边形,但不渲染纹理/图像?

替代文字

I'm trying to render an image to the window. Super simple (or so I thought). No matter what I do, I always get this purple square. Some tidbits of my code:

// load image from file, called one time on load:
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

RgbImage theTexMap( filename );

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData() );

// render quad and texture, called inside glutDisplayFunc callback
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, -50.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-50.0, 50.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(50.0, 50.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(50.0, -50.0, 0.0);
glEnd();

glFlush();
glDisable(GL_TEXTURE_2D);

I'm cutting out a lot of code because I'm attempting to extend an example from third party library (ARToolkit). There's a lot more code in there than needs displaying here.

Any ideas for a mistake I might be making that would display the quad, but not render the texture/image?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜嗑 2024-10-02 22:55:48

glutDisplayFunc() 回调中重新绑定纹理对象,以防万一™。

另外,我对 GL_RGBA8 有点怀疑。尝试GL_RGBA。不过,可能是我的迷信。

Rebind your texture object in your glutDisplayFunc() callback, Just In Case™.

Also, I'm slightly leery of the GL_RGBA8. Try GL_RGBA. Probably superstition on my part though.

坐在坟头思考人生 2024-10-02 22:55:48

不知道自己没有尝试过,但是您使用 GL_RGBA8 来表示内部格式,使用 GL_RGB 来表示像素格式,这似乎有点奇怪。

就我个人而言,除非我在纹理上使用它,否则我也会对纹理对象执行 GL_DISABLE(GL_LIGHTING) ,不知道这是否有帮助,但我知道我遇到了一些我没有遇到的事情不太了解光和光。涉及纹理组合。

Dunno without trying it myself, but it seems a bit strange that you're using GL_RGBA8 for internal format and GL_RGB for pixel format.

Personally, unless I'm using it on my texture I'll also do a GL_DISABLE(GL_LIGHTING) too for textured objects, dunno if that will help but I know I've run into some things I didn't really understand as far as light & texture combinations are concerned.

我早已燃尽 2024-10-02 22:55:48

一些想法:

在配置和上传纹理之前,您不应该调用 glEnable(GL_TEXTURE_2D) 吗?

你确定纹理尺寸是2的幂吗?

如果您想使用 GL_LINEAR 放大/缩小功能,您可能需要从原始纹理生成 mipmap。

A few ideas:

Shouldn't you call glEnable(GL_TEXTURE_2D) before you configure and upload the texture?

Did you make sure that texture dimensions are powers of 2?

If you want to use GL_LINEAR magnification/minification functions, you will probably want to generate mipmaps from your original texture.

随梦而飞# 2024-10-02 22:55:48

您是否尝试使用 GL_REPLACE 而不是 GL_MODULATE ?

我没有看到您在顶点中传递任何颜色,并且 GL_MODULATE 将使用当前颜色进行调制。

did you try to use GL_REPLACE rather than GL_MODULATE ?

I don't see you passing any color in with your vertices, and GL_MODULATE will modulate with whatever is the current color.

迷途知返 2024-10-02 22:55:48

您的纹理仅指定 MIP 级别 0,而不指定其他 MIP 级别,因此如果未在 MIP 级别 0 渲染,结果可能是未定义的。快速修复方法是:

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

这是一个很容易犯的错误。一个很好的参考:

http://www.opengl.org/wiki/Common_Mistakes

Your texture only specifies MIP level 0, and not the other MIP levels, so the result may be undefined if it isn't rendering at MIP level 0. The quick fix is:

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

It's an easy mistake. A good reference:

http://www.opengl.org/wiki/Common_Mistakes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文