gluSphere()

837 views
Skip to first unread message

Dag Henrik

unread,
Jul 8, 2009, 8:49:33 AM7/8/09
to pyglet-users
Hello! I am trying out pyglet, and I would like to draw a sphere.

Here's some code that I'm trying to use right now:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
from pyglet.gl import *

class Viewer:
"""Display self-organizing map."""
def __init__(self, n_neurons):
self.window = pyglet.window.Window(640, 480)
self.n_neurons = n_neurons
self.window.push_handlers(self)

def on_draw(self):
"""Continuously updated."""
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()

sphere = gluNewQuadric()
gluSphere(sphere, 1.0, 100, 100)

if __name__ == "__main__":
v = Viewer(10)
pyglet.app.run()

It shows a black background, but without any sphere. I am able to draw
other
primitive creatures though, such as GL_LINES_LOOP etc.

Casey Duncan

unread,
Jul 8, 2009, 5:35:34 PM7/8/09
to pyglet...@googlegroups.com
self.window = pyglet.window.Window(640, 480)

This will setup a 2D orthographic projection by default, obviously a
sphere is 3D, which is why you can't see it. You need to setup a 3D
projection. Which you can do with something like this:

glViewport(0, 0, self.window.width, self.window.height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(70, 1.0*self.window.width/self.window.height, 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

This sets up a perspective projection, you can also setup a 3D ortho
projection if you don't want foreshortening. Use glOrtho for that.

If you make your window resizeable put the above code in an
on_resize(width, height) handler method on the window.

hth,

-Casey

Dag Henrik

unread,
Jul 9, 2009, 7:04:27 AM7/9/09
to pyglet-users
Thank you for the feedback. I tried running the code with a different
projection
without much luck. However, I'm starting the think the problem is
related to the
camera. Current code:

http://paste.pocoo.org/show/127450/

On Jul 8, 11:35 pm, Casey Duncan <casey.dun...@gmail.com> wrote:
> self.window = pyglet.window.Window(640, 480)
>
> This will setup a 2D orthographic projection by default, obviously a
> sphere is 3D, which is why you can't see it. You need to setup a 3D
> projection. Which you can do with something like this:
>
>     glViewport(0, 0, self.window.width, self.window.height)
>     glMatrixMode(GL_PROJECTION)
>     glLoadIdentity()
>     gluPerspective(70, 1.0*self.window.width/self.window.height, 0.1, 1000.0)
>     glMatrixMode(GL_MODELVIEW)
>     glLoadIdentity()
>
> This sets up a perspective projection, you can also setup a 3D ortho
> projection if you don't want foreshortening. Use glOrtho for that.
>
> If you make your window resizeable put the above code in an
> on_resize(width, height) handler method on the window.
>
> hth,
>
> -Casey
>

Tristam MacDonald

unread,
Jul 9, 2009, 7:34:59 AM7/9/09
to pyglet...@googlegroups.com
On Thu, Jul 9, 2009 at 7:04 AM, Dag Henrik <dagh...@gmail.com> wrote:

Thank you for the feedback. I tried running the code with a different
projection
without much luck. However, I'm starting the think the problem is
related to the
camera. Current code:

http://paste.pocoo.org/show/127450/ 


        glPushMatrix()
        glTranslatef(0.0, 0.0, -100.0)
        glPopMatrix()

The code I have copied here does absolutely nothing - you have to push and translate before drawing the sphere, and not pop till afterwards.

--
Tristam MacDonald
http://swiftcoder.wordpress.com/

Dag Henrik

unread,
Jul 9, 2009, 7:39:51 AM7/9/09
to pyglet...@googlegroups.com
Right. I tried that, with the same result: a black window. The camera
is zoomed out,
so I can't be inside the sphere... Hmm.

Tristam MacDonald

unread,
Jul 9, 2009, 8:19:01 AM7/9/09
to pyglet...@googlegroups.com
On Thu, Jul 9, 2009 at 7:39 AM, Dag Henrik <dagh...@gmail.com> wrote:

Right. I tried that, with the same result: a black window. The camera
is zoomed out,
so I can't be inside the sphere... Hmm.

Just to make sure the camera is in the correct place, try using gluLookAt (http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html), which lets you precisely position the camera.

Also make sure that you have disabled lighting and texturing - gluQuadrics don't generate normals or texture coordinates by default.

Jimmy Chan

unread,
Jul 9, 2009, 1:55:23 PM7/9/09
to pyglet-users
Here is a simple example which uses a trackball camera. I found the
camera here http://www.rogerandwendy.com/roger/code/trackball_camera.txt

http://bitbucket.org/jimmyhchan/pyglet_glusphere_test/src/
download and run trackball_camera_test.py

jimmy



On Jul 9, 5:19 am, Tristam MacDonald <swiftco...@gmail.com> wrote:
> On Thu, Jul 9, 2009 at 7:39 AM, Dag Henrik <daghen...@gmail.com> wrote:
>
> > Right. I tried that, with the same result: a black window. The camera
> > is zoomed out,
> > so I can't be inside the sphere... Hmm.
>
> Just to make sure the camera is in the correct place, try using gluLookAt (http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/...),

Dag Henrik

unread,
Jul 10, 2009, 5:37:46 AM7/10/09
to pyglet...@googlegroups.com
After banging my head against the wall for a while, I found out that removing
self.window.push_handlers(self) and subclassing window.Window solved
my problem...
Reply all
Reply to author
Forward
0 new messages