Thread: Animating Multiple Object - [Open Gl]

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Animating Multiple Object - [Open Gl]

    I am trying to animate several objects in opengl, and I am becoming confused. I have a drawing routine like this.

    Code:
    void app::draw()
    {
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	shape s(dp3(0.1f, 0.1f, 0.1f));
    	shape p(dp3(0.3f, 0.3f, 0.3f));
    	
    /*
    
    Add points to shapes
    
    */
    
    	static float x = 0.0f, y = 0.0f;
    	static float x2 = 0.0f, y2 = 0.0f;
    
    	keyboard keys = window.key_get(); // get status of keyboard
    
    	if(keys[VK_LEFT]) x -= 0.0005f;
    	if(keys[VK_RIGHT]) x += 0.0005f;
    
    	if(keys[VK_UP]) y += 0.0005f;
    	if(keys[VK_DOWN]) y -= 0.0005f;
    
    	if(keys[0x41]) x2 -= 0.0005f;
    	if(keys[0x44]) x2 += 0.0005f;
    
    	if(keys[0x57]) y2 += 0.0005f;
    	if(keys[0x53]) y2 -= 0.0005f;
    	
    	std::wstringstream ss;
    
    	ss << x << " " << y << " " << x2 << " " << y2 << std::endl;
    
    	::OutputDebugString(ss.str().c_str());
    
    	s.draw(x, y);
    	p.draw(x2, y2);
    
    	window.swap();
    }
    A shape is like this

    Code:
    class shape
    {
    	public:
    		shape();
    		shape(const dp3 &);
    		~shape();
    
    		void add(const dp3 &);
    		void color(const dp3 &);
    		void draw(float x, float y) const;
    
    	private:
    		std::vector<dp3> pts;
    		dp3 col;
    };
    And to draw it is like this

    Code:
    void shape::draw(float x, float y) const
    {
    	std::vector<dp3>::const_iterator i;
    
    	glPushMatrix();
    	glColor3f(col.g(X), col.g(Y), col.g(Z));
    	glTranslatef(x, y, 0.0f);
    	glBegin(GL_POLYGON);
    
    	for(i = pts.begin(); i != pts.end(); ++i)
    	{
    		glVertex3f(i->g(X), i->g(Y), i->g(Z));
    	}
    
    	glEnd();
    	glPopMatrix();
    }
    If I do not have the glPush/PopMatrix then the two shapes are moved as a single unit (bad), and it accelerates (good). But if I do not have those, then the objects do move seperately (good) but do not accelerate (bad).

    I check the values of x, y, x2, y2, and it seems that only x, y are modified by arrow keys and wasd keys when glPush/PopMatrix is not in the code, but they seem to be reset to 0 when glPush/PopMatrix is in the code. I don't get it, they're static.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is there anything I explained wierdly? I'll try to replicate a 'smaller segment replicating the problem' I guess.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I think I'm dumb, but more importantly, I think I need to do a glLoadIdentity in each draw. I can not test right now, but I'm pretty sure.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What are you trying to do? That does not look like animation.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Translate a primitive around on the screen? That might be a little more precise.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I'm not an OpenGL guy but the concept is you translate the vertices from local or model space to world space, camera space, clip space, and then screen space.

    Create your vertices and then multiply them by a translation transformation matrix. OpenGL uses right hand matrices if I recall so I cannot help you with that as I'm used to left-handed. Here is a translation matrix for left handed.

    Code:
    1 0 0 dx
    0 1 0 dy
    0 0 1 dz
    0 0 0 1
    Where dx,dy, and dz are distance translated from 0,0,0 on x, y and z axes.

    This code will concatenate two matrices however it is not the best or fastest implementation, but it is the easiest to show.

    Code:
    void MatMul(float mat1[4][4],float mat2[4][4],float matResult[4][4])
    {
      for (int i=0;i<4;i++)
      {
        for (int j=0;j<4;j++)
        {
          matResult[i][j]=0;
          for (int k=0;k<4;k++)
          {
             matResult[i][j]+=mat1[i][k]*mat2[k][j];
          }
        }
     }
    }
    You will get more efficiency if you unroll the inner loop and even more efficiency if you remove the 2D arrays.

    To multiply a vector by a 4x4 matrix:

    Code:
    void Transform(Vector3 &vec,float mat[4][4])
    {
      Vector3 vecOut;
      vecOut.x=vec.x*mat[0][0]+vec.y*mat[0][1]+vec.z*mat[0][2]+mat[0][3];
      vecOut.y=vec.x*mat[1][0]+vec.y*mat[1][1]+vec.z*mat[1][2]+mat[1][3];
      vecOut.z=vec.x*mat[2][0]+vec.y*mat[2][1]+vec.z*mat[2][2]+mat[2][3];
    
      vec.x=vecOut.x;
      vec.y=vecOut.y;
      vec.z=vecOut.z;
    }

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Luckily glTranslatef handles that matrix math for me, but thank you for the explanation of some of that math, it's good to know what's going on behind the scenes. Little question. Is it at all sensible to make some sort of object orientation around what I'm going to be doing in opengl (like, for objects that would be drawn between glBegin/End)? I ask, because I gave it a quick google and didn't really find anything, so could it be illogical outside of what I'm doing with just the most basic of basic 2-D things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Object Declarations
    By sean in forum C# Programming
    Replies: 3
    Last Post: 09-26-2004, 10:42 PM
  2. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  3. multiple object instances at runtime
    By mrukok in forum C++ Programming
    Replies: 1
    Last Post: 03-25-2003, 07:26 AM
  4. Animating Multiple Sprites
    By Tommaso in forum Game Programming
    Replies: 2
    Last Post: 10-11-2002, 12:06 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM