glOrtho 不起作用它仍然被映射到默认坐标
glOrtho doesent work it still gets mapped to default coordinates
所以我在 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()
加载 Identity matrix. Change the matrix mode after glOrtho
with glMatrixMode
。这确保了投影矩阵被保留,但单位矩阵被加载到模型视图矩阵中。 OpenGL 是一个状态引擎。一旦状态被改变,它会一直保留直到再次改变,甚至超出帧。
def reshape(w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, w, 0, h, 0, 1000)
glMatrixMode(GL_MODELVIEW)
所以我在 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()
加载 Identity matrix. Change the matrix mode after glOrtho
with glMatrixMode
。这确保了投影矩阵被保留,但单位矩阵被加载到模型视图矩阵中。 OpenGL 是一个状态引擎。一旦状态被改变,它会一直保留直到再次改变,甚至超出帧。
def reshape(w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, w, 0, h, 0, 1000)
glMatrixMode(GL_MODELVIEW)