在PyOpenGL中,使用(可变的)变量来更新gl\u PointSize有着奇怪的行为

2024-04-16 12:37:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试动态设置顶点的点大小,只有在使用静态不变值时才能这样做:

#version 450 core

layout(location = 0) in vec4 vPosition;
layout(location = 4) uniform float Size = 50;

void main()
{
    gl_Position = vPosition;
    gl_PointSize = Size;
}

结果: enter image description here

但是,当我尝试动态更新它们时,我看到只有一个顶点大小发生变化,其他顶点则完全消失:

#version 450 core

layout(location = 0) in vec4 vPosition;
layout(location = 4) in float Size;

void main()
{
    gl_Position = vPosition;
    gl_PointSize = Size;
}

然后我设置它的值:

def drawWithoutVBOs(vertices, indices):
    ...
    glEnableVertexAttribArray(4)
    glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 0, 50)
    glDrawElementsus(GL_POINTS, indices)

结果: enter image description here

有人能帮我找出我做错了什么吗?你知道吗


此项目的源代码

#!/usr/bin/env python2

import time

from OpenGL.GL import *
from OpenGL.GL.shaders import *

import pygame
from pygame.locals import *
import numpy

width = 640
height = 480

vertices = numpy.array([0.0, 0.5, 0.0,
                       -0.5, -0.5, 0.0,
                        0.5, -0.5, 0.0], numpy.float32)
indices = numpy.array([0, 1, 2], numpy.ushort)

def init():
    vertexShader = compileShader("""
        #version 450 core

        layout(location = 0) in vec4 vPosition;
        layout(location = 4) in float Size;
        //layout(location = 4) uniform float Size = 50;

        void main()
        {
            gl_Position = vPosition;
            gl_PointSize = Size;
        }""", GL_VERTEX_SHADER)
    fragmentShader = compileShader("""
        #version 450 core

        void main()
        {
            gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        }""", GL_FRAGMENT_SHADER)
    program = glCreateProgram()
    glAttachShader(program, vertexShader)
    glAttachShader(program, fragmentShader)
    glBindAttribLocation(program, 0, "vPosition")
    glBindAttribLocation(program, 4, "Size")
    glLinkProgram(program)
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE) #allow the program to specify the point size

    glClearColor(0.0, 0.0, 0.0, 1.0)
    return program

def drawWithoutVBOs(vertices, indices):
    glEnableVertexAttribArray(0)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices)

    glEnableVertexAttribArray(4)
    glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 0, 50)
    glDrawElementsub(GL_POINTS, indices)
    #glDrawElements(GL_POINTS, indices.size, GL_UNSIGNED_INT, indices)

def main():
    pygame.init()
    pygame.display.set_mode((width, height), HWSURFACE|OPENGL|DOUBLEBUF)

    program = init()
    glViewport(0, 0, width, height)
    glClear(GL_COLOR_BUFFER_BIT)
    glUseProgram(program)

    drawWithoutVBOs(vertices, indices)
    pygame.display.flip()
    time.sleep(3)


if __name__ == '__main__':
    main()

Tags: inimportnumpysizemainversionlocationprogram