OpenGL VBO未呈现到屏幕

时间:2019-02-08 23:40:35

标签: java opengl lwjgl

我试图让OpenGL在我的窗口上绘制纹理四边形,但是当我运行代码时,什么也没有画,甚至没有黑色四边形。没有引发错误,我尝试在片段着色器中将像素颜色设置为黑色,但没有四边形绘制。

GitHub上的

项目: https://github.com/BigBadE/ScratchSpigot

在OpenGL 2.1上试用着色器,它们可以正常工作,并且加载图像不是问题。

主游戏循环:

Texture texture = new Texture("resources/textures/player.png");
    while (running) {
        glClearColor(1, 1, 1, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        /* Check if game should close */
        if (glfwWindowShouldClose(id)) {
            running = false;
        }

        // Poll GLFW events
        glfwPollEvents();

        renderer.drawTexture(texture, (short) 0, (short) 0, 380, 260);
        glfwSwapBuffers(id);
        glfwPollEvents();
    }

绘制纹理:

public void drawTexture(Texture texture, short x, short y, int width, int height) {
    vertices.put(x).put(y).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 0).put((short) 0);
    vertices.put(x).put((short) (y+height)).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 0).put((short) 1);
    vertices.put((short) (x+width)).put((short) (y+height)).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 1).put((short) 1);

    vertices.put(x).put(y).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 0).put((short) 0);
    vertices.put((short) (x+width)).put((short) (y+height)).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 1).put((short) 1);
    vertices.put((short) (x+width)).put(y).put((short) 0).put((short) 0).put((short) 0).put((short) 1).put((short) 0).put((short) 0);

    flush(texture.getId());
}

冲洗:

private void flush(int id) {
    vertices.flip();
    if (vao == -1) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        specifyVertexAttributes();
    }

    program.use();

    /* Set texture uniform */
    int uniTex = program.getUniformLocation("texImage");
    program.setUniform(uniTex, id);

    /* Upload the new vertex data */
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertices);

    /* Draw batch */
    glDrawElements(GL_TRIANGLES, vertices);

    /* Clear vertex data for next batch */
    vertices.clear();
}

设置着色器:

if (main.isDefaultContext()) {
        /* Generate Vertex Array Object */
        vao = glGenVertexArrays();
        glBindVertexArray(vao);
    } else {
        vao = -1;
    }

    vbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    /* Create FloatBuffer */
    vertices = MemoryUtil.memAllocShort(4096);

    /* Upload null data to allocate storage for the VBO */
    long size = vertices.capacity()*Float.BYTES;
    glBufferData(GL_ARRAY_BUFFER, size, GL_DYNAMIC_DRAW);

    veo = glGenBuffers();
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer elements = stack.mallocInt(2*3);
        elements.put(0).put(1).put(2);
        elements.put(2).put(3).put(0);
        elements.flip();

        /* Generate Element Buffer Object */
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, veo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements, GL_STATIC_DRAW);
    }

    /* Load shaders */
    Shader vertexShader, fragmentShader;
    if (main.isDefaultContext()) {
        vertexShader = Shader.loadShader(GL_VERTEX_SHADER, "resources/shaders/vertex.txt");
        fragmentShader = Shader.loadShader(GL_FRAGMENT_SHADER, "resources/shaders/fragment.txt");
    } else {
        vertexShader = Shader.loadShader(GL_VERTEX_SHADER, "resources/shaders/legacyVertex.txt");
        fragmentShader = Shader.loadShader(GL_FRAGMENT_SHADER, "resources/shaders/legacyFragment.txt");
    }

    /* Create shader program */
    program = new ShaderProgram();
    program.attachShader(vertexShader);
    program.attachShader(fragmentShader);
    if (main.isDefaultContext()) {
        program.bindFragmentDataLocation(0, "fragColor");
    }
    program.link();
    program.use();

    /* Delete linked shaders */
    vertexShader.delete();
    fragmentShader.delete();

    /* Get width and height of framebuffer */
    long window = GLFW.glfwGetCurrentContext();
    int width, height;
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer widthBuffer = stack.mallocInt(1);
        IntBuffer heightBuffer = stack.mallocInt(1);
        GLFW.glfwGetFramebufferSize(window, widthBuffer, heightBuffer);
        width = widthBuffer.get();
        height = heightBuffer.get();
    }

    /* Specify Vertex Pointers */
    specifyVertexAttributes();

    /* Set texture uniform */
    int uniTex = program.getUniformLocation("texImage");
    program.setUniform(uniTex, 0);

    /* Set model matrix to identity matrix */
    Matrix4f model = new Matrix4f();
    int uniModel = program.getUniformLocation("model");
    program.setUniform(uniModel, model);

    /* Set view matrix to identity matrix */
    Matrix4f view = new Matrix4f();
    int uniView = program.getUniformLocation("view");
    program.setUniform(uniView, view);

    /* Set projection matrix to an orthographic projection */
    Matrix4f projection = Matrix4f.orthographic(0f, width, 0f, height, -1f, 1f);
    int uniProjection = program.getUniformLocation("projection");
    program.setUniform(uniProjection, projection);

设置顶点属性:

/* Specify Vertex Pointer */
    int posAttrib = program.getAttributeLocation("position");
    program.enableVertexAttribute(posAttrib);
    program.pointVertexAttribute(posAttrib, 2, 8*Float.BYTES, 0);

    /* Specify Color Pointer */
    int colAttrib = program.getAttributeLocation("color");
    program.enableVertexAttribute(colAttrib);
    program.pointVertexAttribute(colAttrib, 4, 8*Float.BYTES, 2*Float.BYTES);

    /* Specify Texture Pointer */
    int texAttrib = program.getAttributeLocation("texcoord");
    program.enableVertexAttribute(texAttrib);
    program.pointVertexAttribute(texAttrib, 2, 8*Float.BYTES, 6*Float.BYTES);

我使用LWJGL作为唯一的库。 我很确定问题出在setupShaders()方法中,因为从不调用着色器。

0 个答案:

没有答案