c++ - 如何加快在 OpenGL 中绘制纹理的速度? (3.3+/4.1)

标签 c++ opengl drawing textures

我正在用鼠标在 3D 对象上绘图。

Teapot with stack overflow written on it

我现在的做法是通过片段着色器将 UV 坐标打包到 RG 像素:

in highp vec2 UV;
out vec4 fragColor;
void main()
{
    fragColor.r = UV.x;
    fragColor.g = UV.y;
}

我将它们渲染到屏幕外 FBO,然后在鼠标下读取像素以获取 CPU 端的 UV 坐标。

float pixel_array[4];

CALL_GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
CALL_GL(glReadPixels(normMouseX*WINDOW_WIDTH,normMouseY*WINDOW_HEIGHT,1,1,GL_RGBA,GL_FLOAT,pixel_array));

float u = pixel_array[0];
float v = pixel_array[1];

然后我使用这些 UV 坐标在正确的位置“绘制”纹理。

CALL_GL(glBindTexture(GL_TEXTURE_2D, mesh.diffuseTexture));
CALL_GL(glTexSubImage2D(GL_TEXTURE_2D,
        0,
        u*diffuseTextureWidth,
        v*diffuseTextureHeight,
        5,
        5,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        brush_pixels));

问题在于,根据纹理的分辨率,这个过程非常缓慢。有什么方法可以加快速度吗?

最佳答案

I render those to an Off-screen FBO, then readPixel under the mouse to get the UV coords on the CPU side.

请确保仅在真正需要时才从该纹理进行回读。也尝试突发回读。读取单个像素通常是低效的。读取整个纹理一次更新(在 View 更改之后)是高效的。

要加速绘制到纹理而不是 glTexSubImage2D,只需将它作为颜色附件绑定(bind)到 FBO 并使用常规 OpenGL 绘制操作绘制到它。

关于c++ - 如何加快在 OpenGL 中绘制纹理的速度? (3.3+/4.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220887/

相关文章:

linux - 使用 FireBreath 在浏览器插件中使用 openGL

android - 禁用缩放 View 的剪裁?

c++ - 使用 C++ 进行事件驱动的客户端/服务器设计

c++ - 虚函数默认参数行为

c++ - Gtkmm3 : wait for css transition to finish

c++ - 为什么当返回类型为const时可以修改返回值?

opengl - 修改OpenGL顶点缓冲区的正确方法是什么?

c++ - FLTK + OpenGL - 未绘制矩形

c - 为什么这段代码在快速移动鼠标时会跳过点?

javascript - Canvas : Stripped region between two concentric circles