PDA

View Full Version : Help to create a mouse funtion for free rotation



Muhammad Asif Hussain
02-11-2011, 11:45 PM
Salam everyone!

I am a petroleum engineer, and working to develope a reservoir rock model.

I don't know how to make a rotation logic i.e. angle etc...
when using mouse!

The objective is to rotate the whole model(with axes and font!) with mouse FREELY and continues to rotate until next mouse click...

Can anyone tell me how to develope the logic for mouse function?
I am posting thr whole code here


#include <windows.h> //suitable when using Windows 95/98/NT
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
#include <math.h>
#include<stdio.h>
#include <dos.h>
// Prototype Decalartion
void displayWire(void);
void filledCube(GLfloat xLocation, GLfloat yLocation, GLfloat zLocation, GLfloat widthofCube, GLfloat lenghtofCube, GLfloat heightofCube, GLfloat* fillcolor);

// Variable declaraction
double P[50];int counter1=0; // global variables
GLfloat angle= 0.0;

//<<<<<<<<<<<<<<<<<< myMouse >>>>>>>>>>>>>>>>>>>>>>>>>>
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN){
//angle=arctan(x/y); How it should be calculated as there is no arctan function in math.h?!
angle+=10.0;
}
glutPostRedisplay();
}

/////////////////////////// Axis function ///////////////////////////////
void axis(double length)
{ // draw a axis, with cone at end
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0, 0, 0); glVertex3d(0,0,length); // along the z-axis
glEnd();
glTranslated(0, 0,length -0.2);
glutSolidCone(0.04, 0.2, 12, 9);
glPopMatrix();
}

///////////////////////////////// bmpfont //////////////////////////////
void
bitmap_output(GLfloat x, GLfloat y, GLfloat z, char *string, void *font)
{
int len, i;

glRasterPos3f(x, y, z);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
/////////////////////bmp font end here/////////////////////////////////




//<<<<<<<<<<<<<<<<<<<<<<<<<<<<< displayWire >>>>>>>>>>>>>>>>>>>>>>
void myDisplay()
{
glMatrixMode(GL_PROJECTION); // set the view volume shape
glLoadIdentity();
glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100);

glMatrixMode(GL_MODELVIEW); // position and aim the camera
glLoadIdentity();
gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT); // clear the screen



// AXES
glColor3d(1,0,0); // draw black lines
axis(2); // z-axis
glPushMatrix();
glRotated(90, 0,1.0, 0);
glColor3d(0,1,0);
axis(2); // y-axis
glRotated(-90.0, 1, 0, 0);
glColor3d(0,0,1);
axis(2); // z-axis
glPopMatrix();
glPushMatrix();


P[0]=0.1; P[1]=0.2; P[2]=0.3; P[3]=0.4; P[4]=0.5; //Arbitrary numbers
GLdouble PMAX=0.5;
int N=5;

GLfloat j=0.0;

for (GLint i=0;i<N; i=i+1)

{

glLineWidth (0.5);
GLfloat color[]={(P[i]/PMAX)+0.2, (P[i]/PMAX)-0.2, 0.0, 1};
printf("color=%lf\n",P[i]);
filledCube(j,0,0,.1,.1,.1, color); //location (x,y,z), size (length, width, height), color (set by array color[] above)
j=j+.1;
}

glFlush();
//}
//////////////////////////////some text ///////////////////////////////////////
glColor3f(1.0, 1.0, 1.0);
bitmap_output(-2.2, 1.9, 1.5, "simulation viewer", GLUT_BITMAP_HELVETICA_18);
bitmap_output(2.2, 0.0, 0.0, "X-AXIS", GLUT_BITMAP_9_BY_15);
bitmap_output(0.0, 2.2, 0.0, "Y-AXIS", GLUT_BITMAP_9_BY_15);
bitmap_output(0.0, 0.0, 2.2, "Z-AXIS", GLUT_BITMAP_9_BY_15);
glFlush();
}


/////////////////////////////////// filledcube function /////////////////////////////////////////
void filledCube(GLfloat xLocation, GLfloat yLocation, GLfloat zLocation, GLfloat widthofCube, GLfloat lenghtofCube, GLfloat heightofCube, GLfloat* fillcolor) {
GLfloat hWidth = widthofCube*0.5;
GLfloat hLength = lenghtofCube*0.5;
GLfloat hHeight = heightofCube*0.5;

glPushMatrix();

// Rotation
glRotatef(angle, 1.0f, 0.0f, 0.0f); //where it should be placed in myDisplay() or in filledCube()?
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);

glTranslatef(xLocation, yLocation, zLocation);

glBegin(GL_QUADS);
glColor4fv(fillcolor);
//face 1
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, hLength);
glVertex3f(hWidth, hHeight, hLength);
//face 2
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth , -hHeight, hLength);
glVertex3f(hWidth, -hHeight, -hLength);
//face 3
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);
//face 4
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
glVertex3f(hWidth, -hHeight, -hLength);
//face 5
glVertex3f(-hWidth , hHeight, -hLength);
glVertex3f(-hWidth, hHeight, hLength);
glVertex3f(-hWidth, -hHeight, hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
//face 6
glVertex3f(hWidth, -hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);

glEnd();
glPopMatrix();
}

//<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(640,480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" Reservoir Rock Model");
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse); // register the mouse action function
glClearColor(1.0f, 0.0f, 1.0f,0.0f); // background is white
glViewport(0, 0, 640, 480);
glutMainLoop();
return 0;
}

mobeen
02-12-2011, 12:50 AM
Walaaikumus salam.
You would need to find the deltaX,deltaY from one click pos to next to see the amount of drag then calculate the rotation amount for x and y axis and then apply the rotation to ur current modelview matrix. So to sumup this is how u should do. One more thing the glPushMatrix /glPopMatrix work in pair.
See if this helps,


#include <windows.h> //suitable when using Windows 95/98/NT
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
#include <math.h>
#include<stdio.h>
#include <dos.h>
// Prototype Decalartion
void displayWire(void);
void filledCube(GLfloat xLocation, GLfloat yLocation, GLfloat zLocation, GLfloat widthofCube, GLfloat lenghtofCube, GLfloat heightofCube, GLfloat* fillcolor);

// Variable declaraction
double P[50];int counter1=0; // global variables
GLfloat angle= 0.0;
GLfloat rX=0, rY=0;
int oldX=0, oldY=0;
void Mouse(int button, int s, int x, int y)
{
if (s == GLUT_DOWN)
{
oldX = x;
oldY = y;
}
}

void Motion(int x, int y)
{
rY += (x - oldX)/5.0f;
rX += (y - oldY)/5.0f;

oldX = x;
oldY = y;
glutPostRedisplay();
}


/////////////////////////// Axis function ///////////////////////////////
void axis(GLfloat length)
{ // draw a axis, with cone at end
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0, 0, 0);
glVertex3d(0,0,length); // along the z-axis
glEnd();
glTranslated(0, 0,length -0.2);
glutSolidCone(0.04, 0.2, 12, 9);
glPopMatrix();
}

///////////////////////////////// bmpfont //////////////////////////////
void
bitmap_output(GLfloat x, GLfloat y, GLfloat z, char *string, void *font)
{
int len, i;

glRasterPos3f(x, y, z);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
/////////////////////bmp font end here/////////////////////////////////




//<<<<<<<<<<<<<<<<<<<<<<<<<<<<< displayWire >>>>>>>>>>>>>>>>>>>>>>
void myDisplay()
{
glMatrixMode(GL_PROJECTION); // set the view volume shape
glLoadIdentity();
glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100);

glMatrixMode(GL_MODELVIEW); // position and aim the camera
glLoadIdentity();
gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

glRotatef(rX,1,0,0);
glRotatef(rY,0,1,0);
// AXES
glColor3d(1,0,0); // draw black lines
axis(2); // x-axis
glPushMatrix();
glRotated(90, 0,1.0, 0);
glColor3d(0,1,0);
axis(2); // y-axis
glRotated(-90.0, 1, 0, 0);
glColor3d(0,0,1);
axis(2); // z-axis
glPopMatrix();


P[0]=0.1; P[1]=0.2; P[2]=0.3; P[3]=0.4; P[4]=0.5; //Arbitrary numbers
GLdouble PMAX=0.5;
int N=5;

GLfloat j=0.0;

for (GLint i=0;i<N; i=i+1)

{

glLineWidth (0.5);
GLfloat color[]={(P[i]/PMAX)+0.2, (P[i]/PMAX)-0.2, 0.0, 1};
printf("color=%lf\n",P[i]);
filledCube(j,0,0,.1,.1,.1, color); //location (x,y,z), size (length, width, height), color (set by array color[] above)
j=j+.1;
}


//}
glPushMatrix();
//////////////////////////////some text ///////////////////////////////////////
glColor3f(1.0, 1.0, 1.0);
bitmap_output(2.2, 1.9, 1.5, "simulation viewer", GLUT_BITMAP_HELVETICA_18);
bitmap_output(2.2, 0.0, 0.0, "X-AXIS", GLUT_BITMAP_9_BY_15);
bitmap_output(0.0, 2.2, 0.0, "Y-AXIS", GLUT_BITMAP_9_BY_15);
bitmap_output(0.0, 0.0, 2.2, "Z-AXIS", GLUT_BITMAP_9_BY_15);
glPopMatrix();
glFlush();
}


/////////////////////////////////// filledcube function /////////////////////////////////////////
void filledCube(GLfloat xLocation, GLfloat yLocation, GLfloat zLocation, GLfloat widthofCube, GLfloat lenghtofCube, GLfloat heightofCube, GLfloat* fillcolor) {
GLfloat hWidth = widthofCube*0.5;
GLfloat hLength = lenghtofCube*0.5;
GLfloat hHeight = heightofCube*0.5;

glPushMatrix();

// Rotation
glRotatef(angle, 1.0f, 0.0f, 0.0f); //where it should be placed in myDisplay() or in filledCube()?
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);

glTranslatef(xLocation, yLocation, zLocation);

glBegin(GL_QUADS);
glColor4fv(fillcolor);
//face 1
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, hLength);
glVertex3f(hWidth, hHeight, hLength);
//face 2
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth , -hHeight, hLength);
glVertex3f(hWidth, -hHeight, -hLength);
//face 3
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth, hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);
//face 4
glVertex3f(hWidth, hHeight, -hLength);
glVertex3f(-hWidth, hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
glVertex3f(hWidth, -hHeight, -hLength);
//face 5
glVertex3f(-hWidth , hHeight, -hLength);
glVertex3f(-hWidth, hHeight, hLength);
glVertex3f(-hWidth, -hHeight, hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
//face 6
glVertex3f(hWidth, -hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, -hLength);
glVertex3f(-hWidth, -hHeight, hLength);
glVertex3f(hWidth, -hHeight, hLength);

glEnd();
glPopMatrix();
}

//<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(640,480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" Reservoir Rock Model");
glutDisplayFunc(myDisplay);
glutMouseFunc(Mouse); // register the mouse action function
glutMotionFunc(Motion); // register the mouse action function
glClearColor(1.0f, 0.0f, 1.0f,0.0f); // background is white
glViewport(0, 0, 640, 480);
glutMainLoop();
return 0;
}

Muhammad Asif Hussain
02-12-2011, 08:41 AM
It moves that is good.
but I want it to move like animation via an indefinite loop.
I have the example of a torus which have this function which I am telling..., but unfortunately lost in my computer.

mobeen
02-12-2011, 09:22 AM
It moves that is good.
but I want it to move like animation via an indefinite loop.
I have the example of a torus which have this function which I am telling..., but unfortunately lost in my computer.
You can do so in two ways
1) attach an idle function glutIdleFunc(OnIdle);
2) a timer function using glutTimerFunc(OnTimer) assuming u have the functions (OnIdle/OnTimer) defined already.
Check this http://www.lighthouse3d.com/opengl/glut/index.php?4