opengl中四边形的颜色与指定的不同

Color of quad in opengl is different than specified

我创建的四边形与我指定的颜色不同。四边形应该是完全红色的,如下面的代码所示。每行中的最后三个值是指定颜色的值。

quad = [-0.5, -0.5, 0.0, width / height, 1.0, 0.0, 0.0, 
        -0.5, 0.5, 0.0, width / height, 1.0, 0.0, 0.0,
        0.5, 0.5, 0.0, width / height, 1.0, 0.0, 0.0,
        0.5, -0.5, 0.0, width / height, 1.0, 0.0, 0.0]

我得到的不是预期的结果:

如何让四边形看起来全红,而不是多种颜色?

完整代码:

import math
import glfw
import numpy
import pyrr
from OpenGL.GL import *
from OpenGL.GL.shaders import *

width, height = 500, 500


def draw():
    global shader
    quad = [-0.5, -0.5, 0.0, width / height, 1.0, 0.0, 0.0,
            -0.5, 0.5, 0.0, width / height, 1.0, 0.0, 0.0,
            0.5, 0.5, 0.0, width / height, 1.0, 0.0, 0.0,
            0.5, -0.5, 0.0, width / height, 1.0, 0.0, 0.0]

    quad = numpy.array(quad,
                       dtype='float32')

    indices = [0, 1, 2, 3]

    indices = numpy.array(indices, dtype='uint32')

    vertex_shader_ = """
        #version 140
        in vec4 position;
        in vec3 color;
        uniform mat4 transform;
        out vec4 out_color;
        void main(){
            
            gl_Position = position;
            out_color = vec4(color.rgb, 1);

        };
    
    """

    fragment_shader_ = """
        #version 140
        in vec4 out_color;
        
        void main(){
        
            gl_FragColor = out_color;
        };
    
    """

    shader = compileProgram(compileShader(vertex_shader_, GL_VERTEX_SHADER),
                            compileShader(fragment_shader_, GL_FRAGMENT_SHADER))
    glUseProgram(shader)

    VBO = glGenBuffers(1)

    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, quad.nbytes, quad, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)

    position = glGetAttribLocation(shader, "position")
    glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE, 28, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
    color = glGetAttribLocation(shader, "color")
    glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(16))
    glEnableVertexAttribArray(color)


def Screen():
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glDrawArrays(GL_POLYGON, 0, 4)
    draw()
    glViewport(0, 0, width, height)


def main():
    global width, height
    if not glfw.init():
        return
    window = glfw.create_window(500, 500, "Opengl GLFW Window", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()

        Screen()
        width, height = glfw.get_window_size(window)

        print(width, height)
        glViewport(0, 0, width, height)
        glfw.swap_buffers(window)

    glfw.terminate()


if __name__ == '__main__':
    main()

每个 glVertexAttribPointer() 中的步幅参数被赋予了两个不同的数字。感谢@genpfault 指出这一点!