PyOpenGL下GlutBitmapCharacter的替代

虽然pyinstaller支持pyopengl,但是调用GLUT之后,在其它电脑上面运行就会出现错误,索性按照之前C#上面的办法,把字体数据和函数用python重写

fontData.py

#! /usr/bin/env python
#coding=utf-8

'''
Copy from freeglut_font.c and freeglut_font_data.c
 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
 * Creation date: Thu Dec 16 1999
 * Modified by T.Q., <pasuka(at)foxmail(dot)com>, 25-10-2013
'''
from OpenGL import GL

from fontData import fgGetFontById
fontDict = {"GLUT_BITMAP_8_BY_13":1,
            "GLUT_BITMAP_9_BY_15":2,
            "GLUT_BITMAP_HELVETICA_10":3,
            "GLUT_BITMAP_HELVETICA_12":4,
            "GLUT_BITMAP_HELVETICA_18":5,
            "GLUT_BITMAP_TIMES_ROMAN_10":6,
            "GLUT_BITMAP_TIMES_ROMAN_24":7}
            
def BitmapCharacter(slt,cc):
    '''
    Draw a bitmap character
    '''
    if not cc:return -3
    if not 0<=ord(cc)<=255:return -1
    if not fontDict.has_key(slt): return -2
    GL.glPushClientAttrib(GL.GL_CLIENT_PIXEL_STORE_BIT)
    GL.glPixelStorei(GL.GL_UNPACK_SWAP_BYTES,GL.GL_FALSE)
    GL.glPixelStorei(GL.GL_UNPACK_LSB_FIRST,GL.GL_FALSE)
    GL.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH,0)
    GL.glPixelStorei(GL.GL_UNPACK_SKIP_ROWS,0)
    GL.glPixelStorei(GL.GL_UNPACK_SKIP_PIXELS,0)
    GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT,1)
    font = fgGetFontById(fontDict[slt])
    face = font['Characters'][ord(cc)]
    GL.glBitmap(face[0], font['Height'], # The bitmap's width and height
        font['xorig'], font['yorig'], # The origin in the font glyph
        float(face[0]),0.0, #The raster advance -- inc. x,y
        face[1:]) # The packed bitmap data...
    GL.glPopClientAttrib()
    return 0

def BitmapString(slt,s):
    '''
    Draw a bitmap string
    '''
    if not s:return -3
    for k in s:
        if not 0<=ord(k)<=255:
            return -1
    if not fontDict.has_key(slt):return -2
    font = fgGetFontById(fontDict[slt])
    GL.glPushClientAttrib(GL.GL_CLIENT_PIXEL_STORE_BIT)
    GL.glPixelStorei(GL.GL_UNPACK_SWAP_BYTES,GL.GL_FALSE)
    GL.glPixelStorei(GL.GL_UNPACK_LSB_FIRST,GL.GL_FALSE)
    GL.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH,0)
    GL.glPixelStorei(GL.GL_UNPACK_SKIP_ROWS,0)
    GL.glPixelStorei(GL.GL_UNPACK_SKIP_PIXELS,0)
    GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT,1)
    x = 0.0
    for k in s:
        face = font['Characters'][ord(k)]
        if k=='\n':
            '''
            Step through the string, drawing each character.
            A newline will simply translate the next character's insertion
            point back to the start of the line and down one line.
            '''
            
            GL.glBitmap(0,0,0.0,0.0,-x,-float(font['Height']),0)
            x = 0.0
        else:
            GL.glBitmap(face[0],font['Height'],font['xorig'],font['yorig'],float(face[0]),0.0,face[1:])
            x += float(face[0])
    GL.glPopClientAttrib()
    return 0

fontData.py

下略,因为行数实在太多

posted @ 2013-10-25 16:19  pasuka  阅读(837)  评论(0编辑  收藏  举报