Join 132,379 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,175 people online right now. Registration is fast and FREE... Join Now!
I am trying to use the mouse with glut passive motion func to rotate the camera/scene with the mouse. the arrow keys and the page up page down seem to work ok but the mouse is not working correctly. It seems that if you run your cursor across the top of the page that the cube is rotating one way on the y axis and then back the other direction. I don't know what to do to get it to rotate properly. Also the rotation on the x axis for looking up and down is also screwy. actually I think the cursor should stay positioned in the center of the screen. I did a cout test to see if glut was recognizing the center of the window but even though it knows exactly where the cusor is on the screen it does not seem to be able to aknowlede the center of the window. Probably the best way for you to see what I am trying to explain is to compile the program and see for ypurself what it is doing. I used the 2008 express version of microsofts IDE to compile this. Please try to keep the answer as simple as possible because I am new to all of this (programming in general). Much Thanks , L.J.Bush
CODE
#include <math.h> #include <stdlib.h> #include <GL/glut.h> #include <iostream> //#define pi = 3.14159; using namespace std;
lx = sin(ang); lz = -cos(ang); glLoadIdentity(); gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f); }
void moveFB(int direction) { x = x + direction*(lx)*0.1; z = z + direction*(lz)*0.1; glLoadIdentity(); gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f); }
void handleKeypress(unsigned char key, int x, int y) { switch (key) { case 27: exit(0);
} }
void handleSpecialKey(int key, int x, int y) {
switch (key) { case GLUT_KEY_LEFT : angle -= 0.1f; RotY(angle); break; case GLUT_KEY_RIGHT : angle +=0.1f; RotY(angle); break; case GLUT_KEY_UP : moveFB(1); break; case GLUT_KEY_DOWN : moveFB(-1); break; /*case GLUT_KEY_UP && GLUT_KEY_RIGHT: angle+=0.1f ((moveFB(1), RotY(angle)); break; case GLUT_KEY_UP && GLUT_KEY_LEFT: angle-=0.1f (moveFB(1), RotY(angle)); break; case GLUT_KEY_DOWN && GLUT_KEY_RIGHT: angle+=0.1f (moveFB(-1), RotY(angle)); break; case GLUT_KEY_DOWN && GLUT_KEY_LEFT: angle-=0.1f (moveFB(-1), RotY(angle)); break;*/ case GLUT_KEY_PAGE_UP : angle +=0.01f; RotX(angle); break; case GLUT_KEY_PAGE_DOWN : angle -=0.01f; RotX(angle); break;
} } void mouse(int button, int state, int x, int y) { if (button == GLUT_RIGHT_BUTTON) { if (state == GLUT_DOWN) glScalef(2.0,2.0,2.0); else
cout << "right button lifted at (" << x << "," << y << ")" << endl; } else if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) cout << "you shot me" << endl;
} }
void motionPassive(int x, int y) { glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);
cout << "Mouse moved at " << "(" << x << "," << y << ")" << endl;
if(x >= (GLUT_WINDOW_WIDTH/2)){ cout << "Mouse is Greater than the center of the width" << endl;
angle = x/10 * 0.5f;//- (GLUT_WINDOW_WIDTH)* 0.1f; RotY(angle); } else if(x <= (GLUT_WINDOW_WIDTH)){ cout << "Mouse is Less than the center of the width" << endl;
if(y >= ((GLUT_WINDOW_HEIGHT)/2)){ cout << "Mouse is Greater than the center of the height" << endl;
angle = y/10 * 0.1f;//- (GLUT_WINDOW_HEIGHT)* 0.1f; RotX(angle); } else if(y <= ((GLUT_WINDOW_HEIGHT)/2)){ cout << "Mouse is Less than the center of the height" << endl;
glRotatef(xRot,1.0f,0.0f,0.0f); //rotate our camera on teh x-axis (left and right) glRotatef(yRot,0.0f,1.0f,0.0f); //rotate our camera on the y-axis (up and down) glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
and just change the rotation values when u move the mouse.
so your render code should look something like
CODE
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The ModelView Matrix
//placeCamera glRotatef(xRot,1.0f,0.0f,0.0f); //rotate our camera on teh x-axis (left and right) glRotatef(yRot,0.0f,1.0f,0.0f); //rotate our camera on the y-axis (up and down) glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
//Cross right bottom side glVertex3f(0.5, 0.0, 0.5); glVertex3f(0.5, -3.0, 0.5); glVertex3f(0.5, -3.0, -0.5); glVertex3f(0.5, 0.0, -0.5);
//Cross right side bottom glVertex3f(0.5, 0.0, 0.5); glVertex3f(0.5, 0.0, -0.5); glVertex3f(2.0, 0.0, -0.5); glVertex3f(2.0, 0.0, 0.5); //Cross right side end glVertex3f(2.0, 1.0, 0.5); glVertex3f(2.0, 0.0, 0.5); glVertex3f(2.0, 0.0, -0.5); glVertex3f(2.0, 1.0, -0.5);
//Cross right side top glVertex3f(0.5, 1.0, 0.5); glVertex3f(2.0, 1.0, 0.5); glVertex3f(2.0, 1.0, -0.5); glVertex3f(0.5, 1.0, -0.5); //Cross right side top glVertex3f(0.5, 1.0, 0.5); glVertex3f(0.5, 1.0, -0.5); glVertex3f(0.5, 2.5, -0.5); glVertex3f(0.5, 2.5, 0.5); glEnd(); glPopMatrix();
}
also there is alot of things that dont need to be in your code eg.
glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid - not so! this does nothing, it translates 0 on all axis
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); this does not need to be called each loop, just do this once at startup therefore add this code to the: void initRendering() function.
add this to the bottom of the handleResize function glMatrixMode(GL_MODELVIEW); //remove this from drawScene function glLoadIdentity();
This post has been edited by bobjob: 22 Aug, 2008 - 09:56 PM
glRotatef(xRot,1.0f,0.0f,0.0f); //rotate our camera on teh x-axis (left and right) glRotatef(yRot,0.0f,1.0f,0.0f); //rotate our camera on the y-axis (up and down) glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
and just change the rotation values when u move the mouse.
so your render code should look something like
CODE
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The ModelView Matrix
//placeCamera glRotatef(xRot,1.0f,0.0f,0.0f); //rotate our camera on teh x-axis (left and right) glRotatef(yRot,0.0f,1.0f,0.0f); //rotate our camera on the y-axis (up and down) glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
//Cross right bottom side glVertex3f(0.5, 0.0, 0.5); glVertex3f(0.5, -3.0, 0.5); glVertex3f(0.5, -3.0, -0.5); glVertex3f(0.5, 0.0, -0.5);
//Cross right side bottom glVertex3f(0.5, 0.0, 0.5); glVertex3f(0.5, 0.0, -0.5); glVertex3f(2.0, 0.0, -0.5); glVertex3f(2.0, 0.0, 0.5); //Cross right side end glVertex3f(2.0, 1.0, 0.5); glVertex3f(2.0, 0.0, 0.5); glVertex3f(2.0, 0.0, -0.5); glVertex3f(2.0, 1.0, -0.5);
//Cross right side top glVertex3f(0.5, 1.0, 0.5); glVertex3f(2.0, 1.0, 0.5); glVertex3f(2.0, 1.0, -0.5); glVertex3f(0.5, 1.0, -0.5); //Cross right side top glVertex3f(0.5, 1.0, 0.5); glVertex3f(0.5, 1.0, -0.5); glVertex3f(0.5, 2.5, -0.5); glVertex3f(0.5, 2.5, 0.5); glEnd(); glPopMatrix();
}
also there is alot of things that dont need to be in your code eg.
glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid - not so! this does nothing, it translates 0 on all axis
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); this does not need to be called each loop, just do this once at startup therefore add this code to the: void initRendering() function.
add this to the bottom of the handleResize function glMatrixMode(GL_MODELVIEW); //remove this from drawScene function glLoadIdentity();
Thanks man I'll see if I can make it work I actually am just starting to learn to do anything with OpenGL and it seems to be pretty easy it's just hard for me to remember everything ,my memory does not seem to be as good as it used to be . The reason I was using glut is because I really did not want to have to learn windows it just takes too much code and is not that easy to remember,I think that very little of what I have covered in windows has stuck very well. I guess if I have problems I'll be back. Much Thanks again, L.J. Bush