LWJGL glDrawArrays不绘制

时间:2018-09-04 12:23:00

标签: lwjgl

我是LWJGL库的新手,我试图绘制一个简单的三角形。只有我的代码看起来还可以,不能画出我的三角形。它也没有给出错误或其他任何信息。我到处搜索,但根本找不到解决方案。有解决方案的人吗?

代码:

public class Cube {

private int width, height;

private int ID, bufferID;
private int count;
private float[] vertices;

public Cube() {
    vertices = new float[] {
            0, 0, 0,
            1, 0, 0,
            0, 1, 0,    
    };

    count = vertices.length / 3;

    FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
    buffer.put(vertices);
    buffer.flip();

    ID = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(ID);

    bufferID = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

    GL20.glEnableVertexAttribArray(0);
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
    GL30.glBindVertexArray(0);
    GL20.glDisableVertexAttribArray(0);
}

public void render() {
    GL30.glBindVertexArray(ID);
    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, count);
    GL30.glBindVertexArray(0);
}

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getHeight() {
    return height;
}

public void setHeight(int height) {
    this.height = height;
}

}

public class Launcher {

public static void main(String[] args) {
    Window window = new Window("Cube Wave", 800, 600);
    window.setBackground(1, 0, 1);
    window.setup();
}

}

public class Window {

private String title;
private int width, height;
private Vector3f color;

private long window;

private ArrayList<Cube> cubes = new ArrayList<Cube>();

public Window(String title, int width, int height) {
    this.title = title;
    this.width = width;
    this.height = height;
    setBackground(0, 0, 0);
}

public void setup() {
    if (!glfwInit()) {
        return;
    }

    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    window = glfwCreateWindow(width, height, title, NULL, NULL);

    if (window == NULL) {
        return;
    }

    GLFWVidMode monitor = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (monitor.width() - width) / 2, (monitor.height() - height) / 2);

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    GL.createCapabilities();

    glfwShowWindow(window);

    loop();
}

public long getWindow() {
    return window;
}

public void loop() {
    init();

    glClearColor(color.x, color.y, color.z, 1.0f);

    while(!closed()) {
        tick();
        render();
    }
}

public void render() {
    for(Cube cube : cubes) {
        cube.render();
    }
    glfwSwapBuffers(window);
}

public void tick() {
    glfwPollEvents();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
}

public void init() {
    cubes.add(new Cube());
}

public boolean closed() {
    return glfwWindowShouldClose(window);
}

public void setBackground(float r, float g, float b) {
    color = new Vector3f(r, g, b);
}

}

1 个答案:

答案 0 :(得分:0)

似乎您的渲染功能有误。

下面是一个如何在渲染函数中使用glDrawArrays的示例。

public void render() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

    // bind the VBO and enable the attribute
    GL30.glBindVertexArray(ID);
    GL20.glEnableVertexAttribArray(0);

    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, count);

    // disable the VBO and disable the attribute
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
}

完成缓冲区后,取消绑定也是一种最佳实践。因此,您的初始化函数应如下所示。

public Cube() {
    // ... vertex stuff

    ID = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(ID);

    bufferID = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);

    // unbind both the Array Buffer and the Vertex Array
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);
}

您可以从lwjgl wiki

中找到更深入的讨论和示例。