Jelajahi Sumber

修复文本颠倒问题,使用 GLUT 渲染文本

zhong (钟鹏群) 1 bulan lalu
induk
melakukan
cf07765bd2
1 mengubah file dengan 15 tambahan dan 53 penghapusan
  1. 15 53
      ball-demo.py

+ 15 - 53
ball-demo.py

@@ -12,12 +12,16 @@ except ImportError:
 from pygame.locals import *
 from OpenGL.GL import *
 from OpenGL.GLU import *
+from OpenGL.GLUT import *
 
 # 初始化字体
 pygame.font.init()
 # 使用默认字体,增大字体大小
 font = pygame.font.Font(None, 24)
 
+# 初始化 GLUT
+glutInit()
+
 # 模拟模式开关(True: 使用模拟数据,False: 扫描真实目录)
 # USE_MOCK_DATA = True
 USE_MOCK_DATA = False
@@ -265,31 +269,6 @@ def draw_text(x, y, z, text):
     glDisable(GL_LIGHTING)
     glColor3f(1.0, 1.0, 1.0)  # 白色文本
     
-    # 尝试使用系统字体
-    try:
-        # 尝试使用 Arial 字体
-        font = pygame.font.SysFont("Arial", 24)
-    except:
-        # 如果 Arial 不可用,使用默认字体
-        font = pygame.font.Font(None, 24)
-    
-    # 渲染文本到 surface
-    text_surface = font.render(text, True, (255, 255, 255), (0, 0, 0, 0))
-    text_data = pygame.image.tostring(text_surface, "RGBA", True)
-    
-    # 创建纹理
-    texture_id = glGenTextures(1)
-    glBindTexture(GL_TEXTURE_2D, texture_id)
-    
-    # 设置纹理参数
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
-    
-    # 加载纹理数据
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, text_surface.get_width(), text_surface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, text_data)
-    
     # 保存当前矩阵
     glPushMatrix()
     
@@ -299,42 +278,25 @@ def draw_text(x, y, z, text):
     # 旋转文本,使其面向相机
     glRotatef(rotation_angle, 0.0, 1.0, 0.0)
     
-    # 增大文本的缩放比例,使文本更清晰
-    scale = 0.01
+    # 缩小文本,使其适合3D空间
+    scale = 0.001
     glScalef(scale, scale, scale)
     
-    # 启用纹理
-    glEnable(GL_TEXTURE_2D)
-    glBindTexture(GL_TEXTURE_2D, texture_id)
-    
-    # 启用混合,确保透明度正确显示
-    glEnable(GL_BLEND)
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
-    
-    # 绘制四边形
-    glBegin(GL_QUADS)
-    glTexCoord2f(0, 1)
-    glVertex3f(-text_surface.get_width()/2, -text_surface.get_height()/2, 0)
-    glTexCoord2f(1, 1)
-    glVertex3f(text_surface.get_width()/2, -text_surface.get_height()/2, 0)
-    glTexCoord2f(1, 0)
-    glVertex3f(text_surface.get_width()/2, text_surface.get_height()/2, 0)
-    glTexCoord2f(0, 0)
-    glVertex3f(-text_surface.get_width()/2, text_surface.get_height()/2, 0)
-    glEnd()
+    # 计算文本宽度,以便居中显示
+    text_width = 0
+    for char in text:
+        text_width += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12, ord(char))
     
-    # 禁用混合
-    glDisable(GL_BLEND)
+    # 设置文本位置,使其居中
+    glRasterPos2f(-text_width / 2, 0)
     
-    # 禁用纹理
-    glDisable(GL_TEXTURE_2D)
+    # 绘制文本
+    for char in text:
+        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ord(char))
     
     # 恢复矩阵
     glPopMatrix()
     glEnable(GL_LIGHTING)
-    
-    # 删除纹理
-    glDeleteTextures([texture_id])
 
 # 初始化选中节点(根节点)
 selected_node_index = 0