Beispiel #1
0
def opengl清楚绘图循环(所有图层, psd尺寸):
    def 生成纹理(img):
        w, h = img.shape[:2]
        d = 2**int(max(math.log2(w), math.log2(h)) + 1)
        纹理 = np.zeros([d, d, 4], dtype=img.dtype)
        纹理[:w, :h] = img
        return 纹理, (w / d, h / d)

    Vtuber尺寸 = 512, 512

    glfw.init()
    glfw.window_hint(glfw.RESIZABLE, False)
    window = glfw.create_window(*Vtuber尺寸, 'Vtuber', None, None)
    glfw.make_context_current(window)
    glViewport(0, 0, *Vtuber尺寸)

    glEnable(GL_TEXTURE_2D)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    for 图层数据 in 所有图层:
        纹理编号 = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, 纹理编号)
        纹理, 纹理座标 = 生成纹理(图层数据['npdata'])
        width, height = 纹理.shape[:2]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,
                     GL_FLOAT, 纹理)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                        GL_LINEAR_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)
        图层数据['纹理编号'] = 纹理编号
        图层数据['纹理座标'] = 纹理座标

    while not glfw.window_should_close(window):
        glfw.poll_events()
        glClearColor(1, 1, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT)
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        for 图层数据 in 所有图层:
            a, b, c, d = 图层数据['位置']
            q, w = 图层数据['纹理座标']
            p1 = np.array([a, b, 0, 1, 0, 0])
            p2 = np.array([a, d, 0, 1, w, 0])
            p3 = np.array([c, d, 0, 1, w, q])
            p4 = np.array([c, b, 0, 1, 0, q])
            model = matrix.scale(2 / psd尺寸[0], 2 / psd尺寸[1], 1) @ \
                matrix.translate(-1, -1, 0) @ \
                matrix.rotate_ax(-math.pi / 2, axis=(0, 1))
            glBindTexture(GL_TEXTURE_2D, 图层数据['纹理编号'])
            glColor4f(1, 1, 1, 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBegin(GL_QUADS)
            for p in [p1, p2, p3, p4]:
                a = p[:4]
                b = p[4:6]
                a = a @ model
                glTexCoord2f(*b)
                glVertex4f(*a)
            glEnd()
        glfw.swap_buffers(window)
Beispiel #2
0
def mapImageGL(srcImg, mesh, leftTop, rightBottom):
    log.debug("mapImageGL: 1")

    dstImg = gui3d.app.selectedHuman.meshData.object3d.textureTex

    dstW = dstImg.width
    dstH = dstImg.height

    left, top = leftTop
    right, bottom = rightBottom

    camera = getCamera(mesh)

    coords = mesh.r_texco

    texmat = gui3d.app.modelCamera.getConvertToScreenMatrix(mesh)
    texmat = matrix.scale((1/(right - left), 1/(top - bottom), 1)) * matrix.translate((-left, -bottom, 0)) * texmat
    texmat = np.asarray(texmat)

    texco = mesh.r_coord

    alpha = np.sum(mesh.r_vnorm * camera[None,:], axis=-1)
    alpha = np.maximum(alpha, 0)
    color = (np.array([0, 0, 0, 0])[None,...] + alpha[...,None]) * 255

    color = np.ascontiguousarray(color, dtype=np.uint8)
    texco = np.ascontiguousarray(texco, dtype=np.float32)

    result = mh.renderSkin(dstImg, mesh.vertsPerPrimitive, coords, index = mesh.index,
                           texture = srcImg, UVs = texco, textureMatrix = texmat,
                           color = color, clearColor = None)

    return result
Beispiel #3
0
 def transform(self):
     m = matrix.translate(self.loc)
     if any(x != 0 for x in self.rot):
         m = m * matrix.rotx(self.rx)
         m = m * matrix.roty(self.ry)
         m = m * matrix.rotz(self.rz)
     if any(x != 1 for x in self.scale):
         m = m * matrix.scale(self.scale)
     return m
Beispiel #4
0
 def transform(self):
     m = matrix.translate(self.loc)
     if any(x != 0 for x in self.rot):
         m = m * matrix.rotx(self.rx)
         m = m * matrix.roty(self.ry)
         m = m * matrix.rotz(self.rz)
     if any(x != 1 for x in self.scale):
         m = m * matrix.scale(self.scale)
     return m
Beispiel #5
0
def opengl绘图循环(所有图层):
    glfw.init()
    glfw.window_hint(glfw.RESIZABLE, False)
    window = glfw.create_window(500, 500, 'Vtuber', None, None)
    glfw.make_context_current(window)
    glViewport(0, 0, 500, 500)

    glEnable(GL_TEXTURE_2D)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    for 图层数据 in 所有图层:
        纹理编号 = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, 纹理编号)
        纹理 = cv2.resize(图层数据['npdata'], (512, 512))
        width, height = 纹理.shape[:2]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,
                     GL_FLOAT, 纹理)
        glGenerateMipmap(GL_TEXTURE_2D)
        图层数据['纹理编号'] = 纹理编号

    while not glfw.window_should_close(window):
        glfw.poll_events()
        glClearColor(1, 1, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT)
        for 图层数据 in 所有图层:
            a, b, c, d = 图层数据['位置']
            z = 图层数据['深度']
            p1 = np.array([a, b, z, 1, 0, 0])
            p2 = np.array([a, d, z, 1, 1, 0])
            p3 = np.array([c, d, z, 1, 1, 1])
            p4 = np.array([c, b, z, 1, 0, 1])
            model = matrix.scale(1 / 250, 1 / 250, 1) @ \
                    matrix.translate(-1, -1, 0) @ \
                    matrix.rotate_ax(-math.pi / 2, axis=(0, 1))
            glBindTexture(GL_TEXTURE_2D, 图层数据['纹理编号'])
            glColor4f(1, 1, 1, 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBegin(GL_QUADS)
            for p in [p1, p2, p3, p4]:
                a = p[:4]
                b = p[4:6]
                a = a @ model
                a[0] *= a[2]
                a[1] *= a[2]
                if not 图层数据['名字'][:2] == '身体':
                    横角, 竖角 = 特征缓冲()
                    a = a @ \
                        matrix.translate(0, 0, -1) @ \
                        matrix.rotate_ax(横角, axis=(0, 2)) @ \
                        matrix.rotate_ax(竖角, axis=(2, 1)) @ \
                        matrix.translate(0, 0, 1)
                a = a @ matrix.perspective(999)
                glTexCoord2f(*b)
                glVertex4f(*a)
            glEnd()
        glfw.swap_buffers(window)
Beispiel #6
0
def mapImageGL(srcImg, mesh, leftTop, rightBottom):
    progress = Progress()(0)
    log.debug("mapImageGL: 1")

    dstImg = G.app.selectedHuman.meshData.object3d.textureTex

    dstW = dstImg.width
    dstH = dstImg.height

    left, top = leftTop
    right, bottom = rightBottom

    camera = getCamera(mesh)

    coords = mesh.r_texco

    texmat = G.app.modelCamera.getConvertToScreenMatrix(mesh)
    texmat = matrix.scale(
        (1 / (right - left), 1 / (top - bottom), 1)) * matrix.translate(
            (-left, -bottom, 0)) * texmat
    texmat = np.asarray(texmat)

    texco = mesh.r_coord

    alpha = np.sum(mesh.r_vnorm * camera[None, :], axis=-1)
    alpha = np.maximum(alpha, 0)
    color = (np.array([0, 0, 0, 0])[None, ...] + alpha[..., None]) * 255

    color = np.ascontiguousarray(color, dtype=np.uint8)
    texco = np.ascontiguousarray(texco, dtype=np.float32)

    progress(0.5, 0.99)
    result = mh.renderSkin(dstImg,
                           mesh.vertsPerPrimitive,
                           coords,
                           index=mesh.index,
                           texture=srcImg,
                           UVs=texco,
                           textureMatrix=texmat,
                           color=color,
                           clearColor=None)

    progress(1)
    return result
Beispiel #7
0
 def getFlipMatrix():
     t = matrix.translate((0, G.windowHeight, 0))
     s = matrix.scale((1, -1, 1))
     return t * s
Beispiel #8
0
 def getFlipMatrix():
     t = matrix.translate((0, G.windowHeight, 0))
     s = matrix.scale((1, -1, 1))
     return t * s
Beispiel #9
0
def runCommands(commands, knobs):
    """
    Runs the given commands and returns the resulting screen
    """
    stack = [matrix.ident()]
    view = screen.Screen()
    for command in commands:
        if command[0] == "ignore":
            pass
        if command[0] == "pop":
            stack.pop()
            if not stack:
                stack = [matrix.ident()]
        if command[0] == "push":
            stack.append(stack[-1].clone())
        if command[0] == "screen":
            view = screen.Screen(command[1], command[2])
        if command[0] == "save":
            view.save(command[1])
        if command[0] == "display":
            if len(command) == 2:
                screen.display(command[1])
            else:
                screen.display(view)
        if command[0] == "set":
            knobs[command[1]] = float(command[2])
        if command[0] == "set_knobs":
            for name in knobs.keys():
                knobs[name] = float(command[1])
        if command[0] == "sphere":
            m = matrix.FaceMatrix()
            m.add_sphere(*command[1:])
            m.apply(stack[-1])
            view.draw_FaceMatrix(m, [255, 255, 255])
        if command[0] == "torus":
            m = matrix.FaceMatrix()
            m.add_torus(*command[1:])
            m.apply(stack[-1])
            view.draw_FaceMatrix(m, [255, 255, 255])
        if command[0] == "box":
            m = matrix.FaceMatrix()
            m.add_box(*command[1:])
            m.apply(stack[-1])
            view.draw_FaceMatrix(m, [255, 255, 255])
        if command[0] == "line":
            m = matrix.EdgeMatrix()
            m.add_edge(*command[1:])
            m.apply(stack[-1])
            view.draw_EdgeMatrix(m, [255, 255, 255])
        if command[0] == "bezier":
            m = matrix.EdgeMatrix()
            m.add_bezier_curve(*command[1:])
            m.apply(stack[-1])
            view.draw_EdgeMatrix(m, [255, 255, 255])
        if command[0] == "hermite":
            m = matrix.EdgeMatrix()
            m.add_hermite_curve(*command[1:])
            m.apply(stack[-1])
            view.draw_EdgeMatrix(m, [255, 255, 255])
        if command[0] == "circle":
            m = matrix.EdgeMatrix()
            m.add_circle(command[1], command[2], command[3], command[4])
            stack.append(stack[-1].clone())
            #
            #
            #
            #
            #
            m.apply(stack[-1])
            stack.pop()
            view.draw_EdgeMatrix(m, [255, 255, 255])
        if command[0] == "move":
            if command[4]:
                val = float(knobs[command[4]])
            else:
                val = 1.0
            stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val)
        if command[0] == "scale":
            if command[4]:
                val = float(knobs[command[4]])
            else:
                val = 1.0
            stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val)
        if command[0] == "rotate":
            if command[3]:
                val = float(knobs[command[3]])
            else:
                val = 1.0
            stack[-1] *= matrix.rotate(command[1], command[2] * val)
    return view
Beispiel #10
0
def opengl循环(所有图层, psd尺寸):
    Vtuber尺寸 = 512, 512
    
    glfw.init()
    glfw.window_hint(glfw.RESIZABLE, False)
    window = glfw.create_window(*Vtuber尺寸, 'Vtuber', None, None)
    glfw.make_context_current(window)
    glViewport(0, 0, *Vtuber尺寸)

    glEnable(GL_TEXTURE_2D)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    for 图层数据 in 所有图层:
        纹理编号 = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, 纹理编号)
        纹理, 纹理座标 = 生成纹理(图层数据['npdata'])
        width, height = 纹理.shape[:2]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_FLOAT, 纹理)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)
        图层数据['纹理编号'] = 纹理编号
        图层数据['纹理座标'] = 纹理座标

    while not glfw.window_should_close(window):
        glfw.poll_events()
        glClearColor(1, 1, 1, 0)
        glClear(GL_COLOR_BUFFER_BIT)
        for 图层数据 in 所有图层:
            a, b, c, d = 图层数据['位置']
            z = 图层数据['深度']
            if type(z) in [int, float]:
                z1, z2, z3, z4 = [z, z, z, z]
            else:
                [z1, z2], [z3, z4] = z
            q, w = 图层数据['纹理座标']
            p1 = np.array([a, b, z1, 1, 0, 0, 0, 1])
            p2 = np.array([a, d, z2, 1, w, 0, 0, 1])
            p3 = np.array([c, d, z3, 1, w, q, 0, 1])
            p4 = np.array([c, b, z4, 1, 0, q, 0, 1])

            model = matrix.scale(2 / psd尺寸[0], 2 / psd尺寸[1], 1) @ \
                matrix.translate(-1, -1, 0) @ \
                matrix.rotate_ax(-math.pi / 2, axis=(0, 1))
            glBindTexture(GL_TEXTURE_2D, 图层数据['纹理编号'])
            glColor4f(1, 1, 1, 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBegin(GL_QUADS)
            for p in [p1, p2, p3, p4]:
                a = p[:4]
                b = p[4:]
                a = a @ model
                z = a[2]
                a[0:2] *= z
                b *= z
                横旋转量 = 0
                if not 图层数据['名字'] == '身体':
                    横旋转量 = math.sin(time.time() * 5) / 30
                a = a @ matrix.translate(0, 0, -1) \
                      @ matrix.rotate_ax(横旋转量, axis=(0, 2)) \
                      @ matrix.translate(0, 0, 1)
                a = a @ matrix.perspective(999)
                glTexCoord4f(*b)
                glVertex4f(*a)
            glEnd()
        glfw.swap_buffers(window)
Beispiel #11
0
def opengl绘图循环(所有图层, psd尺寸):
    def 生成纹理(img):
        w, h = img.shape[:2]
        d = 2**int(max(math.log2(w), math.log2(h)) + 1)
        纹理 = np.zeros([d, d, 4], dtype=img.dtype)
        纹理[:w, :h] = img
        return 纹理, (w / d, h / d)

    Vtuber尺寸 = 512, 512

    glfw.init()
    超融合()
    glfw.window_hint(glfw.RESIZABLE, False)
    window = glfw.create_window(*Vtuber尺寸, 'Vtuber', None, None)
    glfw.make_context_current(window)
    monitor_size = glfw.get_video_mode(glfw.get_primary_monitor()).size
    glfw.set_window_pos(window, monitor_size.width - Vtuber尺寸[0],
                        monitor_size.height - Vtuber尺寸[1])

    glViewport(0, 0, *Vtuber尺寸)

    glEnable(GL_TEXTURE_2D)
    glEnable(GL_BLEND)
    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE,
                        GL_ONE_MINUS_SRC_ALPHA)

    for 图层数据 in 所有图层:
        纹理编号 = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, 纹理编号)
        纹理, 纹理座标 = 生成纹理(图层数据['npdata'])
        width, height = 纹理.shape[:2]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,
                     GL_FLOAT, 纹理)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                        GL_LINEAR_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)
        图层数据['纹理编号'] = 纹理编号
        图层数据['纹理座标'] = 纹理座标

    while not glfw.window_should_close(window):
        glfw.poll_events()
        glClearColor(0, 0, 0, 0)
        glClear(GL_COLOR_BUFFER_BIT)
        横旋转量, 竖旋转量 = 特征缓冲()
        for 图层数据 in 所有图层:
            a, b, c, d = 图层数据['位置']
            z = 图层数据['深度']
            if type(z) in [int, float]:
                z1, z2, z3, z4 = [z, z, z, z]
            else:
                [z1, z2], [z3, z4] = z
            q, w = 图层数据['纹理座标']
            p1 = np.array([a, b, z1, 1, 0, 0, 0, z1])
            p2 = np.array([a, d, z2, 1, z2 * w, 0, 0, z2])
            p3 = np.array([c, d, z3, 1, z3 * w, z3 * q, 0, z3])
            p4 = np.array([c, b, z4, 1, 0, z4 * q, 0, z4])

            model = matrix.scale(2 / psd尺寸[0], 2 / psd尺寸[1], 1) @ \
                matrix.translate(-1, -1, 0) @ \
                matrix.rotate_ax(-math.pi / 2, axis=(0, 1))
            glBindTexture(GL_TEXTURE_2D, 图层数据['纹理编号'])
            glColor4f(1, 1, 1, 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBegin(GL_QUADS)
            for p in [p1, p2, p3, p4]:
                a = p[:4]
                b = p[4:8]
                a = a @ model
                a[0:2] *= a[2]
                if not 图层数据['名字'][:2] == '身体':
                    a = a @ matrix.translate(0, 0, -1) \
                          @ matrix.rotate_ax(横旋转量, axis=(0, 2)) \
                          @ matrix.rotate_ax(竖旋转量, axis=(2, 1)) \
                          @ matrix.translate(0, 0, 1)
                a = a @ matrix.perspective(999)
                glTexCoord4f(*b)
                glVertex4f(*a)
            glEnd()
        glfw.swap_buffers(window)
Beispiel #12
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    knobs = {}
    for s in symbols:
        if s[0] == "knob":
            knobs[s[1]] = 0.0
    knobs = getKnobValues(knobs)
    while 1:
        stack = [matrix.ident()]
        view = screen.Screen()
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [matrix.ident()]
            if command[0] == "push":
                stack.append(stack[-1].clone())
            if command[0] == "screen":
                view = screen.Screen(command[1], command[2])
            if command[0] == "save":
                view.save(command[1])
            if command[0] == "display":
                if len(command) == 2:
                    screen.display(command[1])
                else:
                    screen.display(view)
            if command[0] == "set":
                knobs[command[1]] = float(command[2])
            if command[0] == "set_knobs":
                for name in knobs.keys():
                    knobs[name] = float(command[1])
            if command[0] == "sphere":
                m = matrix.FaceMatrix()
                m.add_sphere(*command[1:])
                m.apply(stack[-1])
                view.draw_FaceMatrix(m, [255, 255, 255])
            if command[0] == "torus":
                m = matrix.FaceMatrix()
                m.add_torus(*command[1:])
                m.apply(stack[-1])
                view.draw_FaceMatrix(m, [255, 255, 255])
            if command[0] == "box":
                m = matrix.FaceMatrix()
                m.add_box(*command[1:])
                m.apply(stack[-1])
                view.draw_FaceMatrix(m, [255, 255, 255])
            if command[0] == "line":
                m = matrix.EdgeMatrix()
                m.add_edge(*command[1:])
                m.apply(stack[-1])
                view.draw_EdgeMatrix(m, [255, 255, 255])
            if command[0] == "bezier":
                m = matrix.EdgeMatrix()
                m.add_bezier_curve(*command[1:])
                m.apply(stack[-1])
                view.draw_EdgeMatrix(m, [255, 255, 255])
            if command[0] == "hermite":
                m = matrix.EdgeMatrix()
                m.add_hermite_curve(*command[1:])
                m.apply(stack[-1])
                view.draw_EdgeMatrix(m, [255, 255, 255])
            if command[0] == "circle":
                m = matrix.EdgeMatrix()
                m.add_circle(command[1], command[2], command[3], command[4])
                stack.append(stack[-1].clone())
                #
                #
                #
                #
                #
                m.apply(stack[-1])
                stack.pop()
                view.draw_EdgeMatrix(m, [255, 255, 255])
            if command[0] == "move":
                if command[4]:
                    val = float(knobs[command[4]])
                else:
                    val = 1.0
                stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val)
            if command[0] == "scale":
                if command[4]:
                    val = float(knobs[command[4]])
                else:
                    val = 1.0
                stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val)
            if command[0] == "rotate":
                if command[3]:
                    val = float(knobs[command[3]])
                else:
                    val = 1.0
                stack[-1] *= matrix.rotate(command[1], command[2] * val)
        while 1:
            text = raw_input("Continue?\n> ")
            if not text in ["yes", "no", "n", "y"]:
                print "I don't understand."
            elif text in ["yes", "y"]:
                print
                break
            else:
                return
        knobs = getKnobValues(knobs)
Beispiel #13
0
def runCommands(commands, knobs, constants, coord_systems, meshesE, meshesF, base_matrix, focalLength):
    """
    Runs the given commands and returns the resulting screen
    """
    stack = [base_matrix.clone()]
    view = screen.Screen()
    constants[None] = [255, 255, 255, 200, 200, 200, 100, 100, 100, 0, 0, 0]
    lights = []
    ambient = [0, 0, 0]
    shading_type = "wireframe"
    for command in commands:
        if command[0] == "ignore":
            pass
        elif command[0] == "pop":
            stack.pop()
            if not stack:
                stack = [base_matrix.clone()]
        elif command[0] == "push":
            stack.append(stack[-1].clone())
        elif command[0] == "screen":
            view = screen.Screen(command[1], command[2])
            stack = [base_matrix.clone()]
        elif command[0] == "save":
            view.save(command[1])
        elif command[0] == "display":
            if len(command) == 2:
                screen.display(command[1])
            else:
                screen.display(view)
        elif command[0] == "camera":
            base_matrix = matrix.ident()
            base_matrix *= matrix.move(0 - command[1], 0 - command[2], 0 - command[3])
            xaim = command[4] - command[1]
            yaim = command[5] - command[2]
            zaim = command[6] - command[3]
            if xaim == 0 and zaim == 0:
                if yaim >= 0:
                    base_matrix *= matrix.rotate("x", -90)
                else:
                    base_matrix *= matrix.rotate("x", 90)
            else:
                theta = (math.atan2(yaim, math.sqrt(math.pow(xaim, 2) + math.pow(zaim, 2)))) * -180.0 / 3.14159265358979323
                base_matrix *= matrix.rotate("x", theta)
                theta = ((math.atan2(zaim, xaim) - math.atan2(1, 0)) * 180.0 / 3.14159265358979323)
                base_matrix *= matrix.rotate("y", theta)
            stack = [base_matrix.clone()]
        elif command[0] == "focal":
            focalLength = command[1]
            view.focalLength = focalLength
        elif command[0] == "set":
            knobs[command[1]] = float(command[2])
        elif command[0] == "set_knobs":
            for name in knobs.keys():
                knobs[name] = float(command[1])
        elif command[0] == "ambient":
            ambient = command[1:]
        elif command[0] == "light":
            lights.append(command[1:])
        elif command[0] == "sphere":
            m = matrix.FaceMatrix()
            m.add_sphere(*[command[i] for i in range(2, 8) if i != 5])
            if command[5]:
                m.apply(coord_systems[command[5]])
            else:
                m.apply(stack[-1])
            view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "torus":
            m = matrix.FaceMatrix()
            m.add_torus(*[command[i] for i in range(2, 9) if i != 5])
            if command[5]:
                m.apply(coord_systems[command[5]])
            else:
                m.apply(stack[-1])
            view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "box":
            m = matrix.FaceMatrix()
            m.add_box(*(command[2:8]))
            if command[8]:
                m.apply(coord_systems[command[8]])
            else:
                m.apply(stack[-1])
            view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "line":
            m = matrix.PointMatrix()
            n = matrix.PointMatrix()
            m.add_point(*command[2:5])
            n.add_point(*command[6:9])
            if command[5]:
                m.apply(coord_systems[command[5]])
            else:
                m.apply(stack[-1])
            if command[9]:
                n.apply(coord_systems[command[9]])
            else:
                n.apply(stack[-1])
            [x0, y0, z0] = [m.get(i, 0) for i in range(3)]
            [x1, y1, z1] = [n.get(i, 0) for i in range(3)]
            m = matrix.EdgeMatrix()
            m.add_edge(x0, y0, z0, x1, y1, z1)
            view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "bezier":
            xs = []
            ys = []
            zs = []
            for i in range(4):
                m = matrix.PointMatrix()
                m.add_point(*command[2 + 4 * i:5 + 4 * i])
                if command[5 + 4 * i]:
                    m.apply(coord_systems[command[5 + 4 * i]])
                else:
                    m.apply(stack[-1])
                xs.append(m.get(0, 0))
                ys.append(m.get(1, 0))
                zs.append(m.get(2, 0))
            m = matrix.EdgeMatrix()
            m.add_bezier_curve(xs[0], ys[0], zs[0], xs[1], ys[1], zs[1], xs[2], ys[2], zs[2], xs[3], ys[3], zs[3], command[18])
            view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "hermite":
            xs = []
            ys = []
            zs = []
            for i in range(4):
                m = matrix.PointMatrix()
                m.add_point(*command[2 + 4 * i:5 + 4 * i])
                if command[5 + 4 * i]:
                    m.apply(coord_systems[command[5 + 4 * i]])
                else:
                    m.apply(stack[-1])
                xs.append(m.get(0, 0))
                ys.append(m.get(1, 0))
                zs.append(m.get(2, 0))
            m = matrix.EdgeMatrix()
            m.add_hermite_curve(xs[0], ys[0], zs[0], xs[1], ys[1], zs[1], xs[2], ys[2], zs[2], xs[3], ys[3], zs[3], command[18])
            view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "mesh":
            if meshesE.has_key(command[2]):
                m = meshesE[command[2]].clone()
                if command[3]:
                    m.apply(coord_systems[command[3]])
                else:
                    m.apply(stack[-1])
                view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights])
            elif meshesF.has_key(command[2]):
                m = meshesF[command[2]].clone()
                if command[3]:
                    m.apply(coord_systems[command[3]])
                else:
                    m.apply(stack[-1])
                view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights])
        elif command[0] == "save_coord_system":
            coord_systems[command[1]] = stack[-1].clone()
        elif command[0] == "constants":
            constants[command[1]] = command[2:]
        elif command[0] == "shading":
            shading_type = command[1]
        elif command[0] == "move":
            if command[4]:
                val = float(knobs[command[4]])
            else:
                val = 1.0
            stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val)
        elif command[0] == "scale":
            if command[4]:
                val = float(knobs[command[4]])
            else:
                val = 1.0
            stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val)
        elif command[0] == "scaleXYZ":
            if command[3]:
                val = float(knobs[command[3]])
            else:
                val = 1.0
            if command[1] == "x":
                stack[-1] *= matrix.scale(command[2] * val, 1, 1)
            elif command[1] == "y":
                stack[-1] *= matrix.scale(1, command[2] * val, 1)
            else:
                stack[-1] *= matrix.scale(command[2] * val, 1, 1)
        elif command[0] == "rotate":
            if command[3]:
                val = float(knobs[command[3]])
            else:
                val = 1.0
            stack[-1] *= matrix.rotate(command[1], command[2] * val)
    return view
Beispiel #14
0
def opengl绘图循环(所有图层, psd尺寸):
    Vtuber尺寸 = 500, 500

    glfw.init()
    glfw.window_hint(glfw.RESIZABLE, False)
    glfw.window_hint(glfw.DECORATED, False)
    glfw.window_hint(glfw.TRANSPARENT_FRAMEBUFFER, True)
    glfw.window_hint(glfw.FLOATING, True)
    window = glfw.create_window(*Vtuber尺寸, 'Vtuber', None, None)
    glfw.make_context_current(window)
    monitor_size = glfw.get_video_mode(glfw.get_primary_monitor()).size
    glfw.set_window_pos(window, monitor_size.width - Vtuber尺寸[0],
                        monitor_size.height - Vtuber尺寸[1])

    glViewport(0, 0, *Vtuber尺寸)

    glEnable(GL_TEXTURE_2D)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    for 图层数据 in 所有图层:
        纹理编号 = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, 纹理编号)
        纹理, 纹理座标 = 生成纹理(图层数据['npdata'])
        width, height = 纹理.shape[:2]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,
                     GL_FLOAT, 纹理)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                        GL_LINEAR_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)
        图层数据['纹理编号'] = 纹理编号
        图层数据['纹理座标'] = 纹理座标

    while not glfw.window_should_close(window):
        glfw.poll_events()
        glClearColor(0, 0, 0, 0)
        glClear(GL_COLOR_BUFFER_BIT)
        横旋转量, 竖旋转量 = 特征缓冲()
        for 图层数据 in 所有图层:
            a, b, c, d = 图层数据['位置']
            z1, z2, z3, z4 = 图层数据['深度']
            q, w = 图层数据['纹理座标']
            p1 = np.array([a, b, z1, 1, 0, 0, 0, z1])
            p2 = np.array([a, d, z2, 1, z2 * w, 0, 0, z2])
            p3 = np.array([c, d, z3, 1, z3 * w, z3 * q, 0, z3])
            p4 = np.array([c, b, z4, 1, 0, z4 * q, 0, z4])
            model = matrix.scale(2 / psd尺寸[0], 2 / psd尺寸[1], 1) @ \
                matrix.translate(-1, -1, 0) @ \
                matrix.rotate_ax(-math.pi / 2, axis=(0, 1))
            glBindTexture(GL_TEXTURE_2D, 图层数据['纹理编号'])
            glColor4f(1, 1, 1, 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBegin(GL_QUADS)
            t = []
            for p in [p1, p2, p3, p4]:
                a = p[:4]
                b = p[4:8]
                a = a @ model
                a[0] *= a[2]
                a[1] *= a[2]
                if not 图层数据['名字'][:2] == '身体':
                    a = a @ \
                        matrix.translate(0, 0, -1) @ \
                        matrix.rotate_ax(横旋转量, axis=(0, 2)) @ \
                        matrix.rotate_ax(竖旋转量, axis=(2, 1)) @ \
                        matrix.translate(0, 0, 1)
                a = a @ matrix.perspective(999)
                glTexCoord4f(*b)
                glVertex4f(*a)
            glEnd()
        glfw.swap_buffers(window)