I got some problem with OpenGL matrix system.
I made five instances like below. I want to get their individual working.
C_IMGMGR cImgmgr1( 200 , 170 );
C_IMGMGR cImgmgr2( 200 , 170 );
C_IMGMGR cImgmgr3( 200 , 170 );
C_IMGMGR cImgmgr4( 200 , 170 );
C_IMGMGR cImgmgr5( 200 , 170 );
this is main,
int main( int argc, char** argv )
{
int nWinSizeX = 1280;
int nWinSizeY = 768;
glutInit( &argc , argv );
mainWindow = CreateMain( nWinSizeX , nWinSizeY );
SetProjection( nWinSizeX , nWinSizeY );
SetStateSettings();
NowFrameMove();
return 1;
}
and SetProjection()
void SetProjection( int nWinSizeX , int nWinSizeY )
{
cImgmgr1.Init_Viewport( 540 , 599 , nWinSizeX , nWinSizeY );
cImgmgr2.Init_Viewport( 40 , 299 , nWinSizeX , nWinSizeY );
cImgmgr3.Init_Viewport( 540 , 299 , nWinSizeX , nWinSizeY );
cImgmgr4.Init_Viewport( 1040 , 299 , nWinSizeX , nWinSizeY );
cImgmgr5.Init_Viewport( 540 , -1 , nWinSizeX , nWinSizeY );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0 , 1280 , 0.0 , 768 , -10000.0 , 10000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( (200 / 2) , (170 / 2) , 0 );
}
and SetStateSetting()
void SetStateSettings()
{
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_TEXTURE_2D );
cImgmgr1.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr2.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr3.Init_Sprite( "data\\Image\\symbolTemp_Pay\\" );
cImgmgr4.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr5.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
}
here is Update() for glutDisplayFunc() and glutIdleFunc(),
void Update()
{
//////////////////////////////////////////////////////////////////////////
//Process
cImgmgr1.Process( FLIP_VERTICAL );
cImgmgr2.Process( FLIP_HORIZONTAL );
cImgmgr3.Process( NORMAL );
cImgmgr4.Process( NORMAL );
cImgmgr5.Process( NORMAL );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//Render
glClearColor( 0.0F , 0.0F , 0.0F , 0.0F );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearDepth( 1.0 );
cImgmgr1.Render_Sprite( cImgmgr1.Get_Frame() );
cImgmgr2.Render_Sprite( cImgmgr2.Get_Frame() );
cImgmgr3.Render_Sprite( cImgmgr3.Get_Frame() );
cImgmgr4.Render_Sprite( cImgmgr4.Get_Frame() );
cImgmgr5.Render_Sprite( cImgmgr5.Get_Frame() );
//////////////////////////////////////////////////////////////////////////
glutSwapBuffers();
}
and IMGMGR class,
void C_IMGMGR::Process( E_RENDER_MODE eMode )
{
if( m_nFrame >= m_nFileCount )
m_nFrame = 0;
switch ( eMode )
{
case NORMAL :
break;
case FLIP_VERTICAL :
m_fRotationAngle = 1.0F;
m_fRotationX = 1.0F;
break;
case FLIP_HORIZONTAL :
m_fRotationAngle = 1.0F;
m_fRotationY = 1.0F;
break;
case FAN_LEFT :
m_fRotationAngle = 1.0F;
m_fRotationZ = 1.0F;
break;
case FAN_RIGHT :
m_fRotationAngle = 1.0F;
m_fRotationZ = -1.0F;
break;
case TRANSLATE_VERTICAL :
break;
case TRANSLATE_HORIZONTAL :
break;
}
}
void C_IMGMGR::Render_Sprite( int nFrame )
{
glBindTexture( GL_TEXTURE_2D , m_pLoadedSprite[nFrame] );
glViewport( m_nViewPortX , m_nViewPortY , m_nScreenW , m_nScreenH );
glPushMatrix();
glRotatef( m_fRotationAngle , m_fRotationX , m_fRotationY , m_fRotationZ );
glTranslatef( m_fTranslateX , m_fTranslateY , m_fTranslateZ );
glScalef( m_fScaleX , m_fScaleY , m_fScaleZ );
glBegin( GL_QUADS );
glTexCoord2f( 0.0F , 1.0F); glVertex2f( -(float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 1.0F); glVertex2f( (float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 0.0F); glVertex2f( (float)m_nImgX_half , (float)m_nImgY_half);
glTexCoord2f( 0.0F , 0.0F); glVertex2f( -(float)m_nImgX_half , (float)m_nImgY_half);
glEnd();
glPopMatrix();
}
they rotating together that looks like sharing FLIP_VERTICAL and FLIP_HORIZONTAL.
so, how can I separate their matrix for individual instance working?
There are a couple of issues with the posted code:
OpenGL operates as a state machine. When you make a draw call, it uses the state set at the time of the draw call. Therefore, the transformations need to be specified before the draw call if they should be applied to a draw call.
Related, calls like glRotatef()
and glTranslatef()
modify the current transformation matrix. Unless you do something about it, each of these calls combines the specified transformation with the previous current transformation. If you simply make a few of these calls for each draw call, all the transformations will be combined.
To fix these two problems, you need to change the order of the calls, and use glPushMatrix()
and glPopMatrix()
to save/restore the previous transformation when you apply the transformations for a specific draw call. The code will then look like this:
glPushMatrix();
glRotatef( m_fRotationAngle , m_fRotationX , m_fRotationY , m_fRotationZ );
glTranslatef( m_fTranslateX , m_fTranslateY , m_fTranslateZ );
glScalef( m_fScaleX , m_fScaleY , m_fScaleZ );
glBegin( GL_QUADS );
glTexCoord2f( 0.0F , 1.0F); glVertex2f( -(float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 1.0F); glVertex2f( (float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 0.0F); glVertex2f( (float)m_nImgX_half , (float)m_nImgY_half);
glTexCoord2f( 0.0F , 0.0F); glVertex2f( -(float)m_nImgX_half , (float)m_nImgY_half);
glEnd();
glPopMatrix();
I hope you are already aware that almost all of the OpenGL calls used in this code are deprecated, and not available anymore in modern versions of OpenGL. If you want to learn current OpenGL, look for tutorials for the OpenGL Core Profile, or for OpenGL ES 2.0 or later.