Sign in to follow this  

Mouse Click - Unhandle Exception Problem

This topic is 2587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I am creating an explosion on a plane and that is explode when the mouse click on the plane. But i fail to get the mouse click event. It will have unhandle exception break when i click on the plane. Below is some part of my coding. Hope can get some ideas or opinions from you guys. Thanks in advanced.

**break: Unhandled exception at 0x00ffd7dc in project2.exe: 0xC0000005: Access violation writing location 0x010516c0.


#define PLANESIZE 150
float Plane[2][PLANESIZE][PLANESIZE];


void OnKeyPress(unsigned char key,int,int) {
switch (key)
{
case 27:
exit (0);
break;

case 'a':
//explosion happen at the middle of the plane if divide by 2
Plane[1][PLANESIZE/2][PLANESIZE/2] = 100;
break;
}
// ask glut to redraw the screen for us...
glutPostRedisplay();
}

void OnMouse(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {

if (x>100 && x<300 && y>110 && y<600)
{
Plane[1][x][y] = -10;
}
}
}

Share this post


Link to post
Share on other sites
It's not an OpenGL bug.
It fails because the 'x' and 'y' are out of range of 'Plane' array.
Make sure that 'x' < 'PLANESIZE' and 'y' < 'PLANESIZE' in "void OnMouse(int button, int state, int x, int y)". Or just set 'PLANESIZE' to your screen dimensions.

Share this post


Link to post
Share on other sites
i set it to the code below, still have the same problem. May i know what is the problem? And how to solve it?

Between, if i set it to a point like Plane[1][PLANESIZE/2][PLANESIZE/2], the explosion happen in the middle of plane, it will not have any problem in the program, just the problem is Plane[1][x][y] = -10. How to set the explosion when i click in certain point?


void OnMouse(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {

if (0<=x<(PLANESIZE-1) && 0<=y<(PLANESIZE-1))
{
Plane[1][x][y] = -10;
}
}
}

Share this post


Link to post
Share on other sites
If you are using 2D mode you can just set the projection like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,WindowWidth,WindowHeight,0,-1,1);
Also I recommend to use ^ this when you resing your window...

I don't know what and how you doing next... but...
To test mouse focus you can use this:
glPointSize(5.0f);//for example
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
And make sure that your model view matrix is identity...

Share this post


Link to post
Share on other sites
SionFly, there is no difference between:

glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();

and:

glBegin(GL_QUADS);
glVertex2f(x+5.0f,y+5.0f);
glVertex2f(x-5.0f,y+5.0f);
glVertex2f(x-5.0f,y-5.0f);
glVertex2f(x+5.0f,y-5.0f);
glEnd();

for projection:
glOrtho(0,WindowWidth,WindowHeight,0,-1,1);

And how to do with GL_TRIANGLES you must know that quadratic consists of 2 trangles. If you doesn't know how to work with primitives you can read OpenGL Red Book to learn it out...

Sorry, but it's hard to help you without knowing of what method u are using to creating explosions. I wrote a simple code that small quadratic (or point with dimensions that looks like small quadratic) how it follows your cursor. Maybe when you check it out you can understand how to solve your problem yourself....

Share this post


Link to post
Share on other sites

This topic is 2587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this