在我的LWJGL项目中,我一直在使用VBO来平铺2D地形。它在macOS和Windows上运行良好,然而,在我的linux分区中,游戏并没有运行。我能找到的唯一主要区别是linux发行版将OpenGL版本报告为OpenGL version 3.0 Mesa 11.2.2
,而在macOS上,它报告OpenGL version 2.1 ATI-1.48.21
。我尝试过使用不同OpenGL版本的各种设备,我唯一能看到的问题是mesa库。这是我用来构建和渲染VBO的类:
import java.nio.FloatBuffer;
import java.util.ArrayList;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import data.DataManager;
public class TerrainVBO {
FloatBuffer verticies;
FloatBuffer textCoords;
ArrayList<Float> verticiesArray = new ArrayList<Float>();
ArrayList<Float> textCoordsArray = new ArrayList<Float>();
int vaoHandle;
float invAtlasWidth;
float invAtlasHeight;
public TerrainVBO(DataManager dm) {
invAtlasWidth = 1f/dm.settings.atlasMap.get("dimensions")[0];
invAtlasHeight = 1f/dm.settings.atlasMap.get("dimensions")[1];
}
public void addVertex(float x, float y) {
verticiesArray.add(x);
verticiesArray.add(y);
}
public void addTexture(float x, float y) {
textCoordsArray.add(x);
textCoordsArray.add(y);
}
public void addQuad(float x, float y, float width, float height) {
addVertex(x,y);
addVertex(x+width,y);
addVertex(x+width,y+height);
addVertex(x,y+height);
}
public void addTextureQuad(int[] pos) {
float x = pos[0] * invAtlasWidth + 0.001f;
float y = pos[1] * invAtlasHeight + 0.001f;
addTexture(x,y);
addTexture(x+invAtlasWidth - 0.002f,y);
addTexture(x+invAtlasWidth - 0.002f,y+invAtlasHeight - 0.002f);
addTexture(x,y+invAtlasHeight - 0.002f);
}
public void preWrite() {
verticies = BufferUtils.createFloatBuffer(verticiesArray.size());
textCoords = BufferUtils.createFloatBuffer(textCoordsArray.size());
for (int i = 0; i < verticiesArray.size(); i++) {
verticies.put(verticiesArray.get(i));
textCoords.put(textCoordsArray.get(i));
}
verticies.flip();
textCoords.flip();
}
public void write(Texture texture) {
Color.white.bind();
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int verticiesBufferHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, verticiesBufferHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticies, GL15.GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, 0L);
int textureBufferHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, textureBufferHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, textCoords, GL15.GL_STATIC_DRAW);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, verticiesArray.size());
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
通过运行addQuad将纹理四边形添加到VBO中。之后,preWrite准备VBO渲染到屏幕并写入实际执行渲染。任何关于为何能够在OpenGL的mesa实现上工作的想法都将受到高度赞赏。
由于程序冻结,它从不在技术上崩溃,因此没有堆栈跟踪。但是,这是由LWJGL创建的错误日志:https://cdn.discordapp.com/attachments/225439941415403520/278922035550355457/hs_err_pid24149.log