在OpenGL中显示大图像的首选方式是什么? [英] What is the preferred way to show large images in OpenGL

查看:132
本文介绍了在OpenGL中显示大图像的首选方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题几次。比方说,我想在OpenGL上下文中显示启动画面或其他内容(或者DirectX,这更像是一个概念性的东西),现在,我可以只加载2048x2048纹理,并希望显卡能够应付它(现在大部分都是我想的),但随着老派图形卡的增长,我有这种不良良知倾向于我,告诉我不应该使用大的纹理。

<现在什么是首选方式?它只是将这些东西塞进视频内存,平铺它,还是让CPU完成工作和glDrawPixels?如果这是一个没有动画的单帧飞溅图像,那么使用<$ c是没有害处的。

$ C> glDrawPixels 。如果性能至关重要,并且您需要使用纹理,那么正确的方法是使用代理纹理在运行时确定支持的最大纹理大小。

  GLint width = 0; 
while(0 == width){/ *使用更好的条件来防止无限循环* /
glTexImage2D(GL_PROXY_TEXTURE_2D,
0,/ * mip map level * /
GL_RGBA,/ *内部格式* /
desiredWidth,/ *图像宽度* /
desiredHeight,/ *图像高度* /
0,/ *纹理边框* /
GL_RGBA / *像素数据格式,* /
GL_UNSIGNED_BYTE,/ *像素数据类型* /
NULL / *空指针,因为这是代理纹理* /
);

* *如果纹理格式被支持,查询的宽度将不为0 * /
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,& width);

desiredWidth / = 2; desiredHeight / = 2;
}

一旦您知道系统的OpenGL驱动程序支持的最大纹理大小,至少有两个选项,如果您的图片不适合:




  • 图像平铺:分割后使用多个四边形图像分成较小的支持块。像闪屏这样的图像拼贴应该不会太棘手。您可以使用 glPixelStorei GL_PACK_ROW_LENGTH 参数将较大图像的各个部分加载到纹理中。 图像调整大小:调整图像大小适合支持的最大纹理尺寸。甚至有一个GLU辅助函数可以为你做这件事, gluScaleImage


I've had this problem a couple of times. Let's say I want to display a splash-screen or something in an OpenGL context (or DirectX for that matter, it's more of a conceptual thing), now, I could either just load a 2048x2048 texture and hope that the graphics card will cope with it (most will nowadays I suppose), but growing with old-school graphics card I have this bad conscience leaning over me and telling me I shouldn't use textures that large.

What is the preferred way nowadays? Is it to just cram that thing into video memory, tile it, or let the CPU do the work and glDrawPixels? Or something more elaborate?

解决方案

If this is a single frame splash image with no animations, then there's no harm using glDrawPixels. If performance is crucial and you are required to use a texture, then the correct way to do it is to determine at runtime, the maximum supported texture size, using a proxy texture.

GLint width = 0;
while ( 0 == width ) {  /* use a better condition to prevent possible endless loop */
    glTexImage2D(GL_PROXY_TEXTURE_2D, 
                      0,                /* mip map level */
                      GL_RGBA,          /* internal format */
                      desiredWidth,     /* width of image */
                      desiredHeight,    /* height of image */
                      0,                /* texture border */
                      GL_RGBA           /* pixel data format, */ 
                      GL_UNSIGNED_BYTE, /* pixel data type */
                      NULL              /* null pointer because this a proxy texture */
    );

    /* the queried width will NOT be 0, if the texture format is supported */
    glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);

    desiredWidth /= 2; desiredHeight /= 2;
}

Once you know the maximum texture size supported by the system's OpenGL driver, you have at least two options if your image doesn't fit:

  • Image tiling: use multiple quads after splitting your image into smaller supported chunks. Image tiling for something like a splash screen should not be too tricky. You can use glPixelStorei's GL_PACK_ROW_LENGTH parameter to load sections of a larger image into a texture.
  • Image resizing: resize your image to fit the maximum supported texture size. There's even a GLU helper function to do this for you, gluScaleImage.

这篇关于在OpenGL中显示大图像的首选方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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