Sign in to follow this  

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

This topic is 2545 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
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
jyk thanks for response. I have changed my function to operate on array instead of pointer and it works right now, thanks :)

considering the bool arguments of function I just wanted to make opengl equivalent but to use only one axis at a time. Do you have any other ideas how to do that ?

I am going to sleep I will post tomorrow my other translate and scale functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by lordmonkey
considering the bool arguments of function I just wanted to make opengl equivalent but to use only one axis at a time. Do you have any other ideas how to do that ?
It's really up to you, but a typical solution would be to create three separate functions, one for each cardinal axis.

(That said, the function you have there doesn't really equate to OpenGL's glRotate*() function, as the latter accepts an arbitrary axis as input rather than being limited to the three cardinal axes.)

Share this post


Link to post
Share on other sites
ok I have encountered some kind of unknown problem , my function (rotation) is working but sometimes it rotates correctly but after let's say a second the object disappear and I have some artefacts on the screen and my modelview matrix conatins only numbers like 4.4e+38
If i add some cin in the function all work

i have uploaded a video for you to see - the disappearing head in the beginning is rotated with my function and then disappear and artefacts appear.

http://www.youtube.com/watch?v=Dlj3Hd9zrRs

Share this post


Link to post
Share on other sites
Ok, I have found out that all my function do this trick but when I use myTranslate function and I write

glTranslatef(0,0,0); // before multiplication
glMultMatrixf(m.convert2OpenGlMatrix());



then it works like it should.

Share this post


Link to post
Share on other sites

This topic is 2545 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this