有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OpenGL glDrawArrays非图形数组

我正在查看this tutorial并查看代码,因为某些原因,它们的数组会绘制到屏幕上(我测试了它),但我稍微编辑的代码不会将任何内容绘制到屏幕上

这是GitHub:https://github.com/BigBadE/GLHelp

以下是相关代码:

这被称为绘制一切:

private void drawTextureRegion(float x1, float y1, float x2, float y2, float s1, float t1, float s2, float t2, Color c) {
    if (vertices.remaining() < 8*6) {
        /* We need more space in the buffer, so flush it */
        flush();
    }

    float r = c.getRed();
    float g = c.getGreen();
    float b = c.getBlue();
    float a = c.getAlpha();

    vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
    vertices.put(x1).put(y2).put(r).put(g).put(b).put(a).put(s1).put(t2);
    vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);

    vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
    vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);
    vertices.put(x2).put(y1).put(r).put(g).put(b).put(a).put(s2).put(t1);

    numVertices += 6;

    flush();
}

冲洗方法:

private void flush() {
    if (numVertices > 0) {
        vertices.flip();

        if (vao != null) {
            vao.bind();
        } else {
            vbo.bind(GL_ARRAY_BUFFER);
            specifyVertexAttributes();
        }

        program.use();

        int uniTex = program.getUniformLocation("texImage");
        program.setUniform(uniTex, 0);

        /* Upload the new vertex data */
        vbo.bind(GL_ARRAY_BUFFER);
        vbo.uploadSubData(GL_ARRAY_BUFFER, 0, vertices);

        /* Draw batch */
        glDrawArrays(GL_TRIANGLES, 0, numVertices);

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

以下是我设置VAO、VBO和着色器的方式:

private void setupShaders() {
    if (!main.isLegacy()) {
        /* Generate Vertex Array Object */
        vao = new VertexArrayObject();
        vao.bind();
    } else {
        vao = null;
    }

    /* Generate Vertex Buffer Object */
    vbo = new VertexBufferObject();
    vbo.bind(GL_ARRAY_BUFFER);

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

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

    /* Initialize variables */
    numVertices = 0;

    /* Load shaders */
    Shader vertexShader, fragmentShader;
    if (!main.isLegacy()) {
        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.isLegacy()) {
        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);
}

还有一些关于OpenGL的问题:

我应该在每一帧绑定纹理,还是在屏幕上/屏幕下绑定/解除绑定纹理

我应该对所有东西都使用一个VAO,还是对相同的纹理使用一个VAO

看看教程,我的代码基本上是一样的,唯一的区别是我将纹理存储在一个数组中,并在每一帧绘制它们,而不是在其他地方使用名为“每一帧”的绘制方法。为了改进这个系统,但现在这是一个很好的渲染引擎

从这个角度来看,纹理应该被绑定到每一帧,而VAI最好使用相同的帧


共 (0) 个答案