LWJGL3 - 无法渲染三角形但可以清除颜色

LWJGL3 - can't render triangle but can clear color

过去一天我一直在尝试在我的屏幕上渲染一个三角形,但我不知道我做错了什么。唯一可行的是显示 window 并使用清晰的颜色渲染它。有谁知道我做错了什么?我所期望的只是看到一个黑色三角形呈现在屏幕上,就像我过去做过一次一样,但目前我无法做到这一点。

代码如下:

主要:

public class Main {
    public static void main(String[] args){
        System.out.println("Hello");
        // Create Window
        Window.createWindow();
        long window = Window.getWindow();
        // Create Renderer
        Renderer renderer = new Renderer();
        // Data
        float[] positions = {
                0.5f, 0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f,
                0.5f, -0.5f, 0.0f
        };
        // Create Model
        Model triangle = Loader.dataToModel(positions);
        // Render on window
        while(!GLFW.glfwWindowShouldClose(window)){
            GLFW.glfwPollEvents();
            renderer.clear();
            renderer.render(triangle);

            GLFW.glfwSwapBuffers(window);   // !@#!@#!@# remember to add!!!  !@#!@# Order important prob
        }
        System.out.println("TERMINATED");
        Window.terminateWindow();
    }
}

Window:

public class Window {
    private static long window;
    private static Callback debugCallback;

    public static void createWindow(){
        // Creates a window

        // Init
        if(!GLFW.glfwInit()){
            throw new IllegalStateException("ERROR: could not GLFW.glfwInit()");
        }

        // Hints
        // Hint opengl version
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR,3);
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR,2);
        // Hint compatibility (profile, forward compat)
        GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE,GLFW.GLFW_OPENGL_CORE_PROFILE);
        GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT,GLFW.GLFW_TRUE);
        // Hint to hide initially
        GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE,GLFW.GLFW_FALSE);
        // Hint debug
        GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);


        // Actually create window
        window = GLFW.glfwCreateWindow(500, 500, "thinMatrix4", 0, 0);
        if(window == 0){
            throw new IllegalStateException("ERROR: during GLFW.glfwCreateWindow");
        }

        // Context and Capabilities
        GLFW.glfwMakeContextCurrent(window);    // create context for window
        GL.createCapabilities();    // find all the opengl stuff to give capabilities

        // Debugging enable
        GL11.glEnable(org.lwjgl.opengl.KHRDebug.GL_DEBUG_OUTPUT_SYNCHRONOUS);
        debugCallback = GLUtil.setupDebugMessageCallback();

        // Position window
        GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(window,vidMode.width()/2,vidMode.height()/2);

        // Window show
        GLFW.glfwShowWindow(window);
    }

    public static void terminateWindow(){
        // Termination
        GLFW.glfwTerminate();
    }

    // GETTERS
    public static long getWindow() {
        return window;
    }
}

渲染器:

public class Renderer {
    public void render(Model model){
        // Enable to allow model rendering
        GL30.glBindVertexArray(model.getVAO());
        GL20.glEnableVertexAttribArray(0);
        // Render
        GL11.glDrawArrays(GL11.GL_TRIANGLES,model.getVertexCount(),0);
        // Clean up
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);
    }
    public void clear(){
        GL11.glClearColor(0f,0.8f,.6f,1f);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    }
}

型号:

public class Model {
    private int vertexCount;
    private int vao;
    public Model(int vao, int count){
        this.vao = vao;
        this.vertexCount = count;
    }
    // GETTERS
    public int getVertexCount(){
        return this.vertexCount;
    }
    public int getVAO(){ return this.vao; }
}
public class Loader {
    private static List<Integer> vaos = new ArrayList<Integer>();
    private static List<Integer> vbos = new ArrayList<Integer>();

    public static Model dataToModel(float[] data){
        // Create VAO
        int vao = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vao);
        vaos.add(vao);

        // Create Buffer (VBO) for actual data
        int vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER,toFloatBuffer(data), GL15.GL_STATIC_DRAW);      //!@#!@#!@# What happens if data is not a float buffer
        // vertex attribute pointer
        GL30.glVertexAttribPointer(0,3, GL11.GL_FLOAT,false,0,0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
        vbos.add(vbo);

        // Create model
        int count = data.length/3;
        Model model = new Model(vao, count);
        return model;
    }
    private static FloatBuffer toFloatBuffer(float[] data){
        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }
}

您使用 glDrawArrays 不正确。第二个参数是要渲染的第一个顶点的索引,第三个参数是顶点数:

GL11.glDrawArrays(GL11.GL_TRIANGLES,model.getVertexCount(),0);

GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());