Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: 2D rotation

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    18

    2D rotation

    Hi,
    Im writing a small 2D game Engine using SDL and OpenGL.So far its coming good.right now im having a trouble in rotating Images(or textured quads in a more OpenGL context).

    first I had a problem of rotating it around its centre rather than origin but with few googling i found a way.but dont know whether its a proper way though.so this code worked for me:

    Code :
    int Images::drawImage(float x,float y,int width,int height) {
    		if(rotateEnabled){
    			glTranslatef(width/2,height/2,-1.0f);
    			glRotatef(rotAngle,0.0f,0.0f,1.0f);
    			glTranslatef(-(width/2),-(height/2),1.0f);
    			rotAngle+=1;
    		}
    	glBindTexture(GL_TEXTURE_2D, m_handler);
    	glEnable (GL_BLEND);
    	glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    	glBegin(GL_QUADS);
     
    	glTexCoord2f( 0.0f, 1.0f ); glVertex3f( x, y+height, 0 );
    	glTexCoord2f( 1.0f, 1.0f ); glVertex3f( x+width,y+height, 0 );
    	glTexCoord2f( 1.0f, 0.0f ); glVertex3f( x+width,y, 0 );
    	glTexCoord2f( 0.0f, 0.0f ); glVertex3f( x,y, 0 );
     
    	glEnd();
    	glDisable (GL_BLEND);
    	return 0;
    }

    and the rotate function is

    Code :
    int Images::rotateImage(float angle) {
    	rotAngle = angle;
    	rotateEnabled = true;
    	return 0;
    }

    the problem is it is rotating the entire screen (i.e) all the images and sprites as if the screen is rotating.I dont want that.I only want that particular image which is calling the rotate function to rotate. for example only the cannon of the tank should rotate not the entire world.

    also please tell me is there any other way to achieve this.thanks in advance!!

  2. #2
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    The rotation transformation will remain and affect everything else unless you call glLoadIdentity after drawing the object you want to rotate.

  3. #3
    Junior Member Newbie
    Join Date
    May 2013
    Location
    India
    Posts
    13
    void make_scene()
    {
    init();


    glRotatef(scene_zangel,0.0,0.0,1.0);
    glRotatef(scene_xangel,1.0,0.0,0.0);
    glRotatef(scene_yangel,0.0,1.0,0.0);

    glPushMatrix();
    glRotatef(obj1_zangel,0.0,0.0,1.0);
    glRotatef(obj1_yangel,0.0,1.0,0.0);
    glRotatef(obj1_xangel,1.0,0.0,0.0);
    make_object2();
    glPopMatrix();
    glTranslatef(2.0,0.0,0.0);
    glPushMatrix();
    glRotatef(obj2_zangel,0.0,0.0,1.0);
    glRotatef(obj2_yangel,0.0,1.0,0.0);
    glRotatef(obj2_xangel,1.0,0.0,0.0);

    make_object2();
    glPopMatrix();

    glutSwapBuffers();

    }

Posting Permissions

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