PDA

View Full Version : Problems with my mouse function after resizing



Schieby
10-08-2013, 11:28 AM
Hey,

I have some problems with my resize method. When I change my window size, all looks fine except for drawing a point or something else. It doesn't have the right coordinates. But I can't figure out, how to code my mouse function. Maybe someone can help me.



GLsizei gPosX = 0, gPosY = 0;
GLsizei gCur_Win_Width = 0, gCur_Win_Height = 0;

#define WINDOW_POS_X 400
#define WINDOW_POS_Y 150

GLfloat ratio = 1.0;

GLfloat zoomFactor = 1.0;
GLfloat panX = 0.0;
GLfloat panY = 0.0;

GLfloat left = 0.0;
GLfloat bottom = 0.0;
GLfloat right = 0.0;
GLfloat top = 0.0;

int main( int argc, char **argv )
{
glutInit( &argc, argv);
InitGraphics();

glutDisplayFunc( myDisplay );
glutReshapeFunc( myChangeSize );
glutMouseFunc(myMouseFunc);
glutMainLoop();

return 0;
}

void setWindow(float width, float height) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if(width <= height) {

left = ((-WINDOW_LENGTH/zoomFactor)+width)+panX;
right = (WINDOW_LENGTH/zoomFactor)+panX;
bottom = ((-WINDOW_LENGTH/zoomFactor)+width)+panY;
top = (float)(WINDOW_LENGTH * ((float)height/(float)width)/zoomFactor)+panY;

gluOrtho2D(left, right, bottom, top);
}
if(width > height) {
left = (((float)-WINDOW_LENGTH/zoomFactor)+height)+panX;
right = ((float)(WINDOW_LENGTH/zoomFactor) * ratio)+panX;
bottom = (((float)-WINDOW_LENGTH/zoomFactor)+height)+panY;
top = (WINDOW_LENGTH/zoomFactor)+panY;

gluOrtho2D(left, right, bottom,top );
}
}

void setViewport(float width, float height) {
if (ratio>=1) {
glViewport(0,0, width, (float)(width/ratio));
}
if (ratio<1) {
glViewport(0,0, (float)(height*ratio), height);
}
}

void InitGraphics(void){
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
glutInitWindowSize( WINDOW_LENGTH, WINDOW_LENGTH );
glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y);
glLoadIdentity();
InitMenu();
}

void myChangeSize(int width, int height ){
gCur_Win_Width= width;
gCur_Win_Height = height;
ratio = (float) gCur_Win_Width / (float)gCur_Win_Height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

setWindow(gCur_Win_Width, gCur_Win_Height);
setViewport(width, height);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void myMouseFunc(int x, int y) {
gPosX = (float)((x*ratio)/zoomFactor)+panX;
gPosY = (float)(((gCur_Win_Height - y)*ratio)/zoomFactor)+panY;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
LineArray[LineNumPts][0] = gPosX;
LineArray[LineNumPts][1] = gPosY;
LineNumPts++;
drawLine();
}

}

CroCodile
10-09-2013, 06:53 AM
check out my post.
http://www.opengl.org/discussion_boards/showthread.php/181850-Simple-Collision?p=1253491#post1253491