代码之家  ›  专栏  ›  技术社区  ›  Dan

无法呈现简单顶点数组

  •  1
  • Dan  · 技术社区  · 6 年前

    我正在从传统的现代OpenGL迁移二维游戏。对我来说,这意味着使用着色和顶点数组而不是 glProjection , glOrtho glBegin / glEnd .

    我将相关代码简化为一个非常基本的示例。相机在(0,0,-1)处查看(0,0,0),我正在尝试绘制一个带点的三角形:

    (0, 0, 0)
          X
          | '
          |   '
          |     '
          X-------X
    (0, 1, 0)   (1, 1, 0)
    

    即使使用一个非常简单的明暗器将所有片段渲染为白色,我也看不到这个三角形,只看到闪烁的背景色。

    代码

    package client.render.opengl;
    
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.FloatBuffer;
    
    import org.joml.Matrix4f;
    import org.lwjgl.BufferUtils;
    import org.lwjgl.glfw.GLFW;
    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 client.res.gfx.ScreenShader;
    
    public class RenderingTest {
    
        private static final int NUM_VERTICES = 3;
    
        private static long window;
        private static int shaderProgram;
        private static int vao;
        private static int uniformLoc;
        private static Matrix4f viewProjMatrix = new Matrix4f();
        private static FloatBuffer fb16 = BufferUtils.createFloatBuffer(16);
    
        public static void main(String[] args) throws IOException {
    
            init();
    
            while (!GLFW.glfwWindowShouldClose(window)) {
                GLFW.glfwPollEvents();
                render();
                GLFW.glfwSwapBuffers(window);
            }
        }
    
        private static void init() throws IOException {
    
            // Setup an error callback
            GLFW.glfwSetErrorCallback(
                    GLFWErrorCallback.createPrint(System.err)
            );
    
            // Initialise GLFW
            if (!GLFW.glfwInit()) {
                throw new IllegalStateException("Unable to initialize GLFW");
            }
    
            // Create window
            window = GLFW.glfwCreateWindow(800, 600, "test", 0, 0);
            GLFW.glfwMakeContextCurrent(window);
            GL.createCapabilities();
            GLFW.glfwShowWindow(window);
    
            // Load shader
            // Assume this code is good for now, it's too long to post here
            ScreenShader.initialise();
            shaderProgram = ScreenShader.shader.getProgram();
            uniformLoc = ScreenShader.shader.getUniformLoc(ScreenShader.UNIFORM_VIEW_PROJ_MATRIX);
    
            // Define our vertices
            ByteBuffer vertexBuffer = ByteBuffer.allocateDirect(NUM_VERTICES * 2 * Float.BYTES);
            vertexBuffer.putFloat(0).putFloat(0); // Vertex 1
            vertexBuffer.putFloat(0).putFloat(1); // Vertex 2
            vertexBuffer.putFloat(1).putFloat(1); // Vertex 3
            vertexBuffer.flip();
    
            // Create VAO
            vao = GL30.glGenVertexArrays();
            GL30.glBindVertexArray(vao);
    
            // Create VBO and fill it with vertex positions
            int vboIdPositions = GL15.glGenBuffers();
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboIdPositions);
            GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
            GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Deselect
    
            // Unbind the VAO
            GL30.glBindVertexArray(0);
        }
    
        public static void render() {
    
            // Render a random background to prove the loop is working
            GL11.glClearColor(
                    (float) (Math.random() * 0.2f),
                    (float) (Math.random() * 0.2f),
                    (float) (Math.random() * 0.2f),
                    1.0f);
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    
            // Use shader
            GL20.glUseProgram(shaderProgram);
    
            // Get camera position
            float cameraX = 0;
            float cameraY = 0;
    
            // Set view and projection
            viewProjMatrix
                    .setOrtho(
                            0,
                            800,
                            600,
                            0,
                            0.01f,
                            10
                    ).lookAt(
                            cameraX, cameraY, -1,    // Camera position
                            cameraX, cameraY, 0,     // Camera "look" vector
                            0, 1, 0                  // Camera "up" vector
                    );
    
            // Pass this matrix to our shader
            GL20.glUniformMatrix4fv(uniformLoc, false, viewProjMatrix.get(fb16));
    
            // Bind our vertex array
            GL30.glBindVertexArray(vao);
    
            // Enable vertex attributes
            GL20.glEnableVertexAttribArray(0);
    
            // Draw the vertices
            GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, NUM_VERTICES);
    
            // Disable vertex attributes
            GL20.glDisableVertexAttribArray(0);
    
            // Unbind vertex array
            GL30.glBindVertexArray(0);
    
            // Release shader
            GL20.glUseProgram(0);
        }
    
    }
    

    顶点着色器

    #version 330
    
    uniform mat4 view_proj_matrix;
    
    layout(location = 0) in vec2 in_vertex;
    
    void main(void) {
        gl_Position = view_proj_matrix * vec4(in_vertex, 0, 1);
    }
    

    片段着色器

    #version 330
    
    out vec4 frag_colour;
    
    void main(void) {
        frag_colour = vec4(1, 1, 1, 1);
    }
    

    我不知道如何进步,因为我甚至不能让这个玩具例子发挥作用。有人能看到我做错了什么,或者给我指出一个使用joml和lwjgl2和shaders的工作示例吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Rabbid76    6 年前
    viewProjMatrix.setOrtho(0, 800, 600, 0, 0.01f, 10)
    

    float aspect = 800.0f/600.0f;
    viewProjMatrix.setOrtho(-aspect, aspect, 1, -1, 0.01f, 10)
    


    lookAt(0, 0, -1, 0, 0, 0, 0, 1, 0 );
    

    Right-handed

    viewspace

    position = (0, 0, -1)
    center   = (0, 0, 0)
    up       = (0, 1, 0)
    

    y-axis = up = (0, 1, 0)
    z-axis = position - center = (0, 0, -1)
    

    x-axis = cross(y-axis, z-axis) = (-1, 0, 0)
    

            (0, 0)
                  X---------------------------x
                / |                           |
              /   |                           |
            X-----X                           |
    (-100, 100)   |         viewport          |
                  |                           |
                  |                           |
                  |                           |
                  x---------------------------x 
                                                (800, 600)
    

    lookAt(0, 0, 1, 0, 0, 0, 0, 1, 0 );
    
        2
  •  1
  •   Ripi2 user10587824    6 年前

    GLFW

    GLFW.glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    GLFW.glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    GLFW.glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = GLFW.glfwCreateWindow(800, 600, "test", 0, 0);
    

    glCullFace back