How can I create 3D with pyglet?

Viewed 292

I tried to create a cube in pyglet:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window(400,400,"Paka Paka",True)
@window.event
def on_draw():
    window.clear()
    glBegin(GL_QUADS)
    glVertex3f(0,40,0)
    glColor3f(1,0,0)
    glVertex3f(100,40,0)
    glColor3f(0,1,0)
    glVertex3f(100,140,0)
    glColor3f(0,0,1)
    glVertex3f(100,40,100)
    glColor3f(1,1,0)
    glVertex3f(100,140,100)
    glColor3f(1,0,1)
    glVertex3f(0,140,100)
    glColor3f(0,1,1)
    glVertex3f(0,40,100)
    glColor3f(2,0,0)
    glVertex3f(0,140,0)
    glColor3f(0,0,2)
    glEnd()
pyglet.app.run()

and when I run the program, I saw a black screen.

and when I run the program, I saw a black screen.

(except up) should I need to change vertex kind or something different?

edit: I used the codes given by Rabbid76. but it didn't worked.

1 Answers

Seme of the geometry is clipped by the near respectively far plane of the (default) orthographic projection.

I recommend to setup a projection matrix by glOrtho. e.g.:

glOrtho(-200, 200, -200, 200, -200, 200) 

respectively

glOrtho(0, 400, 400, 400, -200, 200)  

Further more it is recommended to flush a single buffered window by glFlush()

Set the projection at the begin of on_draw and call glFlush() at the end:

@window.event
def on_draw():
    w, h = 400, 400
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-w/2, w/2, -h/2, h/2, -200, 200)

    glMatrixMode(GL_MODELVIEW)

    window.clear() 

    glBegin(GL_QUADS)
    # [...]
    glEnd()

    glFlush()
Related