PDA

View Full Version : line not being drawn



bugmenot
03-11-2011, 06:48 AM
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()

McLeary
03-11-2011, 07:09 AM
You need to define your projection matrix. See http://www.songho.ca/opengl/gl_projectionmatrix.html

bugmenot
03-12-2011, 07:26 AM
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
03-12-2011, 08:39 AM
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);

bugmenot
03-12-2011, 11:41 AM
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?

Alfonse Reinheart
03-12-2011, 12:12 PM
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?