1

I am using OpenGL with Python, and have the following code:

glTranslatef(0, 0, -3.0)
glRotatef(rotation * 100.0, 0.0, 1.0, 0.0)
square.render()

- where rotation is a integer which increases by frame and square.render() simply renders a quad to the screen.

Most sources will advise translating the matrix and then rotating it, as I have done here. However, when the quad renders to the screen, it rotates around a circle rather than a fixed point. My issue is that I want the quad to rotate around a fixed point, and I have been unsuccessful thus far in doing so. What am I doing wrong?

Fitzy
  • 1,871
  • 6
  • 23
  • 40
  • I would consider a different way of describing this in the future. When I read ***"fixed point"*** in the context of computer graphics, the first thing that comes to mind is an integer data type that has a scale to define a radix point... maybe "fixed location?" :) – Andon M. Coleman Nov 14 '13 at 05:04
  • @AndonM.Coleman Ah, yes. Damn these technicalities always escaping me! Thanks for the heads up :) – Fitzy Nov 14 '13 at 11:16

1 Answers1

2

Think about glRotatef() as if you actually "draw" an axis, which starts from (0, 0, 0) and points to whatever coordinates you give to it. Your example uses the (0, 1, 0) which is the World Y axis.

But let's say, you want to rotate your P point (10, 10, 0) around the A axis [(3, 3, 3), (3, 4, 3)].This axis -- as you can see -- is parallel with Y.

First you have to translate your whole world so now your A axis will lie on World Y axis.

glClear( GL_COLOR_BUFFER_BIT )
glTranslatef( 3, 3, 3 )

Now you have to rotate everything around the World Y axis:

glRotatef( rotation*100.0, 0, 1, 0 )

And then you have to translate it back:

glTranslatef( -3, -3, -3 )

And now you can draw your P point:

glBegin( GL_POINTS )
glVertex3f( 10, 10, 0 )
glEnd()

NOTE: forget my stupid glBegin/End example, it is ancient, use VAOs and VBOs!


I don't know what framework are you using, but here is a pretty basic example in pyglet:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window( 256, 256 )

x = 0
def update(dt):
    global x
    x += 1


@window.event
def on_draw():
    global x

    glClear( GL_COLOR_BUFFER_BIT )
    glLoadIdentity()
    glTranslatef( 128, 128, 0 )
    glRotatef( x, 0, 0, 1 )
    glTranslatef( -128, -128, 0 )

    glBegin( GL_TRIANGLES )
    glColor3f( 1, 0, 0 )
    glVertex3f( 128, 128, 0 )
    glColor3f( 0, 1, 0 )
    glVertex3f( 200, 128, 0 )
    glColor3f( 0, 0, 1 )
    glVertex3f( 200, 200, 0 )
    glEnd()

pyglet.clock.schedule_interval(update, 1/60)
pyglet.app.run()

AND THE RESULT IS HERE!

Peter Varo
  • 11,726
  • 7
  • 55
  • 77
  • 1
    Wonderful! Thank you so much, this makes a lot more sense now and I was able to fix my problem! :) – Fitzy Nov 13 '13 at 10:10