10 Replies - 1283 Views - Last Post: 28 March 2011 - 03:40 AM Rate Topic: ***** 1 Votes

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

opengl - beginner rotation problem

Posted 26 March 2011 - 11:16 PM

After google didn't help me i thought i'd post here

I have a very basic program, I built a box from quads and now i want to rotate the box on the
x-axis using my timer which increases the angle.

My problem is that when i use the glRotatef() function it is rotating the camera and not the
box object.

My code:
/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* File for "Basic Shapes" lesson of the OpenGL tutorial on
 * www.videotutorialsrock.com
 */

#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Global variables
GLfloat _cameraAngle = 0.0f;
GLfloat _angle = 30.0f;
GLfloat size = 1.0f;
GLfloat f = -5.0f;
GLfloat b = -6.0f;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
					int x, int y) {    //The current mouse coordinates
	switch (key) {
		case 27: //Escape key
			exit(0); //Exit the program
	}
}

//Initializes 3D rendering
void initRendering() {
	//Makes 3D drawing work when something is in front of something else
	glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
	//Tell OpenGL how to convert from coordinates to pixel values
	glViewport(0, 0, w, h);
	
	glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
	
	//Set the camera perspective
	glLoadIdentity(); //Reset the camera
	gluPerspective(45.0,                  //The camera angle
				   (double)w / (double)h, //The width-to-height ratio
				   1.0,                   //The near z clipping coordinate
				   200.0);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	
	glRotatef(_angle,1.0f,0.0f,0.0f);
	//========== BOX ===========//
	glBegin(GL_QUADS); //Begin quadrilateral coordinates
	
	//front
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(size,size,f);//top-right
	glVertex3f(-size,size,f);//top-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(size,-size,f);//bottom-right
	//back
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(size,size,B)/>;//top(back)-right
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	glVertex3f(size,-size,B)/>;//bottom(back)-right
	//left
	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,size,f);//top-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	//right
	glColor3f(0.5f,0.5f,0.0f);
	glVertex3f(size,size,B)/>;//top(back)-right
	glVertex3f(size,size,f);//top-right
	glVertex3f(size,-size,f);//bottom-right
	glVertex3f(size,-size,B)/>;//bottom(back)-right
	//top
	glColor3f(0.2f,0.4f,0.6f);
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,size,f);//top-left
	glVertex3f(size,size,f);//top-right
	glVertex3f(size,size,B)/>;//top(back)-right
	//bottom
	glColor3f(0.2f,0.1f,0.7f);
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(size,-size,f);//bottom-right
	glVertex3f(size,-size,B)/>;//bottom(back)-right

	glEnd(); //End quadrilateral coordinates

	glutSwapBuffers(); //Send the 3D scene to the screen
}
void update(int value) {
	_angle += 2.0f;
	if (_angle > 360) {
		_angle -= 360;
	}
	
	glutPostRedisplay(); //Tell GLUT that the display has changed
	
	//Tell GLUT to call update again in 25 milliseconds
	glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400); //Set the window size
	
	//Create the window
	glutCreateWindow("Box");
	initRendering(); //Initialize rendering
	
	//Set handler functions for drawing, keypresses, and window resizes
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutReshapeFunc(handleResize);
	
	glutTimerFunc(25, update, 0); //Add a timer

	glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
	return 0; //This line is never reached
}




Thanks

This post has been edited by Java Student: 26 March 2011 - 11:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: opengl - beginner rotation problem

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 10:00 AM

That's because you are applying your rotations to the camera position matrix (view matrix) rather than your model matrix (the one for the cube object.

You need to review glPushMatrix() and glPopMatrix().
Was This Post Helpful? 1
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 10:17 AM

View PostButchDean, on 27 March 2011 - 10:00 AM, said:

That's because you are applying your rotations to the camera position matrix (view matrix) rather than your model matrix (the one for the cube object.

You need to review glPushMatrix() and glPopMatrix().


Yes i tried to encapsulate the draw and rotate within a push and pop matrix but that didn't work either and still rotated the camera.

This post has been edited by Java Student: 27 March 2011 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 10:23 AM

So you did you camera stuff, then popped that matrix, then pushed you model matrix, performed your rotation match and it still rotated the camera?
Was This Post Helpful? 1
  • +
  • -

#5 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 11:00 AM

It looks like this:
//Draws the 3D scene
void drawScene() {
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	
	glPushMatrix();
	glRotatef(_angle,1.0f,0.0f,0.0f);
	//========== BOX ===========//
	glBegin(GL_QUADS); //Begin quadrilateral coordinates
	
	//front
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(size,size,f);//top-right
	glVertex3f(-size,size,f);//top-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(size,-size,f);//bottom-right
	//back
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(size,size,B)/>;//top(back)-right
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	glVertex3f(size,-size,B)/>;//bottom(back)-right
	//left
	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,size,f);//top-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	//right
	glColor3f(0.5f,0.5f,0.0f);
	glVertex3f(size,size,B)/>;//top(back)-right
	glVertex3f(size,size,f);//top-right
	glVertex3f(size,-size,f);//bottom-right
	glVertex3f(size,-size,B)/>;//bottom(back)-right
	//top
	glColor3f(0.2f,0.4f,0.6f);
	glVertex3f(-size,size,B)/>;//top(back)-left
	glVertex3f(-size,size,f);//top-left
	glVertex3f(size,size,f);//top-right
	glVertex3f(size,size,B)/>;//top(back)-right
	//bottom
	glColor3f(0.2f,0.1f,0.7f);
	glVertex3f(-size,-size,B)/>;//bottom(back)-left
	glVertex3f(-size,-size,f);//bottom-left
	glVertex3f(size,-size,f);//bottom-right
	glVertex3f(size,-size,B)/>;//bottom(back)-right

	glEnd(); //End quadrilateral coordinates

	glPopMatrix(); 
	glutSwapBuffers(); //Send the 3D scene to the screen
}


I thought that having the rotation and drawing inside the matrix would rotate the box and
avoid camera rotation.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 12:23 PM

Where have you pushed and popped your projection matrix?
Was This Post Helpful? 1
  • +
  • -

#7 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 12:40 PM

View PostButchDean, on 27 March 2011 - 12:23 PM, said:

Where have you pushed and popped your projection matrix?

Im not sure i know what you mean, i just started in opengl a few days ago.
Can you show me in code what that would look like?
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 01:03 PM

Damn, this is sending me back.

In your handleResize(int w, int h) function, move the code

	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective



immediately after your call to gluPerspective(). This ensures both matrices are initialized. It doesn't seem right that you should be reinitializing your modelview matrix on every call to drawScene().
Was This Post Helpful? 1
  • +
  • -

#9 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 04:46 PM

View PostButchDean, on 27 March 2011 - 01:03 PM, said:

It doesn't seem right that you should be reinitializing your modelview matrix on every call to drawScene().

I moved
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective


into the init (i looked at NeHe code and your right that was odd)

I don't however need to move
glLoadIdentity();


into the init i just keep it once at the top of my draw() method.

It looks like it's working now, but i have one last question if you'd be so kind.

So i was woundering, is
gluLookAt();


used for changing the cameras location, position? and changeing the cameras rotation
is done with
glRotatef();
? :bigsmile:

This post has been edited by Java Student: 27 March 2011 - 04:53 PM

Was This Post Helpful? 1
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: opengl - beginner rotation problem

Posted 27 March 2011 - 05:15 PM

gluLookAt() just changes where the camera is pointing by actually rotating the world, as I recall, since the camera in OpenGL always looks down the z-axis.

Glad you got it working now - Kudos! :)
Was This Post Helpful? 1
  • +
  • -

#11 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: opengl - beginner rotation problem

Posted 28 March 2011 - 03:40 AM

gluLookAt() can be used to look at a location from any location. You pass in the camera x,y,z - the point you want to look at x,y,z and the up vector which in almost all cases will be in the positive y.

You have to include the glu library to use it which adds an additional dependency so you might want to look into writing your own camera routines as the gluLookAt function can be achieved using regular translate and rotate calls :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1