OpenGL - 如何滚动纹理?
我正在从 pango 布局创建纹理,并使用 OpenGL 和 GLUT 将纹理映射到屏幕。我想滚动窗口中的纹理。我不关心滚动的控件,但如何将我想要看到的纹理部分映射到屏幕上?我假设我使用 glTranslate,但是我在哪里应用它?
提前致谢。
这是我目前所做的:
glEnable(GL_TEXTURE_2D);
{
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f+x, 0.0f+y);
glTexCoord2f(1.0f, 0.0f); glVertex2f(_width+x, 0.0f+y);
glTexCoord2f(1.0f, 1.0f); glVertex2f(_width+x, _height+y);
glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f+x, _height+y);
}
glEnd();
}
glFlush();
glDisable(GL_TEXTURE_2D);
I am creating a texture from a pango layout and mapping the texture to the screen using OpenGL and GLUT. I want to scroll the texture in the window. I'm not concerned with the controls to scroll, but how do I cause map the portion of the texture I want to see onto the screen? I assume I use glTranslate, but where do I apply it?
Thanks in advance.
Here is what I currently do:
glEnable(GL_TEXTURE_2D);
{
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f+x, 0.0f+y);
glTexCoord2f(1.0f, 0.0f); glVertex2f(_width+x, 0.0f+y);
glTexCoord2f(1.0f, 1.0f); glVertex2f(_width+x, _height+y);
glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f+x, _height+y);
}
glEnd();
}
glFlush();
glDisable(GL_TEXTURE_2D);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种方法是使用纹理矩阵(矩阵模式 GL_TEXTURE)或在着色器中进行。
取决于需要修改的顶点数量。如果只有几个顶点,它可能会更高效,但对于许多顶点,纹理矩阵/着色器方法可能会更好。请注意,内置矩阵运算已被弃用,即现在建议使用纹理。
在着色器中滚动很容易:
out.tex = in.tex + offset
其中 offset 可以是每帧设置的统一值或根据其他输入计算得出的值。由于您的示例使用立即模式,因此我将使用纹理矩阵模式对其进行扩展。
但请注意,这是已弃用的方法。
Another way would be to use a texture matrix (matrix mode GL_TEXTURE) or do it in a shader.
Depends on how many vertices would have to be modified. If just a few it could be more performant but for many vertices a texture matrix/shader approach could be better. Note that built in matrix operations are deprecated, i.e. it now is recommended to use textures.
Scrolling in a shader would be easy:
out.tex = in.tex + offset
where offset could be a uniform which is set per frame or be calculated from other input.Since your example uses immediate mode, I'll expand it with the texture matrix mode.
But please be aware that this is deprecated methodology.
除非您也想平铺,否则我只会修改您输出的顶点的 UV 坐标。然后,您只需更改提供给 glTexCoord2f 的坐标即可指定裁剪。
这里还有一个很好的纹理转换示例 http://potatoland.org/glart/week5/GLART_5_texture_samples .java(它是java,但你可以轻松地将其转换为C)
Unless you also want to tile I would just modify the UV coordinates of the vertices you are outputting. Then you just change the coordinates you feed to glTexCoord2f to specify a crop.
There is also a nice example of texture transformations here http://potatoland.org/glart/week5/GLART_5_texture_samples.java (it's java but you can easily translate it to C)