Im trying to draw a cube with lines.
And here's my code. It just gives me a white frame with nothing inside it. Nothing happens. What am I doing wrong here? Is it a problem with the order of calling out the functions or something wrong with the projection?
def myInit():
glClearColor(0.0, 0.0, 0.0, 1.0)
glColor3f(0.2, 0.5, 0.4)
gluPerspective(45, 1.33, 0.1, 50.0)
vertices= (
(100, -100, -100),
(100, 100, -100),
(-100, 100, -100),
(-100, -100, -100),
(100, -100, 100),
(100, 100, 100),
(-100, -100, 100),
(-100, 100, 100)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Display():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(800, 600)
myInit()
glutDisplayFunc(Display)
glutMainLoop()
All the geometry which is not in the Viewing frustum is clipped. The size of the cube i 200x200x200. You've to create a viewing frustum which is large enough.
For instance set a perspective projection matrix gluPerspective
with a far plane of 1000.0. The projection matrix is intended to be set to the current projection matrix (GL_PROJECTION
). See glMatrixMode
:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.33, 0.1, 1000.0)
Translate ([`glTranslate`](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml)) the model along the negative z axis, in between the near plane (0.1) and far (plane). The model or view matrix has to be set to the current model view matrix (`GL_MODELVIEW`):
```py
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(0, 0, -500)
Clear the display in every frame by glClear
:
def Display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# [...]
Swaps the buffers of the current double buffered window by glutSwapBuffers
and continuously update the display by invoking glutPostRedisplay
.
def Display():
# [...]
glutSwapBuffers()
glutPostRedisplay()
See also Immediate mode and legacy OpenGL
See the example:
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.33, 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(0, 0, -500)
def Display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glColor3f(0.2, 0.5, 0.4)
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
glutSwapBuffers()
glutPostRedisplay()