This topic is 4043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I am having a bitter problem, my program has the following stucture in a class, list is member of the class GLWidget: void GLWidget::initalizeGL() { qglClearColor(trolltechPurple.blue()); glShadeModel(GL_FLAT); glEnable( GL_DEPTH_TEST); glFrontFace(GL_CW); list= makeList(); } void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScaled(zoom, zoom, zoom); glRotated(xRot / 16.0, 1.0, mid, mid); glRotated(yRot / 16.0, mid, 1.0, mid); glRotated(zRot / 16.0, mid, mid, 1.0); draw(list1, true, 0.0, 0.0, 0.0, -2.0 * (zRot / 16.0) - 9.0, 1, 1); } GLuint GLWidget::makeObject() { GLuint list; list = glGenLists(1); glNewList(list, GL_COMPILE); glBegin(GL_LINE_STRIP); do some drawing........ glEnd(); glPushMatrix(); glMatrixMode(GL_MODELVIEW_MATRIX); glLoadMatrixd(mat); // mat is my own 16 integer array matrix glBegin(GL_LINE_STRIP); do some drawing........ glEnd(); glPopMatrix(); glEndList(); return list ; } void GLWidget::draw(GLuint list, bool model_trafo, GLdouble dx, GLdouble dy, GLdouble dz, GLdouble angle, GLdouble scale_x, GLdouble scale_y) { glPushMatrix(); glTranslated(mx, my, mz); glCallList(list); glPopMatrix(); } void GLWidget::setZRotation(int angle) { normalizeAngle(&angle); if (angle != zRot) { zRot = angle; emit zRotationChanged(angle); updateGL(); } } Now it works perfectly fine, the rotation for the drawing i draw Without the block of glLoadMatrix. And transformation, rotation, scaling etc work fine that drawing. But NOT ON THE DRAWING DRAWN WITH MY OWN TRANSFORMATION MATRIX LOADED WITH glLoadMatrix. Note that PaintGL() is called each time the UpdateGL() is called. Can any body help me ? Thanks.

##### Share on other sites
Is your matrix a one dimension column major order?

##### Share on other sites
My matrix is column major matrix.

Like
double mat[16];

mat[0]=temp[0];
mat[1]=temp[1];
mat[2]=Z;
mat[3]=0;
mat[4]=temp[2];
mat[5]=temp[3];
mat[6]=Z;
mat[7]=0;
mat[8]=0;
mat[9]=0;
mat[10]=1;
mat[11]=0;
mat[12]=trans_mat.ef.xp;
mat[13]=trans_mat.ef.yp;
mat[14]=Z;
mat[15]=1;
Problem i have is that i want to change transformation without calling whole drawing function each time. I want the change of transformation by just calling glScale or glRotate() and then calling the display list. THis works fine if i dont load my own matrix.

##### Share on other sites
if mat[2]=Z; then this is row major.

##### Share on other sites
or some combination of both as the translation lives in 12 13 14 15

##### Share on other sites
or some combination of both as the translation lives in 12 13 14 15

##### Share on other sites
basically Z is always 0. I may also can write

mat[0]=temp[0];
mat[1]=temp[1];
mat[2]=0;
mat[3]=0;
mat[4]=temp[2];
mat[5]=temp[3];
mat[6]=0;
mat[7]=0;
mat[8]=0;
mat[9]=0;
mat[10]=1;
mat[11]=0;
mat[12]=trans_mat.ef.xp;
mat[13]=trans_mat.ef.yp;
mat[14]=0;
mat[15]=1;

12,13,14 are translation coordinates. So is it correct to load this mat like

??

##### Share on other sites
IMHO whether the matrix is in row or column major format is not directly obvious since the definition of the local variables used is unkown. mat[2] in column major order is xz, i.e. the z co-ordinate of the x vector, so assigning Z may be okay. However, assigning Z to both the rotational/scaling part (mat[2], mat[6]) but also the translational part (mat[14]) is curious. Could you please explain what those local variables should denote?

EDIT: Yes, [12] to [14] store the translational part for OpenGL.

##### Share on other sites
Basically i am converting a 2D matrix into 3D with Z=0. And for this, i have the following matrix.

mat[0]=temp[0];
mat[1]=temp[1];
mat[2]=0;
mat[3]=0;
mat[4]=temp[2];
mat[5]=temp[3];
mat[6]=0;
mat[7]=0;
mat[8]=0;
mat[9]=0;
mat[10]=1;
mat[11]=0;
mat[12]=trans_mat.ef.xp; //X coordinate
mat[13]=trans_mat.ef.yp; // Y coordinate
mat[14]=0; // Z = 0
mat[15]=1;

temp contains the 2D transformation matrix, and trans_mat.ef.* has translation vector.

##### Share on other sites
Not necessarily the one problem currently looked at, but: The order of glPushMatrix and glMatrixMode inside makeObject seems me wrong.
GLuint GLWidget::makeObject() {  GLuint list; list = glGenLists(1); glNewList(list, GL_COMPILE);  glBegin(GL_LINE_STRIP); do some drawing........ glEnd(); /// the following two lines should be exchanged IMHO glPushMatrix(); glMatrixMode(GL_MODELVIEW_MATRIX); glLoadMatrixd(mat); // mat is my own 16 integer array matrix glBegin(GL_LINE_STRIP); do some drawing........ glEnd(); glPopMatrix();  glEndList(); return list ; }

The current order would make problems only if another matrix than MODELVIEW is active when this routine is called. However, it would be better to clarify the code at all.

##### Share on other sites
Thanks for anticipation! MatrixMode is not the problem , even i tried. The current matrix mode is already in ModelviewMode.

the problem i have is, i want some thing like this

---------------
Do standard intialization of OpenGL...
Create and BeginList list1;
Add some drawing commands in the display list1 with default transformation Modelview matrix (1)
add more more drawing commands with custom modelview in the same display list1 (this draws fine)(2)
End Display list1
---------------

The paint function, which is called on each paintwindow call,
--------------------
do some translation/transformation
Call list1;
---------------------

And later, on a mouse handler function ,

-----------------
Clear every thing ;
some calls to gltranslate, glRotate, etc
Call the display list1 again
----------------------

Now in the mouse handler function, the rotation/scaling/transformation applied to the Current modelview matrix are reflected in the drawing at level. But not in the drawing drawn at level 2.
The drawing at level 2 remeins always static..does not moves or accepts any transformation further applied.
How could i do so ?

The problem seem to me that, the modelview matrix i load, does not accept transformations later, like calls to glRotate etc. Why could it be so ?

Any idea?

##### Share on other sites
Ah, now I see the problem :)

Notice that glLoadMatrix function _replaces_ the current matrix! Perhaps you want to apply a glMultMatrix instead, hence applying the 2nd transformation w/o dropping the 1st one. But notice that in that case the transformation of stage (2) local, i.e. it is applied before the current MODELVIEW matrix when entering the routine.

##### Share on other sites
Quote:
 Original post by TahirThe problem seem to me that, the modelview matrix i load, does not accept transformations later, like calls to glRotate etc. Why could it be so ?

To clarify my previous post a bit: If you apply a composed transformation like this one (notice the order!)
glTranslatef(...);
glRotatef(...);
glVertex3f(p.x,p.y,p.z);
to a point p then its mathematical equivalent is (notice that I use column vectors as usual for OpenGL)
T * R * p
where T denotes the translation and R the rotation, then the effect is that the point p is 1st rotated by R and the result is then translated by T (although OpenGL does this in 1 step).

Hence, if you do this
glTranslatef(...);
glRotatef(...);
glMultMatrix(...);
glVertex3f(p.x,p.y,p.z);
then your own matrix is applied 1st, and the already pushed translation and rotation acts on the result your own transformation has left.

[Edited by - haegarr on November 24, 2006 7:19:58 AM]

##### Share on other sites
Great !

It works now :)

Thanks alot haegarr.

[Edited by - Tahir on November 24, 2006 8:46:19 AM]

##### Share on other sites

This topic is 4043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.