尝试在 pyOpenGL 中绘制一个简单的正方形
我正在尝试使用 pyopengl 自学 OpenGL,并且我对尝试渲染一个以原点为中心的简单 2D 正方形感到惊讶。每当我设置一个大于或等于 1 的数组值时,该形状就会占据整个屏幕,就好像我只查看轴的一小部分一样。我试图以 pyopengl 重写的 NeHe 教程为基础,但我找不到我做错了什么。
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_QUADS)
glVertex3f(2,-2,0)
glVertex3f(2,2,0)
glVertex3f(-2,2,0)
glVertex3f(-2,-2,0)
glEnd()
glutSwapBuffers()
if __name__ == '__main__':
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(640,480)
glutCreateWindow("Hello World :'D")
glutDisplayFunc(display)
glutIdleFunc(display)
glutMainLoop()
I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to render a simple 2D square centered at the origin. Whenever I set an array value greater than or equal to 1, the shape takes up the entire screen as if I am only viewing a small section of the axis. I have tried to base it off the NeHe tutorials rewritten in pyopengl but I can't find what I'm doing wrong.
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_QUADS)
glVertex3f(2,-2,0)
glVertex3f(2,2,0)
glVertex3f(-2,2,0)
glVertex3f(-2,-2,0)
glEnd()
glutSwapBuffers()
if __name__ == '__main__':
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(640,480)
glutCreateWindow("Hello World :'D")
glutDisplayFunc(display)
glutIdleFunc(display)
glutMainLoop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置投影矩阵和视口。 Python 允许我们使用一点函数式编程来以合理的方式做到这一点:
You need to set the projection matrix and the viewport. Python allows us to use a little bit functional programming to do it the sane way:
尝试设置非默认投影矩阵:
Try setting a non-default projection matrix: