in following code written in python, the background color is being set but the lines and polygons are not being drawn.please help out
try:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
except: print '''ERROR: PyOpenGL not installed properly.'''
import sys
def init():
glClearColor(0.0,0.0,1.0,0.0)
glColor3d(1.0,1.0,1.0)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize (640,480)
glutInitWindowPosition (0,0)
window=glutCreateWindow ("Solar")
glutDisplayFunc(mydisplay)
#glutKeyboardFunc(mydisplay)
init()
glutMainLoop()
exit()
def mydisplay():
glClear (GL_COLOR_BUFFER_BIT)
glBegin(GL_LINES)
glVertex2d(100.0,200.0)
glVertex2d(200.0,400.0)
glEnd()
glBegin(GL_POLYGON)
glVertex2d(100.9,33.0)
glVertex2d(50.9, 50.0)
glVertex2d(51.0, 123)
glVertex2d(123.1, 200)
glEnd()
glFlush()
main()
i added the following code
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
so the complete code becomes
try:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
except: print '''ERROR: PyOpenGL not installed properly.'''
import sys
def initialize():
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glClearColor(0.0,1.0,1.0,0.0)
glColor3d(1.0,1.0,1.0)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize (640,480)
glutInitWindowPosition (0,0)
window=glutCreateWindow ("Solar")
glutDisplayFunc(mydisplay)
#glutKeyboardFunc(mydisplay)
initialize()
glutMainLoop()
exit()
def mydisplay():
initialize()
glClear (GL_COLOR_BUFFER_BIT)
glBegin(GL_LINES)
glVertex2d(100.0,200.0)
glVertex2d(200.0,400.0)
glEnd()
glBegin(GL_POLYGON)
glVertex2d(100.9,33.0)
glVertex2d(50.9, 50.0)
glVertex2d(51.0, 123)
glVertex2d(123.1, 200)
glEnd()
glFlush()
main()
but the program is still not displaying any lines.
please help
mobeen
March 12, 2011, 7:39am
4
Thats because u have not setup the projection matrix and the viewport. Currently the projection matrix is set to identity matrix. Use glOrtho or gluOrtho2D and specify the projectoion volume as follows.
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0,640,0,480);
thanks a lot. can you please explain me why we need these steps. in computer graphics using opengl by fs hill, these steps are not mentioned. btw what is the ‘red’ book of computer graphics?
in computer graphics using opengl by fs hill, these steps are not mentioned.
I highly doubt that in 920 pages, they never bothered to mention projection matrices. Maybe you just missed that part?