Search code examples
pythonlistopenglletters

How to render more than one polygon from the same list


So basically what I am trying to do is to add a letter package with OpenGL, since there is no real alternative on it. (glBitmap just shows some weirds stuff on my screen)

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

The problem is that OpenGL only render one shape for each letter. If I create an instance for the word hello it will just show he lo on the screen. I also tried to create an instance for hel, and then lo and this do not work either.


Solution

  • The major issue is that you store a point for each letter. But you don't store a point for each letter of the text which you want to render, instead you store a one point for each letter which is used. This causes, that if a letter is used twice, only the position of the last occurrence of the letter is stored.

    I recommend to omit the calculation of the letter positions and to render the text on the fly:

    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
    

    See the preview:

    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