示例#1
0
 def testCompile(self):
     try:
         program = Program()
         program.attachShader(
             Shader('resources/shaders/test_vert.vs', GL_VERTEX_SHADER))
         program.attachShader(
             Shader('resources/shaders/test_frag.fs', GL_FRAGMENT_SHADER))
         program.link()
         self.assertNotEqual(program.getId(), 0)
     except RuntimeError:
         self.assertTrue(False)
示例#2
0
 def testErrorCompile(self):
     try:
         program = Program()
         program.attachShader(
             Shader('resources/shaders/test_vert.vs', GL_VERTEX_SHADER))
         program.attachShader(
             Shader('resources/shaders/error.fs', GL_FRAGMENT_SHADER))
         program.link()
         self.assertTrue(False)
     except RuntimeError:
         self.assertTrue(True)
def getLinkedProgram(vertPath, fragPath):
    program = Program()
    program.attachShader(Shader(vertPath, GL_VERTEX_SHADER))
    program.attachShader(Shader(fragPath, GL_FRAGMENT_SHADER))
    program.link()
    return program
 def testCompileError(self):
     shader = Shader('resources/shaders/error.fs', GL_FRAGMENT_SHADER)
     self.assertRaises(RuntimeError, shader.compile)
 def testShaderFileNotExist(self):
     try:
         shader = Shader('resources/shaders/not_exist.vs', GL_VERTEX_SHADER)
         self.assertTrue(False)
     except RuntimeError:
         self.assertTrue(True)
 def testCompile(self):
     shader = Shader('resources/shaders/test_vert.vs', GL_VERTEX_SHADER)
     shader.compile()
     self.assertEqual(shader.getId(), 1)
def main():
    if not glfw.init():
        print('Failed to initialize GLFW.')
        return

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    glfw.window_hint(glfw.SAMPLES, config['sampling_level'])

    if config['fullscreen']:
        global width, height
        mode = glfw.get_video_mode(glfw.get_primary_monitor())
        width, height = mode.size.width, mode.size.height
        window = glfw.create_window(mode.size.width, mode.size.height,
                                    config['app_name'],
                                    glfw.get_primary_monitor(), None)
    else:
        window = glfw.create_window(width, height, config['app_name'], None,
                                    None)
    if not window:
        print('Failed to create GLFW Window.')
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, resizeCallback)
    glfw.set_key_callback(window, keyCallback)

    glEnable(GL_DEPTH_TEST)
    glEnable(GL_MULTISAMPLE)

    program = Program()
    program.attachShader(Shader('resources/shaders/vert.vs', GL_VERTEX_SHADER))
    program.attachShader(
        Shader('resources/shaders/frag.fs', GL_FRAGMENT_SHADER))
    program.link()

    depthProgram = Program()
    depthProgram.attachShader(
        Shader('resources/shaders/depth.vs', GL_VERTEX_SHADER))
    depthProgram.attachShader(
        Shader('resources/shaders/depth.gs', GL_GEOMETRY_SHADER))
    depthProgram.attachShader(
        Shader('resources/shaders/depth.fs', GL_FRAGMENT_SHADER))
    depthProgram.link()

    shadow = Shadow(config['near_plane'], config['far_plane'])
    shadow.create(config['shadow_width'], config['shadow_height'])

    lightPos = glm.vec3(0, 2, 0)
    viewPos = glm.vec3(0, 0.5, 0)
    perspective = glm.perspective(45, width / height, config['near_plane'],
                                  config['far_plane'])

    room = Model('resources/models/room.json')

    lastTime = glfw.get_time()
    while not glfw.window_should_close(window):
        if config['debug_mode']:
            print(glGetError())
        currentTime = glfw.get_time()
        deltaTime = currentTime - lastTime
        lastTime = currentTime

        camX = math.sin(currentTime * 0.2)
        camZ = math.cos(currentTime * 0.2)
        viewDir = glm.vec3(camX, -0.3, camZ)
        view = glm.lookAt(viewPos, viewPos + viewDir, glm.vec3(0, 1, 0))

        shadow.castShadow(depthProgram, lightPos)
        room.draw(depthProgram)
        shadow.endCastShadow(program)

        glViewport(0, 0, width, height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glClearColor(0, 0, 0, 1)

        program.use()
        program.setMat4('viewProject', perspective * view)
        program.setVec3('viewPos', viewPos)
        program.setVec3('lightPos', lightPos)
        room.draw(program)

        glfw.poll_events()
        glfw.swap_buffers(window)

    glfw.terminate()
def main():
    if not glfw.init():
        print('Failed to initialize GLFW.')
        return
    
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    glfw.window_hint(glfw.SAMPLES, config['sampling_level'])
    
    if config['fullscreen']:
        global width, height
        mode = glfw.get_video_mode(glfw.get_primary_monitor())
        width, height = mode.size.width, mode.size.height
        window = glfw.create_window(mode.size.width, mode.size.height, config['app_name'], glfw.get_primary_monitor(), None)
    else:
        window = glfw.create_window(width, height, config['app_name'], None, None)
    if not window:
        print('Failed to create GLFW Window.')
        glfw.terminate()
        return

    # подключаем наши функции для эвентов 
    #
    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, ResizeCallback)
    glfw.set_cursor_pos_callback(window, MouseLookCallback)
    glfw.set_key_callback(window, KeyInputCallback) 
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)
    
    # подключаем фрагментный и вершинный шейдеры
    #
    program = Program()
    program.attachShader(Shader('resources/shaders/vert.vs', GL_VERTEX_SHADER))
    program.attachShader(Shader('resources/shaders/frag.fs', GL_FRAGMENT_SHADER))
    program.link()
    
    # подключаем шейдер для создания карты теней
    #  
    depthProgram = Program()
    depthProgram.attachShader(Shader('resources/shaders/depth.vs', GL_VERTEX_SHADER))
    depthProgram.attachShader(Shader('resources/shaders/depth.fs', GL_FRAGMENT_SHADER))
    depthProgram.attachShader(Shader('resources/shaders/depth.gs', GL_GEOMETRY_SHADER))
    depthProgram.link()

    # создаем depthBuffer и frameBuffer
    #
    shadow = Shadow(config['near_plane_depth'], config['far_plane_depth'])
    shadow.create(config['shadow_width'], config['shadow_height'])

    program.use()
    program.setInt("diffuseTexture", 0)
    program.setInt("depthMap", 1)

    # позиция источника света
    #
    lightPos = glm.vec3(0.0, 2.35, 0.0)
    
    # загрузка всех объектов сцены
    #
    room = Model('resources/models/dinning_room.json')
    
    # цикл обработки
    #
    while not glfw.window_should_close(window):
        if config['debug_mode']:
            print(glGetError())

        # обработка нажатий клавиатуры для камеры
        #
        DoMovement()
        
        # движение источника света
        #
        if light_movement:
            lightPos.z = math.sin(glfw.get_time() * 0.5) * 3.0

        glClearColor(0.1, 0.1, 0.1, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        
        # обработка с помощью depthProgram - карта теней
        #
        shadow.castShadow(depthProgram, lightPos)
        room.draw(depthProgram)
        shadow.endCastShadow(program)

        
        glViewport(0, 0, width, height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # обработка с помощью program - на основе полученной карты теней
        #
        program.use()
        view = cam.get_view_matrix()
        viewPos = glm.vec3(cam.camera_pos[0],cam.camera_pos[1],cam.camera_pos[2])
        perspective = glm.perspective(45, width / height, config['near_plane'], config['far_plane'])
        program.setMat4('projection', perspective)
        program.setMat4('view', view)
        program.setVec3('lightPos', lightPos)
        program.setVec3('viewPos', viewPos)
        program.setInt('shadows', True)
        program.setFloat("far_plane", config["far_plane_depth"])
        glActiveTexture(GL_TEXTURE1)
        glBindTexture(GL_TEXTURE_CUBE_MAP, shadow.depthbuffer.texture)
        room.draw(program)

        glfw.swap_buffers(window)
        glfw.poll_events()
        

    glfw.terminate()