Esempio n. 1
0
    def draw(self, projection, view, width, height):
        model_matrix = self._transform.matrix

        # Set uniforms
        self._program["u_projection"] = projection
        # Note: OpenGL matrix multiplication works on column-major oriented storage (as least for pre-multiplication).
        # Also glumpy.glm is using column-major assumption for its operations.
        view_model_matrix = np.transpose(
            np.matmul(np.transpose(view), np.transpose(model_matrix)))
        self._program["u_view_model_matrix"] = view_model_matrix
        # self._program["u_view"] = view
        # self._program["u_model"] = self._model
        view_model_normal_matrix = np.transpose(
            np.linalg.inv(view_model_matrix))
        self._program["u_view_model_normal_matrix"] = view_model_normal_matrix
        self._program["u_color_scale"] = self._color_scale
        self._program["u_normal_scale"] = self._normal_scale
        self._program["u_depth_scale"] = self._depth_scale
        if self._shader_type == self.PHONG:
            self._program["u_light_position"] = self._light_position
            self._program[
                "u_light_ambient_intensity"] = 0.4 * self._light_intensity
            self._program[
                "u_light_diffuse_intensity"] = 0.4 * self._light_intensity
            self._program[
                "u_light_specular_intensity"] = 0.2 * self._light_intensity
            self._program["u_material_ambient"] = self._material
            self._program["u_material_diffuse"] = self._material
            self._program["u_material_specular"] = self._material
            self._program["u_material_shininess"] = 32

        with self._program.activate():
            # Bind index buffer and draw
            if self.use_depth_test:
                gl.glEnable(gl.GL_DEPTH_TEST)
            else:
                gl.glDisable(gl.GL_DEPTH_TEST)
            if self.use_face_culling:
                gl.glFrontFace(gl.GL_CCW)
                gl.glCullFace(gl.GL_BACK)
                gl.glEnable(gl.GL_CULL_FACE)
            else:
                gl.glDisable(gl.GL_CULL_FACE)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._gl_index_buffer)
            gl.glDrawElements(gl.GL_TRIANGLES, 3 * len(self._faces),
                              opengl_utils.get_gl_type(self._faces), None)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0)
Esempio n. 2
0
    def draw(self, mode=gl.GL_TRIANGLES, indices=None):  #first=0, count=None):
        """ Draw using the specified mode & indices.

        :param gl.GLEnum mode: 
          One of
            * GL_POINTS
            * GL_LINES
            * GL_LINE_STRIP
            * GL_LINE_LOOP,
            * GL_TRIANGLES
            * GL_TRIANGLE_STRIP
            * GL_TRIANGLE_FAN

        :param IndexBuffer|None indices:
            Vertex indices to be drawn. If none given, everything is drawn.
        """

        self.activate()
        attributes = self._attributes.values()

        # Get buffer size first attribute
        # We need more tests here
        #  - do we have at least 1 attribute ?
        #  - does all attributes report same count ?
        # count = (count or attributes[0].size) - first

        if isinstance(indices, IndexBuffer):
            indices.activate()
            gltypes = {
                np.dtype(np.uint8): gl.GL_UNSIGNED_BYTE,
                np.dtype(np.uint16): gl.GL_UNSIGNED_SHORT,
                np.dtype(np.uint32): gl.GL_UNSIGNED_INT
            }
            gl.glDrawElements(mode, indices.size, gltypes[indices.dtype], None)
            indices.deactivate()
        else:
            first = 0
            # count = (self._count or attributes[0].size) - first
            count = len(tuple(attributes)[0])
            gl.glDrawArrays(mode, first, count)

        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
        self.deactivate()
Esempio n. 3
0
    def draw(self, mode=gl.GL_TRIANGLES, indices=None):  # first=0, count=None):
        """ Draw the attribute arrays in the specified mode.

        Parameters
        ----------
        mode : GL_ENUM
            GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP,
            GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN

        first : int
            The starting vertex index in the vertex array. Default 0.

        count : int
            The number of vertices to draw. Default all.
        """

        self.activate()
        attributes = self._attributes.values()

        # Get buffer size first attribute
        # We need more tests here
        #  - do we have at least 1 attribute ?
        #  - does all attributes report same count ?
        # count = (count or attributes[0].size) - first

        if isinstance(indices, IndexBuffer):
            indices.activate()
            gltypes = {
                np.dtype(np.uint8): gl.GL_UNSIGNED_BYTE,
                np.dtype(np.uint16): gl.GL_UNSIGNED_SHORT,
                np.dtype(np.uint32): gl.GL_UNSIGNED_INT,
            }
            gl.glDrawElements(mode, indices.size, gltypes[indices.dtype], None)
            indices.deactivate()
        else:
            first = 0
            # count = (self._count or attributes[0].size) - first
            count = len(tuple(attributes)[0])
            gl.glDrawArrays(mode, first, count)

        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
        self.deactivate()
Esempio n. 4
0
    def draw(self, mode = gl.GL_TRIANGLES, indices=None): #first=0, count=None):
        """ Draw using the specified mode & indices.

        :param gl.GLEnum mode: 
          One of
            * GL_POINTS
            * GL_LINES
            * GL_LINE_STRIP
            * GL_LINE_LOOP,
            * GL_TRIANGLES
            * GL_TRIANGLE_STRIP
            * GL_TRIANGLE_FAN

        :param IndexBuffer|None indices:
            Vertex indices to be drawn. If none given, everything is drawn.
        """

        self.activate()
        attributes = self._attributes.values()

        # Get buffer size first attribute
        # We need more tests here
        #  - do we have at least 1 attribute ?
        #  - does all attributes report same count ?
        # count = (count or attributes[0].size) - first

        if isinstance(indices, IndexBuffer):
            indices.activate()
            gltypes = { np.dtype(np.uint8) : gl.GL_UNSIGNED_BYTE,
                        np.dtype(np.uint16): gl.GL_UNSIGNED_SHORT,
                        np.dtype(np.uint32): gl.GL_UNSIGNED_INT }
            gl.glDrawElements(mode, indices.size, gltypes[indices.dtype], None)
            indices.deactivate()
        else:
            first = 0
            # count = (self._count or attributes[0].size) - first
            count = len(tuple(attributes)[0])
            gl.glDrawArrays(mode, first, count)

        gl.glBindBuffer( gl.GL_ARRAY_BUFFER, 0 )
        self.deactivate()
Esempio n. 5
0
    def draw(self, mode = gl.GL_TRIANGLES, indices=None): #first=0, count=None):
        """ Draw the attribute arrays in the specified mode.

        Parameters
        ----------
        mode : GL_ENUM
            GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP,
            GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN

        first : int
            The starting vertex index in the vertex array. Default 0.

        count : int
            The number of vertices to draw. Default all.
        """

        self.activate()
        attributes = self._attributes.values()

        # Get buffer size first attribute
        # We need more tests here
        #  - do we have at least 1 attribute ?
        #  - does all attributes report same count ?
        # count = (count or attributes[0].size) - first

        if isinstance(indices, IndexBuffer):
            indices.activate()
            gltypes = { np.dtype(np.uint8) : gl.GL_UNSIGNED_BYTE,
                        np.dtype(np.uint16): gl.GL_UNSIGNED_SHORT,
                        np.dtype(np.uint32): gl.GL_UNSIGNED_INT }
            gl.glDrawElements(mode, indices.size, gltypes[indices.dtype], None)
            indices.deactivate()
        else:
            first = 0
            # count = (self._count or attributes[0].size) - first
            count = len(attributes[0])
            gl.glDrawArrays(mode, first, count)

        gl.glBindBuffer( gl.GL_ARRAY_BUFFER, 0 )
        self.deactivate()