Jump to content
  • Advertisement
Sign in to follow this  
Koshmaar

OpenGL glRotatef in 2D

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys I've got little problem with forcing glRotatef to work in my 2d game. Basically, I've got tomatoe bullet, which should rotate (in Z axis) as it's flying through level, and I know that it can be achieved by use of glRotatef. I've already done some experiments on putting glRotatef on the beggining of whole level rendering - the effect was rotating level (it looked funny :-) ). When I put glRotatef before code rendering bullet, with noting to reset after it, whole level was rotating (after all, that's logical). Ok, so I tried adding glLoadIdentity(), another glRotatef with opposite angle, pair of glPush/glPopMatrix(), changing MatrixMode etc. but nothing worked - bullet doesn't rotate, it moves in strange way. It looks like its position on screen is changed, not its position in local coordinate. I'm using this code to render bullet:

  /*        
  // I've tried those (even in combinations)
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  
  glRotatef( angle, 0, 0, 1);
  */

  getRenderer.UseTexture(bulletLogic.picture);

  getRenderer.RenderTexture(pos, bulletLogic.picture.physicalWidth, bulletLogic.picture.physicalHeight, 0, TScrPnt(0, 0),
                            bulletLogic.picture.physicalWidth, bulletLogic.picture.physicalHeight);

/*                            
  glPopMatrix();
  glLoadIdentity();
  glRotatef( angle, 0, 0, 1);

*/


RenderTexture uses immediate mode to render textured quad:

void SC :: Renderer :: RenderTexture(const TScrPoint & _scrPnt, usint _scrWidth, usint _scrHeight, ulint _depth,
                                     const TScrPoint & _texPnt, ssint _texWidth, ssint _texHeight,
                                     bool _flipH, bool _flipW)
 {
 if (_flipH) _texWidth = -_texWidth;
 if (_flipW) _texHeight = -_texHeight;

 float leftMargin = (float)_texPnt.x / (float)boundTexture.width;
 float rightMargin = leftMargin + ((float)_texWidth / (float)boundTexture.width);

 float downMargin = (float)_texPnt.y / (float)boundTexture.height;
 float upMargin = downMargin + ((float)_texHeight / (float)boundTexture.height);
                          
  glBegin(GL_QUADS);
  
    glTexCoord2f(leftMargin, downMargin); 
	glVertex3i(_scrPnt.x, _scrPnt.y + _scrHeight, _depth); // left down
    
	glTexCoord2f(leftMargin, upMargin); 
	glVertex3i(_scrPnt.x, _scrPnt.y, _depth); // left up
	
	glTexCoord2f(rightMargin, upMargin); 
	glVertex3i(_scrPnt.x + _scrWidth, _scrPnt.y, _depth); // right up	
		      
        glTexCoord2f(rightMargin, downMargin); 
	glVertex3i( _scrPnt.x + _scrWidth, _scrPnt.y + _scrHeight, _depth); // right down		
		        		      
  glEnd();

}


And that's how I set OpenGL properties:

  glShadeModel(GL_FLAT);
  glCullFace(GL_BACK);
  glEnable(GL_CULL_FACE);
  glEnable(GL_TEXTURE_2D);
    
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // no specific byte alignment

  glViewport( 0, 0, screenWidth, screenHeight );
         
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  /* 
   Inits orthographical projection. In this mode, screen looks like this:
     (0,0) is left down corner of screen
     (screenWidth-1, screenHeight-1) is upper right corner of screen
  */
  glOrtho (0, screenWidth, 0, screenHeight, 0, -100); // it must be called for MODELVIEW  
  
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();


I'll be glad to hear what I'm doing wrong :-) Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
You should call glTranslate2f() or whatever, to translate the modelview matrix so that you can draw the bullet at the origin.

Then when you call glRotatef, it will rotate around the current origin (which will be the centre of the bullet) rather than another point, which will cause the bullet to appear to spin around rediculously.

Make sure that for each bullet you're calling glPushMatrix() and glPopMatrix() before and after so that the translations don't get compounded.

Another option would be to just do the rotation calculations yourself and change the texture (or vertex) coordinates accordingly.

But translating the modelview matrix would probably be easiest.

Mark

Share this post


Link to post
Share on other sites

Thanks for the help, it's now working perfectly :-) Of course, ++rating.

If anyone is curious, I've changed bullet rendering code to:



glMatrixMode(GL_MODELVIEW);

glPushMatrix();

// pos is now (0, 0), bullet i
glTranslatef(position.x + 7, position.y + 3, 1);

glRotatef( angle, 0, 0, 1);

getRenderer.UseTexture(bulletLogic.picture);

getRenderer.RenderTexture(pos, bulletLogic.picture.physicalWidth, bulletLogic.picture.physicalHeight, 0, TScrPnt(0, 0),
bulletLogic.picture.physicalWidth, bulletLogic.picture.physicalHeight);

glPopMatrix();




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!