Skip to main content

OpenGL wrapper for Glass-Engine

Project description

Glass -- 为 Glass Engine 提供 OpenGL 易用封装

GlassGlass Engine 的子项目,为 OpenGL Assistant 的缩写。 OpenGL 函数的底层接口的设计为了跨平台,牺牲了易用性。 为了使 OpenGL 的调用对 Python 用户更友好,封装了 Glass 库。 该库简化了大量 OpenGL 概念以及函数调用方法,几项重点简化包括:

  • 支持从文件编译 Shader,支持在 Shader 中使用 include,并将报错行号指向真实的 include 文件;
  • ShaderProgram 自动采用增量编译策略,仅当文件和依赖改变时重新编译;
  • 设置 Uniform/Uniform Block/Shader Storage Block 变量时,Python 对象可直接赋值给 Shader 中的对应结构变量;
  • 提供 Vertices/Indices 类对顶点和索引进行管理,用户无需接触 VBO/VAO/EBO 等直接操作显存的概念;
  • 变化的顶点和索引采用增量拷贝算法同步到显存,提高拷贝效率。

下面是一段不完整的代码展示了 glass 操作 OpenGL 的方法:

from glass import *

# 在一个合法的 OpenGL 上下文中:

# 创建 shader 程序
program = ShaderProgram()
program.compile("path/to/vertex_shader.vs")
program.compile("path/to/fragment_shader.fs")
# 上述两个 compile 并不会每次运行都编译,仅首次以及 Shader 文件修改后才编译
# shader 文件中可含有 #include 语法

# 将 Python 变量 pyvar 直接赋值给 Shader 端的 Uniform 结构体变量 uniform_var
# 只要 Python 变量中含有对应的属性
program["uniform_var"] = pyvar

# 纹理 uniform 变量可用如下方法赋值
# 对用户隐藏纹理单元概念
program["sampler_var"] = sampler2D("path/to/image.png")

# 创建顶点数组
vertices = Vertices() # 像 list 一样操作 vertices,只不过元素只能为 Vertex 类型

# 添加顶点
vertices.append(Vertex(position=glm.vec2(-0.5,-0.5), color=glm.vec3(1,0,0)))
vertices.append(Vertex(position=glm.vec2(0.5,-0.5), color=glm.vec3(0,1,0)))
vertices.append(Vertex(position=glm.vec2(0,0.5), color=glm.vec3(0,0,1)))
# 构建顶点 Vertex 时,属性名可为任意值
# 只要在 vertex shader 中的 layout 指定了该属性名并且类型匹配

# 创建索引数组
indices = Indices() # 像 list 一样操作 indices,只不过元素只能为 glm.uvec3 类型
indices.append(glm.uvec3(0, 1, 2))

# 绘制三角形
program.draw_triangles(vertices=vertices, indices=indices)

# vertices 和 indices 可在任意时刻动态修改内部元素,以及动态增加删除元素
# 所有修改将在下次绘制时同步到显存

你可以在任意需要使用 OpenGL 的地方使用 Glass, 但其初衷是为 Glass Engine 提供 OpenGL 的封装,因此不过多介绍 Glass 的用法,欢迎访问 Glass Engine 项目:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_glass-0.1.64.tar.gz (202.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_glass-0.1.64-py3-none-any.whl (224.8 kB view details)

Uploaded Python 3

File details

Details for the file python_glass-0.1.64.tar.gz.

File metadata

  • Download URL: python_glass-0.1.64.tar.gz
  • Upload date:
  • Size: 202.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for python_glass-0.1.64.tar.gz
Algorithm Hash digest
SHA256 980f5171b9bd8e7a7fa2b9d18cb0dbbbbcd61478929cf663447af4be41ac25be
MD5 f142a14aa42aca5d5cf20aabd9186fec
BLAKE2b-256 807d2f5dac8a57780bf867b4682ce5d04a94776cdfdba1bc14a764f6b4322f49

See more details on using hashes here.

File details

Details for the file python_glass-0.1.64-py3-none-any.whl.

File metadata

  • Download URL: python_glass-0.1.64-py3-none-any.whl
  • Upload date:
  • Size: 224.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for python_glass-0.1.64-py3-none-any.whl
Algorithm Hash digest
SHA256 06eb10a7b98146e48af6c368bd1f734d675ee30ac41166549e70d33abb0c1417
MD5 6c378f41b9bba64ca7218bc077444425
BLAKE2b-256 dc805c645f12db501574be7178ee26f881568dbb123f2a2f088a624afee1be27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page