zhong (钟鹏群) 1 月之前
父节点
当前提交
2403320430
共有 1 个文件被更改,包括 84 次插入0 次删除
  1. 84 0
      ball-demo.py

+ 84 - 0
ball-demo.py

@@ -13,6 +13,11 @@ from pygame.locals import *
 from OpenGL.GL import *
 from OpenGL.GL import *
 from OpenGL.GLU import *
 from OpenGL.GLU import *
 
 
+# 初始化字体
+pygame.font.init()
+# 使用默认字体,增大字体大小
+font = pygame.font.Font(None, 24)
+
 # 模拟模式开关(True: 使用模拟数据,False: 扫描真实目录)
 # 模拟模式开关(True: 使用模拟数据,False: 扫描真实目录)
 # USE_MOCK_DATA = True
 # USE_MOCK_DATA = True
 USE_MOCK_DATA = False
 USE_MOCK_DATA = False
@@ -255,6 +260,82 @@ root_path = "E:\\agricultural_research_platform"
 
 
 tree_nodes, tree_edges, node_positions, tree_children = build_directory_tree(root_path)
 tree_nodes, tree_edges, node_positions, tree_children = build_directory_tree(root_path)
 
 
+def draw_text(x, y, z, text):
+    """在3D空间中绘制文本"""
+    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()
+    
+    # 移动到指定位置
+    glTranslatef(x, y, z)
+    
+    # 旋转文本,使其面向相机
+    glRotatef(rotation_angle, 0.0, 1.0, 0.0)
+    
+    # 增大文本的缩放比例,使文本更清晰
+    scale = 0.01
+    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()
+    
+    # 禁用混合
+    glDisable(GL_BLEND)
+    
+    # 禁用纹理
+    glDisable(GL_TEXTURE_2D)
+    
+    # 恢复矩阵
+    glPopMatrix()
+    glEnable(GL_LIGHTING)
+    
+    # 删除纹理
+    glDeleteTextures([texture_id])
+
 # 初始化选中节点(根节点)
 # 初始化选中节点(根节点)
 selected_node_index = 0
 selected_node_index = 0
 
 
@@ -412,6 +493,9 @@ while running:
         gluSphere(quad, current_radius, 32, 32)
         gluSphere(quad, current_radius, 32, 32)
         gluDeleteQuadric(quad)
         gluDeleteQuadric(quad)
         glPopMatrix()
         glPopMatrix()
+        
+        # 绘制文件名
+        draw_text(x, y + current_radius + 0.05, z, name)
     
     
     # 交换缓冲区
     # 交换缓冲区
     pygame.display.flip()
     pygame.display.flip()