PDA

View Full Version : Problem with mouse func



07-01-2001, 05:00 PM
any one see any problems with this ?


#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <math.h>
#include <GL/Glu.h>


void myinit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

void myMouse (int button , int state, int x, int y)
{
#define NUM 20
static GLintPoint List[NUM];
static int last = -1;


// test for mouse button & a full array

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && last < (NUM-1))
{
List[++last].x=x;
List[ last].y=screenHeight-y;

glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_LINE_STRIP);
for (int i = 0; i<last;i++)
glVertex2i(n[i].x,n[i].y);
glEnd();
glFlush();
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
last= -1;
}

void main (int argc, char ** argv)
{
glutInit (&argc,argv);
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(680,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Drawing With Mouse");
glutMouseFunc(myMouse);
myinit();
glutMainLoop();
}

ffish
07-01-2001, 06:12 PM
There's no display func. I don't know if GLUT requires one or not, but I've never written a GLUT app without one.

glClear(GL_LINE_STRIP) should be glBegin(...) (typo?).

You change to the projection matrix in your init function but never change back to the modelview matrix for your drawing code.

Hope that helps.

07-01-2001, 08:50 PM
many mistakes, I see. I am only a beginner but I am pretty sure that you need to add a glutDisplayFunc(somefunction); line to the main so that when the window is created or changes this function is called( just like the glutMouseFunc but receives no arguments). Put the drawing procedure in this fuction instead of the mouse function and in the mouse fuction write glutPostRedisplay(somefuction).
So , lets review:

create a fuction like
void myDisplay(){
glBegin(GL_WHATEVER);
glVertex3f(whatever ... )
glEnd();
}

add the following line to the main function :
glutDisplayFunc(myDisplay);
add the following line to the mouse function:
glutPostRedisplay();

Notice that you have a command like glClear(GL_LINES); or somthing. I am sure you missunderstood, it must be glBegin( ...

hope it helped

Ritchie
07-01-2001, 11:40 PM
There are many mistakes here, as stated in an earlier post. It's easiest to walk through your code from top to bottom.

First of all get rid of (most of) the header files. Your program only needs to include glut.h and stdlib.h

glut.h makes sure the appropriate headers (such as windows.h ) are included.

In your myinit function you set your drawing color to black. This is allright, but this is something you typically want to do in your display function (which you lack, but more on that later).

Your init function then sets the Projection Matrix. Three things on this:
1. You *don't* want to do this here. Instead, put this in a reshape function. The reshape function is called whenever the screen is resized.
2. You might want to set up a viewport too (glViewport) (also in the reshape function, and prior to switching to the projection matrix).
3. After setting the perspective, switch back to the modelview matrix again, to make sure you are working with the correct matrix. Restoring the modelview matrix to the identity matrix is an option, but you'll have to decide whether or not this is what you want.

myMouse function:
Your intention, if I understand correctly, is to let the user click on the screen, remember the coordinates, and draw (stipled) lines on the screen.

Actually your mouse function also holds the display code. It is best (I do not know if it is arbitrary) to take these two apart. Your display function then handles the drawing, and your mouse function keeps a list of coordinates.

Keep in mind that the GL_LINES* function needs two calls to glVertex* in order to draw a line. Using a for loop you can make the 'dots' connect.

Separation of the mouse and display functions requires you to make your variables List and last global(!). In this case it is ok to do that. The definition of NUM (smart, although I would call it MAX_POINTS or something like that) should really go before your function definitions.

One more remark about drawing. Drawing always occurs between glBegin and glEnd function calls.

The glClear function allows you to clear multiple buffers by using |. Your two lines of code clearing all could also be
glClear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT) (in this case you have no depth buffer, but as an illustration). Reviewing your code I think the second call to glClear you did is a mistake and that you meant to call glBegin.

The main function looks allright, however call myinit() gefore you assign your glut functions. Remember that the main function should return an integer (0).

Ok this is quite a long post. Don't be discouraged though and just keep trying.

Regards,
Ritchie

07-02-2001, 05:08 PM
Thanks all , I finally got my first opengl program working. I have the "Computer Graphics using OPEN GL" book by Hill. It's helped alot too. Any suggestions on other beginer resourses ?