PDA

View Full Version : interactive mouse & drawing points



philae
10-08-2009, 01:52 PM
I'm reaaly a newbie to openGL:) Here I wrote a code that opens a window of 500*500 , when the user clicks the window it draws a point.Here's the code and it doesn't work:




#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>

using namespace std;

void drawPoint(int x, int y) {
x = x - 250;
y = 250-y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(x , y);
glEnd();
glFlush();
}


void mouse(int bin, int state , int x , int y) {
if(bin == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN) drawPoint(x,y);
}



void display (void){}



void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}


int main (int argc,char** argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("My Window");
glutMouseFunc(mouse);
glutMotionFunc(drawSquare);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;

}



I don't understand why it doesn't work.It creates the window, gets the coordinates of the point under the marker but it doesn't draw the point.Can anyone help??Any help would be appreciated.I also wonder one more thing.Say , I manage to draw the points how can I delete them? Maybe I can draw point with background-color onto the old point , but what if I wanna delete a line?

kowal
10-08-2009, 03:04 PM
You did not set the projection, and you probably want orthographic one.

Add this code in your init() function


glViewport( 0,0, 500, 500 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 500.0, 0.0, 500.0, 1.0, -1.0 );


before setting GL_MODELVIEW matrix (to perform rotation and translations, current matrix must be GL_MODELVIEW)
and remove glFlush() from there.

man for glOrtho: http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml

Why not use double buffering? When I use single buffered window, I always get problems.Replace GLUT_SINGLE with GLUT_DOUBLE, and glFlush() in drawPoint(...), with glutSwapBuffers();

How to delete points?
Hold their coordinates in some array. On point deletion clear background (glClear()), and redraw everything You want to see. Same with lines.

philae
10-09-2009, 02:41 AM
I did what you say and things got worse !! Strange things happened : Now I can't create a window , only name of the window appears in the bar , I can't view the window.When I left click its icon in the bar , it doesn't give any reaction but I can close it by right click menu.

philae
10-09-2009, 01:49 PM
I again have strange problems : When I click I get the coordinates , but the clicked point is not drawn immediately.I clcik 8 or 9 time to draw a point .I think it's due to glFlush() or glutSwapBuffers().I read their manual pages but I think I'm doing sth wrong.Here's the code:




#include <GL/glut.h>
#include <GL/gl.h>
#include<iostream>
using namespace std;

void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();

}



void drawSquare(int x, int y) {
y = 250-y;
x = x-250;

glPointSize(10);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
glVertex2f(x , y);
glEnd();
glutSwapBuffers();
//glFlush();
}





void mouse(int bin, int state , int x , int y) {
if(bin == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN) drawSquare(x,y);
}


void init (void)
{
/* select clearing (background) color */
glClearColor (1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glViewport( 0,0, 500, 500 );
glMatrixMode( GL_PROJECTION );
glOrtho( 0.0, 500.0, 0.0, 500.0, 1.0, -1.0 );

/* initialize viewing values */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/*Main*/

int main (int argc,char** argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
/*Set the posotion of window*/
glutInitWindowPosition(0,0);
glutCreateWindow("My Vindow");
glutDisplayFunc(display);
glutMouseFunc(mouse);
init();
glutMainLoop();


}

kowal
10-10-2009, 01:32 PM
this code works:


#include <GL/glut.h>
#include <GL/gl.h>
#include<iostream>
using namespace std;

void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();

}



void drawSquare(int x, int y) {
//y = 250-y;
//x = x-250;
y = 500-y;

glPointSize(10);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
glVertex2f(x , y);
glEnd();
glutSwapBuffers();
//glFlush();
}





void mouse(int bin, int state , int x , int y) {
if(bin == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN) drawSquare(x,y);
}


void init (void)
{
/* select clearing (background) color */
glClearColor (1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glViewport( 0,0, 500, 500 );
glMatrixMode( GL_PROJECTION );
glOrtho( 0.0, 500.0, 0.0, 500.0, 1.0, -1.0 );

/* initialize viewing values */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/*Main*/

int main (int argc,char** argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize(500,500);
/*Set the posotion of window*/
glutInitWindowPosition(0,0);
glutCreateWindow("My Vindow");
glutDisplayFunc(display);
glutMouseFunc(mouse);
init();
glutMainLoop();


}