PDA

View Full Version : problem regardind mouse movement



somnath
04-27-2011, 06:56 AM
hello sir
i have one problem regardind mouse movement ...when i pick a object...the object will slightly displaced from it's position and then after the slight displacement the object move smoothly with mouse....



here is the code i m using plz check it out and reply me the solution
thanks


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>

int flag=0;




void render(int x,int y);


GLfloat objectRotX, objectRotY;
int curx, cury, width, height;
GLfloat sphere_x = 0.0;
GLfloat sphere_y = 0.0;
GLfloat sphere_z = 0.0;





int mouse_state,mouse_button;







void initialize(void)
{
glMatrixMode(GL_PROJECTION);
glFrustum(-0.50, 0.50, -0.50, 0.50, 1.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -4.0);
}

void redraw(void)
{

glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);
glPushMatrix();
glTranslatef( sphere_x, sphere_y, sphere_z);
glutWireSphere(0.5,100,100);

glPopMatrix();


}





void
display(void)
{
redraw();
glFlush();
glutSwapBuffers();

}


void
motion(int x, int y)
{
GLdouble model[4*4];
GLdouble proj[4*4];
GLint view[4];
GLdouble pan_x, pan_y, pan_z;



if (mouse_state == GLUT_DOWN &amp;&amp; mouse_button == GLUT_RIGHT_BUTTON) {
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluProject((GLdouble)x, (GLdouble)y, 0.0,
model, proj, view,
&amp;pan_x, &amp;pan_y, &amp;pan_z);
gluUnProject((GLdouble)x, (GLdouble)y, pan_z,
model, proj, view,
&amp;pan_x, &amp;pan_y, &amp;pan_z);
pan_y = -pan_y;


sphere_x = pan_x;
sphere_y = pan_y;
sphere_z = pan_z;


glutPostRedisplay();
}
}

void
mouse(int button, int state, int x, int y)
{

mouse_state =state;
mouse_button =button;



}



void myReshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);
}



int main (int argc, char **argv)
{
int i;

glutInit(&amp;argc, argv);
glutInitWindowSize(width = 500, height = 500);

glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

glutCreateWindow("envphong");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
initialize();
glutMainLoop();
return 0;
}

u can easily observe the slight displacement

_arts_
04-27-2011, 07:11 AM
This sounds normal. Try to pick the object with a mouse click, and move it with mouse motion only (and not mouse click).