PDA

View Full Version : please see what is wrong with this code?



03-04-2001, 11:46 AM
Hi I tried to run this code but it doesn't interact with my drawing immeditly .. so can u tell me what is wrong or can i find better programs like it?
-----------------
thanx
----------------------
/* A simple curve drawing program, 1/31/01 by Mark Harris
Illustrates mouse, mouse motion callback functions
*/

#include <GL/glut.h>

int squarecolor=1;
int anchorx,anchory;
int WindowWidth=500;
int WindowHeight=500;

void myinit(void)
{

/* attributes */

glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */

/* set up viewing */
/* 500 x 500 window with origin lower left */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}

void display( void )
{
glClear(GL_COLOR_BUFFER_BIT); /*clear the window */
glFlush(); /* clear buffers */
}

void mouse(int button,int state,int x,int y)
{
y=500-y; // convert system orientation
anchorx=x;
anchory=y;
return;
}

void motion(int x, int y)
{
y=500-y; // convert system orientation
glColor3f(1,0,0); /* draw in red */
glBegin(GL_LINES);
glVertex2f(anchorx,anchory);
glVertex2f(x,y);
glEnd();
anchorx=x; // update anchor point
anchory=y;
}

void main(int argc, char** argv)
{

glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(WindowWidth,WindowHeight);
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Draw"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
glutMouseFunc(mouse);
glutMotionFunc(motion);
myinit(); /* set attributes */

glutMainLoop(); /* enter event loop */
}

03-04-2001, 09:10 PM
I think its not working because you need a glut Idle Function where you would call glutPostRedisplay(); This causes Glut to call the display function, The display function is not called automatically. You don't need the Idle function but it is normally a good place to call glutPostRedisplay(); from.

Bahamut_france
03-05-2001, 12:32 AM
You should add a camera, if you want look at your drawing....
gluLookAt(cx, cy, cz, tx, ty, tz, 0, 1, 0);
with (cx,cy,cz) position of the camera
(ty,ty,tz) target of the camera
Then, you must add the method 'glutIdleFunc(glutIdle);'
in this method (using for animation calculs generaly), you call the display function:
example:

void glutIdle(void)
{
if (ani)
{
rot += vec3_t(0.0, 1.5, 0.0);
rot.Clamp(-360, 360);
glutPostRedisplay();
}
}

bye