Attempting a simple square graph in pyOpenGL

I'm trying to teach myself OpenGL using pyopengl, and I'm amazed at trying to make a simple 2D square centered at the origin. Whenever I set the array value to greater than or equal to 1, the form occupies the entire screen, as if I were only viewing a small part of the axis. I tried to base it on NeHe tutorials rewritten in pyopengl, but I cannot find what I am 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() 
+2
source share
2 answers

Try setting the projection matrix without a default value:

 def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho( 0, 640, 0, 480, -10, 10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() ... 
0
source

You need to set the projection matrix and viewport. Python allows us to use a bit of functional programming to do this in a smart way:

 from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def display(w, h): aspect = float(w)/float(h) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-aspect * 5, aspect * 5, -5, 5, -1, 1) glMatrixMode(GL_MODELVIEW); glLoadIdentity() 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() def reshape(w, h): glutDisplayFunc(lambda: display(w, h)) glutPostRedisplay(); if __name__ == '__main__': glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640,480) glutCreateWindow("Hello World :'D") glutReshapeFunc(reshape) glutIdleFunc(glutPostRedisplay) glutMainLoop() 
+2
source

All Articles