我正在使用 LWJGL 在 java 中创建游戏。但是它不是渲染,我将其设置为简单地渲染 6 个平面,每个平面都朝向内部以将相机包围在一个盒子中,这是非常简单的代码,而且我的工作代码几乎是准确的,只是更混乱。
public void start()
{
try
{
Display.create();
Display.setVSyncEnabled(true);
}
catch(LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
fps = new FPSCameraControl(64F, 20F, 64F);
world = new World("Test", 0, new Vector2f(1024,1024));
world.generate(128, 128);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_ALPHA);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
Settings.paused = false;
Settings.paused_state = false;
disableOrthoView();
start_game_loop();
}
private static void start_game_loop()
{
enableOrthoView();
while(Settings.active)
{
GL11.glColor3f(1F, 1F, 1F);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
//Handle Pause Control
if(Keyboard.isKeyDown(Keyboard.KEY_P))
{
if(!Settings.paused_state)
{
Settings.paused = (!Settings.paused);
Settings.paused_state = true;
}
}
else
{
Settings.paused_state = false;
}
if(!Settings.paused && Display.isActive())
{
fps.onUpdate();
}
fps.lookThrough();
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Settings.active = false;
}
GL11.glPushMatrix();
world.render();
GL11.glColor3f(1F, 1F, 1F);
GL11.glPopMatrix();
GL11.glColor3f(1F, 1F, 1F);
Display.update();
}
Display.destroy();
}
public static void enableOrthoView()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glOrtho( 0, 800 , 600 , 0, -1, 1 );
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
}
/**
* Disables the orthographic view.
*/
public static void disableOrthoView()
{
GL11.glMatrixMode(GL11.GL_PROJECTION );
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW );
GL11.glPopMatrix();
}
世界代码如下:
public class World
{
HashMap<Integer, HashMap<Integer, Object>> tiles = new HashMap<Integer, HashMap<Integer, Object>>();
private int mx=0;
private int my=0;
public static int height = 0;
private String name;
public World(String _name, int _height, Vector2f size)
{
name = _name;
height = _height;
generate((int)size.x, (int)size.y);
}
public void generate(int _x, int _y)
{
for(int x = 0;x<_x;x++)
{
tiles.put(x, new HashMap<Integer, Object>());
for(int y = 0;y<_y;y++)
{
tiles.get(x).put(y, null);
}
}
}
public void render()
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1F, 1F, 1F);
GL11.glNormal3f(0.0F, 1.0F, 0.0F);
GL11.glVertex3f(0F, 1F, 0F);
GL11.glVertex3f(0F, 1F, my);
GL11.glVertex3f(mx, 1F, my);
GL11.glVertex3f(mx, 1F, 0F);
GL11.glNormal3f(1.0F, 0.0F, 0.0F);
GL11.glVertex3f(0F, 1F, 0F);
GL11.glVertex3f(0F, 100F, 0F);
GL11.glVertex3f(0F, 100F, my);
GL11.glVertex3f(0F, 1F, my);
GL11.glNormal3f(-1.0F, 0.0F, 0.0F);
GL11.glVertex3f(mx, 1F, 0F);
GL11.glVertex3f(mx, 100F, 0F);
GL11.glVertex3f(mx, 100F, my);
GL11.glVertex3f(mx, 1F, my);
GL11.glNormal3f(0.0F, 0.0F, -1.0F);
GL11.glVertex3f(0F, 1F, my);
GL11.glVertex3f(0F, 100F, my);
GL11.glVertex3f(mx, 100F, my);
GL11.glVertex3f(mx, 1F, my);
GL11.glNormal3f(0.0F, 0.0F, 1.0F);
GL11.glVertex3f(0F, 1F, 0F);
GL11.glVertex3f(0F, 100F, 0F);
GL11.glVertex3f(mx, 100F, 0F);
GL11.glVertex3f(mx, 1F, 0F);
GL11.glNormal3f(0.0F, -1.0F, 0.0F);
GL11.glVertex3f(0F, 100F, 0F);
GL11.glVertex3f(0F, 100F, my);
GL11.glVertex3f(mx, 100F, my);
GL11.glVertex3f(mx, 100F, 0F);
//COMMENTED OUT - UNIMPLEMENTED - REMOVED FOR TESTING
/*for(int x = 0;x<mx;x++)
{
if(tiles.containsKey(x))
{
for(int y = 0;y<my;y++)
{
if(tiles.get(x).containsKey(y))
{
((Item)tiles.get(x).get(y)).render(x, y);
}
}
}
}*/
GL11.glEnd();
}
public void placeEntityAt(int x, int y, Object entity)
{
tiles.get(x).put(y, entity);
}
public void save() throws IOException
{
PrintWriter file = new PrintWriter(new File("..\\data\\worlds\\"+name+".rnw"));
for(int x = 0;x<mx;x++)
{
if(tiles.containsKey(x))
{
for(int y = 0;y<my;y++)
{
if(tiles.get(x).containsKey(y))
{
Object o = tiles.get(x).get(y);
String line = "-t ";
line += WorldObjectType.getObjectTypeID(o)+"[";
line += ((Item)o).getSaveParams()+"]{"+x+"."+y+"}";
file.println(line);
}
}
}
}
file.println("-m '"+name+"' "+mx+"|"+my);
}
public void load()
{
}
}
FPSCameraControl 非常简单,只需使用鼠标位置添加偏航和俯仰,然后使用具有正确轴的 GL11.glRotatef,因为该类只是从完全有效的早期测试中复制而来。
到目前为止,我已经遇到这个问题 3 天了,一直在检查我的代码,如果有人能发现问题所在,请尽快回复。