Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sunset ru

Coordinates of vertex after glRotate.

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

At first, sorry me for my bad english. I have a function in a c#, that make's a 3d cube (or something that, don't know how to name it correctly, maybe parallelepiped):
		public static void Cube(float x, float y, float z, float d_x, float d_y, float d_z) 
		{
			GL.glBegin(GL.GL_QUADS);
			// licevaja perednjaja
			GL.glVertex3f(x-d_x,y-d_y,z-d_z);
			GL.glVertex3f(x-d_x,y+d_y,z-d_z);
			GL.glVertex3f(x+d_x,y+d_y,z-d_z);
			GL.glVertex3f(x+d_x,y-d_y,z-d_z);
			// licevaja zadnjaja
			GL.glVertex3f(x-d_x,y-d_y,z+d_z);
			GL.glVertex3f(x-d_x,y+d_y,z+d_z);
			GL.glVertex3f(x+d_x,y+d_y,z+d_z);
			GL.glVertex3f(x+d_x,y-d_y,z+d_z);
			// bokovaja levaja
			GL.glVertex3f(x-d_x,y-d_y,z-d_z);
			GL.glVertex3f(x-d_x,y+d_y,z-d_z);
			GL.glVertex3f(x-d_x,y+d_y,z+d_z);
			GL.glVertex3f(x-d_x,y-d_y,z+d_z);
			// bokovaja pravaja
			GL.glVertex3f(x+d_x,y-d_y,z-d_z);
			GL.glVertex3f(x+d_x,y+d_y,z-d_z);
			GL.glVertex3f(x+d_x,y+d_y,z+d_z);
			GL.glVertex3f(x+d_x,y-d_y,z+d_z);
			// verhnaja niznjaja
			GL.glVertex3f(x-d_x,y-d_y,z-d_z);
			GL.glVertex3f(x-d_x,y-d_y,z+d_z);
			GL.glVertex3f(x+d_x,y-d_y,z+d_z);
			GL.glVertex3f(x+d_x,y-d_y,z-d_z);
			// verhnaja verhnjaja
			GL.glVertex3f(x-d_x,y+d_y,z-d_z);
			GL.glVertex3f(x-d_x,y+d_y,z+d_z);
			GL.glVertex3f(x+d_x,y+d_y,z+d_z);
			GL.glVertex3f(x+d_x,y+d_y,z-d_z);
			GL.glEnd();
		}
   
here are 6 arguments in a function: x, y and z - where from is this cube will be draw; d_x, d_y and d_z - distance between x, y and z and -+d_x, -+d_y and -+d_z. So, next i want to rotate this "Cube". How i do that?
GL.glRotatef(angle,0.0f,1.0f,0.0f);
Cube(0,0.2f,0,0.5f,0.2f,1.0f);
Cube(0.6f,0.2f,0,0.1f,0.2f,1.0f);
Cube(-0.6f,0.2f,0,0.1f,0.2f,1.0f);
Cube(0,0.6f,0,0.3f,0.2f,0.5f);
Cube(0,0.7f,-1.0f,0.05f,0.05f,0.5f);
   
All is OK, but I need to know (for a collision) where all of the vertex in the parallelepiped are be, after the rotate. All x, y, z coordinates i need (maybe without y, because it isn't change). I know that, this question was at this forum, but there are answers are so bad, so i can't understand, what i would to do. Please, help me, and sorry for my bad english. [edited by - sunset ru on June 1, 2004 9:44:04 AM] [edited by - sunset ru on June 1, 2004 9:45:18 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
In other words,what you want to do is to be able to transform the vertices in software,so you can store the results.It''s pretty simple,really.I''ll give you an example:

typedef float TVector[4];//4-component vector
typedef TVector TMatrix[4];//4x4 matrix represented by 4 vectors

float Dot4(float v1[],float v2[])
{
return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]+v1[3]*v2[3];
}

void ApplyMatrix(TMatrix &m,TVector &v)
{TVector temp;
for (int i=0;i<4;++i) temp=v[i];
v[0]=Dot4(m[0],temp);
v[1]=Dot4(m[1],temp);
v[2]=Dot4(m[2],temp);
v[3]=Dot4(m[3],temp);
}

You can now use the ApplyMatrix to transform any 4D vector using a 4x4 matrix.The 4th component of the vector is the homogenous coordinate and it''s 1 for vertices,0 for directions(usually normals).

Now if you want to transform vertices using you modelview matrix,so the results you get matches the one you get on the screen,all you have to do is this:

TVector v;
TMatrix mv;

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0][0]);//Get the current modelview matrix

ApplyMatrix(mv,v);




Share this post


Link to post
Share on other sites
Here is another problem (with matrix didn't understand how-to):
private static void Cube(float x, float y, float z, float d_x, float d_y, float d_z, double angle) 
{
float cos = (float)Math.Cos(degrees(angle));
float sin = (float)Math.Sin(degrees(angle));
GL.glBegin(GL.GL_QUADS);

GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);
GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);
GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);
GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos);

GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos);
GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);
GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);
GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin);

GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);
GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);
GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);
GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos);

GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos);
GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);
GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);
GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin);

GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);
GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos);
GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin);
GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos);

GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);
GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);
GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);
GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);

GL.glEnd();
}

Something wrong. And I know what is, but can't fix it. This formulas are good for cube, but for parallelepiped isn't.
How to fix it, or try to help me with this matrixes..

[edited by - sunset ru on June 1, 2004 1:05:58 PM]

Share this post


Link to post
Share on other sites
Hiya. I would advise you to do it AP's (Anonymous Poster's) way. Any transformation - rotation, translation, scaling - is done using matrices. For example, the MODELVIEW matrix is the one that is modified to do the transformations, the PROJECTION matrix is the one that is used to hold the projection, the TEXTURE ones can be used to mess around with the textures. Even though there are 3 coordinates (x, y, z), matrices in OpenGL are 4x4 (4 rows, 4 columns). (Read up on this if you want to know why.) Because a matrix is 4x4, the vector, which is holding only 3 coordinates, needs to hold 4 values so it can be modified by the matrix - so the transformations can be applied to it. Now, each side of your cube is made up of 4 vertices - AP used a vector in the example because vectors hold the same basic information (4 values) but they have different operations that are involved in using them. To do whatever transformation, you call whatever OpenGL function. For example, to do rotation, you call glRotate*(), to do a translation, you call glTranslate*(), to do scaling, you call glScale*(). What each of these functions does is modify the current matrix (which should be the MODELVIEW matrix if you want the transformations to be applied to the objects in your scene). The vertices you then supply to glVertex*() are then multipled by the current matrix so that your transformations are applied. So, to know the coordinates or vertex information after you apply a transformation, all you have to do is store your original vertex (or vector as AP had it) as a set of 4 values, the first 3 being the x, y, z coordinates, and the 4th value being, as AP said, 1 for vertices or points, and 0 for vectors or directions (normals and such). You apply your transformations via glTranslate*(), glRotate*(), etc. and then you get the MODELVIEW matrix which holds all of your transformations via glGetFloat*(...) like AP outlined. You then apply this matrix to your original vertex to get the position where each vertex is drawn on the screen - you can do this via AP's ApplyMatrix function or make one of your own.

I hope this helps you. If some of the english in here is tough, let me know (you can do it via email if you like, just click on the email link above this post to get my e-mail) and I'll try and word it better for you. In any case, I really recommend that you learn the Linear Algebra of vectors, vertices, matrices, etc if you haven't already. Good luck!

~Urayami

[edited by - urayami on June 1, 2004 2:32:55 PM]

Share this post


Link to post
Share on other sites
Although the OP seems to be a bit lost, APs post helped me quite a bit. I started another thread on how to access the matrix stack to retrieve x, y, z positions, and the answer was right here all along. Thanks.

Just to be sure I got it here.. All I do is use the matrix stack for transformations, ie;

glPushMatrix();
glLoadIdentity();
glRotatef(...);
glTranslatef(...);

TVector v;
TMatrix mv;

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0][0]);
// TVector v now contains x, y, z position of the transformed matrix?

glPopMatrix();

I'm rusty at math and not too great at it, so I'm just making sure I understand what's going on with the calculations. Basically you're multiplying vector by the matrix to get a new vector containing the x, y, z values?

It seems for this to work properly I'd have to load the vector (v) with all 1's first, right?

Meh, like I said, my math sucks. Maybe I'm missing something here.

[edited by - wyrd on June 2, 2004 6:03:05 PM]

[edited by - wyrd on June 2, 2004 6:03:56 PM]

Share this post


Link to post
Share on other sites

  • 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!