Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: user interaction with opengl

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    8

    user interaction with opengl

    Hello everyone! I'm new to OpenGL and new to this forum.
    I am trying to make a program, in which I draw on screen using the mouse. When I press Esc in keyboard, I would like to delete everything I've drawn. For that purpose I use this keyboard function:

    void ExitDraw(unsigned char key, int x, int y)
    {
    if(key==27) glClear(GL_COLOR_BUFFER_BIT); //clear the window by pressing Esc
    glutPostRedisplay();
    }


    This deletes every point I've drawn except for the last one! What is wrong? I don't get it...
    Thanks for everyone's help in advance!

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    741
    You should not issue draw commands (including glClear) from input handlers (mouse or keyboard). Instead your input handlers should fill in data structures that describe where and what to draw and your display function then does all drawing based on the recorded data.

    As for your specific problem: you are not showing your mouse handler or display function, so it's not possible to be sure, but my guess is one of those runs after clearing the screen and redraws the last point.

    BTW: please take a look at the Forum Posting Guidelines for suggestion on what information to include in questions and proper use of [ code]/[ /code] tags around source code.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    8
    Thank for your help! My whole code is:

    Code :
    #include "stdafx.h"
    #include <stdio.h>
    #include <glut.h>
    #include <math.h>
     
    int x_wind=400,y_wind=400; //the size of the window
    float x_position=0., y_position=0.; //the initial position of the point
    float x_max=1000.,y_max=1000.; //the size of the system
     
    void initmywindow()
    {
    	glutInitWindowPosition(700,200);
    	glutInitWindowSize(x_wind,y_wind);
    	glutCreateWindow("Draw with your mouse!");
    	glClearColor(0.8,0.9,0.9,0.);
    	glClear(GL_COLOR_BUFFER_BIT);//clear the window with the clear color
    	gluOrtho2D(0.,x_max,0.,y_max);
    }
     
    void Drawing (float x, float y)
    {
    	glBegin(GL_POINTS);
    		glVertex2f(x,y); //draw a point
    	glEnd();
    }
     
    void MouseDraw()
    {
    	glColor3f(0.,0.,0.8);
    	glPointSize(2.);
    	glEnable(GL_POINT_SMOOTH);
    	Drawing(x_position,y_position);
    	glFlush();
    }
     
    void ExitDraw(unsigned char key, int x, int y)
    {
    	if(key==27) glClear(GL_COLOR_BUFFER_BIT); //clear the window by pressing Esc
    	glutPostRedisplay();
    }
     
    void MouseControl (int x, int y)
    {
    	x_position=x_max*x/x_wind; // draw in the position of the mouse in pixels
    	y_position=y_max-y_max*y/y_wind;
    	glutPostRedisplay(); // run the display function again
    }
     
    void main(int argc, char **argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    	initmywindow();        
    	glutDisplayFunc(MouseDraw); 
    	glutKeyboardFunc(ExitDraw);
    	glutMotionFunc(MouseControl);
    	glutMainLoop();
    }

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    741
    Ok, as i guessed: your display function (re)draws the last point.
    If you have trouble figuring out the sequence of events, put a printf("name of function") at the beginning of the functions you register as callbacks with glut. That way you will see that after ExitDraw MouseDraw gets called (you request that your display function is run with the call to glutPostRedisplay()).

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    8
    I understand... Thank you for your help, I really appreciate it! I'll get to fix it!

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    8
    I spend so many hours trying to figure this out! I finally did it right! Thank you very much!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •