hi everyone . this is my first post here . I have wrote a code to respond to mouse click . it’s supposed to some points where the mouse’s been clicked . but when I click the mouse , instead of drawing points , it blacks my screen and I don’t have any idea why?
can anyone help me with this?
tnx
#include <windows.h> // use as needed for your system
#include <gl/Gl.h>
#include <gl/glut.h>
int flag=0;
int rx,ry;
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0.0, 640.0, 0.0, 480.0);
// glViewport(-320.0, 320.0, -240.0, 240.0);
// gluOrtho2D(-160.0, 160.0, -120.0, 120.0);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
if (flag==1)
{
glBegin(GL_POINTS);
glVertex2i(rx, ry); // draw three points
glEnd();
}
glFlush(); // send all output to display */
}
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
flag=1;
rx=x;
ry=480-y;
}
glutPostRedisplay();
}
/void myMouseMotion(int x, int y)
{
flag=1;
rx=x;
ry=480-y;
glutPostRedisplay();
}/
/*void myPassiveMotion(int x, int y)
{
flag=1;
rx=x;
ry=480-y;
glutPostRedisplay();
}
*/
/*void myKeyboard(unsigned char ch, int x, int y)
{
switch (ch)
{
case 'e':
exit (-1);
case 'a':
flag=0;
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
break;
}
glutPostRedisplay();
}*/
/*void mySpecialKeyboard(int ch, int x, int y)
{
switch (ch)
{
case GLUT_KEY_LEFT:
flag=0;
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
break;
}
glutPostRedisplay();
}*/
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow(“my first attempt”); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMouseFunc(myMouse);
//glutKeyboardFunc(myKeyboard);
//glutSpecialFunc (mySpecialKeyboard);
//glutMotionFunc (myMouseMotion);
// glutPassiveMotionFunc(myPassiveMotion);
myInit();
glutMainLoop(); // go into a perpetual loop
}