CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2005
    Posts
    478

    Animating Multiple Primitives - 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.
    Windows XP, Visual Studio 2008, SVN

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: Animating Multiple Primitives - Open GL

    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.
    Windows XP, Visual Studio 2008, SVN

  3. #3
    Join Date
    Oct 2005
    Posts
    166

    Re: Animating Multiple Primitives - Open GL

    I do not understand your code, what's that Draw(x, y)? What are x and y in that function? The center of draw? Anyway, try to call glLoadIdentity at the beginning of every shape.draw() and remove that push/pop, I think you do not need the matrix stack.

  4. #4
    Join Date
    Aug 2005
    Posts
    478

    Re: Animating Multiple Primitives - Open GL

    Yeah, those draw parameters were not very sensible (they set the position of the object to pass to glTranslatef). So, this is a somewhat related 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.
    Windows XP, Visual Studio 2008, SVN

  5. #5
    Join Date
    Sep 2006
    Posts
    17

    Re: Animating Multiple Primitives - Open GL

    shape s(dp3(0.1f, 0.1f, 0.1f));
    shape p(dp3(0.3f, 0.3f, 0.3f));


    Remove them from void app:: draw ().
    You call each time the constructor and draw figures in the same place.
    Without glPush/PopMatrix you change the centre of coordinates. Therefore goes together and it is accelerated.

    With glPush/PopMatrix you change coordinates of object. And they constantly come back in an initial condition. Therefore acceleration it is impossible.

    Probably.

  6. #6
    Join Date
    Aug 2005
    Posts
    478

    Re: Animating Multiple Primitives - Open GL

    Yes. I have removed glPush/PopMatrix and added glLoadIdentity.

    I have also made a chain of 'object's *' which belong to the window, which is an pure virtual interface which defines (declares?) a draw method that all that derive from it implement, which I loop through in the render routine.

    So that problem solved, is it logical to implement object orientation in my project? I mean, would it be compatiable with thing like lighting, textures, misc. stuff in opengl I don't know about?
    Windows XP, Visual Studio 2008, SVN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured