0

只是试图让一个三角形绘制到屏幕上,遵循 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);
    }

}
}
4

1 回答 1

1

我认为问题是您必须在任何 openGl 调用之前创建显示。尝试移动

   try {
        Display.setDisplayMode(new DisplayMode(800,600));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }

在 Triangle 构造函数的顶部。

于 2012-06-07T12:29:53.910 回答