PDA

View Full Version : Rotating an object with a mouse click.



CrackDown
12-02-2001, 06:44 AM
I have a cube, and a 2d square in my scene. And I want to know how I can make the cube rotate when someone clicks the 2d shape.

I know how to rotate the square normally. And I also realise I will have to use a mousefunc that checks for GLUT_LEFT_BUTTON, and GLUT_RIGHT_BUTTON states. Any help please?

Thanx

jide
12-03-2001, 01:27 AM
you're in the good way.
do you use glut ? glx ? wgl ?
under glut, there is just a function, so use it to modify!

CrackDown
12-03-2001, 07:46 AM
I know how to do the rotating and that. I suppose the only thing I need to know is to how to register a mouse click in a certain area (say, a 2d square). I can't use an x and y coordinate, because then the mouse click would have to be on the exact x,y point for it to work. I need it so that when a mouse clicks in a square. Any ideas?

Thanx

jmg
12-03-2001, 08:20 AM
picking.
http://www.opengl.org/developers/faqs/technical/selection.htm#sele0010

Good luck.

CrackDown
12-03-2001, 10:45 AM
Ok, here is what I have so far. And it doesn't work the way I want it to.


#include <windows.h> /* must include this before GL/gl.h */
#include <GL/gl.h>
#include <GL/glut.h> /* OpenGL utilities header file */
//#include <stdio.h>
//#include <string.h>

float ambientlightsource[] = {0.3, 0.3, 0.45, 1.0};
float source_light[] = {0.9, 0.8, 0.8, 1.0};
float light_pos[] = {7.0, 0.0, 0.0, 1.0};


void teapot(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(0.0f,0.0f, -7.0f);

glColor3f(1.0, 0.0, 0.0);
glutSolidTeapot(2.0);
glPopMatrix();

}


void myInit(void)
{
glEnable ( GL_LIGHTING );
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, ambientlightsource );
glLightfv ( GL_LIGHT0, GL_DIFFUSE, source_light );
glLightfv ( GL_LIGHT0, GL_POSITION, light_pos );
glEnable ( GL_LIGHT0 );
glEnable ( GL_COLOR_MATERIAL );
glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
//glEnable ( GL_CULL_FACE );
// glMatrixMode(GL_PROJECTION);
//glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glEnable ( GL_DEPTH_TEST );
}

void mouse ( int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && x > 520 && x < 600 && y > 280 && y < 300 ) {
glRotatef(15,1.0,0.0,0.0);
teapot();
glutPostRedisplay();
}
}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

teapot();

glutSwapBuffers();

}


void
reshape(int w, int h)
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}

int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(640, 480);
glutInitWindowPosition(100,150);
glutCreateWindow("");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc (mouse);
myInit();
glutMainLoop();
return 0;

}


When I click on an area on the right of the screen, the teapot rotates on its X axis, just once, and not on it's own axis either. I want it so that it revolves on its z axis, on the spot, and also to get the area where i am clicking to be more specific. Because where i click (bottomish right), is not where i have said (x > 520 && x < 600 && y > 280 && y < 300). Any ideas?


Thanx

rixed
12-05-2001, 03:21 AM
as i see it, your problem is not with glut callbacks but with you glLoadIdentity/glRotate.
If you load id matrix in the model view stack after drawing your teapot, then the next glRotate can't rotate any farther.

You should just accumulate a rotation angle in you callback (instead of glRotate, do my_angle+=15 then glRotate(angle,1,0,0) )

I could have missread something, though.