# [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.

## 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 on other sites
Take a look onto glLoadMatrix and glMultMatrix. That is if you're dealing with fixed pipeline (i.e no shaders)

##### Share on other sites
Quote:
 Original post by lordmonkeyI 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.

##### 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 on other sites
Translation, rotation, and scaling can be expressed entirely by matrices which you can multiply appropriately to get the same effect that OpenGL gets:

Quick Reference I found in Google

##### Share on other sites
Quote:
 Original post by lordmonkeyno, 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 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 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
{
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 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 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 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 on other sites
Quote:
 Original post by lordmonkeyconsidering 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 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.

##### 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628724
• Total Posts
2984404

• 25
• 11
• 10
• 16
• 14