Jump to content
  • Advertisement
Sign in to follow this  
Cirus

OpenGL Concept Clarification - Transformations

This topic is 4440 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

Hello All, I am newto OpenGl and have been refering to The Red Book... for a few days. The book says that you can either view al transformations by taking coordinate axes as fixed or in terms of local coordinate system( tied to a rendered object). Question 1. : the glLoadIdentity maps a window to a [-1,+1] scale ( for each X,Y and Z). Now if I perfrom following glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); glRotatef(90.0 , 0.0 ,1.0 , 0.0) ; //Rotation around Y axis , anticlockwise, Will my -X (negative X axis) take on the position of +Z axis and + Z axis replaces +X axis and +X axis replace -Z axis ??? Question 2: My second question reuires the answer of Ques 1 because I am aiming to construct an external control mesh , housing the object. The object I am taking is a cube such that the origin of the axes lie at the centroid of the cube. I am planning to contruct the external control mesh by asking user to mark points near to object , so that the points becomes lines( when I rotate the object). Problem is: At default Y axis is up-down, X is sideways and Z is front-back. When I do anticlockwise rotation across Z and mark point (by calling glVertex3f), the line so drawn is not horizontal but vertical . Any suggestions on my queries? I will post the code also , but need to get a suggestion first. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Cirus
Question 1. : the glLoadIdentity maps a window to a [-1,+1] scale ( for each X,Y and Z). Now if I perfrom following

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(90.0 , 0.0 ,1.0 , 0.0) ; //Rotation around Y axis , anticlockwise,

Will my -X (negative X axis) take on the position of +Z axis and + Z axis replaces +X axis and +X axis replace -Z axis ???


Yes; you should really do the math on some paper to get a better feel for what's actually happening, though.

glRotatef(90, 0, 1, 0) puts this matrix

[ 0 0 1 0 ]
[ 0 1 0 0 ]
[ -1 0 0 0 ]
[ 0 0 0 1 ]

onto the modelview matrix stack (it is postmultiplied by the current contents of the modelview matrix, which happens to be the identity matrix now). Try multiplying (column) vectors with this matrix to get a good feel.

Quote:
Original post by Cirus
Question 2: My second question reuires the answer of Ques 1 because I am aiming to construct an external control mesh , housing the object.
The object I am taking is a cube such that the origin of the axes lie at the centroid of the cube.

I am planning to contruct the external control mesh by asking user to mark points near to object , so that the points becomes lines( when I rotate the object).

Problem is: At default Y axis is up-down, X is sideways and Z is front-back.
When I do anticlockwise rotation across Z and mark point (by calling glVertex3f), the line so drawn is not horizontal but vertical .


I'm not really sure what you mean by this, perhaps you can rephrase your issue?

Share this post


Link to post
Share on other sites
Quote:
Original post by Cirus
Problem is: At default Y axis is up-down, X is sideways and Z is front-back.
When I do anticlockwise rotation across Z and mark point (by calling glVertex3f), the line so drawn is not horizontal but vertical .


Im not sure what your saying here but im going to take a guess..

When you say rotation 'across Z' are you doing:
glRotatef(angle, 0.0, 0.0, 1.0);

If this is the case then your actually rotating around the Z-axis. In this situation if you rotate and plot a point then rotate a little bit more and plot another point (possibly to draw a line?) then it would appear that the points are moving vertically (actually vertically and horizontally), in actual fact if you continued like this you would end up drawing a circle.

To plot points across the Z axis you need to rotate around the Y-axis (as you did in Q1), but bear in mind the points would also be plotted across the X-axis as well as the Z-axis.

Im not sure if that helps at all, if not then re-phrase the question.

Happy Coding

Share this post


Link to post
Share on other sites
Regarding Qeus 2:

I am given a task to construct an external contrl mesh around my object.


How do I do this? :
I take the object , and mouse click near to its edges and expect a point there.
This point is not a point but actually a line ( Imagine a line when placed in front can be viewed as a point ) .

To be precise , the line points in +Z direction that is out of computer screen.

Now , after this , I am rotating my object ( coordinate axes) in anti-clock wise Y or X direction .

After performing a rotation the point (the tip of the line ) will be viewed as a line.

Problem:
If a rotate the object around +Z axis and then mouse click I am getting the point at some different position on the screen other than the expected position.

For reference please follow the code below:
--------------------------------------------------------------------------
void drawPoint()
{
int i =0 ;

glEnable(GL_DEPTH_TEST);
// Set the material color to follow the current color

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glRotatef(fAngle_Y , 0.0 , 1.0 , 0.0); //Move left
glRotatef(fAngle_X , 1.0 , 0.0 , 0.0); //Move above
glRotatef(fAngle_Z , 0.0 , 0.0 , 1.0); //Move above

glBegin(GL_POINTS);

while( i != iNumOfClicks)
{
glColor3f(1.0 , 0.0 , 1.0);
glVertex3f(pfArray + 0.25, pfArray[i + 1], pfArray[i + 2] );
glVertex3f(pfArray + 0.25, pfArray[i + 1], pfArray[i + 2] - 0.5 );

++i;

}
glEnd();
glPopMatrix();
i = 0; //Reset counter.

if(bFlag_LineDraw)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(fAngle_Y , 0.0 , 1.0 , 0.0); //Move left
glRotatef(fAngle_X , 1.0 , 0.0 , 0.0); //Move above
glRotatef(fAngle_Z , 0.0 , 0.0 , 1.0); //Move above
glBegin(GL_LINES);
while( i != iNumOfClicks)
{
glColor3f(1.0 , 0.0 , 1.0);
glVertex3f(pfArray + 0.25, pfArray[i + 1], pfArray[i + 2] );
glVertex3f(pfArray + 0.25, pfArray[i + 1], pfArray[i + 2] - 0.5 );
i++;
}
glEnd();
glPopMatrix();

bFlag_LineDraw = FALSE;
}
-----------------------------------------------------------------------------

This is the function that is converting viewport coordinates to world coordinates {-1,+1] range
----------------------------------------------------------------------------
void MarkPointOnScreen(int iX , int iY, int iWinHt , int iWinWidth, float *pfArray)
{
//Draw the point on screen.
float fx = 0.0f , fy = 0.0f;
if( iX > (iWinWidth/2) )
{
fx = ( (float)iX - (float)iWinWidth/2 ) / (float)(iWinWidth/2);
}
else if( iX == iWinWidth/2)
fx = 0.0f;
else
{

fx = -1 * ( (float)iWinWidth/2 - (float)iX ) / (float)(iWinWidth/2);

}

if(iY > iWinHt/2)
{

fy = -1 * ( (float)iY - (float)(iWinHt/2) ) / (float)(iWinHt/2);
}
else if(iY == iWinHt/2)
fy = 0.0f;
else
{

fy = ( (float)(iWinHt/2) - (float)iY ) / (float)(iWinHt/2);

}


pfArray[iEle++] = fx;
pfArray[iEle++] = fy;
pfArray[iEle++] = 0.0f;

printf("\niX = %d and iY = %d , fx = %f and fy = %f , Clicks = %d \n", iX,iY,fx ,fy, iNumOfClicks);
}

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!