PDA

View Full Version : 3D- rotaion and zoom in/out pan function in opengl



klmishra
05-01-2013, 02:22 AM
i want to create sphere/cube , then with mouse click and mouse movement i want to rotate and zoom in /out in opengl

The Little Body
05-01-2013, 09:50 AM
With a coffee and a sugar ? :)
(note that you have forgotten the "please" or the "thank you in advance" in your request, only "I want" ...)

More seriously, you can find an example about one regular cube rotation where you can select the axis of rotation with the mouse's buttons at http://www.macs.hw.ac.uk/~mjc/wp-mjc-downloads-for-students/Computer_Graphics/Example_7_2/Example_7_2.cpp
(you can immediatly destroy the stdafx.h include because it's seem me very too platform dependant and I don't see why is it here ...)

You can easily add variables for to handling the rotation and scaling speeds using rotation/scaling variables coupled with a detection of mouse movements
(for example, left/right for the rotation and bottom/top for scaling)
=> this is typically an excellent exercice for to begin with the OpenGL development :)

Here an working example :


// Example_7_2.cpp : Using Rotation with Vertex Arrays
//
// Author : E. Angel, Interactive Computer Graphics
// A Top-Down Approach with OpenGL, Third Edition
// Addison-Wesley Longman, 2003
// Version : 1.1 - Commenting changed to match other examples style
//
// Using Vertex Arrays
//
// Program behaviour:
// Left Mouse Button (LMB) spins the cube on its X-Axis
// Right Mouse Button (RMB) spins the cube on its Y-Axis
// Middle Mouse Button (MMB) spins the cube on its Z-Axis
//
// Callback commentary sent to normal command window.
//
// Last tested in Visual C++ 2010 Express

// YLP 01/05/2013 : add a dynamic rotation and scaling (+ comment the stdafx.h include for to be more cross-platform compliant)
// mouse left/right movments during mouse button down : handle the rotation speed
// mouse top/bottom movments durinf mouse button down : handle the general scaling of the cube
// tested on Linux

// #include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

//================================================== ====
// GLOBAL VARIABLES
//================================================== ====

GLfloat vertices[] =
{-1.0,-1.0,-1.0,
1.0,-1.0,-1.0,
1.0,1.0,-1.0,
-1.0,1.0,-1.0,
-1.0,-1.0,1.0,
1.0,-1.0,1.0,
1.0,1.0,1.0,
-1.0,1.0,1.0};

GLfloat colors[] =
{0.0,0.0,0.0,
1.0,0.0,0.0,
1.0,1.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0,
1.0,0.0,1.0,
1.0,1.0,1.0,
0.0,1.0,1.0};

// define cube faces
// each set of 4 indices indicates a different face
GLubyte cubeIndices[]=
{0,3,2,1,
2,3,7,6,
0,4,7,3,
1,2,6,5,
4,5,6,7,
0,1,5,4};

static GLfloat theta[] = {0.0, 0.0, 0.0}; // Rotation (X,Y,Z)
static GLint axis = 2; // Changed by the Mouse Callback Routine

// YLP 01/05/2013 : add scaling and dynamics rotations speed
static int lastx = 0, lasty = 0;
static GLfloat rotate_speed = 0.5f;
static GLfloat scaling = 1.0f;

//================================================== ====
// MOUSE CALLBACK ROUTINE
//================================================== ====
void mouseCallBack(int btn, int state, int x, int y)
{

// Changes the rotation axis depending on the mouse button pressed.
if ( state == GLUT_DOWN )
{
if( btn == GLUT_LEFT_BUTTON ) axis = 0;
if( btn == GLUT_MIDDLE_BUTTON ) axis = 1;
if( btn == GLUT_RIGHT_BUTTON) axis = 2;
}
}

// YLP 01/05/2013 : add dynamics rotation and scaling speed
void motionCallBack(int x, int y)
{
int movx, movy;

movx = lastx - x;
movy = lasty - y;

if ( abs(movx) < 10 ){ rotate_speed += (float)(movx) / 50.0f; }
if ( abs(movy) < 10 ){ scaling += (float)(movy) / 100.0f; }

if ( scaling < 00.1f) scaling = 0.1f;
if ( scaling > 10.0f) scaling = 10.0f;

lastx = x;
lasty = y;
}

//================================================== ====
// IDLE CALLBACK ROUTINE
//================================================== ====
// YLP 01/05/2013 : add dynamic rotation speed + scaling speed/limits
void idleCallBack()
{
// Spins the cube around the set axis.
theta[axis] += rotate_speed;

while( theta[axis] > 360.0 ) theta[axis] -= 360.0;
while( theta[axis] < 360.0 ) theta[axis] += 360.0;

glutPostRedisplay();
}

//================================================== ====
// WINDOW RESHAPE ROUTINE
//================================================== ====
void displayReshape(int w, int h)
{
// If the display is re-sized in any way, the cube is redrawn
// so that it fits the display properly. Try it!

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-4.0, 4.0, -3.0 * (GLfloat) h / (GLfloat) w,
5.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
4.0 * (GLfloat) w / (GLfloat) h, -3.0, 5.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}

//================================================== ====
// DISPLAY CALL BACK ROUTINE
//================================================== ====
void displayCallBack()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
glTranslatef(0.0, 1.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
glScalef(scaling, scaling, scaling);

// Now draw the cube
glColorPointer(3,GL_FLOAT, 0, colors);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

glutSwapBuffers();
}

//================================================== ====
// MAIN PROGRAM
//================================================== ====
int main(int argc, char** argv)
{
// Allow cmd line arguments to be passed to the glut
glutInit(&argc, argv);

// Create and name window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Need both double buffering and z buffer
glutInitWindowSize(500, 500);
glutCreateWindow("Example 7.2 - Vertex Arrays");

// Add Display & Mouse CallBacks
glutReshapeFunc(displayReshape);
glutDisplayFunc(displayCallBack);
glutIdleFunc(idleCallBack);
glutMouseFunc(mouseCallBack);
glutMotionFunc(motionCallBack);

glShadeModel(GL_FLAT); //use one colour per face
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3,GL_FLOAT, 0, colors);
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(0.0,0.0,0.0);

// Print information about the application in the command console
printf("Cube Rotation & Vertex Array Example\nChange Axis of Rotation by pressing Left, Right or Middle Mouse Buttons\n\n");

//Enter infinite loop calling 'displayCallBack'
glutMainLoop();

return 0;
}


(this compile fine using "gcc scalerot.c -lGL -lglut -lGLU -o scalrot" on a linux box)

PS : my pleasure :)