所以我在python中使用pyopengl来使用opengl但是当我在我的重塑函数中使用glOrtho来映射我的坐标时,它不起作用它仍然被映射到-1 -1这是我的代码:
from OpenGL.GLUT import *
from OpenGL.GL import *
width, height = 500,500
def rectangle(x, y, width, height, color, fill):
if fill:
glBegin(GL_POLYGON)
glColor3ub(color[0], color[1], color[2])
glVertex2f(x - width/2, y + height/2)
glVertex2f(x + width/2, y + height/2)
glVertex2f(x + width/2, y - height/2)
glVertex2f(x - width/2, y - height/2)
glColor3ub(255, 255, 255)
glEnd()
else:
glBegin(GL_LINES)
glColor3ub(color[0], color[1], color[2])
glVertex2f(x - width/2, y + height/2)
glVertex2f(x + width/2, y + height/2)
glVertex2f(x + width/2, y - height/2)
glVertex2f(x - width/2, y - height/2)
glColor3ub(255, 255, 255)
glEnd()
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClearColor(0.25, 0.25, 0.25, 1)
glLoadIdentity()
glBegin(GL_POLYGON)
glVertex2f(1, 1)
glVertex2f(-1, 1)
glVertex2f(-1, -1)
glVertex2f(100, -1)
glEnd()
glutSwapBuffers()
def reshape(w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, w, 0, h, 0, 1000)
glutInit()
glutInitDisplayMode(GLUT_RGBA) # Set the display mode to be colored
glutInitWindowSize(width, height) # Set the w and h of your window
glutInitWindowPosition(0, 0) # Set the position at which this windows should appear
window = glutCreateWindow("OpenGL") # Set a window title
glutReshapeFunc(reshape)
glutDisplayFunc(draw) # defines display func
glutIdleFunc(draw) # Keeps the window open
glutMainLoop() # Keeps the above created window displaying/running in a loop
帮我解决这个问题我不明白为什么它不工作
glOrtho
不工作,因为在draw
函数中的glLoadIdentity()
指令。glLoadIdentity()
加载单位矩阵。将glOrtho
后的矩阵模式改为glMatrixMode
。这确保了投影矩阵被保留,但是单位矩阵被加载到模型视图矩阵中。OpenGL是一个状态引擎。一旦一个状态被改变了,它将被保留,直到它再次被改变,甚至超越帧。
def reshape(w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, w, 0, h, 0, 1000)
glMatrixMode(GL_MODELVIEW)