Jump to content
  • Advertisement
Sign in to follow this  
lordmonkey

[OpenGL] How to make my own version of glTranslate etc.?

This topic is 2758 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,
I have to make my own version of glTranslatef, glRotatef and glScalef. I have already my very own math library with matrices, quaternions etc. Can anyone help me with this ? Any ideas ?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by lordmonkey
I have to make my own version of glTranslatef, glRotatef and glScalef. I have already my very own math library with matrices, quaternions etc.
Presumably your math library allows you to translate, rotate and scale matrices, no? In that case, you are done - all glTranslate/Rotate/Scale do is to perform that matching operation on the current matrix.

Just translate/scale/rotate your own matrices, and upload them to OpenGL with glLoadMatrixf().

Share this post


Link to post
Share on other sites
no, my library doesn't translate, scale nor rotate because it consists only of classes of vectors, matrices, and quaternions and ofc methods that these classes need e.g. transforming matrix to quaternion etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by lordmonkey
no, my library doesn't translate, scale nor rotate because it consists only of classes of vectors, matrices, and quaternions and ofc methods that these classes need e.g. transforming matrix to quaternion etc.
The creation of translation, rotation and scaling matrices are all basic matrix operations, and you can find references for them all over the internet. glTranslate/Rotate/Scale just create the relevant translation/rotation/scaling matrix, and then multiply the current matrix by it (because multiplication concatenates matrix transform).

If you are going to do this type of math yourself, you need to have a basic understanding of the concepts involved. I would recommend picking up a linear algebra textbook and reading through the relevant sections. Or for a softer introduction, try Wolfire's overview of matrix transformations.

Share this post


Link to post
Share on other sites
You may have your own math lib, but I strongly recommend using CML or some other math lib. I had my own math lib and came to terms that others had far more knowledge on the subject and got sick of bugs/errors so I decided to use a stable math lib that works...

http://cmldev.net/

Share this post


Link to post
Share on other sites
Ok, I have came up with something like this ( I still need some help):
I calculate matrices as on this site http://www.ruthless.zathras.de/facts/apps/polygonesia/3d-transformation-matrix.php

void myRotate(float angle, bool xRot, bool yRot , bool zRot) // take angle in degrees
{
angle = DEG2RAD(angle);
Matrix m(4);
if(xRot)
{
m.getArray()[0][0] = 1;
m.getArray()[1][1] = cos(angle);
m.getArray()[1][2] = sin(angle);
m.getArray()[2][1] = -1*sin(angle);
m.getArray()[2][2] = cos(angle);
m.getArray()[3][3] = 1;
}
else if(yRot)
{
m.getArray()[0][0] = cos(angle);
m.getArray()[0][2] = 1*sin(angle);
m.getArray()[1][1] = 1;
m.getArray()[2][0] = sin(angle);
m.getArray()[2][2] = cos(angle);
m.getArray()[3][3] = 1;
}
else if(zRot)
{
m.getArray()[0][0] = cos(angle);
m.getArray()[0][1] = sin(angle);
m.getArray()[1][0] = -1*sin(angle);
m.getArray()[1][1] = cos(angle);
m.getArray()[2][2] = 1;
m.getArray()[3][3] = 1;
}
cout << m;

glMultMatrixf(m.convert2OpenGlMatrix());
}


then in the end of this function I use convert2OpenGlMatrix() which convert my 4x4 matrix to float * with 16 floats.(and put it in column major form as OpenGl uses it )

float* Matrix::convert2OpenGlMatrix(int size )// for now works only for matrices size 4
{
float * a = new float[16];
for(int i = 0; i < 16; ++i)
{
*a = this->getArray()[i%4][i/4];
a++;
}
return a;
}

but after putting myRotate( 5 , 1 , 0 , 0 ); in the code nothing is displayed on the screen.

BTW why there is no code tag on this forum ? ( and no post preview ? )

Share this post


Link to post
Share on other sites
Ok I have something like this:


first is original modelview matrix, second is my transformation matrix and the last one is modelview matrix after calling glMultMatrix with my transformation matrix as argument.

Share this post


Link to post
Share on other sites
Quote:
BTW why there is no code tag on this forum ?
[source][/source] (you can also use the HTML 'pre' tag for small bits of code).
Quote:
( and no post preview ? )
Right below the post content box.
Quote:
Ok, I have came up with something like this
Couple of observations:

- Although it's unlikely to make any difference performance-wise, you can avoid computing the sine and cosine redundantly be pre-computing and storing each, and then using the stored value.
- Instead of '-1*sin(angle)', you can just write '-sin(angle)'.
- The way your function is formulated doesn't make much sense, in that there are three 'rot' arguments (one for each axis), yet it only makes sense for one of them to be true. Better to create separate functions or to use a different method for specifying the axis of rotation, IMO.
Quote:
then in the end of this function I use convert2OpenGlMatrix() which convert my 4x4 matrix to float * with 16 floats.(and put it in column major form as OpenGl uses it )
OpenGL actually doesn't require that the matrix be in column-major format (or at least the fixed-function pipeline doesn't); it simply requires that the elements of the matrix be contiguous in memory, and that the elements of each basis vector of the transform be contiguous in memory.
Quote:
float* Matrix::convert2OpenGlMatrix(int size )// for now works only for matrices size 4
{
float * a = new float[16];
for(int i = 0; i < 16; ++i)
{
*a = this->getArray()[i%4][i/4];
a++;
}
return a;
}
You almost certainly don't want to do it this way. At worst you'll leak memory, and at best you'll introduce an unnecessary and potentially expensive allocation and deallocation every time you want to access the matrix data as a 1-d array.

What I'd recommend is to store the matrix data in a 1-d array to begin with (or at least in a form that can be interpreted as such); that way, no conversion will be necessary.
Quote:
but after putting myRotate( 5 , 1 , 0 , 0 ); in the code nothing is displayed on the screen.
There's a million ways to display nothing onscreen in OpenGL and Direct3D :) As such, it's hard to say what the problem might be based on the information presented so far.

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!