为什么这个投影会给出错误的结果?

时间:2018-12-08 22:38:56

标签: opengl glsl nim

我尝试通过创建正交投影矩阵来获得所谓的2D渲染世界空间坐标。但是结果令人失望,我期望有一个蓝色三角形,但是右下角只有一些蓝色像素。

enter image description here

这是我用Nim语言编写的代码。这是一个非常简化的版本,可以重现该问题。

重要的部分在函数“ projection2D”中,或者在顶点着色器中。我认为问题不在其他地方,但是为了安全起见,我发布了完整的示例

type 
    OGLfloat = float32
    OGLuint = uint32
    OGLint = int32

type Mat4x4* = array[16, OGLfloat] # 4 x 4 Matrix

# Here OpenGL constants should be here but not pasted to save space.
#....

const 
    POSITION_LENGTH = 3.OGLint
    COLOR_LENGTH = 4.OGLint

const
    WINDOW_W = 640
    WINDOW_H = 480

let
    colorDataOffset = POSITION_LENGTH * OGLint(sizeof(OGLfloat))

# OpenGL function import should be here.
#...

var 
    # Thanks to an orthogonal projection matrix I expect to use coordinates in pixels.

    vertices = @[OGLfloat(420), 0, 0, # Position   
                                0, 0, 1, 1, # Color
                          640, 480, 0,
                                0, 0, 1, 1,
                          0, 480, 0,
                                0, 0, 1, 1]

    indices = @[OGLuint(0), 1 , 2]

proc projection2D(left, right, bottom, top, far, near:float):Mat4x4 =
    result = [OGLfloat(1), 0, 0, 0, # Start from an identity matrix.
                        0, 1, 0, 0, 
                        0, 0, 1, 0, 
                        0, 0, 0, 1]

    # Orthographic projection, inspired from a Wikipedia article example.

    result[0] = OGLfloat(2.0 / (right - left))
    result[5] = OGLfloat(2.0 / (top - bottom))
    result[3] = OGLfloat(- ((right + left) / (right - left)))
    result[7] = OGLfloat(- ((top + bottom) / (top - bottom)))
    result[10] = OGLfloat(-2 / (far - near))
    result[11] = OGLfloat(-((far + near)/(far - near)))

# These parameters comes from "learnopengl.com". 
var projectionMatrix = projection2D(0.0, OGLfloat(WINDOW_W), OGLfloat(WINDOW_H), 0.0, - 1.0, 1.0)

var glfwErr = glfwInit()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
var winHandle = glfwCreateWindow(WINDOW_W, WINDOW_H)
glfwMakeContextCurrent(winHandle)
var glewErr = glewInit()

var
    shadID:OGLuint
    vertSrc:cstring = """
        #version 330 core
        layout (location = 0) in vec3 aPos;
        layout (location = 1) in vec4 aColor;

        out vec4 vColor;

        uniform mat4 projection;

        void main()
        {
            gl_Position = projection * vec4(aPos, 1.0f);
            vColor = aColor;
        }
        """
    fragSrc:cstring = """
        #version 330 core
        out vec4 FragColor;    
        in vec4 vColor;

        void main()
        {
            FragColor = vColor;
        }

        """

proc send_src(vert:var cstring, frag:var cstring):OGLuint =
    var success:OGLint
    # vertex
    var vertexShader = glCreateShader(GL_VERTEX_SHADER)
    glShaderSource(vertexShader, 1, addr vert, nil)
    glCompileShader(vertexShader)
    # Check compilation errors.
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, addr success)
    if bool(success) == false:
        echo(" vertex shader compilation failed (send_src)")
    else:
        echo("vertexShader compiled (send_src)")

    # fragment
    var fragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
    glShaderSource(fragmentShader, 1, addr frag, nil)
    glCompileShader(fragmentShader)
    # Check compilation errors.
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, addr success)
    if bool(success) == false:
        echo("fragment shader compilation failed (send_src)")
    else:
        echo("fragmentShader compiled (send_src)")

    # Shader program
    result = glCreateProgram()
    glAttachShader(result, vertexShader)
    glAttachShader(result, fragmentShader)
    glLinkProgram(result)
    # Check for linkage errors.
    glGetProgramiv(result, GL_LINK_STATUS, addr success)
    if success == 0:
        echo("program linking failed (send_src)") 
    else:
        echo("shader linked (send_src)")

    glDeleteShader(vertexShader)
    glDeleteShader(fragmentShader)

glViewport(0, 0, WINDOW_W, WINDOW_H)
shadID = send_src(vertSrc, fragSrc)

var VAO, VBO, EBO:OGLuint
glGenVertexArrays(1, addr VAO)
glGenBuffers(1, addr VBO)
glGenBuffers(1, addr EBO)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.len * sizeof(OGLfloat), 
             addr vertices[0], GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.len * sizeof(OGLuint), 
             addr indices[0], GL_STATIC_DRAW)
# Position layout
glVertexAttribPointer(0, POSITION_LENGTH, GL_FLOAT, GL_FALSE, (POSITION_LENGTH + COLOR_LENGTH) * OGLint(sizeof(OGLfloat)), 
                      nil)
glEnableVertexAttribArray(0)
# Color layout
glVertexAttribPointer(1, COLOR_LENGTH, GL_FLOAT, GL_FALSE, (POSITION_LENGTH + COLOR_LENGTH) * OGLint(sizeof(OGLfloat)), 
                      cast[pointer](colorDataOffset))
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
glUseProgram(shadID)

while bool(glfwWindowShouldClose(winHandle)) == false:
    glClearColor(0.2, 0.3, 0.3, 1.0)
    glClear(GL_COLOR_BUFFER_BIT)
    glBindVertexArray(VAO)
    glUniformMatrix4fv(glGetUniformLocation(shadID, "projection"), 1, GL_FALSE, addr projectionMatrix[0])
    glDrawElements(GL_TRIANGLES, OGLint(indices.len), GL_UNSIGNED_INT, nil)
    glfwSwapBuffers(winHandle)
    glfwPollEvents()

glDeleteVertexArrays(1, addr VAO)
glDeleteBuffers(1, addr VBO)
glDeleteBuffers(1, addr EBO)
glfwDestroyWindow(winHandle)
glfwTerminate()

不要犹豫,分享您的想法!

1 个答案:

答案 0 :(得分:3)

您必须转置投影矩阵,这意味着glUniformMatrix4fv的第三个参数必须为GL_TRUE

glUniformMatrix4fv(glGetUniformLocation(shadID, "projection"), 
                   1, GL_TRUE, addr projectionMatrix[0])

或者,您必须根据规范初始化矩阵(请参见下文):

 proc projection2D(left, right, bottom, top, far, near:float):Mat4x4 =
    result = [OGLfloat(1), 0, 0, 0, # Start from an identity matrix.
                        0, 1, 0, 0, 
                        0, 0, 1, 0, 
                        0, 0, 0, 1]

    # Orthographic projection, inspired from a Wikipedia article example.

    result[0] = OGLfloat(2.0 / (right - left))
    result[5] = OGLfloat(2.0 / (top - bottom))
    result[12] = OGLfloat(- ((right + left) / (right - left)))
    result[13] = OGLfloat(- ((top + bottom) / (top - bottom)))
    result[10] = OGLfloat(-2 / (far - near))
    result[14] = OGLfloat(-((far + near)/(far - near)))

请参见The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 101

  

要通过指定矢量或标量来初始化矩阵,必须以列大顺序将分量分配给矩阵元素。

mat4(float, float, float, float,  // first column
     float, float, float, float,  // second column
     float, float, float, float,  // third column
     float, float, float, float); // fourth column

请注意,与将列从上到下写的数学矩阵相比,这感觉很自然,在OpenGL矩阵初始化时,列是从左到右写的。这带来的好处是,轴或平移的x,y,z分量在内存中直接连续。

另请参阅Data Type (GLSL) - Matrix constructors