我目前正在使用 LWJGL/OpenGL 创建一个 2D、自上而下的游戏,但是在使用顶点缓冲区对象渲染实体之后,我在绘制实体、让它们四处移动时遇到了一些问题。这是渲染线程的 run() 方法,以及 setUp 方法:
// Render thread
public void run() {
setUpDisplay();
setUpOpenGL();
setUpEntities();
while (isRunning) {
getFPS();
drawEntities();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
isRunning = false;
}
}
destroyEntities();
Display.destroy();
System.exit(0);
}
// Initialisation method for the display
private void setUpDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("Roguelike");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
// Initialisation method for OpenGL
private void setUpOpenGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
lastFrame = Main.getTime();
}
实体从抽象超类继承这些方法来设置 VBO 并绘制实体(渲染线程中的 drawEntities 方法只调用该方法,实体更新方法(见下文),而 setUpEntities 调用 setUp 方法):
// Method to initialise VBOs for a given entity
public void setUp() {
vertexData = BufferUtils.createFloatBuffer(12);
vertexData.put(getVertices());
vertexData.flip();
textureData = BufferUtils.createFloatBuffer(12);
textureData.put(textureVertices);
textureData.flip();
vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vboTextureCoordHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboTextureCoordHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Method to draw the entity
public void draw() {
glBindTexture(GL_TEXTURE_2D, loadTexture(this.textureKey).getTextureID());
glBindBuffer(GL_ARRAY_BUFFER, this.vboVertexHandle);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, this.vboTextureCoordHandle);
glTexCoordPointer(2, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
到目前为止,我一直在尝试在屏幕上移动单个实体以使翻译工作,但无济于事 - 我尝试使用以下更新方法让实体更新其位置,但它会导致在屏幕上绘制实体的副本,这显然不是我想要的:
public void update(int delta) { // Updates the position of the object
this.x += this.dx * delta;
this.y += this.dy * delta;
this.setBounds();
this.vertexData.rewind();
this.vertexData.put(getVertices());
this.vertexData.flip();
glBindBuffer(GL_ARRAY_BUFFER, this.vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, this.vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
这是使用此方法的结果(精灵只是我在几分钟内绘制的占位符以演示问题) -单击
任何帮助将不胜感激,如果您需要更多信息,我会更新。
提前致谢!
编辑:快速说明,getVertices():
public float[] getVertices() { // Returns array of vertices (for vertex VBO)
float[] vertices = { this.bounds[0], this.bounds[3], this.bounds[2], this.bounds[3],
this.bounds[2], this.bounds[1], this.bounds[2], this.bounds[1],
this.bounds[0], this.bounds[1], this.bounds[0], this.bounds[3] };
return vertices;
}
而 setBounds() 如下:
public void setBounds() {
this.bounds[0] = this.x;
this.bounds[1] = this.y;
this.bounds[2] = this.x + this.width;
this.bounds[3] = this.y + this.height;
}
当方法被调用时,它有效地更新为新的 x/y 值。