【问题标题】:creating a rotating 3d shape in opengl with proper clipping在opengl中使用适当的剪辑创建一个旋转的3d形状
【发布时间】:2014-04-16 19:12:11
【问题描述】:

我正在尝试创建一个不断旋转的三角金字塔。我从教程示例中获取了一些代码来获取旋转,我认为它正在工作,但我的身材在 z 轴上被剪裁了。从我在各种教程、论坛等中看到的情况来看,openGL 的默认 z 范围为 (-1, 1)。如何更改它以显示我的图的全部范围?

谢谢(这是我的代码)

import pyglet

from pyglet.gl import *

rx = ry = rz = 0

config = Config(sample_buffers=1, samples=4, depth_size=1000, double_buffer=True,)
win = pyglet.window.Window(resizable=True, config=config)

@win.event

def update(dt):
    global rx, ry, rz
    rx += dt * 1
    ry += dt * 80
    rz += dt * 30
    rx %= 360
    ry %= 360
    rz %= 360
pyglet.clock.schedule(update)

@win.event
def on_draw():

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(200, 200, -4)
    glRotatef(rz, 0, 0, 1)
    glRotatef(ry, 0, 1, 0)
    glRotatef(rx, 1, 0, 0)


    glBegin(GL_TRIANGLES)

    glVertex3f(-100.0, 0.0, 0.0)
    glVertex3f(100.0, 0.0, 0.0)
    glVertex3f(0, 200.0, 0.0)

    glVertex3f(-100.0, 0.0, 0.0)
    glVertex3f(0.0, 0.0, -100.0)
    glVertex3f(0.0, 200.0, 0.0)

    glVertex3f(100.0, 0.0, 0.0)
    glVertex3f(0.0, 0.0, -100.0)
    glVertex3f(0.0, 200.0, 0.0)

    glVertex3f(-100.0, 0.0, 0.0)
    glVertex3f(0.0, 0.0, -100.0)
    glVertex3f(100.0, 0.0, 0.0)
    glEnd()

    glLoadIdentity()
    #glTranslatef(1.5, 0.0, -10.0)          

pyglet.app.run()

【问题讨论】:

  • 你能张贴一张图片,说明你说“在 z 轴上剪辑”是什么意思吗?我不熟悉这个词
  • glOrtho 用于您使用的 OpenGL 版本。
  • 显然无法发布图片,因为我的“声誉”不够高。
  • 我应该在哪里实现 glOrtho?我尝试将其放入“on_draw”,但没有任何效果。

标签: opengl enthought pyglet canopy depth-buffer


【解决方案1】:

OpenGL 有 Projection 和 ModelView 的矩阵。

您想要的是将投影矩阵从默认的更改为更合适的。

而不是

glLoadIdentity()
glTranslatef(200, 200, -4)

写

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1000, 1000, -1000, 1000, -1000, 1000)

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(200, 200, -4)

glOrtho(x_min, x_max, y_min, y_max, z_min, z_max) 是您最有可能获得的实现。

如果您想要 3d 投影,请使用 gluPerspective

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多