LWJGL 屏幕外 2D 纹理映射

发布于 2024-11-25 11:58:57 字数 532 浏览 1 评论 0原文

我正在尝试将 2D 图像映射到四边形作为背景图像。图像绘制在屏幕的中心,因此它最终只覆盖了右上角的四分之一,其余部分则超出了屏幕。我是否可以翻译或更改 TexCoord,以便它从左下角绘制?下面是我正在使用的代码。

        background.bind();          
        GL11.glBegin(GL11.GL_QUADS);
             GL11.glTexCoord2f(1,1);
             GL11.glVertex2i(0,0);
             GL11.glTexCoord2f(1,0);
             GL11.glVertex2i(0,600);
             GL11.glTexCoord2f(0,0);
             GL11.glVertex2i(800,600);
             GL11.glTexCoord2f(0,1);
             GL11.glVertex2i(800,0);
        GL11.glEnd();

I am trying to map a 2D image to a quad as a background image. The image is drawn at the center of the screen so it ends up only covering the top right quater with the rest going offscreen. Is there anyway I can translate or change the TexCoord so it wil draw from the bottom left? Below is the code I am using.

        background.bind();          
        GL11.glBegin(GL11.GL_QUADS);
             GL11.glTexCoord2f(1,1);
             GL11.glVertex2i(0,0);
             GL11.glTexCoord2f(1,0);
             GL11.glVertex2i(0,600);
             GL11.glTexCoord2f(0,0);
             GL11.glVertex2i(800,600);
             GL11.glTexCoord2f(0,1);
             GL11.glVertex2i(800,0);
        GL11.glEnd();

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

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

发布评论

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

评论(1

Saygoodbye 2024-12-02 11:58:57

这与纹理无关;如果你拿走纹理,它的渲染效果将与现在完全相同。

OpenGL 自然不会采用窗口坐标中的顶点。为了允许它这样做,您通常需要设置某种形式的正交投影矩阵,通常使用 glOrtho。这看起来像:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, 0, height, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

//Render stuff in window coordinates here.

glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

矩阵压入和弹出确保先前的矩阵被保留。

This has nothing to do with textures; if you took your texture away, it would render exactly the same as it does now.

OpenGL does not naturally take vertices in window coordinates. In order to allow it to do so, you generally need to set up an orthographic projection matrix of some form, typically with glOrtho. This would look something like:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, 0, height, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

//Render stuff in window coordinates here.

glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

The matrix pushing and popping ensures that the prior matrix is preserved.

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