Ejemplo n.º 1
0
def create_program_attribute_layout(program):

    active_attribute = c_int(0)
    GL.glGetProgramiv(program, GL.GL_ACTIVE_ATTRIBUTES, active_attribute)

    max_name_length = GL.glGetProgramiv(program, GL.GL_ACTIVE_ATTRIBUTE_MAX_LENGTH)
    name_length = GL.GLsizei(0)
    attribute_size = GL.GLint(0)
    attribute_type = GL.GLenum(0)
    name = (GL.GLchar * max_name_length)()

    attribute_layout = []
    for index in range(active_attribute.value):
        GL.glGetActiveAttrib(program, index, max_name_length, name_length, attribute_size, attribute_type, name)
        attribute_location = GL.glGetAttribLocation(program, name.value)
        attribute_layout.append((attribute_location, ATTRIBUTE_SIZE_BY_TYPES[attribute_type.value]))

    attribute_layout.sort(key=lambda pair: pair[0])

    attribute_locations = []
    attribute_sizes = []
    for attribute_location, attribute_size in attribute_layout:
        attribute_locations.append(attribute_location)
        attribute_sizes.append(attribute_size)

    return attribute_locations, attribute_sizes
Ejemplo n.º 2
0
    def build_attribute_map(self):
        """
        Builds an internal attribute map by querying the program.
        This way we don't have to query OpenGL (can cause slowdowns)
        This information is also used when the shader and VAO negotiates the buffer binding.
        """
        attribute_count = GL.glGetProgramiv(self.program,
                                            GL.GL_ACTIVE_ATTRIBUTES)
        bufsize = GL.glGetProgramiv(self.program,
                                    GL.GL_ACTIVE_ATTRIBUTE_MAX_LENGTH)
        print("Shader {} has {} attribute(s)".format(self.name,
                                                     attribute_count))
        for i in range(attribute_count):
            # Unlike glGetActiveUniform the attrib version do not return a convenient tuple
            # and we'll have to use to ugly version. (Most people make wrappers for this one)
            length, size, type, name = GL.GLsizei(), GL.GLint(), GL.GLenum(), (
                GL.GLchar * bufsize)()
            GL.glGetActiveAttrib(self.program, i, bufsize, length, size, type,
                                 name)

            # Get the actual location. Do not trust the original order
            location = GL.glGetAttribLocation(self.program, name.value)
            attribute = Attribute(name.value, type.value, location)
            self.attribute_map[attribute.name] = attribute
            self.attribute_list.append(attribute)
            print(" - {}".format(attribute))

        self.attribute_key = ','.join(
            name for name in sorted(self.attribute_map.keys()))
        print("Shader attribute key:", self.attribute_key)
Ejemplo n.º 3
0
    def _get_attribs(self) -> Dict[str, IndexSizeType]:
        _active_attrib = GL.glGetProgramiv(self.program,
                                           GL.GL_ACTIVE_ATTRIBUTES)

        # For some reason the PyOpenGL Binding is sorta broken
        # use the full ctypes-style access
        bufSize = 50
        ct_nameSize = GL.GLsizei()
        ct_size = GL.GLint()
        ct_type = GL.GLenum()
        ct_name = (GL.GLchar * bufSize)()

        _attribs = {}

        for attrib_index in range(_active_attrib):
            GL.glGetActiveAttrib(self.program, attrib_index, bufSize,
                                 ct_nameSize, ct_size, ct_type, ct_name)

            #in python3 attribute names are bytestrings. Convert to UTF8
            name = ct_name.value.decode("ascii")

            _attribs[name] = IndexSizeType(attrib_index, ct_size.value,
                                           ct_type.value)

        return _attribs