PDA

View Full Version : rectangle moving problem

glcrazy
11-19-2003, 03:05 PM
Hi all,

In my project I'm trying to move a rectangle ( to the left & right) with pressing a right mouse button nothing more.

Here is the code :
// Rectangle para.s
float x1=10.00f ,y1=-40.00f,x2=-60.00,y2=-50.00, D=0.0;

void drawbar (void)
{

glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);

}

void myGlutMouse(int button, int button_state, int x, int y )
{
switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN) { x1=x1+5.5 ; x2= x2+5.5; D = x2-x1;}
break;
case GLUT_LEFT_BUTTON:
if (button_state == GLUT_DOWN) { x1=x1-5.5 ; x2= x2-5.5; D = x2-x1;}
// glutIdleFunc(drawbar);
break;
default:
break;
}
}

void myGlutMotion(int x, int y )
{
glutPostRedisplay();
}
The previous code move the rectangle using left& right mouse button , and this what I don't aim to .. I beleive that I have to use GlutMotion function to return the x,y for GlutMouse function .Moreover,My code above move the rectangle each time (anywhere !) the right button is pressed !! What I want is once the rightbutton is pressed ( in the area of the rectangle) ( and still pressed) the user can move the rectangle ( as the same manner of the scroll bar of the window) !! I try a lot to do that but still with no result till now !!!!

Any help ..

virtualchetan
11-19-2003, 04:14 PM
U basically want to implement 'dragging'

bool dragging = false;
float lastMouseX, lastMouseY;

//register as glutMouseFunc
void onMouseButton(int button, int state, int x, int y)
{
if(button == GLUT_RIGHT_BUTTON)
{
if(state == GLUT_DOWN)
{
dragging = true;
y = WinHeight - y;//Win (0,0)
//is Left-Top
lastMouseX = x;
lastMouseY = y;
}
else
dragging = false;
}
}

//register as glutMotionFunc
void onMouseDrag(int x, int y)
{
if(dragging)
{
y = WinHeight - y;//Win (0,0)
//is Left-Top
float dragX = x - lastMouseX;
float dragY = y - lastMouseY;
lastMouseX = x;
lastMouseY = y;
//do whatever u wanna do with dragX
//and dragY
}
}

- Chetan

glcrazy
11-20-2003, 03:49 AM
Hi Chetan

Thank you very much for your help ..

I did the steps you mentioned above but the result was wrong . The rectangle now disappear !! I beleive I did somthing wrong somewhere .. but I don't know where exactly ..

Here is my modified code :

float lastMouseX, lastMouseY;
float dragx, dragy;
bool dragging = false;

void drawbar (void)
{

glTranslateflastMouseX,lastMouseY,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);

}

void myGlutMouse(int button, int button_state, int x, int y )
{

switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN)
{ dragging = true ;// y = 600 - y ; // Win(0,0)
// is left top
lastMouseX = x;
lastMouseY = y ;}
else
dragging = false ;

// default:
// break;
}
}

void myGlutMotion(int x, int y )
{
if (dragging) {// y = 600 - y; // win(0,0)
//is left top
dragx = x - lastMouseX;
dragy = y - lastMouseY;
glutPostRedisplay( );
lastMouseX = x;
lastMouseY = y ;}

}

Maybe you'll ask why I changed dragx, and dragy to global variables , I do that because I want to use them in the (drawbar) function but the result I have was wrong also ( the rectangle stop moving neither left nor right !!)

What to do know http://www.opengl.org/discussion_boards/ubb/confused.gif

virtualchetan
11-20-2003, 05:01 AM
One thing I can see is that ur rectangle drawing code is not within
glPushMatrix()- glPopMatrix()pair.

Originally posted by glcrazy:
void drawbar (void)
{

glTranslateflastMouseX,lastMouseY,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);

}

If that doesn't solve the problem, post complete code and I'll try running it on my machine to see what the problem is.

-Chetan

glcrazy
11-20-2003, 01:39 PM
Hi again Chetan ..

There is a little bit progress ( the rectangle is shaking up, down, left & right when the right mouse button is pressed now http://www.opengl.org/discussion_boards/ubb/confused.gif http://www.opengl.org/discussion_boards/ubb/smile.gif )

Here is the complete code:

#include <string.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <stdio.h>
//#include "glui.h"

float xy_aspect;
int last_x, last_y;
float lastMouseX, lastMouseY;
float dragx, dragy;
bool dragging = false;
int x, y;

int main_window;

// Rectangle para.s
float x1=10.00f ,y1=-40.00f,x2=-60.00,y2=-50.00, D=0.0;

/**************************************** Draw user bar **********/
void drawbar (void)
{

glPushMatrix();

glTranslatef(dragx,dragy,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);
glPopMatrix();

}

/**************************************** myGlutKeyboard() **********/

void myGlutKeyboard(unsigned char Key, int x, int y)
{
switch(Key)
{
case 27:
case 'q':
case 'Q':
case 'x':
case 'X':
exit(0);
break;
};

glutPostRedisplay();
}

{
myGlutKeyboard( value, 0, 0 );
}

/***************************************** myGlutIdle() ***********/

void myGlutIdle( void )
{
/* According to the GLUT specification, the current window is
undefined during an idle callback. So we need to explicitly change
it if necessary */
if ( glutGetWindow() != main_window )
glutSetWindow(main_window);

glutPostRedisplay();
}

/***************************************** myGlutMouse() **********/

void myGlutMouse(int button, int button_state, int x, int y )
{

switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN)
{ dragging = true ; y = 600 - y ; // Win(0,0)
// is left top
lastMouseX = x;
lastMouseY = y ;}
else
dragging = false ;

// default:
// break;
}
}

/***************************************** myGlutMotion() **********/

void myGlutMotion(int x, int y )
{
if (dragging) {// y = 600 - y; // win(0,0)
//is left top
dragx = x - lastMouseX;
dragy = y - lastMouseY;
glutPostRedisplay( );
lastMouseX = x;
lastMouseY = y ;}

}

/**************************************** myGlutReshape() *************/

void myGlutReshape( int x, int y )
{
int tx, ty, tw, th;
//viewport_area( &tx, &ty, &tw, &th );
glViewport( tx, ty, tw, th );

xy_aspect = (float)tw / (float)th;

glutPostRedisplay();
}

/***************************************** myGlutDisplay() *****************/

void myGlutDisplay( void )
{

glViewport(0, 0, 580, 600);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, (GLfloat)580/(GLfloat)600, 1.0f, 1000.0f);

glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen.
glLoadIdentity(); // Reset modelview matrix for new frame.
glTranslatef(0.0f,0.0f, -200.0f); //zoom the "camera" out a bit

drawbar ( ) ;

glutSwapBuffers();
}

/**************************************** main() ********************/

void main(int argc, char* argv[])
{
/****************************************/
/* Initialize GLUT and create window */
/****************************************/

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition( 50, 50 );
glutInitWindowSize( 800, 600 );

main_window = glutCreateWindow( "My New Game " );
glutDisplayFunc( myGlutDisplay );
glutReshapeFunc( myGlutReshape );
glutKeyboardFunc( myGlutKeyboard );
glutSpecialFunc( NULL );
glutMouseFunc( myGlutMouse );
glutMotionFunc( myGlutMotion );

/****************************************/
/* Enable z-buferring */
/****************************************/

glEnable(GL_DEPTH_TEST);

glutIdleFunc( myGlutIdle );

/**** Regular GLUT main loop ****/

glutMainLoop();

}

Thanks a lot Chetan ,even if you can't solve the problem . http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by glcrazy (edited 11-20-2003).]

glcrazy
11-21-2003, 02:23 PM
...

virtualchetan
11-21-2003, 04:15 PM
Originally posted by glcrazy:

...

What do u mean ???

- Chetan

glcrazy
11-21-2003, 09:33 PM
Hi ,

I faced a problem with my browser for that I wrote (...) to check the it's acceptance for posting replies here http://www.opengl.org/discussion_boards/ubb/redface.gif http://www.opengl.org/discussion_boards/ubb/smile.gif ..

virtualchetan
11-22-2003, 08:28 AM
U r using dragx and dragy as values for translation, ie. in glTranslatef.

In the code u posted,

1)y value should be subtracted from window height before using.

2) drag values should be accumulated,
i.e. instead of drag =, use drag +=

- Chetan

glcrazy
11-23-2003, 09:01 AM
Hi Chetan ...

Originally posted by virtualchetan:
U r using dragx and dragy as values for translation, ie. in glTranslatef.

In the code u posted,

.. OK! .. I did that because you wrote "//do whatever u wanna do with dragX
//and dragY " .. if you didn't mean to translate the rectangle .. what did you mean then .. ??

1)y value should be subtracted from window height before using.

I did that ,, http://www.opengl.org/discussion_boards/ubb/smile.gif

2) drag values should be accumulated,
i.e. instead of drag =, use drag +=

- Chetan

.. Also No result .. I don't know where is my mistake exactly !!! ??? http://www.opengl.org/discussion_boards/ubb/frown.gif

virtualchetan
11-24-2003, 06:06 AM
//g++ -L/usr/X11/lib -L/usr/X11R6/lib -lglut -lGLU -lm test.C -o test

#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library

#include <string.h>
#include <GL/glut.h>
//#include <GL/glaux.h>
#include <stdio.h>
//#include "glui.h"
#include <stdlib.h>

float xy_aspect;
int last_x, last_y;
float lastMouseX, lastMouseY;
float dragx, dragy;
bool dragging = false;
int x, y;

int main_window;

// Rectangle para.s
float x1=10.00f ,y1=-40.00f,x2=-60.00,y2=-50.00, D=0.0;

/**************************************** Draw user bar **********/
void drawbar (void)
{

glPushMatrix();
glTranslatef(dragx,dragy,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);
glPopMatrix();

}

/**************************************** myGlutKeyboard() **********/

void myGlutKeyboard(unsigned char Key, int x, int y)
{
switch(Key)
{
case 27:
case 'q':
case 'Q':
case 'x':
case 'X':
exit(0);
break;
};

glutPostRedisplay();
}

{
myGlutKeyboard( value, 0, 0 );
}

/***************************************** myGlutIdle() ***********/

void myGlutIdle( void )
{
/* According to the GLUT specification, the current window is
undefined during an idle callback. So we need to explicitly change
it if necessary */
if ( glutGetWindow() != main_window )
glutSetWindow(main_window);

glutPostRedisplay();
}

/***************************************** myGlutMouse() **********/

void myGlutMouse(int button, int button_state, int x, int y )
{

switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN)
{ dragging = true ; y = 600 - y ; // Win(0,0)
// is left top
lastMouseX = x;
lastMouseY = y ;}
else
dragging = false ;

// default:
// break;
}
}

/***************************************** myGlutMotion() **********/

void myGlutMotion(int x, int y )
{
if (dragging) {
y = 600 - y; // win(0,0)
//is left top
dragx += x - lastMouseX;
dragy += y - lastMouseY;
glutPostRedisplay( );
lastMouseX = x;
lastMouseY = y ;}

printf("drag %0.2f %0.2f\n", dragx, dragy);
}

/**************************************** myGlutReshape() *************/

void myGlutReshape( int x, int y )
{
int tx, ty, tw, th;
//viewport_area( &amp;tx, &amp;ty, &amp;tw, &amp;th );
glViewport( tx, ty, tw, th );

xy_aspect = (float)tw / (float)th;

glutPostRedisplay();
}

/***************************************** myGlutDisplay() *****************/

void myGlutDisplay( void )
{

glViewport(0, 0, 580, 600);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, (GLfloat)580/(GLfloat)600, 1.0f, 1000.0f);

glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen.
glLoadIdentity(); // Reset modelview matrix for new frame.
glTranslatef(0.0f,0.0f, -200.0f); //zoom the "camera" out a bit

drawbar ( ) ;

glutSwapBuffers();
}

/**************************************** main() ********************/

int main(int argc, char* argv[])
{
/****************************************/
/* Initialize GLUT and create window */
/****************************************/

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition( 50, 50 );
glutInitWindowSize( 800, 600 );

main_window = glutCreateWindow( "My New Game " );
glutDisplayFunc( myGlutDisplay );
glutReshapeFunc( myGlutReshape );
glutKeyboardFunc( myGlutKeyboard );
glutSpecialFunc( NULL );
glutMouseFunc( myGlutMouse );
glutMotionFunc( myGlutMotion );

/****************************************/
/* Enable z-buferring */
/****************************************/

glEnable(GL_DEPTH_TEST);

glutIdleFunc( myGlutIdle );

/**** Regular GLUT main loop ****/

glutMainLoop();

return 1;
}

This code works. Still not totally correct... U have to find out whats wrong now.
A hint : u dont have to setup projection matrix and glviewport in ur display function, set them up correctly in glReshape

- Chetan

glcrazy
11-25-2003, 11:47 AM
Hi Chetan http://www.opengl.org/discussion_boards/ubb/smile.gif

IT WORKS .. Thanks millions ..

I believe you mean the view port by the mistake you talked about .. I can fix that .. now I'm trying to let dragging of the rectangle if and only if the mouse pointer is inside the rectangle not anywhere else .. I'm thinking about calculate the area of the rectangle or make comparison between x,y and the boundaries of the rectangle ..
I'll work in the tonight .. and if there is no result I'll ask your help if you don't mind ,,

Cheers ..

[This message has been edited by glcrazy (edited 11-25-2003).]

glcrazy
11-26-2003, 09:01 AM
Hi Chetan ..

Sorry for bothering you again .. http://www.opengl.org/discussion_boards/ubb/cool.gif
I tried to change the code as following:

void myGlutMouse(int button, int button_state, int x, int y )
{
switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN)&& ( x1<=x<=x2) && (y1<=y<=y2)
{ dragging = true ;
y = 600 - y ; // Win(0,0)// is left top
lastMouseX = x;
lastMouseY = y ;
}
else dragging = false ;
}
}

It doesn't work .. and I noticed that the values of x & y are hundreds ie, something like 266, 156 , .. etc whereas x1,x2,y1,y2 are fixed as shown above !!!

this confused me alot ..

Do you have any idea !!
Thanks always Chetan ..