Ejemplo n.º 1
0
def draw_lights(lights, shader, models):
    """


    Dependencies are location, rotation, scale, and model (optionally texture).

    Args:
        lights:
        shader:
        models:

    Returns:

    """
    attribute_location = shader.attribute_location
    location_location = attribute_location['location']

    for entity in lights:

        shader.load_uniform_matrix(entity.transformation, name='transform')

        model = models[entity.model]

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)

        glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
        glEnableVertexAttribArray(location_location)
        glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0)

        glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT,
                       0)

    glDisableVertexAttribArray(location_location)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 2
0
    def draw(self, uniforms, text, quad, textures=(), *args, **kwargs):
        glUseProgram(self)
        glDisable(GL_DEPTH_TEST)


        quad.enable()

        glUniform3f(self.uniforms[b'color'], 0.3, 0.3, 0.5)

        glUniformMatrix3fv(
            self.uniforms[b'transformation'], 1, GL_TRUE,
            text.get_transformation_matrix_2D().ctypes.data_as(POINTER(GLfloat))
        )

        textures.enable(slot=0)
        glUniform1i(self.uniforms[b'font_atlas'], 0)

        quad.draw()

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)

        glBindTexture(GL_TEXTURE_2D, 0)

        glDisableVertexAttribArray(0)

        glEnable(GL_DEPTH_TEST)
Ejemplo n.º 3
0
    def draw(self, uniforms, entities, models, *args, **kwargs):
        glUseProgram(self)

        glUniformMatrix4fv(
            self.uniforms[b'perspective'], 1, GL_TRUE, uniforms.get(b'perspective').ctypes.data_as(POINTER(GLfloat))
        )
        glUniformMatrix4fv(
            self.uniforms[b'view'], 1, GL_TRUE, uniforms.get(b'view').ctypes.data_as(POINTER(GLfloat))
        )

        for model_index, entity_list in entities.items():
            model = models[model_index]

            model.enable()

            for entity in entity_list:
                glUniformMatrix4fv(
                    self.uniforms[b'transformation'], 1, GL_TRUE,
                    entity.get_transformation_matrix().ctypes.data_as(POINTER(GLfloat))
                )
                glUniform3f(self.uniforms[b'color'], *entity.color)

                model.draw()

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)

        glBindTexture(GL_TEXTURE_2D, 0)


        glDisableVertexAttribArray(0)
Ejemplo n.º 4
0
    def draw(self, shader):
        attribute_table = shader.attribute_table

        self.indexed_vbo.bind()

        # Bind all attributes.
        location_list = []  # Used to disable all VertexAttribArrays.
        for name, vbo in self.vbo_array.items():
            vbo.bind()
            attribute_location = attribute_table[name]
            location_list.append(attribute_location)
            glEnableVertexAttribArray(attribute_location)
            # specify that the data for the attribute will be pulled from the buffer that is currently bound to
            # GL_ARRAY_BUFFER. (http://stackoverflow.com/questions/30016833/multiple-in-attributes-in-shader-opengl)
            glVertexAttribPointer(attribute_location, vbo.dimension,
                                  vbo.data_type, GL_FALSE, 0, 0)

        # Draw.
        # glDrawArrays(GL_TRIANGLES, 0, vbo.count)
        glDrawElements(GL_TRIANGLES, self.indexed_vbo.count,
                       self.indexed_vbo.data_type, 0)

        # Disable and unbind everything.
        for location in location_list:
            glDisableVertexAttribArray(location)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 5
0
 def draw(self):
     """Draw the object to the display buffer"""
     from pyglet import gl
     gl.glUseProgram(self._program)
     for kind in ('fill', 'line'):
         if self._counts[kind] > 0:
             if kind == 'line':
                 if self._line_width <= 0.0:
                     continue
                 gl.glLineWidth(self._line_width)
                 if self._line_loop:
                     mode = gl.GL_LINE_LOOP
                 else:
                     mode = gl.GL_LINE_STRIP
                 cmd = partial(gl.glDrawArrays, mode, 0, self._counts[kind])
             else:
                 gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,
                                 self._buffers[kind]['index'])
                 cmd = partial(gl.glDrawElements, gl.GL_TRIANGLES,
                               self._counts[kind], gl.GL_UNSIGNED_INT, 0)
             gl.glBindBuffer(gl.GL_ARRAY_BUFFER,
                             self._buffers[kind]['array'])
             loc_pos = gl.glGetAttribLocation(self._program, b'a_position')
             gl.glEnableVertexAttribArray(loc_pos)
             gl.glVertexAttribPointer(loc_pos, 2, gl.GL_FLOAT, gl.GL_FALSE,
                                      0, 0)
             loc_col = gl.glGetUniformLocation(self._program, b'u_color')
             gl.glUniform4f(loc_col, *self._colors[kind])
             cmd()
             # The following line is probably only necessary because
             # Pyglet makes some assumptions about the GL state that
             # it perhaps shouldn't. Without it, Text might not
             # render properly (see #252)
             gl.glDisableVertexAttribArray(loc_pos)
     gl.glUseProgram(0)
Ejemplo n.º 6
0
 def draw(self):
     """Draw the object to the display buffer"""
     from pyglet import gl
     gl.glUseProgram(self._program)
     for kind in ('fill', 'line'):
         if self._counts[kind] > 0:
             if kind == 'line':
                 if self._line_width <= 0.0:
                     continue
                 gl.glLineWidth(self._line_width)
                 if self._line_loop:
                     mode = gl.GL_LINE_LOOP
                 else:
                     mode = gl.GL_LINE_STRIP
                 cmd = partial(gl.glDrawArrays, mode, 0, self._counts[kind])
             else:
                 gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,
                                 self._buffers[kind]['index'])
                 cmd = partial(gl.glDrawElements, gl.GL_TRIANGLES,
                               self._counts[kind], gl.GL_UNSIGNED_INT, 0)
             gl.glBindBuffer(gl.GL_ARRAY_BUFFER,
                             self._buffers[kind]['array'])
             loc_pos = gl.glGetAttribLocation(self._program, b'a_position')
             gl.glEnableVertexAttribArray(loc_pos)
             gl.glVertexAttribPointer(loc_pos, 2, gl.GL_FLOAT, gl.GL_FALSE,
                                      0, 0)
             loc_col = gl.glGetUniformLocation(self._program, b'u_color')
             gl.glUniform4f(loc_col, *self._colors[kind])
             cmd()
             # The following line is probably only necessary because
             # Pyglet makes some assumptions about the GL state that
             # it perhaps shouldn't. Without it, Text might not
             # render properly (see #252)
             gl.glDisableVertexAttribArray(loc_pos)
     gl.glUseProgram(0)
Ejemplo n.º 7
0
    def draw(self):
        """Draw the text to the back buffer"""
        # Border width
        self.box.setLineWidth(
            self.palette['lineWidth'])  # Use 1 as base if border width is none
        #self.borderWidth = self.box.lineWidth
        # Border colour
        self.box.setLineColor(self.palette['lineColor'], colorSpace='rgb')
        #self.borderColor = self.box.lineColor
        # Background
        self.box.setFillColor(self.palette['fillColor'], colorSpace='rgb')
        #self.fillColor = self.box.fillColor

        if self._needVertexUpdate:
            #print("Updating vertices...")
            self._updateVertices()
        if self.fillColor is not None or self.borderColor is not None:
            self.box.draw()

        # self.boundingBox.draw()  # could draw for debug purposes
        gl.glPushMatrix()
        self.win.setScale('pix')

        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.glFont.textureID)
        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glDisable(gl.GL_DEPTH_TEST)

        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)

        gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes)
        gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes)
        gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes)

        self.shader.bind()
        self.shader.setInt('texture', 0)
        self.shader.setFloat('pixel', [1.0 / 512, 1.0 / 512])
        nVerts = len(self._text) * 4

        gl.glDrawArrays(gl.GL_QUADS, 0, nVerts)
        self.shader.unbind()

        # removed the colors and font texture
        gl.glDisableClientState(gl.GL_COLOR_ARRAY)
        gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
        gl.glDisableVertexAttribArray(1)
        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
        gl.glDisable(gl.GL_TEXTURE_2D)

        if self.hasFocus:  # draw caret line
            self.caret.draw()

        gl.glPopMatrix()
Ejemplo n.º 8
0
    def draw(self,
             uniforms,
             entity,
             models,
             textures=(),
             color=(1.0, 1.0, 1.0),
             *args,
             **kwargs):
        glUseProgram(self)
        glDisable(GL_DEPTH_TEST)

        # Draw objects once as invisible to set stencil values.
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE,
                    GL_FALSE)  # Don't write any color values to color buffer.
        glStencilMask(0xFF)  # Enable writing.

        model = models[entity.model]

        model.positions.enable()
        model.indices.enable()

        self.uniforms[b'perspective'].load(
            uniforms.get(b'perspective').ctypes.data_as(POINTER(GLfloat)))
        self.uniforms[b'view'].load(
            uniforms.get(b'view').ctypes.data_as(POINTER(GLfloat)))
        self.uniforms[b'color'].load(*color)

        entity.get_transformation_matrix(
            location=self.uniforms[b'transformation'])

        model.draw()

        # Draw again with larger model.
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)

        glStencilFunc(GL_NOTEQUAL, 1,
                      0xFF)  # Update with 1's where the objects are rendered.
        glStencilMask(
            0x00
        )  # Value that AND's the value written to buffer. 0x00 basically disables writing to stencil.

        glUniformMatrix4fv(
            self.uniforms[b'transformation'], 1, GL_TRUE,
            create_transformation_matrix(
                *entity._location, *entity.rotation,
                *(entity.scale + 0.05)).ctypes.data_as(POINTER(GLfloat)))

        model.draw()

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)

        glDisableVertexAttribArray(0)

        glEnable(GL_DEPTH_TEST)
        glStencilMask(0xFF)
Ejemplo n.º 9
0
    def draw(self, uniforms, text, quad, textures=(), *args, **kwargs):
        glUseProgram(self)
        glDisable(GL_DEPTH_TEST)

        quad.enable()

        self.uniforms[b'color'].load(0.3, 0.3, 0.5)

        text.get_transformation_matrix_2D(
            location=self.uniforms[b'transformation'])

        textures.enable()

        quad.draw()

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)

        glDisableVertexAttribArray(0)

        glEnable(GL_DEPTH_TEST)
Ejemplo n.º 10
0
    def draw(self, uniforms, entities, models, textures=(), lights=(), *args, **kwargs):
        glUseProgram(self)

        # PREPARE SHADER
        glUniformMatrix4fv(  # ctypes.data_as must be here and not at initialization.
            self.uniforms[b'perspective'], 1, GL_TRUE, uniforms.get(b'perspective').ctypes.data_as(POINTER(GLfloat))
        )
        glUniformMatrix4fv(
            self.uniforms[b'view'], 1, GL_TRUE, uniforms.get(b'view').ctypes.data_as(POINTER(GLfloat))
        )

        for i, entity in enumerate(lights):
            glUniform3f(self.uniforms[b'light[' + bytes(str(i), 'utf-8') + b'].position'],  *entity.location)
            glUniform3f(self.uniforms[b'light[' + bytes(str(i), 'utf-8') + b'].color'],     *entity.color)
            glUniform1f(self.uniforms[b'light[' + bytes(str(i), 'utf-8') + b'].constant'],  entity.attenuation[0])
            glUniform1f(self.uniforms[b'light[' + bytes(str(i), 'utf-8') + b'].linear'],    entity.attenuation[1])
            glUniform1f(self.uniforms[b'light[' + bytes(str(i), 'utf-8') + b'].quadratic'], entity.attenuation[1])


        # PREPARE MODELS
        for model_index, texture_mapping in self.entities.items():

            model = models[model_index]
            model.enable()

            # PREPARE TEXTURES
            glActiveTexture(GL_TEXTURE0)
            glActiveTexture(GL_TEXTURE0 + 1)
            for texture_list, entity_list in texture_mapping.items():

                if hasattr(texture_list, '__iter__'):
                    glBindTexture(GL_TEXTURE_2D, textures[0])
                    glUniform1i(self.uniforms[b'diffuse_texture'], 0)
                    glBindTexture(GL_TEXTURE_2D, textures[1])
                    glUniform1i(self.uniforms[b'specular_texture'], 1)

                    # textures[0].enable(slot=0)
                    # textures[1].enable(slot=1)

                else:
                    textures[texture_list].enable(slot=0)
                    glUniform1i(self.uniforms[b'diffuse_texture'], 0)


                # PREPARE ENTITIES
                for entity in entity_list:
                    glUniformMatrix4fv(
                        self.uniforms[b'transformation'], 1, GL_TRUE,
                        entity.get_transformation_matrix().ctypes.data_as(POINTER(GLfloat))
                    )
                    model.draw()

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)

        glBindTexture(GL_TEXTURE_2D, 0)

        glDisableVertexAttribArray(0)
        glDisableVertexAttribArray(1)
        glDisableVertexAttribArray(2)
Ejemplo n.º 11
0
def draw_entities(entities, shader, models):
    """


    Dependencies are location, rotation, scale, and model (optionally texture).

    Args:
        shader:
        models:

    Returns:

    """
    attribute_location = shader.attribute_location
    location_location = attribute_location['location']
    texture_location = attribute_location['texture_coordinate']
    normal_location = attribute_location['normal']

    for entity in entities:
        model = models[entity.model]

        shader.set_uniform_matrix('transform', entity.transformation)

        # glActiveTexture(GL_TEXTURE0)
        # texture = textures[entity.diffuse]
        # glBindTexture(GL_TEXTURE_2D, texture.id)
        # glActiveTexture(GL_TEXTURE0 + 1)
        # texture = textures[entity.specular]
        # glBindTexture(GL_TEXTURE_2D, texture.id)
        # glActiveTexture(GL_TEXTURE0 + 2)
        # texture = textures[entity.emission]
        # glBindTexture(GL_TEXTURE_2D, texture.id)

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)

        glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
        glEnableVertexAttribArray(location_location)
        glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0)

        glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['texture_coordinate'])
        glEnableVertexAttribArray(texture_location)
        glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0, 0)

        glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal'])
        glEnableVertexAttribArray(normal_location)
        glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0)

        glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT,
                       0)

    Entity.needs_update.clear()

    glDisableVertexAttribArray(location_location)
    glDisableVertexAttribArray(texture_location)
    glDisableVertexAttribArray(normal_location)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 12
0
    def draw(self):
        gl.glPushMatrix()
        self.transform()

        # color preserve - at least nvidia 6150SE needs that
        gl.glPushAttrib(gl.GL_CURRENT_BIT)
        # glPointSize(self.get_scaled_particle_size())

        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glEnable(gl.GL_PROGRAM_POINT_SIZE)
        # glBindTexture(GL_TEXTURE_2D, self.texture.id)


        gl.glEnable(gl.GL_POINT_SPRITE)
        gl.glTexEnvi(gl.GL_POINT_SPRITE, gl.GL_COORD_REPLACE, gl.GL_TRUE)

        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        vertex_ptr = PointerToNumpy(self.particle_pos)
        gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertex_ptr)

        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        color_ptr = PointerToNumpy(self.particle_color)
        gl.glColorPointer(4, gl.GL_FLOAT, 0, color_ptr)

        gl.glEnableVertexAttribArray(self.particle_size_idx)

        size_ptr = PointerToNumpy(self.particle_size_scaled)
        gl.glVertexAttribPointer(self.particle_size_idx, 1, gl.GL_FLOAT,
            False, 0, size_ptr)

        gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT)
        gl.glEnable(gl.GL_BLEND)
        if self.blend_additive:
            gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)
        else:
            gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        # mode = GLint()
        # glTexEnviv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode )
        #
        # if self.color_modulate:
        #   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )
        # else:
        #   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE )

        self.sprite_shader.install()
        self.sprite_shader.usetTex('sprite_texture', 0, 
            gl.GL_TEXTURE_2D, self.texture.id)

        gl.glDrawArrays(gl.GL_POINTS, 0, self.total_particles)

        self.sprite_shader.uninstall()
        # un -blend
        gl.glPopAttrib()

        # color restore
        gl.glPopAttrib()

        # restore env mode
        # glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode)

        # disable states
        gl.glDisableVertexAttribArray(self.particle_size_idx)
        gl.glDisableClientState(gl.GL_COLOR_ARRAY)
        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

        gl.glDisable(gl.GL_POINT_SPRITE)
        gl.glDisable(gl.GL_PROGRAM_POINT_SIZE)
        gl.glDisable(gl.GL_TEXTURE_2D)

        gl.glPopMatrix()
Ejemplo n.º 13
0
 def disable(self):
     " Disable the shader attribute "
     glDisableVertexAttribArray(self.loc)
Ejemplo n.º 14
0
 def disable(self):
     for index in range(len(self.vbos)):
         glDisableVertexAttribArray(index)
     glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 15
0
 def disable(self):
     for index in range(len(self.vbos)):
         glDisableVertexAttribArray(index)
     self.ibo.disable()
Ejemplo n.º 16
0
    def draw(self, shader, models, textures):
        """


        Dependencies are location, rotation, scale, and model (optionally texture).

        Args:
            shader:
            models:
            textures:

        Returns:

        """
        attribute_location = shader.attribute_location
        location_location = attribute_location['position']
        texture_location = attribute_location['texture_coordinate']
        normal_location = attribute_location['normal']

        transformation = self.transformation

        diffuse = self.diffuse
        specular = self.specular
        emission = self.emission

        for entity in numpy.where(
            (self.mask & World.COMPONENT_SPRITE) == World.COMPONENT_SPRITE)[0]:
            model = models[self.model[entity]]

            shader.load_uniform_matrix(transformation[entity],
                                       name='transform')

            glActiveTexture(GL_TEXTURE0)
            texture = textures[diffuse[entity]]
            glBindTexture(GL_TEXTURE_2D, texture.id)
            glActiveTexture(GL_TEXTURE0 + 1)
            texture = textures[specular[entity]]
            glBindTexture(GL_TEXTURE_2D, texture.id)
            glActiveTexture(GL_TEXTURE0 + 2)
            texture = textures[emission[entity]]
            glBindTexture(GL_TEXTURE_2D, texture.id)

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)

            glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
            glEnableVertexAttribArray(location_location)
            glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0,
                                  0)

            glBindBuffer(GL_ARRAY_BUFFER,
                         model.vbo_array['texture_coordinate'])
            glEnableVertexAttribArray(texture_location)
            glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0,
                                  0)

            glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal'])
            glEnableVertexAttribArray(normal_location)
            glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0)

            glDrawElements(GL_TRIANGLES, model.indexed_vbo.count,
                           GL_UNSIGNED_INT, 0)

        glDisableVertexAttribArray(location_location)
        glDisableVertexAttribArray(texture_location)
        glDisableVertexAttribArray(normal_location)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 17
0
    def draw_light(self, shader, models):
        """


        Dependencies are location, rotation, scale, and model (optionally texture).

        Args:
            shader:
            models:

        Returns:

        """
        attribute_location = shader.attribute_location
        location_location = attribute_location['position']

        for entity in numpy.where(self.mask == World.COMPONENT_LIGHT)[0]:

            shader.load_uniform_matrix(create_transformation_matrix(
                *self.location[entity], *self.rotation[entity],
                *self.scale[entity]),
                                       name='transform')

            model = models[self.model[entity]]

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)

            glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
            glEnableVertexAttribArray(location_location)
            glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0,
                                  0)

            glDrawElements(GL_TRIANGLES, model.indexed_vbo.count,
                           GL_UNSIGNED_INT, 0)

        glDisableVertexAttribArray(location_location)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)


#
#
#
#
# class World:
#     # Must be of same data type as 'mask' array.
#     COMPONENT_VOID = numpy.uint(0)
#     COMPONENT_DISPLACEMENT = numpy.uint(1 << 0)
#     COMPONENT_MODEL = numpy.uint(1 << 1)
#     COMPONENT_TEXTURE = numpy.uint(1 << 2)
#
#     COMPONENT_LIGHT = COMPONENT_DISPLACEMENT | COMPONENT_MODEL
#     COMPONENT_SPRITE = COMPONENT_DISPLACEMENT | COMPONENT_MODEL | COMPONENT_TEXTURE
#
#     def __init__(self, max_entities):
#         self.max_entities = max_entities
#         self.entity_count = 0
#
#         self.mask = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C')
#
#         # Displacement.
#         self.location = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C')
#         self.rotation = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C')
#         self.scale = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C')
#         self.transformation = numpy.zeros(shape=(max_entities, 4, 4), dtype=numpy.float32, order='C')
#
#         # Model.
#         self.model = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C')
#
#         # Texture.
#         self.diffuse = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C')
#         self.specular = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C')
#         self.emission = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C')
#
#
# def create_entity(world, **attributes) -> int:
#     """
#     Generates a handle for an entity.
#
#     Returns:
#         int
#     """
#     handle = world.entity_count
#     world.entity_count += 1
#
#     if world.entity_count > world.max_entities:
#         raise ValueError('Maximum entity count of %i reached!' % world.max_entities)
#
#     for key, value in attributes.items():
#         attribute_array = getattr(world, key)
#         attribute_array[handle][:] = value
#
#     return handle
#
#
# def remove_entity(world, entity):
#     world.mask[entity] = World.COMPONENT_VOID
#
#
# def draw(world, mask, shader, models, textures):
#     """
#
#
#     Dependencies are location, rotation, scale, and model (optionally texture).
#
#     Args:
#         world:
#         mask:
#         shader:
#         models:
#         textures:
#
#     Returns:
#
#     """
#     attribute_location = shader.attribute_location
#     location_location = attribute_location['location']
#     texture_location = attribute_location['texture_coordinate']
#     normal_location = attribute_location['normal']
#
#     for entity in numpy.where((world.mask & mask) == mask)[0]:
#         model = models[world.model[entity]]
#
#         shader.load_uniform_matrix(world.transformation[entity], name='transform')
#
#         glActiveTexture(GL_TEXTURE0)
#         texture = textures[world.diffuse[entity]]
#         glBindTexture(GL_TEXTURE_2D, texture.id)
#         glActiveTexture(GL_TEXTURE0 + 1)
#         texture = textures[world.specular[entity]]
#         glBindTexture(GL_TEXTURE_2D, texture.id)
#         glActiveTexture(GL_TEXTURE0 + 2)
#         texture = textures[world.emission[entity]]
#         glBindTexture(GL_TEXTURE_2D, texture.id)
#
#         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)
#
#         glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
#         glEnableVertexAttribArray(location_location)
#         glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0)
#
#         glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['texture_coordinate'])
#         glEnableVertexAttribArray(texture_location)
#         glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0, 0)
#
#         glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal'])
#         glEnableVertexAttribArray(normal_location)
#         glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0)
#
#         glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0)
#
#     glDisableVertexAttribArray(location_location)
#     glDisableVertexAttribArray(texture_location)
#     glDisableVertexAttribArray(normal_location)
#     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
#     glBindBuffer(GL_ARRAY_BUFFER, 0)
#
#
# def draw_light(world, shader, models):
#     """
#
#
#     Dependencies are location, rotation, scale, and model (optionally texture).
#
#     Args:
#         world:
#         shader:
#         models:
#
#     Returns:
#
#     """
#     attribute_location = shader.attribute_location
#     location_location = attribute_location['location']
#
#     for entity in numpy.where(world.mask == World.COMPONENT_LIGHT)[0]:
#         shader.load_uniform_matrix(world.transformation[entity], name='transform')
#
#         model = models[world.model[entity]]
#
#         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo)
#
#         glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location'])
#         glEnableVertexAttribArray(location_location)
#         glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0)
#
#         glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0)
#
#     glDisableVertexAttribArray(location_location)
#     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
#     glBindBuffer(GL_ARRAY_BUFFER, 0)
Ejemplo n.º 18
0
    def draw(self):
        """Draw the particles system"""
        gl.glPushMatrix()
        self.transform()

        # color preserve - at least nvidia 6150SE needs that
        gl.glPushAttrib(gl.GL_CURRENT_BIT)
        # glPointSize(self.get_scaled_particle_size())

        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glEnable(gl.GL_PROGRAM_POINT_SIZE)
        # glBindTexture(GL_TEXTURE_2D, self.texture.id)


        gl.glEnable(gl.GL_POINT_SPRITE)
        gl.glTexEnvi(gl.GL_POINT_SPRITE, gl.GL_COORD_REPLACE, gl.GL_TRUE)

        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        vertex_ptr = PointerToNumpy(self.particle_pos)
        gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertex_ptr)

        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        color_ptr = PointerToNumpy(self.particle_color)
        gl.glColorPointer(4, gl.GL_FLOAT, 0, color_ptr)

        gl.glEnableVertexAttribArray(self.particle_size_idx)

        size_ptr = PointerToNumpy(self.particle_size_scaled)
        gl.glVertexAttribPointer(self.particle_size_idx, 1, gl.GL_FLOAT,
                                 False, 0, size_ptr)

        gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT)
        gl.glEnable(gl.GL_BLEND)
        if self.blend_additive:
            gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)
        else:
            gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        # mode = GLint()
        # glTexEnviv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode )
        #
        # if self.color_modulate:
        #   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )
        # else:
        #   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE )

        self.sprite_shader.install()
        self.sprite_shader.usetTex('sprite_texture', 0,
                                   gl.GL_TEXTURE_2D, self.texture.id)

        gl.glDrawArrays(gl.GL_POINTS, 0, self.total_particles)

        self.sprite_shader.uninstall()
        # un -blend
        gl.glPopAttrib()

        # color restore
        gl.glPopAttrib()

        # restore env mode
        # glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode)

        # disable states
        gl.glDisableVertexAttribArray(self.particle_size_idx)
        gl.glDisableClientState(gl.GL_COLOR_ARRAY)
        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

        gl.glDisable(gl.GL_POINT_SPRITE)
        gl.glDisable(gl.GL_PROGRAM_POINT_SIZE)
        gl.glDisable(gl.GL_TEXTURE_2D)

        gl.glPopMatrix()