Java OpenGL(LWJGL)瓷砖贴图仅显示一个瓷砖

Java OpenGL(LWJGL)瓷砖贴图仅显示一个瓷砖,java,lwjgl,Java,Lwjgl,自从我接触Java一段时间以来,我最近一直在学习LWJGL,我了解到LWJGL教程/参考资料不多,我只使用搜索OpenGL教程,因为我知道LWJGL就像OpenGL的Java端口(我想你会这样描述),它们基本上是一样的,除了我总是要调整它一点,我做了这段代码(基本上都是我自己做的),当我运行它时,它只显示一个瓷砖贴图,但它应该总共显示16个瓷砖!为什么会这样 package testandothertutorials; import static org.lwjgl.opengl.GL11.*

自从我接触Java一段时间以来,我最近一直在学习LWJGL,我了解到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();
}
软件包测试和其他教程;
导入静态org.lwjgl.opengl.GL11.*;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.input.Mouse;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入org.newdawn.slick.opengl.Texture;
导入org.newdawn.slick.opengl.TextureLoader;
公共类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;
纹理石头纹理,污垢纹理;
公共TileMapTest(){
试一试{
Display.setDisplayMode(新的显示模式(640480));
显示。设置标题(“游戏”);
Display.create();
}捕获(LWJGLEXE){
}
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(0,640,480,0,1,-1);
glMatrixMode(GLU模型视图);
glEnable(GL_纹理_2D);
//在渲染循环之前加载石头和泥土纹理
试一试{
stone_texture=TextureLoader.getTexture(“PNG”,新文件输入流(新文件(“C://Users//Gannon//Desktop//Java//workspace//Test Game//res//stone.PNG”);
}catch(filenotfound异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
Dirth_texture=TextureLoader.getTexture(“PNG”,新文件输入流(新文件(“C://Users//Gannon//Desktop//Java//workspace//Test Game//res//Dirth.PNG”));
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
而(!Display.isCloseRequested()){
glClear(GLU颜色缓冲位);
drawTiles();
Display.update();
显示同步(60);
}
Display.destroy();
}
公共图书馆{
对于(int x=0;x
}

尝试使用glpushmatrix()和glpopmatrix(),当前您的GL_四边形与最后绘制的四边形相关,因此定位与较高的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。

您的问题出现了,因为您没有充分翻译磁贴,因此,您最终将渲染彼此顶部的所有瓷砖

当前您正在执行的
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();

我实现了你的建议,但仍然不起作用。更新了上面的代码。您是否尝试在(!Display.isCloseRequested())
循环中将glloadidentity()添加为
中的第一个命令?(就像我说的每个循环的开始?