draw dots on mouse drag with openGL/GLUT

  c++, glut, opengl

This is my code, the program creates a dot in the middle of the window, which is not what I wanted it to do, my goal is to create a drawing program which draws dots when I drag my mouse. Could someone provide some insights on how to accomplish this?

float pointSize = 1.0f;
bool leftBottState;
float xvalue;
float yvalue;
void init(){
    glClearColor(0.0, 0.0, 0.0, 0.0);}

void dots(){
    glPointSize(pointSize);
    glBegin(GL_POINTS);
    glVertex2f(xvalue, yvalue);
    glEnd();}

void display(void){
    glColor3f(1, 0, 1);
    dots();
    glFlush();}

void mouse(int button, int state, int x, int y){
    y = 600 - y;
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        leftBottState = true;
        xvalue = x;
        yvalue = y;
    }
    else
    {
        leftBottState = false;
    }
}

void motion(int x, int y) { 
    if (leftBottState) 
    {
        std::cout << "LEFt bUTTON " << std::endl;
        glutPostRedisplay();
    }
}

int main(int argc, char** argv){
    init();
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowSize(600, 600);
    glutCreateWindow("test");       
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
    return(0);                  
}

Source: Windows Questions C++

LEAVE A COMMENT