Pyopengl 镶嵌多边形

2024-02-26

我有以下形式的多边形:

[(1,2)、(2,4)、(3,4)、(5,6)]

我需要镶嵌来绘制它们,但是 glutes 太复杂了。 Opengl 无法处理凸多边形。

我想我需要类似的东西:

http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/OpenGLContext/scenegraph/polygontessellator.py http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/OpenGLContext/scenegraph/polygontessellator.py

但我不明白如何使用它。谁能给我一个明确的例子吗?

它必须使用 pyopengl,因为它必须与另一个项目集成(pygame 等不起作用)

或者,有没有办法从线阵列构建修剪网格?


我实际上正在做同样的事情,在 python 中渲染矢量图形!在将示例代码放在一起时,我发现这个资源 http://www.songho.ca/opengl/gl_tessellation.html对于理解 gluTesselator 很有用(尽管该链接不使用 python)。下面是渲染带有两个孔的凹多边形的示例代码(没有 GLU 的原始 OpenGL 的另一个潜在限制),它没有任何我通常使用的自定义类,并为了简单起见而使用(缓慢且糟糕的)立即模式。 triangulate 函数包含用于创建 gluTesselator 的相关代码。

from OpenGL.GLU import *
from OpenGL.GL import *
from pygame.locals import *
import pygame
import sys

def triangulate(polygon, holes=[]):
    """
    Returns a list of triangles.
    Uses the GLU Tesselator functions!
    """
    vertices = []
    def edgeFlagCallback(param1, param2): pass
    def beginCallback(param=None):
        vertices = []
    def vertexCallback(vertex, otherData=None):
        vertices.append(vertex[:2])
    def combineCallback(vertex, neighbors, neighborWeights, out=None):
        out = vertex
        return out
    def endCallback(data=None): pass

    tess = gluNewTess()
    gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD)
    gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, edgeFlagCallback)#forces triangulation of polygons (i.e. GL_TRIANGLES) rather than returning triangle fans or strips
    gluTessCallback(tess, GLU_TESS_BEGIN, beginCallback)
    gluTessCallback(tess, GLU_TESS_VERTEX, vertexCallback)
    gluTessCallback(tess, GLU_TESS_COMBINE, combineCallback)
    gluTessCallback(tess, GLU_TESS_END, endCallback)
    gluTessBeginPolygon(tess, 0)

    #first handle the main polygon
    gluTessBeginContour(tess)
    for point in polygon:
        point3d = (point[0], point[1], 0)
        gluTessVertex(tess, point3d, point3d)
    gluTessEndContour(tess)

    #then handle each of the holes, if applicable
    if holes != []:
        for hole in holes:
            gluTessBeginContour(tess)
            for point in hole:
                point3d = (point[0], point[1], 0)
                gluTessVertex(tess, point3d, point3d)
            gluTessEndContour(tess)

    gluTessEndPolygon(tess)
    gluDeleteTess(tess)
    return vertices

if __name__ == "__main__":
    width, height = 550, 400
    pygame.init()
    pygame.display.set_mode((width, height), DOUBLEBUF|OPENGL)
    pygame.display.set_caption("Tesselation Demo")
    clock = pygame.time.Clock()
    glClear(GL_COLOR_BUFFER_BIT)
    glClear(GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, -1, 1)#flipped so top-left = (0, 0)!
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    #define the polygon and some holes
    polygon = [(0, 0), (550, 0), (550, 400), (275, 200), (0, 400)]
    hole1 = [(10, 10), (10, 100), (100, 100), (100, 10)]
    hole2 = [(300, 50), (350, 100), (400, 50), (350, 200)]
    holes = [hole1, hole2]
    vertices = triangulate(polygon, holes=holes)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        glColor(1, 0, 0)
        glBegin(GL_TRIANGLES)
        for vertex in vertices:
            glVertex(*vertex)
        glEnd()

        pygame.display.flip()
        clock.tick_busy_loop(60)

The output of the above code should look like this: enter image description here

另外,作为旁注,您可以将 pygame 与 pyopengl 一起使用,如上面的代码示例所示。然而,我个人更喜欢使用 pysdl2 为我的应用程序提供窗口,而不是 pygame,后者基于较旧的 sdl 1.2 并且似乎已被放弃。我只在上面的示例中使用 pygame,因为在演示中使用它更简洁。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pyopengl 镶嵌多边形 的相关文章

  • Python 3 中的递归搜索 JSON/DICT

    我在 Python 3 中实现了一些 API 这些 API 允许我根据班级代码接收有关学校的信息 但我想知道如何通过类代码获取信息 例子 我输入代码GF528S我希望程序告诉我班级 3C INF 地址 Address 1 Milan 如果可
  • 为什么 python 允许没有“pass”语句的空函数(带有文档字符串)主体?

    class SomeThing object Represents something def method one self This is the first method will do something useful one da
  • 如何将一组重叠范围划分为不重叠范围?

    假设您有一组范围 0 100 一 0 75 b 95 150 c 120 130 d 显然 这些范围在某些点上重叠 您将如何剖析这些范围以生成不重叠范围的列表 同时保留与其原始范围相关的信息 在本例中为范围后面的字母 例如 运行算法后的上述
  • Python3 http.server:将日志保存到文件中

    我使用Python3 6编写了一个简单的HTTP服务器来重定向所有请求 我写的文件可以找到here https github com kmahyyg learn py3 blob master antiscanhttp py 我可以在 Ub
  • 如何阻止 Django 中发生级联删除?

    我的 Django 应用程序中有三个模型类 class Folder models Model folder models ForeignKey Folder null True blank True related name folder
  • “初始化 MCI 时出现问题”播放声音问题

    我正在尝试使用 Playsound 播放代码文件夹中的文件 但是每次运行代码时 它似乎都能够调用该文件 但我总是收到以下输出 playsound PlaysoundException Error 277 for command open p
  • 有没有办法清理 jinja2 生成的 html?

    我们使用 jinja2 来创建 html 但是 由于我们在 jinja 中执行许多循环和其他操作来生成 html 所以 html 看起来 很丑 注意 这只是为了美观 我们可以做些什么来清理 html 吗 除了清理我们的 jinja2 代码之
  • 使用 Python-AppKit-Objective C 转换为预组合 Unicode 字符串

    苹果公司的这份文件技术问答 QA1235 http developer apple com qa qa2001 qa1235 html描述了一种将 unicode 字符串从组合版本转换为分解版本的方法 由于我对包含某些字符 例如重音符号 的
  • SQLAlchemy+pymysql 错误:sqlalchemy.util.queue.Empty

    尝试使用 Eclispse 在 Ubuntu 上运行 Python 2 SQLAlchemy 0 8 和 MySQL5 2 但我不断收到以下错误 我使用 pymysql 实际上是 pymysql3 引擎 模块监视器 from sqlalch
  • 使用 Python 读取 App Engine 上的文件?

    是否可以在 GAE 上打开文件来读取其内容并获取最后修改的标签 我收到 IOError Errno 13 文件无法访问 我知道我无法删除或更新 但我相信阅读应该是可能的 有人遇到过类似的问题吗 os stat f r st mtim 您可能
  • 带有 UnboundLocalError 的本地和全局引用

    我不太明白为什么代码 def f print s s foo f 运行得很好但是 def f print s s bar s foo f 给我 UnboundLocalError 我知道我可以通过声明来解决这个问题s作为函数内的全局变量或简
  • 如何从分组数据创建直方图

    我正在尝试根据 pandas 中的分组数据创建直方图 到目前为止 我已经能够创建标准线图 但我不知道如何做同样的事情来获取直方图 条形图 我想获得泰坦尼克号事故中幸存者和未幸存者的 2 个年龄直方图 看看年龄分布是否存在差异 来源数据 ht
  • 如何编辑 QProgressBar 的样式表

    我无法在我的应用程序中编辑进度条的颜色 仅编辑文本颜色 pyhton 3 9 PySide6 QT Creator 7 0 2 Python应用程序 https i stack imgur com 6hKFI png import sys
  • 仅获取图像中的外部轮廓

    我有这段代码 可以在图像中绘制轮廓 但我只需要外部轮廓 import cv2 import numpy as np camino C Users Usuario Documents Deteccion de Objetos 123 jpg
  • Python range() 和 zip() 对象类型

    我了解功能如何range and zip 可以在 for 循环中使用 然而我期望range 输出一个列表 很像seq在 Unix shell 中 如果我运行以下代码 a range 10 print a 输出是range 10 表明它不是一
  • 加载 IPython 笔记本时出错

    一旦我用 Jupyter 打开笔记本文件 它要求我转换文件 我就再也无法在标准 IPython 笔记本中打开它了 我收到以下错误 Error loading notebook Bad Request 2014 12 21 04 13 03
  • ValueError:对于optimize.curve_fit中所需的数组来说对象太深

    我正在尝试拟合化学系统中四个变量 A B C D 的人口增长和衰退的动力学模型 我正在尝试求解以下一组方程 我已将其以矩阵形式附加 方程的矩阵形式 https i stack imgur com ysEdZ png 其中 t 是时间步长 k
  • Python:Scrapy返回元素后面的所有html,而不仅仅是元素的html

    我遇到了 Scrapy 行为异常的问题 几个月前我编写了一个简单的函数 它返回给定 xpath 处的项目列表 def get html response path sel Selector text response page source
  • 为什么 Python ggplot 返回名称“aes”未定义?

    当我使用以下命令时 p ggplot aes x DTM y TMP1 data data 我收到以下错误 NameError name aes is not defined 你可以帮帮我吗 你需要导入aes from ggplot imp
  • 在 python 中使用 ftplib 时

    这是导致错误的相关代码 ftp ftplib FTP server ftp login r user r pass change directories to the incoming folder ftp cwd incoming fil

随机推荐