Help to set gluperspective. How to fit every model on screen?

… when a rotate in x and after in y, at the moment of the second roration there is a new x axis, different from original.
Not sure what you mean. Do you understand Push and PopMatrix? In the code below, the modeling transformations on lines AAAAA, BBBBB, and CCCCC would only be applied to Object_A. Without the Push and Pop statements the transformations would be applied to objects A and B. Does this help you out?


   glPushMatrix ();
      glTranslatef ();               // AAAAA
      glRotatef ();                  // BBBBB
      glScalef ();                   // CCCCC
      Draw_Object_A ();
   glPopMatrix ();

   Draw_Object_B ();

Hey! Thank you (agaaaain :)) for your answer.

Yes. I understand Push/Pop, but actually I don’t draw two objects… I draw and rotate the same object.

The problem is that i need to store how much I am rotating (in angle degrees). For that I am using the variables “rotacionarx”, “rotacionary” and “rotacionarz” (as the code below), but I am not storing it right, because when I apply the stored angles it is messed up.

glRotatef(rotacionarx, 1.0f, 0.0f, 0.0f);
glRotatef(rotacionary, 0.0f, 1.0f, 0.0f);
glRotatef(rotacionarz, 0.0f, 0.0f, 1.0f);

The reference (x,y,z) is changing when I rotate and that is why I am storing the values wrongly. I need the reference axis to be fixed just as you did with the darth vader :slight_smile:

Someone told me that the problem is related to the matrix property AB != BA and that I can’t use glRotatef because it rotates one axis per time.
For solving this I shouldn’t use glRotatef and build my own rotation matrix. I’ve been working on it and trying to understand how it works, but didn’t get it yet…

I also found something like this :

float radAngle = (rotacionarx / 180.f)*3.1415f;
glRotatef(rotacionarx, 1.f, 0.f, 0.f);
glRotatef(rotacionary, 0.f, cos(radAngle), -sin(radAngle));

Which is kind of compensation for the “reference changing” but I could not apply to my project.

Thank you very much for your help and generosity. I hope I am not bothering you too much :smiley:

edit: I just read my problem name is gimble lock

Someone told me that the problem is related to the matrix property AB != BA and that I can’t use glRotatef because it rotates one axis per time. For solving this I shouldn’t use glRotatef and build my own rotation matrix.
This might be your problem. If so, you can still use glRotatef, if done correctly.

I just read my problem name is gimble lock
I doubt the problem is gimbal lock.

But I still don’t understand your problem. Rather than trying to explain it in general terms, can you give us a specific example of what you want to happen, and what actually happens. I’m sure the forum could help you out if you did that. It would also help if you included just the portion of your code where the rotations are applied.

This is my problem:


After each rotation I have a new reference.

I need to rotate the object and keep the reference the same :slight_smile:


void display(GLFWwindow* window)
{	
	static int first = 1;

	while (!glfwWindowShouldClose(window))
	{
		// Scale to window size
		GLint windowWidth, windowHeight;
		glfwGetWindowSize(window, &windowWidth, &windowHeight);
		glViewport(0, 0, windowWidth, windowHeight);

		// Draw stuff


		glClearColor(212.0f/255.0f, 208.0f/255.0f, 200.0f/255.0f, 1.0f); //255  r 236 g 233 b 216
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


		glMatrixMode(GL_PROJECTION_MATRIX);
		glLoadIdentity();
		gluPerspective(60, (double)windowWidth / (double)windowHeight, 1, 10000);
		glMatrixMode(GL_MODELVIEW_MATRIX);
		
		glPushMatrix();

		glTranslatef(0, 0, -3);

	//	drawCube();

		
		glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);		//Setup The Ambient Light
		glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);		//Setup The Diffuse Light
	//	glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);		
		glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);	//Position The Light
		glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);		//Setup The Ambient Light
		glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse);		//Setup The Diffuse Light
		glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);	//Position The Light
		glLightfv(GL_LIGHT2, GL_AMBIENT, lightAmbient);		//Setup The Ambient Light
		glLightfv(GL_LIGHT2, GL_DIFFUSE, lightDiffuse);		//Setup The Diffuse Light
		glLightfv(GL_LIGHT2, GL_POSITION, lightPosition2);	//Position The Light


		glRotatef(rotacionarx, 1.0f, 0.0f, 0.0f);
		glRotatef(rotacionary, 0.0f, 1.0f, 0.0f);
		glRotatef(rotacionarz, 0.0f, 0.0f, 1.0f);

		/*float radAngle = (rotacionarx / 180.f)*3.1415f;
		glRotatef(rotacionarx, 1.f, 0.f, 0.f);
		glRotatef(rotacionary, 0.f, cos(radAngle), -sin(radAngle));*/

		

		
	
		if (first){
			first = 0;

			calculamaxmin();


			// to scale model
			xs = (maxx - minx) / 2.0;
			ys = (maxy - miny) / 2.0;
			zs = (maxz - minz) / 2.0;

			if (xs > ys&&xs > zs) qs = xs;
			else if (ys > zs) qs = ys;
			else qs = zs;

			sk = 1.0 / qs;

			// to fit inside cube
			u = (minx + maxx) / 2.0;
			v = (miny + maxy) / 2.0;
			w = (minz + maxz) / 2.0;

		}


		glScalef(sk, sk, sk);
		
		glTranslatef(-u, -v, -w);
		
		//	drawCube();
		
		drawSTL();
		glPopMatrix();


		// Update Screen
		glfwSwapBuffers(window);

		// Check for any input, or window movement
		glfwPollEvents();
	}
}


Thank you again :smiley:

Thanks for taking the time to post. I am working on a response, but I must be sure I understand what your goal is. Because, there are lots of people out there who would want their rotations to behave exactly as shown in your animation (Euler sequence rotations). I assume that you want the second and third rotations (and maybe more) to take place around the blue axes, not the red or yellow axes? Correct? If so, I can show you a simple fix to your code that will make that happen. Though, you may find, in the long run, that you don’t really want your rotations to behave that way.

I am the one who needs to thank haha :slight_smile:

Yes… that is what I want. All rotations to take place at the blue axes.

After that, I guess I will achieve my second goal: store in 3 variables how much I’ve rotated the object in x,y,z (from the blue fixed axes) in angle degrees.

Thank youuuu :slight_smile:

… what I want. All rotations to take place at the blue axes.
I just posted some code/animation that shows how to do this and then some. The demo shows how to implement rotations around screen-fixed axes and/or body-fixed axes. The type of rotations you want are screen-fixed. I leave it up to you to alter your code to do what my GLUT demo does. My rotations are driven by pushing buttons on the keyboard. What drives your rotations?

It is true that AB != BA when it comes to matrix multiplication. Rotating my robot +15 degs around the X axis, then -10 degs around the Y axis results in a different orientation than rotating -10 degs about Y followed by +15 about X. If you want to reproduce an orientation exactly using a sequence of rotations about axes, you have to keep track of the rotation angles and the order in which the rotations were done. An easier way to do this is to save the orientation matrix (omat in my code), and reload it to reproduce a previous orientation.

Note that glRotatef is not explicitly used in my Display function. What glRotatef does is generate a 4x4 matrix which is applied to the matrix stack or object via matrix multiplication. glMultMatrix(omat) is used in my code instead. ‘omat’ is a matrix that is stored and manipulated in response to keyboard actions. These ‘manipulations’ are simply matrix multiplications accomplished using the appropriate glRotatef commands in the ‘Screen_Orientation’ routine.