按照c ++教程,尝试将三角形绘制到屏幕上。试图运行程序,我在所有Opengl调用上得到一个NullPointerException。另外,我正在关注opengl 3的教程,虽然我的大多数调用都是针对早期版本的,这是如何设置lwjgl,函数驻留在它们源自的版本中?
package examples;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import java.nio.*;
public class Triangle
{
// An array of 3 vectors which represents 3 vertices
static final float vertexData[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
int vertexBufferID;
public static void main(String[] args)
{
new Triangle();
}
public Triangle()
{
// Allocate floatBuffer to hold vertex data
FloatBuffer vertexBuffer = FloatBuffer.allocate(9);
// Put float data into buffer and position ready to read
vertexBuffer.put(vertexData).position(0);
// Generate 1 buffer, put the resulting identifier in vertexbuffer
IntBuffer buffers = IntBuffer.allocate(1); // allocate
GL15.glGenBuffers(buffers);
vertexBufferID = buffers.get(0);
// Binds a buffer to the ARRAY_BUFFER(target) (1 at a time) (breaks other bonds)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID);
// Give our vertices to OpenGL. (creates store for data bound to target(above))
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer,GL15.GL_STATIC_DRAW);
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
while(!Display.isCloseRequested())
{
// Render
// 1st attribute buffer : vertices
GL20.glEnableVertexAttribArray(0); // enable vertex attribute index: 0
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID);
// Specify location of vertex data for index 0
GL33.glVertexAttribP1ui(0, GL11.GL_FLOAT, false, 0);
// Draw the triangle!
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
GL20.glDisableVertexAttribArray(0);
}
}
}
答案 0 :(得分:1)
我认为问题是你必须在任何openGl调用之前创建Display。 尝试移动
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
位于Triangle构造函数的顶部。