OpenGL(LWJGL)TileMap仅显示一个图块

时间:2013-08-02 04:03:08

标签: java lwjgl

我最近一直在学习LWJGL,因为我已经使用Java一段时间了,而且我已经了解到,由于没有很多LWJGL教程/参考资料,我只是使用搜索OpenGL教程,因为我知道LWJGL就像是OpenGL的Java端口(我认为这就是你所描述的),它们基本上是完全相同的,除了我总是要调整一下,我制作了这个代码(基本上都是我自己)和当我运行它时,它只显示一个瓦片地图,但它应该显示总共16个瓦片!这是为什么?

package testandothertutorials;

import static org.lwjgl.opengl.GL11.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class TileMapTest {

int tilemap[][] = {
        { 0, 1, 1, 0 },
        { 0, 1, 1, 0 },
        { 0, 1, 1, 0 },
        { 1, 0, 0, 1 }
};
int TILE_SIZE = 32;
int WORLD_SIZE = 4;

Texture stone_texture, dirt_texture;

public TileMapTest() {
    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("Game");
        Display.create();
    } catch(LWJGLException e) {

    }

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

    //Load the stone and dirt textures before the render loop
    try {
        stone_texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("C://Users//Gannon//Desktop//Java//workspace//Test Game//res//stone.png")));
    } catch (FileNotFoundException e) { 
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        dirt_texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("C://Users//Gannon//Desktop//Java//workspace//Test Game//res//dirt.png")));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    while(!Display.isCloseRequested()) {

        glClear(GL_COLOR_BUFFER_BIT);

        drawTiles();

        Display.update();
        Display.sync(60);
    }

    Display.destroy();
}

public void drawTiles() {
    for(int x = 0; x < WORLD_SIZE; x++) {
        for(int y = 0; y < WORLD_SIZE; y++) {
            if(tilemap[x][y] == 0) { //If the selected tile in the tilemap equals 0, set it to the stone texture to draw
                stone_texture.bind();
            } else if(tilemap[x][y] == 1) { //If the selected tile equals 1, set it to the dirt texture to draw
                dirt_texture.bind();
            }

            glPushMatrix();
            glTranslatef(x, y, 0);
            glBegin(GL_QUADS);
                glTexCoord2f(0, 0);
                glVertex2f(0, 0);
                glTexCoord2f(1, 0);
                glVertex2f(32, 0);
                glTexCoord2f(1, 1);
                glVertex2f(32, 32);
                glTexCoord2f(0, 1);
                glVertex2f(0, 32);
            glEnd();
            glPopMatrix();
        }
    }
}

public static void main(String args[]) {
    new TileMapTest();
}

}

2 个答案:

答案 0 :(得分:0)

尝试使用glpushmatrix()和glpopmatrix(),目前你的GL_QUADS与最后绘制的GL_QUADS相关,因此定位与高x和y相距甚远:

glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(32, 0);
    glTexCoord2f(1, 1);
    glVertex2f(32, 32);
    glTexCoord2f(0, 1);
    glVertex2f(0, 32);
glEnd();
glPopMatrix();

这将允许每个四边形回到世界空间坐标而不是最后绘制的四边形 - 我有一次相同的问题。
加载标识也应该只在每个新帧的开始处完成,并尝试在循环外部加载两个纹理并选择内部,为每个磁贴加载是硬盘驱动器的真正浪费,使用RAM。

答案 1 :(得分:0)

您的问题出现了,因为您没有充分翻译图块,所以最终会将所有图块渲染到彼此之上!

目前你在做glTranslatef(x, y, 0);,并记住你的瓷砖宽度和高度为32像素,但你翻译的范围只有0到4(因为你只渲染了16个瓷砖)你需要更改你的翻译

这是你应该翻译的方式。

glTranslatef(x * TILE_SIZE, y * TILE_SIZE, 0);

所以在渲染部分它看起来像这样。

glPushMatrix();
glTranslatef(x * TILE_SIZE, y * TILE_SIZE, 0);
glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(32, 0);
    glTexCoord2f(1, 1);
    glVertex2f(32, 32);
    glTexCoord2f(0, 1);
    glVertex2f(0, 32);
glEnd();
glPopMatrix();