如何从同一列表中渲染多个多边形

时间:2018-07-18 00:05:30

标签: python list opengl letters

所以基本上我想做的是用OpenGL添加一个letter包,因为它上没有真正的选择。 (glBitmap只是在屏幕上显示了一些奇怪的东西)

alphabet = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]#refer to the drawings of the shape I made, in list
alphastring = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] #refer to the letter in string, it makes it easier to compare
lettreLiens = [liena, lienb, lienc, liend, liene, lienf, lieng, lienh, lieni, lienj, lienk, lienl,lienm,lienn,lieno,lienp,lienq,lienr,lien_s,lient,lienu,lienv,lienw,lienx,lieny,lienz] #the links for the points I've drawn, note that lien_s is correct

class text:
def __init__(self,texte,bigness,x,y):
    self.texte = texte
    self.bigness = bigness
    self.x = x
    self.y = y
    self.textPos = 1

    self.textList = self.texte.split(',') 
    print(len(self.textList)) 
    for self.l in range(0,(len(self.textList))):
        for self.n in range(0,25):
            if self.textList[self.l] == alphastring[self.n]:
                for self.point in alphabet[self.n]: 
                    self.point[0]+=int(self.x)
                    self.point[1]+=int(self.y)
                    self.point[0] += int(self.textPos)
                self.n = 0
                self.textPos+=1


def run(self):
    for self.l in range(0,(len(self.textList))):
        for self.n in range(0,25):
            if self.textList[self.l] == alphastring[self.n]:              
                glBegin(GL_LINES)
                for link in (lettreLiens[self.n]):
                    for vertex in link:
                        glVertex3fv(alphabet[self.n][vertex])
                glEnd()
                self.n = 0

问题是OpenGL仅为每个字母渲染一个形状。如果我为hello单词创建一个实例,它将在屏幕上显示他。我也尝试为hel创建一个实例,然后lo并且这也不起作用。

1 个答案:

答案 0 :(得分:1)

主要问题是为每个字母存储一个点。但是,您不必为要呈现的文本的每个字母存储一个点,而是为每个要使用的字母存储一个点。 这会导致如果两次使用一个字母,则仅存储该字母最后一次出现的位置。

我建议省略字母位置的计算,并即时显示文本:

class text:
    def __init__(self,texte,bigness,x,y):
        self.texte = texte
        self.bigness = bigness
        self.x = x
        self.y = y
        self.textList = self.texte.split(',') 

    def run(self):
        textPos = 0
        for letter in self.textList:
            try:
                i = alphastring.index(letter)
            except:
                continue
            else:
                glBegin(GL_LINES)
                for link in lettreLiens[i]:
                    for vertex in link:
                        x = alphabet[i][vertex][0] + self.x + textPos
                        y = alphabet[i][vertex][1] + self.y
                        z = alphabet[i][vertex][2]
                        glVertex3fv([x, y, z])
                glEnd()
                textPos += 1

查看预览:

def main():
    pygame.init()
    display = (800,800)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(90,(display[0]/display[1]),0.1,50)
    glTranslatef(-1,0.0,-5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == K_r:
                    glRotatef(10,0,1,0)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        textss.run()        
        pygame.display.flip()
        pygame.time.wait(10)

textss = text('h,a,l,l,o',5,-1,0)
main()

preview