Concept Clarification - Transformations

Started by
2 comments, last by Cirus 17 years, 11 months ago
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.
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?
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
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, pfArray ); <br> glVertex3f(pfArray<span style="font-weight:bold;"> + 0.25, pfArray, pfArray - 0.5 );<br> <br> ++i;<br> <br> }<br> glEnd();<br> glPopMatrix();<br> i = 0; //Reset counter.<br> <br> if(bFlag_LineDraw)<br> {<br> glMatrixMode(GL_MODELVIEW); <br> glLoadIdentity();<br> glPushMatrix();<br> glRotatef(fAngle_Y , 0.0 , 1.0 , 0.0); //Move left <br> glRotatef(fAngle_X , 1.0 , 0.0 , 0.0); //Move above <br> glRotatef(fAngle_Z , 0.0 , 0.0 , 1.0); //Move above <br> glBegin(GL_LINES); <br> while( i != iNumOfClicks)<br> { <br> glColor3f(1.0 , 0.0 , 1.0); <br> glVertex3f(pfArray<span style="font-weight:bold;"> + 0.25, pfArray, pfArray ); <br> glVertex3f(pfArray<span style="font-weight:bold;"> + 0.25, pfArray, pfArray - 0.5 );<br> i++;<br> }<br> glEnd();<br> glPopMatrix();<br><br> bFlag_LineDraw = FALSE;<br> }<br>—————————————————————————– <br><br>This is the function that is converting viewport coordinates to world coordinates {-1,+1] range<br>—————————————————————————-<br>void MarkPointOnScreen(int iX , int iY, int iWinHt , int iWinWidth, float *pfArray)<br>{<br> //Draw the point &#111;n screen.<br> float fx = 0.0f , fy = 0.0f;<br> if( iX &gt; (iWinWidth/2) )<br> {<br> fx = ( (float)iX - (float)iWinWidth/2 ) / (float)(iWinWidth/2);<br> }<br> else if( iX == iWinWidth/2)<br> fx = 0.0f;<br> else<br> {<br> <br> fx = -1 * ( (float)iWinWidth/2 - (float)iX ) / (float)(iWinWidth/2);<br> <br> }<br> <br> if(iY &gt; iWinHt/2)<br> {<br> <br> fy = -1 * ( (float)iY - (float)(iWinHt/2) ) / (float)(iWinHt/2);<br> }<br> else if(iY == iWinHt/2)<br> fy = 0.0f;<br> else<br> {<br> <br> fy = ( (float)(iWinHt/2) - (float)iY ) / (float)(iWinHt/2);<br> <br> }<br> <br> <br> pfArray[iEle++] = fx;<br> pfArray[iEle++] = fy;<br> pfArray[iEle++] = 0.0f;<br> <br>printf("\niX = %d and iY = %d , fx = %f and fy = %f , Clicks = %d \n", iX,iY,fx ,fy, iNumOfClicks);<br>}

This topic is closed to new replies.

Advertisement