OpenGL、等距 2D 地图和纹理

发布于 2024-12-23 05:57:37 字数 1514 浏览 0 评论 0原文

在 OpenGL 中,我正在绘制一个 2D 等距网格。 (图片:https://i.sstatic.net/O4jHV.png)。请不要介意草率的代码,因为其中很多只是尝试,在我正确编写实际引擎的平铺部分之前,我正试图亲自接触这些东西。

每个图块的宽度为 76 像素,高度为 38 像素。现在我想用我制作的一点草纹理来对每个瓷砖进行纹理处理。然而,事情并不顺利!

我的问题是,纹理看起来总是一团糟。它比应有的尺寸要小得多(大约是一半大小),并且周围是白色。这是我用来应用纹理的代码(当然,尺寸为 76x38!)。

void drawSquare(int x, int y) {
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2i( 38, -19); glVertex2i(x+38, y);
        glTexCoord2i(-38,  19); glVertex2i(x,    y+19);
        glTexCoord2i( 38, -19); glVertex2i(x-38, y);
        glTexCoord2i(-38,  19); glVertex2i(x,    y-19);
    glEnd();
}

这是计算 X 和 Y 的代码部分。

for (int y = 1; y < gridY+1; y++) {
    for (int x = 1; x < gridX+1; x++) {
        drawSquare((x-y) * 38 + (SCREEN_SIZE_X/2), (x+y) * 19);
    }
}

我可能还应该添加,我像这样放置正交设置。

glOrtho(0, SCREEN_SIZE_X, 0, SCREEN_SIZE_Y, -1, 1);

(SCREEN_SIZE_X 和 SCREEN_SIZE_Y 只是 800x600)

有人可以帮助我吗?我们将不胜感激。也许为了更清楚地说,这就是现在的样子!

编辑: 经过一番帮助,已经改成这样了。

glBegin(GL_QUADS);
//glColor3f(r, g, b);
glTexCoord2f(0,1); glVertex2i(x+38, y);
glTexCoord2f(1,1); glVertex2i(x,    y+19);
glTexCoord2f(1,0); glVertex2i(x-38, y);
glTexCoord2f(0,0); glVertex2i(x,    y-19);
glEnd();

结果: https://i.sstatic.net/JSSXy.png

In OpenGL I'm drawing a 2D isometric grid. (picture: https://i.sstatic.net/O4jHV.png). Please don't mind the sloppy code as a lot of this is just trying things out, I'm trying to get my hands dirty with this stuff before I'll properly write the tiling part of my actual engine.

Each tile is 76 pixels wide and 38 pixels high. Now I want to texture each of the tiles with a little grass texture that I made. However, that ain't going to well!

My problem is, the texture keeps looking all messed up. It's way smaller than it should be (like half the size), and it's surrounded by white. This is the code I use to apply the texture (which is of 76x38 in size, of course!).

void drawSquare(int x, int y) {
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2i( 38, -19); glVertex2i(x+38, y);
        glTexCoord2i(-38,  19); glVertex2i(x,    y+19);
        glTexCoord2i( 38, -19); glVertex2i(x-38, y);
        glTexCoord2i(-38,  19); glVertex2i(x,    y-19);
    glEnd();
}

This is the part of the code that calculates the X and Y.

for (int y = 1; y < gridY+1; y++) {
    for (int x = 1; x < gridX+1; x++) {
        drawSquare((x-y) * 38 + (SCREEN_SIZE_X/2), (x+y) * 19);
    }
}

I should probably also add, that I put the settings of my ortho like this.

glOrtho(0, SCREEN_SIZE_X, 0, SCREEN_SIZE_Y, -1, 1);

(SCREEN_SIZE_X and SCREEN_SIZE_Y just being 800x600)

Anyone that could help me out here? That'd be greatly appreciated. Perhaps to make it more clear, this is how it looks like right now!

EDIT:
After some help it's been changed to this.

glBegin(GL_QUADS);
//glColor3f(r, g, b);
glTexCoord2f(0,1); glVertex2i(x+38, y);
glTexCoord2f(1,1); glVertex2i(x,    y+19);
glTexCoord2f(1,0); glVertex2i(x-38, y);
glTexCoord2f(0,0); glVertex2i(x,    y-19);
glEnd();

Result: https://i.sstatic.net/JSSXy.png

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

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

发布评论

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

评论(1

似最初 2024-12-30 05:57:37

使用 GL_TEXTURE_2D 时,纹理坐标应从 0 到 1,而不是从 -38 到 19。

另外,您应该使用 glTexCoord2f() 代替。

编辑:
这可能是最简单/最好的方法:只需将整个纹理绘制为单个四边形,并启用透明度(混合),只需确保纹理的角 100% 透明即可!

注意:您仍然可以使用 glTexCoord2i(),因为您始终使用整个纹理。但如果你要制作纹理图集,那么glTexCoord2f()是必要的。

void drawSquare(int x, int y) {
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(x-38, y-19);
        glTexCoord2i(1, 0); glVertex2i(x+38, y-19);
        glTexCoord2i(1, 1); glVertex2i(x+38, y+19);
        glTexCoord2i(0, 1); glVertex2i(x-38, y+19);
    glEnd();
}

请记住:纹理坐标零点 (0,0) 是左下角

另外,不要忘记将混合模式设置为:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

另外,请确保使用 Alpha 通道加载纹理,如果它仍然不起作用,确保您的 opengl 纹理中存在 alpha 通道。

Texture coordinates should go from 0 to 1, not from -38 to 19 when using GL_TEXTURE_2D.

Also, you should use glTexCoord2f() instead.

Edit:
This is probably the easiest/best way: just draw the whole texture as a single quad, and enable transparency (blending), just make sure your texture has the corners 100% transparent!

Note: you can still use glTexCoord2i() since you always use the whole texture. But if you are going to make a texture atlas, then glTexCoord2f() is necessary.

void drawSquare(int x, int y) {
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(x-38, y-19);
        glTexCoord2i(1, 0); glVertex2i(x+38, y-19);
        glTexCoord2i(1, 1); glVertex2i(x+38, y+19);
        glTexCoord2i(0, 1); glVertex2i(x-38, y+19);
    glEnd();
}

Remember: Texture coordinates zero point (0,0) is bottom left

Also, dont forget to set the blendmode as:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Also, make sure you load your texture with the alpha channel, if its still not working, make sure the alpha channel exists in your opengl texture.

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