如何在LWJGL 3.1.4中渲染?没有三角形可见

时间:2017-11-23 19:24:08

标签: opengl rendering lwjgl

Bla Bla: 我的问题很简单,对于熟练的人来说;很容易回答。我有很多教程和例子,看过很多YouTube视频。许多代码与更新版本的框架不兼容。在我非常简单的代码中,我找不到任何遗漏的线索。

问题: 运行的结果是一个窗口,其绘图区域(640 X 480)全部为红色。 没有三角形或四边形可见

package lageret01;

import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwDestroyWindow;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.system.MemoryUtil;

public class Test {
    private GLFWErrorCallback errorCallback;
    private long windowID;

    public static void main(String[] args) {
        new Test();
    }

    private Test() {
        this.errorCallback = GLFWErrorCallback.createPrint(System.err);
        glfwSetErrorCallback(errorCallback);

        boolean initResult = glfwInit();

        if (!initResult) {
            return;// The opengl is not initialized
        }

        this.windowID = glfwCreateWindow(640, 480, "Testing...", MemoryUtil.NULL, MemoryUtil.NULL);
        if (this.windowID == MemoryUtil.NULL) {
            return;// the window was not created
        }
        glfwMakeContextCurrent(this.windowID);
        glfwSwapInterval(1);
        glfwShowWindow(this.windowID);
        GLCapabilities c = GL.createCapabilities();

        float[] vertecies = { -0.5f, 0.5f, 0f, -0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, 0.5f, 0f,
                -0.5f, 0.5f, 0f };

        int vaoID = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vaoID);

        int vboID = GL15.glGenBuffers();

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);

        FloatBuffer buffer = BufferUtils.createFloatBuffer(vertecies.length);
        buffer.put(vertecies);
        buffer.flip();
        //GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL30.glBindVertexArray(0);

        while (glfwWindowShouldClose(this.windowID) == false) {
            GL11.glClearColor(1, 0, 0, 1);
            GL11.glClear(GL_COLOR_BUFFER_BIT);

            GL30.glBindVertexArray(vaoID);
            GL20.glEnableVertexAttribArray(0);
            GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertecies.length / 3);
            GL20.glDisableVertexAttribArray(0);
            GL30.glBindVertexArray(0);

            glfwSwapBuffers(this.windowID);
            glfwPollEvents();
        }

        GL30.glDeleteVertexArrays(vaoID);
        GL15.glDeleteBuffers(vboID);

        glfwDestroyWindow(this.windowID);
        glfwTerminate();
    }
}

环境: 我正在运行Eclipse Oxygen,Java 1.8.x,Lubuntu在Lubuntu上的VirtualBox中托管。 :-)

缺乏代码结构的借口: 由于我的代码不起作用,我将其条带化以查找错误...

0 个答案:

没有答案