Example #1
0
class SimplePhysShader(object):
    '''
    Simple shader to project images in log space
    '''
    def __init__(self, shaderName="SimplePhysBuffer"):
        '''
        Constructor
        '''
        #self._incShader = 0
        self._shaderName = shaderName
        self._prepared = False  # Has the shader been initialised?
        self._frameBuffer = None

    # Do this when the program is loaded
    def Init(self):

        if not self._prepared:
            # Set up shaders
            self._shader = Shader(self._shaderName)
            self._prepared = True

    # Do this before the objects are rendered
    def Begin(self):
        self._shader.Bind()

    # Do this after the objects are rendered
    def End(self):
        self._shader.Unbind()

    def Reset(self):
        '''
        Reset the shader; make it run the pre-load operation again
        NOTE: THIS IS PROBABLY UNNEEDED, DELETE TO CLEAN UP INTERFACE
        '''
        self._preloaded = False
Example #2
0
 def Build(self, points,pointSizes=None):
     
     # Make billboarding shader
     self._shader = Shader("Billboard")
     
     # Make display list
     self._points = points
     self._displayList = glGenLists(1)
     glNewList(self._displayList,GL_COMPILE) 
     c = 0.005
     c0 = np.array([-c,-c,0])
     c1 = np.array([-c,+c,0])
     c2 = np.array([+c,+c,0])
     c3 = np.array([+c,-c,0])
     
     glBegin(GL_TRIANGLES)
     for it in self._points:
         glVertex3fv(it+c0)
         glVertex3fv(it+c1)
         glVertex3fv(it+c2)
         
         glVertex3fv(it+c0)
         glVertex3fv(it+c2)
         glVertex3fv(it+c3)
     glEnd()
     
     glEndList()
    def Build(self, points):

        # Make billboarding shader
        self._shader = Shader("PointSpriteScaler")

        # Make the billboarding texture
        texSize = 16
        im = GaussianImage(texSize)
        self._texture = glGenTextures(1)
        glEnable(GL_TEXTURE_2D)
        glBindTexture(GL_TEXTURE_2D, self._texture)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, texSize, texSize, 0,
                     GL_RGBA, GL_FLOAT, im)
        glBindTexture(GL_TEXTURE_2D, 0)
        glEnable(GL_POINT_SMOOTH)
        glEnable(GL_PROGRAM_POINT_SIZE)

        # Make display list
        self._points = points
        self._displayList = glGenLists(1)
        glNewList(self._displayList, GL_COMPILE)

        glBegin(GL_POINTS)
        for it in self._points:
            glVertex3fv(it)
        glEnd()

        glEndList()
Example #4
0
    def setup(self):

        self.shader = Shader("vertex.vs", "fragment.fs")

        self.object = OpenGLObject("box")

        self.object.vbo.data = [ 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0 ]
        
        self.object.ebo.data = [ 0, 1, 2, 0, 2, 3 ]

        self.object.loadData()
Example #5
0
 def __init__(self):
     self.vao = glGenVertexArrays(1)
     glBindVertexArray(self.vao)
     self.vertexSize = 15
     self.NewBuffer = VertexBuffer(self.vertexSize)
     self.buffer = self.NewBuffer.buffer
     self.NewIndex = IndexBuffer()
     self.ibo = self.NewIndex.buffer
     self.NewShader = Shader()
     self.program = ctypes.c_uint
     self.Identity4 = numpy.array(
         [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], dtype='float32')
Example #6
0
    def Init(self, frameBuffer):

        if not self._prepared:
            self._frameBuffer = frameBuffer
            # Set up shaders
            #self._incShader = Shader("IncrementBuffer")
            self._logShader = Shader(self._shaderName)
            size = max(self._frameBuffer.Width(), self._frameBuffer.Height())
            size = 2.0**int(np.log2(size) + 1)
            self._reduction = ReductionShader(self._frameBuffer.Texture(),
                                              size, ["ReduceMaxMin"])

            self._prepared = True
Example #7
0
 def init(self):
     glClearColor(0.0, 0.0, 0.0, 1.0)  # 设置画布背景色。注意:这里必须是4个参数
     glEnable(GL_DEPTH_TEST)  # 开启深度测试,实现遮挡关系
     glDepthFunc(GL_LEQUAL)  # 设置深度测试函数(GL_LEQUAL只是选项之一)
     folderPath = os.path.dirname(os.path.abspath(__file__))
     self.shader = Shader(os.path.join(folderPath, "vertex.glsl"),
                          os.path.join(folderPath, "fragment.glsl"))
Example #8
0
 def __init__(self, textureID, length, fragShaderNames):
     '''
     Constructor
     texture - a texture object to reduce on TODO: OR A FBO???
     fragShaderNames - an iterable container of fragment shader names to reduce on
     '''
     # Set up input texture
     self._inputID = textureID
     self._length = length
     # Set up maximum buffer level (one below level of texture, where length = 2^level)
     self._bufferLevel = int(math.log(length,2))
     # Set up shaders
     self._shaders = list()
     for frag in fragShaderNames:
         self._shaders.append(Shader(frag,"DoNothing"))
     self._outputs = np.zeros((len(self._shaders),2))
     # Generates a FBO handler
     self._frameBuffer = c_uint(0)
     glGenFramebuffers(1, self._frameBuffer)
     # Make quad display list to draw to
     self._quadList = glGenLists(1)
     # Levels to use (leave empty for now)
     self._levels = None
     # Initialise (yes, I know, functional code in a constructor)
     self._Init()
Example #9
0
    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClearDepth(1.0)

        domeStr = "CanvasModel.Dome"
        self.shaders[domeStr + "_proj"] = Shader("shaders/dome_proj.vert",
                                                 "shaders/dome_4proj.frag")
        self.shaders[domeStr + "_edgeblend"] = Shader(
            "shaders/dome_edgeblend.vert", "shaders/dome_edgeblend_4proj.frag")
        self.shaders[domeStr + "_inv_proj"] = Shader(
            "shaders/dome_inv_proj.vert", "shaders/dome_inv_proj.frag")
        #     cycloramaStr = "CanvasModel.Cyclorama"
        #     self.shaders[cycloramaStr+"_proj"] = Shader("shaders/cyclorama_proj.vert","shaders/dome_proj.frag")
        #    self.shaders[cycloramaStr+"_edgeblend"] = Shader("shaders/cyclorama_edgeblend.vert","shaders/cyclorama_edgeblend.frag")
        #   self.shaders[cycloramaStr+"_inv_proj"] = Shader("shaders/dome_inv_proj.vert","shaders/dome_inv_proj.frag")

        for textureFile in self.textureFiles:
            texFile = os.path.join(TEXTURE_PATH, textureFile)
            self.textures[textureFile[:-4]] = Texture(texFile)
Example #10
0
    def __init__(self, file_name):
        global active_camera
        self.models = None
        self.cameras = None
        self.lights = None
        self.active_camera = active_camera

        # shared resources
        self.textures = {}  # name -> tex lookup
        self.shaders = {}  # name -> tex lookup
        self.meshes = {}  # name -> tex lookup

        with open(file_name, "r") as fp:
            data = json.load(fp)

        # models
        self.models = {name: Model(m) for (name, m) in data["models"].items()}
        for model in self.models.values():
            vert_file = model.io['shader']['vert']
            frag_file = model.io['shader']['frag']
            shader_name = vert_file.split('.')[0]
            shader = ResourceManager.get_shader(shader_name)
            if not shader:
                shader = Shader(vert_file, frag_file)
                ResourceManager.add_shader(shader_name, shader)
            model.shader = shader

            mesh_name = model.io['mesh']  # contains relative path string
            mesh = ResourceManager.get_mesh(mesh_name)
            if not mesh:
                mesh = Mesh(mesh_name)
                ResourceManager.add_mesh(mesh_name, mesh)
            model.mesh = mesh

            tex_files = model.io['textures']
            for (layer, tex_file) in tex_files.items():
                tex = ResourceManager.get_texture(tex_file)
                if not tex:
                    tex = Texture(tex_file)
                    ResourceManager.add_texture(tex_file, tex)
                model.textures[layer] = tex

        # cameras, set first camera to active
        self.cameras = {
            name: Camera(c)
            for (name, c) in data["cameras"].items()
        }
        if len(self.cameras) > 0:
            # get first items value
            active_camera = next(iter(self.cameras.items()))[1]
        logging.warn('camera needs to set shader uniforms')

        # lights
        self.lights = {name: Light(l) for (name, l) in data["lights"].items()}
        logging.warn('light needs to set shader uniforms')
Example #11
0
class PointRendererBillboard_Impl1(PointRenderer):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        self._points = None
        self._displayList = 0
        self._name = "PointRenderBillboard"
        self._shader = None
        
    def Name(self):
        return self._name
    
    def Weight(self, weight):
        self._weight = weight
        
    def Build(self, points,pointSizes=None):
        
        # Make billboarding shader
        self._shader = Shader("Billboard")
        
        # Make display list
        self._points = points
        self._displayList = glGenLists(1)
        glNewList(self._displayList,GL_COMPILE) 
        c = 0.005
        c0 = np.array([-c,-c,0])
        c1 = np.array([-c,+c,0])
        c2 = np.array([+c,+c,0])
        c3 = np.array([+c,-c,0])
        
        glBegin(GL_TRIANGLES)
        for it in self._points:
            glVertex3fv(it+c0)
            glVertex3fv(it+c1)
            glVertex3fv(it+c2)
            
            glVertex3fv(it+c0)
            glVertex3fv(it+c2)
            glVertex3fv(it+c3)
        glEnd()
        
        glEndList()
    
    def Draw(self):
        glColor4f(1.0,0.0,1.0,0.4)
        shaders.glUseProgram(self._shader.Shader())
        glCallList(self._displayList)
        shaders.glUseProgram(0)
Example #12
0
    def __init__(self, attributes, bone_nodes, bone_offsets, index=None):

        # setup shader attributes for linear blend skinning shader
        self.vertex_array = VertexArray(attributes, index)

        # feel free to move this up in Viewer as shown in previous practicals
        self.skinning_shader = Shader(SKINNING_VERT, COLOR_FRAG)

        # store skinning data
        self.bone_nodes = bone_nodes
        self.bone_offsets = bone_offsets
Example #13
0
    def createShaders(self):
        self.makeCurrent()
        sh = Shader()

        nSh = len(sh.shaderCodes)
        for i in range(0, nSh):
            prg = QGLShaderProgram(self.context())
            prg.addShaderFromSourceCode(QGLShader.Fragment, sh.shaderCodes[i])
            prg.link()

            self.shadePrograms.append(prg)

        self.doneCurrent()
Example #14
0
    def __init__(self):
        self.context = Context.getInstance()  # NOTE must be initialized
        self.logger = logging.getLogger(__name__)

        self.windowWidth = 640
        self.windowHeight = 480

        self.initialize()

        self.colorShader = Shader(
            self.context.getResourcePath('shaders', 'ADS.vert'),
            self.context.getResourcePath('shaders', 'ADS.frag'))

        self.proj = hm.perspective(hm.identity(), 35,
                                   float(self.windowWidth) / self.windowHeight,
                                   1.0, 1000.0)
        self.view = hm.lookat(
            hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32),
            np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float32),
            np.array([0.0, -1.0, 0.0, 1.0], dtype=np.float32))
        self.cameraMatrix = np.dot(self.proj, self.view)
        self.setCameraMatrix(self.cameraMatrix)
Example #15
0
class myApp(Snowball):

    def setup(self):

        self.shader = Shader("vertex.vs", "fragment.fs")

        self.object = OpenGLObject("box")

        self.object.vbo.data = [ 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0 ]
        
        self.object.ebo.data = [ 0, 1, 2, 0, 2, 3 ]

        self.object.loadData()

    def loop(self, dt):

        glClearColor(0.2, 0.3, 0.8, 1.0)

        glClear(GL_COLOR_BUFFER_BIT)

        self.shader.use()

        self.object.draw()
Example #16
0
    def __init__(self, lightDirection):
        super().__init__()
        # Tronc
        cylindre = load("Utils/cylinder.obj")[0]
        cylindre.shader = Shader(TRONC_VERT, TRONC_FRAG)
        # self.add(cylindre)
        # self.add(Rocher(lightDirection))
        self.cylindre = cylindre

        feuilles = Rocher(lightDirection,
                          complexite_rocher=3,
                          intensite_reduction_longueur=4,
                          taille_initiale_rayon=2,
                          color=[0.3, 0.7, 0.3],
                          borne_intensite=0.4)
        noeud = Node(children=[feuilles],
                     transform=translate(feuilles.centre[0] - 1.5,
                                         feuilles.centre[1] + 1.5,
                                         feuilles.centre[2] - 3))
        noeud2 = Node(children=[cylindre], transform=scale(0.2, 2, 0.2))
        self.add(noeud)
        self.add(noeud2)
        self.shader = Shader(ARBRE_VERT, ARBRE_FRAG)
        self.lightDirection = lightDirection
Example #17
0
    def __init__(self, texture, attributes, indices):
        self.vertex_array = VertexArray(attributes, indices)
        self.shader = Shader(TEXTURE_FILE_VERT, TEXTURE_FILE_FRAG)
        # interactive toggles
        self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT,
                           GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE])
        self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
                             (GL.GL_LINEAR, GL.GL_LINEAR),
                             (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)
        self.file = texture

        # setup texture and upload it to GPU
        self.texture = Texture(texture, self.wrap_mode, *self.filter_mode)
        self.pressable = [False, False] #C MOI KI LA FE CLOCHETEU
Example #18
0
    def __init__(self):
        self.context = Context.getInstance()  # NOTE must be initialized
        self.logger = logging.getLogger(__name__)

        self.windowWidth = 640
        self.windowHeight = 480

        self.initialize()

        self.colorShader = Shader(self.context.getResourcePath('shaders', 'ADS.vert'), self.context.getResourcePath('shaders', 'ADS.frag'))

        self.proj = hm.perspective(hm.identity(), 35, float(self.windowWidth) / self.windowHeight, 1.0, 1000.0)
        self.view = hm.lookat(hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32), np.array([0.0, 0.0, 1.0, 1.0], dtype = np.float32), np.array([0.0, -1.0, 0.0, 1.0], dtype = np.float32))
        self.cameraMatrix = np.dot(self.proj, self.view)
        self.setCameraMatrix(self.cameraMatrix)
Example #19
0
 def __init__(self,
              lightDirection,
              complexite_rocher=6,
              seuilChangement=0,
              taille_initiale_rayon=2,
              intensite_reduction_longueur=3,
              height_spike=0.5,
              color=[0.4, 0.4, 0.4],
              borne_intensite=0.2):
     super().__init__()
     rocher_array, normales, centre = createRandomRockIterative(
         complexite_rocher, seuilChangement, taille_initiale_rayon,
         intensite_reduction_longueur, borne_intensite)
     self.vertex_array = VertexArray(attributes=[rocher_array, normales])
     self.shader = Shader(ROCHER_VERT, ROCHER_FRAG)
     self.lightDirection = lightDirection
     self.centre = centre
     self.color = color
Example #20
0
    def __init__(self):
        # feel free to move this up in the viewer as per other practicals
        self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG)

        # triangle and face buffers
        vertices = 100 * np.array(((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)), np.float32)
        faces = np.array(((0, 1, 2), (0, 2, 3)), np.uint32)
        self.vertex_array = VertexArray([vertices], faces)

        # interactive toggles
        self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT,
                           GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE])
        self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
                             (GL.GL_LINEAR, GL.GL_LINEAR),
                             (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)

        # setup texture and upload it to GPU
        self.texture = TexturePerlin(self.wrap_mode, *self.filter_mode)
        self.pressable = [False, False] #C MOI KI LA FE CLOCHETEU
Example #21
0
    def __init__(self, width=640, height=480):

        # version hints: create GL window with >= OpenGL 3.3 and core profile
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
        glfw.window_hint(glfw.RESIZABLE, True)
        self.win = glfw.create_window(width, height, 'Viewer', None, None)

        # make win's OpenGL context current; no OpenGL calls can happen before
        glfw.make_context_current(self.win)

        # register event handlers
        glfw.set_key_callback(self.win, self.on_key)
        glfw.set_window_size_callback(self.win, self.on_resize)

        # useful message to check OpenGL renderer characteristics
        print(
            'OpenGL',
            GL.glGetString(GL.GL_VERSION).decode() + ', GLSL',
            GL.glGetString(GL.GL_SHADING_LANGUAGE_VERSION).decode() +
            ', Renderer',
            GL.glGetString(GL.GL_RENDERER).decode())

        # initialize GL by setting viewport and default render characteristics
        GL.glClearColor(0.1, 0.1, 0.1, 0.1)
        GL.glEnable(GL.GL_DEPTH_TEST)  # depth test now enabled (TP2)
        GL.glEnable(GL.GL_CULL_FACE)  # backface culling enabled (TP2)

        # compile and initialize shader programs once globally
        # self.color_shader = Shader(COLOR_VERT, COLOR_FRAG)
        # print(COLOR_VERT)
        self.lighting_shader = Shader(PHONG_VERT, PHONG_FRAG)

        # initially empty list of object to draw
        self.drawables = []
        self.trackball = GLFWTrackball(self.win)

        # cyclic iterator to easily toggle polygon rendering modes
        self.fill_modes = cycle([GL.GL_LINE, GL.GL_POINT, GL.GL_FILL])
Example #22
0
 def __init__(self,
              nom_image,
              light_direction,
              longueur_arrete=0.5,
              translate_x=0,
              translate_y=0,
              translate_z=0,
              scale_total=1):
     super().__init__()
     matrice_image = misc.imread(nom_image)
     self.terrain_array, self.normales = generateTerrainFromImage(
         matrice_image, longueur_arrete, translate_x, translate_y,
         translate_z, scale_total)
     self.x_terrain = sorted([v[0] for v in self.terrain_array])
     self.z_terrain = sorted([v[2] for v in self.terrain_array])
     self.height_for_2d_position = {(v[0], v[2]): v[1]
                                    for v in self.terrain_array}
     self.vertex_array = VertexArray(
         attributes=[self.terrain_array, self.normales])
     self.shader = Shader(TERRAIN_VERT, TERRAIN_FRAG)
     self.light_direction = light_direction
     self.translation_y = translate_y
     self.scale = scale_total
Example #23
0
mesh1 = Mesh("resources/meshes/tetra3_p_n_uv1.ply")
mesh1.load()
mesh1.create()
mesh1_mat = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 2]))

mesh2 = Mesh("resources/meshes/plane2_p_n_uv1.ply")
mesh2.load()
mesh2.create()
mesh2_mat = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0]))

# mesh3 = Mesh("resources/meshes/Dragon.ply")
# mesh3.load()
# mesh3.create()
# mesh3_mat = pyrr.matrix44.create_from_translation(pyrr.Vector3([2, 1, .5]))

shader1 = Shader("resources/shaders/shader1.vs", "resources/shaders/shader1.fs")
shader1.load()
shader1.create()
# shader1.use() # needed

tex1 = Texture("resources/images/marble.jpg")
tex1.load()
tex1.create()

tex2 = Texture("resources/images/brickwall.jpg")
tex2.load()
tex2.create()

tex3 = Texture("resources/images/StoneMarbleCalacatta004_COL_2K.jpg")
tex3.load()
tex3.create()
Example #24
0
class Renderer:
    def __init__(self):
        self.context = Context.getInstance()  # NOTE must be initialized
        self.logger = logging.getLogger(__name__)

        self.windowWidth = 640
        self.windowHeight = 480

        self.initialize()

        self.colorShader = Shader(
            self.context.getResourcePath('shaders', 'ADS.vert'),
            self.context.getResourcePath('shaders', 'ADS.frag'))

        self.proj = hm.perspective(hm.identity(), 35,
                                   float(self.windowWidth) / self.windowHeight,
                                   1.0, 1000.0)
        self.view = hm.lookat(
            hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32),
            np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float32),
            np.array([0.0, -1.0, 0.0, 1.0], dtype=np.float32))
        self.cameraMatrix = np.dot(self.proj, self.view)
        self.setCameraMatrix(self.cameraMatrix)

    def setCameraMatrix(self, cameraMatrix):
        self.colorShader.setUniformMat4('view', self.view)
        self.colorShader.setUniformMat4('proj', self.proj)

    def setModelMatrix(self, model):
        self.colorShader.setUniformMat4('model', model)

    def setLightPos(self, lightPos):
        self.colorShader.setUniformVec4('lightPosition', lightPos)

    def setDiffCol(self, diffCol):
        self.colorShader.setUniformVec3('diffuseColor', diffCol)

    def windowOpen(self):
        return glfw.GetWindowParam(glfw.OPENED)

    def startDraw(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    def endDraw(self):
        glfw.SwapBuffers()

    def initialize(self):
        glfw.Init()
        glfw.OpenWindowHint(glfw.FSAA_SAMPLES, 4)
        glfw.OpenWindowHint(glfw.OPENGL_VERSION_MAJOR, 3)
        glfw.OpenWindowHint(glfw.OPENGL_VERSION_MINOR, 2)  # 3.2
        glfw.OpenWindowHint(glfw.OPENGL_PROFILE,
                            glfw.OPENGL_CORE_PROFILE)  # 3.2
        glfw.OpenWindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)  # 3.2
        glfw.OpenWindowHint(glfw.WINDOW_NO_RESIZE, GL_TRUE)

        try:  # in case we fail to init this version
            glfw.OpenWindow(self.windowWidth, self.windowHeight, 0, 0, 0, 0,
                            24, 8, glfw.WINDOW)
        except Exception as e:
            self.logger.warn("Failed to initialize OpenGL: {}".format(str(e)))
            self.logger.warn("Trying lower version...")
            glfw.OpenWindowHint(glfw.OPENGL_VERSION_MINOR, 0)  # 3.0
            glfw.OpenWindowHint(glfw.OPENGL_PROFILE, 0)  # 3.0
            glfw.OpenWindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_FALSE)  # 3.0
            glfw.OpenWindow(self.windowWidth, self.windowHeight, 0, 0, 0, 0,
                            24, 8, glfw.WINDOW)
            self.logger.warn("OpenGL fallback to lower version worked")

        glfw.SetWindowTitle("TANG")
        glfw.SetWindowCloseCallback(self.onWindowClose)

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        # Find out OpenGL version, and store for future use
        self.context.GL_version_string = glGetString(GL_VERSION).split(
            ' '
        )[0]  # must have version at the beginning, e.g. "3.0 Mesa 9.x.x" to extract "3.0"
        self.context.GL_version_major, self.context.GL_version_minor = list(
            int(x) for x in self.context.GL_version_string.split('.'))[
                0:2]  # must have only MAJOR.MINOR
        self.logger.info("OpenGL version {}.{}".format(
            self.context.GL_version_major, self.context.GL_version_minor))
        self.context.GLSL_version_string = glGetString(
            GL_SHADING_LANGUAGE_VERSION).split(' ')[0].replace(
                '.', '')  # "1.50" => "150"
        self.logger.info("GLSL version {}".format(
            self.context.GLSL_version_string))

    def quit(self):
        glfw.CloseWindow()  # NOTE does not generate onWindowClose callback
        #glfw.Terminate()

    def onWindowClose(self):
        #self.logger.debug("Renderer.onWindowClose()")
        return True  # nothing more needs to be done; but an event could be generated
Example #25
0
 def set_shader_indirect(self, vert_shader, frag_shader):
     self.shader = Shader(vert_shader, frag_shader)
Example #26
0
 def __init__(self, attributes, index=None):
     self.vertex_array = VertexArray(attributes, index)
     self.shader = Shader(COLOR_VERT_POS, COLOR_FRAG_POS)
Example #27
0
class ColourShader(object):
    '''
    Simple shader to project images in log space
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self._x, self._y = (0, 0)
        self._width, self._height = (0, 0)
        self._depth = 0  #ctypes.c_int(0)
        self._frame = 0  #ctypes.c_int(0)
        self._texture = 0  #ctypes.c_int(0)
        self._image = 0
        self._incShader = 0
        self._logShader = 0
        self._recalcMaxMin = 0  # Counter to recalculate max/min
        self._logmin = -16.11  # = ln(1e-7)
        self._invlogrng = 0.135  # Arbitrary value
        self._prepared = False
        self._reduction = None  # Reduction shader for calculating min/max

    def ViewPos(self, x, y):
        '''
        Set the view position of the outputted shader
        '''
        self._x = x
        self._y = y

    # Do this when the program is loaded
    def Init(self, display):

        if not self._prepared:
            # Set up image
            self._width, self._height = display.Window().get_size()
            # Set up empty texture to draw to
            #self._texture = glGenTextures(1)
            self._texture = ctypes.c_uint(0)
            glGenTextures(1, self._texture)
            #self._texture = pyglet.image.Texture.create(self._width, self._height, rectangle=True,internalformat=GL_RGBA32F_ARB)

            glEnable(GL_TEXTURE_2D)
            glBindTexture(GL_TEXTURE_2D, self._texture)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            #glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

            glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
            glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
            glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)
            #glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, self._width, self._height, 0,\
            # GL_RGBA, GL_FLOAT, 0)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, self._width, self._height, 0,\
             GL_RGBA, GL_FLOAT, 0)
            glBindTexture(GL_TEXTURE_2D, 0)

            # Set up the frame buffer object
            self._frame = ctypes.c_uint(0)
            glGenFramebuffers(1, self._frame)
            #self._frame = glGenFramebuffers(1)
            glBindFramebuffer(GL_FRAMEBUFFER, self._frame)

            # Set up the depth buffer
            #self._depth = glGenRenderbuffers(1)
            self._depth = ctypes.c_uint(0)
            glGenRenderbuffers(1, self._depth)
            glBindRenderbuffer(GL_RENDERBUFFER, self._depth)

            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
                                  self._width, self._height)
            glBindRenderbuffer(GL_RENDERBUFFER, 0)

            # Attach texture and depth buffer
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                   GL_TEXTURE_2D, self._texture,
                                   ctypes.c_int(0))
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                      GL_RENDERBUFFER, self._depth)

            # Unbind frame buffer for now
            glBindFramebuffer(GL_FRAMEBUFFER, 0)

            status = glCheckFramebufferStatus(GL_FRAMEBUFFER)

            # Create shaders
            self._incShader = Shader("IncrementBuffer")
            self._logShader = Shader("LogBuffer")
            self._reduction = ReductionShader(self._texture,
                                              max(self._width, self._height),
                                              ["ReduceMaxMin"])

            self._prepared = True

        # Pass everything through since we want to make a projected density map
        glEnable(GL_BLEND)
        glBlendFunc(GL_ONE, GL_ONE)

    # Do this before the objects are rendered
    def Begin(self, display):

        # Re-bind frame buffer
        glBindFramebuffer(GL_FRAMEBUFFER, self._frame)

        # Clear frame buffer
        glClearColor(0, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glDisable(GL_TEXTURE_2D)
        glDisable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_ONE, GL_ONE)
        #self._incShader.Bind()
        # At this point we draw...

    # Do this after the objects are rendered
    def End(self, display):
        # Take out the shader currently in use
        #self._incShader.Unbind()
        # Unbind frame buffer
        glBindFramebuffer(GL_FRAMEBUFFER, 0)
        # TEST REDUCTION
        reduce = self._reduction.Run()
        maxval = reduce[0, 0]
        minval = reduce[0, 1]
        # Get the max/min value in the texture to pass to the shader
        self._recalcMaxMin -= 1
        properScale = self._recalcMaxMin <= 0
        properScale = True
        if properScale:
            #data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT)
            #red = data[:,:,0]
            #minval = np.min(red[np.nonzero(red)])
            #maxval = np.max(red)
            if minval == maxval:
                dmin = 0.0
                dmax = 0.0
                invrng = 1.0
            else:
                dmin = float(np.log(minval))
                dmax = float(np.log(maxval))
                #dmin = dmax - 10.0
                try:
                    invrng = 1.0 / (dmax - dmin)
                except:
                    invrng = 1.0
            #print "Image min, range, max:", dmin, 1.0/invrng, dmax
            #newrng = np.min([10.0,1.0/invrng]) # HACK - stops weird hard circle effect
            #dmin = dmax - newrng
            #invrng = 1.0/newrng
            dmax = 3.0
            dmin = -3.0
            self._logmin = dmin
            self._invlogrng = invrng
            self._recalcMaxMin = 10
            #print self._logmin, self._invlogrng
            #del(data)
        # HACK
        #data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT)
        #import scipy
        #scipy.misc.imsave('outfile.jpg', data)
        # HACK END

    def DrawTexture(self):
        logshade = True
        if logshade:
            self._logShader.Bind()
            self._logShader.AddFloat(self._logmin, "texmin")
            self._logShader.AddFloat(self._invlogrng, "texinvrng")
        #shaders.glUseProgram(self._logShader.Shader())
        #minloc = glGetUniformLocation( self._logShader.Shader(), 'texmin' )
        #invrngloc = glGetUniformLocation( self._logShader.Shader(), 'texinvrng' )
        #glUniform1f( minloc,self._logmin)
        #glUniform1f( invrngloc,self._invlogrng)
        #shaders.glUseProgram(0)
        glLoadIdentity()
        glViewport(self._x, self._y, self._width, self._height)
        # Now we need to plot the texture to screen
        # Set up an orthogonal projection
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, 1, 0, 1, -10, 10)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        #glDrawPixels(self._width, self._height, GL_RGBA, GL_FLOAT, self._texture)
        # Now draw the texture with the log shader
        # Draw the texture onto a flat plane
        glColor4f(1.0, 1.0, 1.0, 1.0)
        glEnable(GL_TEXTURE_2D)
        #glDisable(GL_TEXTURE_GEN_S)
        #glDisable(GL_TEXTURE_GEN_T)
        glBindTexture(GL_TEXTURE_2D, self._texture)
        glBegin(GL_TRIANGLES)
        glTexCoord2d(0.0, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glTexCoord2d(1.0, 0.0)
        glVertex3f(1.0, 0.0, 0.0)
        glTexCoord2d(1.0, 1.0)
        glVertex3f(1.0, 1.0, 0.0)

        glTexCoord2d(1.0, 1.0)
        glVertex3f(1.0, 1.0, 0.0)
        glTexCoord2d(0.0, 1.0)
        glVertex3f(0.0, 1.0, 0.0)
        glTexCoord2d(0.0, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glEnd()
        if logshade:
            self._logShader.Unbind()
        glBindTexture(GL_TEXTURE_2D, 0)
Example #28
0
class LogShader(object):
    '''
    Simple shader to project images in log space
    '''
    def __init__(self, shaderName="LogBuffer"):
        '''
        Constructor
        '''
        #self._incShader = 0
        self._logShader = 0
        self._shaderName = shaderName
        self._recalcMaxMin = 0  # Counter to recalculate max/min
        self._logmin = -16.5  # = ln(1e-7)
        self._invlogrng = 0.1  # arbitrary guess
        self._prepared = False  # Has the shader been initialised?
        self._preloaded = False  # Have we found the min/max value of the image?
        self._reduction = None  # Reduction shader for calculating min/max
        self._frameBuffer = None

    # Do this when the program is loaded
    def Init(self, frameBuffer):

        if not self._prepared:
            self._frameBuffer = frameBuffer
            # Set up shaders
            #self._incShader = Shader("IncrementBuffer")
            self._logShader = Shader(self._shaderName)
            size = max(self._frameBuffer.Width(), self._frameBuffer.Height())
            size = 2.0**int(np.log2(size) + 1)
            self._reduction = ReductionShader(self._frameBuffer.Texture(),
                                              size, ["ReduceMaxMin"])

            self._prepared = True

    # Do this before the objects are rendered
    def Begin(self):

        # Reduce the image
        # Get the max/min value in the texture to pass to the shader
        self._recalcMaxMin -= 1
        properScale = self._recalcMaxMin <= 0
        # HACK - turn off properscale counter; turn back on if it runs slowly
        properScale = True  # HACK - always run
        if properScale:
            reduce = self._reduction.Run()
            maxval = reduce[0, 0]
            minval = reduce[0, 1]
            if minval == maxval:
                dmin = 0.0
                dmax = 0.0
                invrng = 1.0
            else:
                dmin = float(np.log(minval))
                dmax = float(np.log(maxval))
                try:
                    invrng = 1.0 / (dmax - dmin)
                except:
                    invrng = 1.0
                self._logmin = dmin
                self._invlogrng = invrng
            self._recalcMaxMin = 10

        # Reset view
        x, y = self._frameBuffer.Pos()
        width = self._frameBuffer.Width()
        height = self._frameBuffer.Height()
        glViewport(x, y, width, height)

        self._logShader.Bind()
        self._logShader.AddFloat(self._logmin, "texmin")
        self._logShader.AddFloat(self._invlogrng, "texinvrng")

    # Do this after the objects are rendered
    def End(self):
        self._logShader.Unbind()

    def Reset(self):
        '''
        Reset the shader; make it run the pre-load operation again
        '''
        self._preloaded = False
Example #29
0
    def Init(self, display):

        if not self._prepared:
            # Set up image
            self._width, self._height = display.Window().get_size()
            # Set up empty texture to draw to
            #self._texture = glGenTextures(1)
            self._texture = ctypes.c_uint(0)
            glGenTextures(1, self._texture)
            #self._texture = pyglet.image.Texture.create(self._width, self._height, rectangle=True,internalformat=GL_RGBA32F_ARB)

            glEnable(GL_TEXTURE_2D)
            glBindTexture(GL_TEXTURE_2D, self._texture)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            #glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

            glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
            glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
            glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
            glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)
            #glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, self._width, self._height, 0,\
            # GL_RGBA, GL_FLOAT, 0)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, self._width, self._height, 0,\
             GL_RGBA, GL_FLOAT, 0)
            glBindTexture(GL_TEXTURE_2D, 0)

            # Set up the frame buffer object
            self._frame = ctypes.c_uint(0)
            glGenFramebuffers(1, self._frame)
            #self._frame = glGenFramebuffers(1)
            glBindFramebuffer(GL_FRAMEBUFFER, self._frame)

            # Set up the depth buffer
            #self._depth = glGenRenderbuffers(1)
            self._depth = ctypes.c_uint(0)
            glGenRenderbuffers(1, self._depth)
            glBindRenderbuffer(GL_RENDERBUFFER, self._depth)

            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
                                  self._width, self._height)
            glBindRenderbuffer(GL_RENDERBUFFER, 0)

            # Attach texture and depth buffer
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                   GL_TEXTURE_2D, self._texture,
                                   ctypes.c_int(0))
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                      GL_RENDERBUFFER, self._depth)

            # Unbind frame buffer for now
            glBindFramebuffer(GL_FRAMEBUFFER, 0)

            status = glCheckFramebufferStatus(GL_FRAMEBUFFER)

            # Create shaders
            self._incShader = Shader("IncrementBuffer")
            self._logShader = Shader("LogBuffer")
            self._reduction = ReductionShader(self._texture,
                                              max(self._width, self._height),
                                              ["ReduceMaxMin"])

            self._prepared = True

        # Pass everything through since we want to make a projected density map
        glEnable(GL_BLEND)
        glBlendFunc(GL_ONE, GL_ONE)
Example #30
0
 def __init__(self, lightDirection):
     rocher_array, normales = essaiTriangleTriforce()
     self.vertex_array = VertexArray(attributes=[rocher_array, normales])
     self.shader = Shader(TRI_VERT, TRI_FRAG)
     self.lightDirection = lightDirection
Example #31
0
class Renderer:
    def __init__(self):
        self.context = Context.getInstance()  # NOTE must be initialized
        self.logger = logging.getLogger(__name__)

        self.windowWidth = 640
        self.windowHeight = 480

        self.initialize()

        self.colorShader = Shader(self.context.getResourcePath('shaders', 'ADS.vert'), self.context.getResourcePath('shaders', 'ADS.frag'))

        self.proj = hm.perspective(hm.identity(), 35, float(self.windowWidth) / self.windowHeight, 1.0, 1000.0)
        self.view = hm.lookat(hm.identity(), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32), np.array([0.0, 0.0, 1.0, 1.0], dtype = np.float32), np.array([0.0, -1.0, 0.0, 1.0], dtype = np.float32))
        self.cameraMatrix = np.dot(self.proj, self.view)
        self.setCameraMatrix(self.cameraMatrix)

    def setCameraMatrix(self, cameraMatrix):
        self.colorShader.setUniformMat4('view', self.view)
        self.colorShader.setUniformMat4('proj', self.proj)

    def setModelMatrix(self, model):
        self.colorShader.setUniformMat4('model', model)

    def setLightPos(self, lightPos):
        self.colorShader.setUniformVec4('lightPosition', lightPos)

    def setDiffCol(self, diffCol):
        self.colorShader.setUniformVec3('diffuseColor', diffCol)

    def windowOpen(self):
        return glfw.GetWindowParam(glfw.OPENED)

    def startDraw(self):        
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    def endDraw(self):
        glfw.SwapBuffers()

    def initialize(self):
        glfw.Init()
        glfw.OpenWindowHint(glfw.FSAA_SAMPLES, 4)
        glfw.OpenWindowHint(glfw.OPENGL_VERSION_MAJOR, 3)
        glfw.OpenWindowHint(glfw.OPENGL_VERSION_MINOR, 2)  # 3.2
        glfw.OpenWindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)  # 3.2
        glfw.OpenWindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)  # 3.2
        glfw.OpenWindowHint(glfw.WINDOW_NO_RESIZE, GL_TRUE)
        
        try:  # in case we fail to init this version
            glfw.OpenWindow(self.windowWidth, self.windowHeight, 0, 0, 0, 0, 24, 8, glfw.WINDOW)
        except Exception as e:
            self.logger.warn("Failed to initialize OpenGL: {}".format(str(e)))
            self.logger.warn("Trying lower version...")
            glfw.OpenWindowHint(glfw.OPENGL_VERSION_MINOR, 0)  # 3.0
            glfw.OpenWindowHint(glfw.OPENGL_PROFILE, 0)  # 3.0
            glfw.OpenWindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_FALSE)  # 3.0
            glfw.OpenWindow(self.windowWidth, self.windowHeight, 0, 0, 0, 0, 24, 8, glfw.WINDOW)
            self.logger.warn("OpenGL fallback to lower version worked")
        
        glfw.SetWindowTitle("TANG")
        glfw.SetWindowCloseCallback(self.onWindowClose)
        
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        
        # Find out OpenGL version, and store for future use
        self.context.GL_version_string = glGetString(GL_VERSION).split(' ')[0]  # must have version at the beginning, e.g. "3.0 Mesa 9.x.x" to extract "3.0"
        self.context.GL_version_major, self.context.GL_version_minor = list(int(x) for x in self.context.GL_version_string.split('.'))[0:2]  # must have only MAJOR.MINOR
        self.logger.info("OpenGL version {}.{}".format(self.context.GL_version_major, self.context.GL_version_minor))
        self.context.GLSL_version_string = glGetString(GL_SHADING_LANGUAGE_VERSION).split(' ')[0].replace('.', '')  # "1.50" => "150"
        self.logger.info("GLSL version {}".format(self.context.GLSL_version_string))

    def quit(self):
        glfw.CloseWindow()  # NOTE does not generate onWindowClose callback
        #glfw.Terminate()

    def onWindowClose(self):
        #self.logger.debug("Renderer.onWindowClose()")
        return True  # nothing more needs to be done; but an event could be generated

if __name__ == "__main__":
    import sys

    sys.path.append("../")
    # import models

    from PIL import Image, ImageOps
    import imageio
    import torchvision.transforms
    import os

    decomposer = Decomposer()
    decomposer.load_state_dict(torch.load("saved/decomposer/state.t7"))
    shader = Shader()
    shader.load_state_dict(torch.load("saved/shader/state.pth"))
    composer_path = "/home/ab2383/intrinsics-network/saved/composer/state.t7"
    composer = Composer(decomposer, shader)
    composer = composer.cuda()
    composer.load_state_dict(torch.load(composer_path))
    print("Composer Built")

    # folders = ["00277", "00339", "00267"]
    # base_path = "/phoenix/S3/ab2383/data/TikTok_dataset/"
    save_path = "/home/ab2383/intrinsics-network/"

    # for folder in folders:
    #     imgs_path = base_path + folder + "/images/"
    #     masks_path = base_path + folder + "/masks/"
    #     for img in os.listdir(imgs_path):