Initial OpenGL Drawing and Animation

Posted by Chris Thu, 07 Feb 2008 20:35:00 GMT

Before I started this little project I didn’t know any OpenGL, and really didn’t have any animation/graphics experience. Primarily I’ve been relying on Google to get me around, but there are some things I’m still struggling with and may just have to figure out on my own.

The first thing I’ve run into is how to approach drawing in a reusable way. OpenGL provides very primitive commands for drawing, and they are all oriented around drawing one frame at a time. The approach I’m settled at as of last night is to create a decorator class that will decorate my domain objects with location information, I’ve called it PositionedItem. It looks like this:

class PositionedItem
  attr_accessor :x, :y, :z, :x_rotation, :y_rotation, :z_rotation
  def initialize(other_obj)
    @decorated_object = other_obj
    @x = 0
    @y = 0
    @z = 0
    @x_rotation = 0
    @y_rotation = 0
    @z_rotation = 0
  end

  def set_position(x, y, z, x_rotation, y_rotation, z_rotation)
    @x = x
    @y = y
    @z = z
    @x_rotation = x_rotation
    @y_rotation = y_rotation
    @z_rotation = z_rotation
  end

  def method_missing(method, *args, &block)
    @decorated_object.send(method, *args, &block)
  end
end

This allows me to take a pure data-class (like a “Picture” class in the code I’m currently working on) and wrap it with position information that my drawing class can use.

In my main drawing method I am currently drawing a few quads on the screen with a texture in them (from my Picture class), there is a main picture and some side pictures. At the beginning of the drawing method, I set the positions of the objects that are going to be drawn (a subset of the entire collection of objects) and then call a draw method for each picture, this method knows how to draw a single positioned item based on it’s position data.

One thing that this approach doesn’t do right now is allow for more complex, position relative items. Each drawing method call does the following steps:

  1. Push the current Transformation matrix
  2. Draw a single quad with a texture
  3. Pop the transformation matrix

This means that right now I can only draw quads and it’s always relative to the original location, not relative to the last quad that was drawn.

As for animation, I still don’t really have that figured out. I do have goals though:

  1. It has to be time-based, not frame count based
  2. It has to be some kind of keyframe animation, I want to define the start and end positions and calculate all of the inbetween steps
  3. it has to be smooth

I think my approach is going to be having animation methods for each type of animation I want to do in my drawing class. Right now I want to animate choosing the next and previous pictures, so I’m going to have a animate_select_next and an animate_select_previous method. So far, I haven’t written any code, but I think the method is going to look something like this (complete pseudocode btw):

start_time = now_time
target_time = start_time + 500ms
save_object_start_positions
define_target_positions
while( now_time < target_time)
  percent_complete = (now_time - start_time) / (target_time - start_time)
  objects.each do |obj|
    new_position = (target_position - saved_start_position) * percent_complete
    set_newposition
    draw obj
  end
  sleep 5ms
end

I might be able to keep this generic enough to use it for all my animation, and just define the target positions when I call the method.

In the meantime, I need to find good sources on how to do simple keyframe animation and drawing loop techniques in OpenGL, because I’m really just trying to figure it out on my own at this point in time.

Trackbacks

Use the following link to trackback from your own site:
http://www.staticmethod.net/trackbacks?article_id=initial-opengl-drawing-and-animation&day=07&month=02&year=2008

Comments

Leave a comment

Comments