コード例 #1
0
ファイル: test_basics.py プロジェクト: almarklein/vispy
def _test_setting_stuff():
    # Set stuff to touch functions
    
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    #
    gl.glBlendColor(1.0, 1.0, 1.0, 1.0)
    gl.glBlendEquation(gl.GL_FUNC_ADD)
    gl.glBlendEquationSeparate(gl.GL_FUNC_ADD, gl.GL_FUNC_ADD)
    gl.glBlendFunc(gl.GL_ONE, gl.GL_ZERO)
    gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE, gl.GL_ZERO)
    #
    gl.glClearColor(0.0, 0.0, 0.0, 1.0)
    gl.glClearDepth(1)
    gl.glClearStencil(0)
    #
    gl.glColorMask(True, True, True, True)
    gl.glDepthMask(False)
    gl.glStencilMask(255)
    gl.glStencilMaskSeparate(gl.GL_FRONT, 128)
    #
    gl.glStencilFunc(gl.GL_ALWAYS, 0, 255)
    gl.glStencilFuncSeparate(gl.GL_FRONT, gl.GL_ALWAYS, 0, 255)
    gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
    gl.glStencilOpSeparate(gl.GL_FRONT, gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
    #
    gl.glFrontFace(gl.GL_CW)
    gl.glHint(gl.GL_GENERATE_MIPMAP_HINT, gl.GL_FASTEST)
    gl.glLineWidth(2.0)
    gl.glPolygonOffset(0.0, 0.0)
    gl.glSampleCoverage(1.0, False)
    
    # And getting stuff
    try:
        with use_log_level('error', print_msg=False):
            r, p = gl.glGetShaderPrecisionFormat(gl.GL_FRAGMENT_SHADER,
                                                 gl.GL_HIGH_FLOAT)
            gl.check_error()  # Sometimes the func is there but OpenGL errs
    except Exception:
        pass  # accept if the function is not there ...
        # We should catch RuntimeError and GL.error.NullFunctionError,
        # but PyOpenGL may not be available.
        # On Travis this function was not there on one machine according
        # to PyOpenGL, but our desktop backend worked fine ...
        
    #
    v = gl.glGetParameter(gl.GL_VERSION)
    assert_true(isinstance(v, string_types))
    assert_true(len(v) > 0)
    
    gl.check_error()
コード例 #2
0
ファイル: test_basics.py プロジェクト: robmcmullen/vispy
def _test_setting_stuff():
    # Set stuff to touch functions

    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    #
    gl.glBlendColor(1.0, 1.0, 1.0, 1.0)
    gl.glBlendEquation(gl.GL_FUNC_ADD)
    gl.glBlendEquationSeparate(gl.GL_FUNC_ADD, gl.GL_FUNC_ADD)
    gl.glBlendFunc(gl.GL_ONE, gl.GL_ZERO)
    gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE, gl.GL_ZERO)
    #
    gl.glClearColor(0.0, 0.0, 0.0, 1.0)
    gl.glClearDepth(1)
    gl.glClearStencil(0)
    #
    gl.glColorMask(True, True, True, True)
    gl.glDepthMask(False)
    gl.glStencilMask(255)
    gl.glStencilMaskSeparate(gl.GL_FRONT, 128)
    #
    gl.glStencilFunc(gl.GL_ALWAYS, 0, 255)
    gl.glStencilFuncSeparate(gl.GL_FRONT, gl.GL_ALWAYS, 0, 255)
    gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
    gl.glStencilOpSeparate(gl.GL_FRONT, gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
    #
    gl.glFrontFace(gl.GL_CW)
    gl.glHint(gl.GL_GENERATE_MIPMAP_HINT, gl.GL_FASTEST)
    gl.glLineWidth(2.0)
    gl.glPolygonOffset(0.0, 0.0)
    gl.glSampleCoverage(1.0, False)

    # And getting stuff
    try:
        with use_log_level('error', print_msg=False):
            r, p = gl.glGetShaderPrecisionFormat(gl.GL_FRAGMENT_SHADER,
                                                 gl.GL_HIGH_FLOAT)
            gl.check_error()  # Sometimes the func is there but OpenGL errs
    except Exception:
        pass  # accept if the function is not there ...
        # We should catch RuntimeError and GL.error.NullFunctionError,
        # but PyOpenGL may not be available.
        # On Travis this function was not there on one machine according
        # to PyOpenGL, but our desktop backend worked fine ...

    #
    v = gl.glGetParameter(gl.GL_VERSION)
    assert_true(isinstance(v, string_types))
    assert_true(len(v) > 0)

    gl.check_error()
コード例 #3
0
    def on_draw(self, event):

        # Read about depth testing and changing stated in vispy here http://vispy.org/gloo.html?highlight=set_state
        gloo.clear(color=[0, 0, 0, 1.0], depth=True)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_REPLACE)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT
                   | gl.GL_STENCIL_BUFFER_BIT)

        gl.glStencilFunc(gl.GL_ALWAYS, 1, 0xFF)
        gl.glStencilMask(0xFF)

        # delta_time
        self.current_frame = time()
        self.delta_time = self.current_frame - self.last_frame
        self.last_frame = self.current_frame

        if self.camera.bool_a:
            self.camera.ProcessKeyboard(Camera_Movement.LEFT, self.delta_time)
        if self.camera.bool_w:
            self.camera.ProcessKeyboard(Camera_Movement.FORWARD,
                                        self.delta_time)
        if self.camera.bool_s:
            self.camera.ProcessKeyboard(Camera_Movement.BACKWARD,
                                        self.delta_time)
        if self.camera.bool_d:
            self.camera.ProcessKeyboard(Camera_Movement.RIGHT, self.delta_time)

        self.view = self.camera.GetViewMatrix()
        self.projection = glm.perspective(glm.radians(self.camera.Zoom),
                                          builtins.width / builtins.height,
                                          0.1, 100.0)

        # vispy takes numpy array in m * n matrix form
        self.view = (np.array(self.view.to_list()).astype(np.float32))
        self.projection = (np.array(self.projection.to_list()).astype(
            np.float32))

        # reshaping to (m, n) to (1, m*n) to support data input in vispy
        self.view = self.view.reshape(
            (1, self.view.shape[0] * self.view.shape[1]))
        self.projection = self.projection.reshape(
            (1, self.projection.shape[0] * self.projection.shape[1]))

        # drawing normal cube
        self.program['view'] = self.view
        self.program['projection'] = self.projection
        self.program['a_position'] = self.vertices
        self.program['aNormal'] = self.aNormal
        self.program['viewPos'] = self.camera.Position
        self.program['texCoords'] = self.texCoord

        self.program['l_ambient[0]'] = [0.2, 0.2, 0.2]
        self.program['l_diffuse[0]'] = [1, 1, 0.5]
        self.program['l_specular[0]'] = [1.0, 1.0, 1.0]
        self.program['l_direction[0]'] = [-0.3, -1, -1]
        self.program['m_diffuse[0]'] = self.diffuse_map
        self.program['m_shininess[0]'] = 32
        self.program['m_specular[0]'] = self.specular_map

        self.model = glm.mat4(1.0)
        # rotate the cube if you want
        # self.model = glm.rotate(self.model, glm.radians((time() - self.startTime) * 10), glm.vec3(0,1.5,1))
        self.model = glm.translate(self.model, glm.vec3(0, 0.5, 0))
        self.model = (np.array(self.model.to_list()).astype(np.float32))
        self.model = self.model.reshape(
            (1, self.model.shape[0] * self.model.shape[1]))
        self.program['model'] = self.model
        self.program.draw('triangles')

        self.model = glm.mat4(1.0)
        self.model = glm.translate(self.model, glm.vec3(0.5, 0.5, 2))
        self.model = (np.array(self.model.to_list()).astype(np.float32))
        self.model = self.model.reshape(
            (1, self.model.shape[0] * self.model.shape[1]))
        self.program['model'] = self.model
        self.program.draw('triangles')

        self.program['a_position'] = self.vertices * 10
        self.model = glm.mat4(1.0)
        self.model = glm.translate(self.model, glm.vec3(0, -5, 0))
        self.model = (np.array(self.model.to_list()).astype(np.float32))
        self.model = self.model.reshape(
            (1, self.model.shape[0] * self.model.shape[1]))
        self.program['model'] = self.model
        self.program.draw('triangles')

        gl.glStencilFunc(gl.GL_NOTEQUAL, 1, 0xFF)
        gl.glStencilMask(0x00)
        gl.glDisable(gl.GL_DEPTH_TEST)

        self.model = glm.mat4(1.0)
        self.model = glm.translate(self.model, [0, 0.5, 0])
        self.model = (np.array(self.model.to_list()).astype(np.float32))
        self.model = self.model.reshape(
            (1, self.model.shape[0] * self.model.shape[1]))

        # drawing light source
        self.programLightSource['model'] = self.model
        self.programLightSource['view'] = self.view
        self.programLightSource['projection'] = self.projection
        self.programLightSource['a_position'] = self.vertices * 1.2

        self.programLightSource.draw('triangles')

        gl.glStencilMask(0xFF)
        gl.glStencilFunc(gl.GL_ALWAYS, 1, 0xFF)
        gl.glEnable(gl.GL_DEPTH_TEST)

        self.update()