Hi ThunderSoul
I had written some examples before. These programs show you to use from the functions suggested by Jide.Please try to complie them. they can hep you to write some complicated programs.
//How could we use from the glutMotionFunc?
//Note: we use from the single buffer.
//For double buffer we must use from another method.
//Ehsan Kamrani
#include <GL/glut.h>
#include <iostream.h>
GLint xx1, yy1, xx2, yy2;
int wh = 0;
void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );
}
/////////////////
//Motion Function
/////////////////
void motion( int x, int y )
{
glBegin( GL_POINTS );
glVertex2f( x, wh - y );
glEnd();
glFlush();
}
void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, width, 0.0, height );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );
wh = height;
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( “How could we use from the glutMotionFunc?” );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
/////////////////////
//Motion
/////////////////////
glutMotionFunc( motion );
init();
glutMainLoop();
return 0;
}
==========
//How could we use from glutPassiveMotion Func?
//Note: we use from the single buffer.
//For double buffer we must use from another method.
//Ehsan Kamrani
#include <GL/glut.h>
#include <iostream.h>
GLint xx1, yy1, xx2, yy2;
int wh = 0;
/////////////////////////
//Passive Motion Function
/////////////////////////
void passiveMotion( int x, int y )
{
static float xx = 0, yy = 0;
glBegin( GL_LINES );
glVertex2f( xx, yy );
glVertex2f( x, wh - y );
glEnd();
glFlush();
xx = x;
yy = wh - y;
}
void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );
glRasterPos2i( 50,50);
}
void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, width, 0.0, height );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );
wh = height;
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glClear( GL_COLOR_BUFFER_BIT );
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, ‘a’ );
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( “How could we use from glutPassiveMotionFunc?” );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutPassiveMotionFunc( passiveMotion );
init();
glutMainLoop();
return 0;
}
===========
//How could we use from the mouse callback?
//Note: we use from the single buffer.
//For double buffer we must use from another method.
//Right click to see the results, left click to exit
#include <GL/glut.h>
#include <iostream.h>
int wh = 0;
void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );
}
void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, width, 0.0, height );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );
wh = height;
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
}
void mouse( int btn, int state, int x, int y )
{
static bool first = true;
static int xx, yy;
if( btn == GLUT_LEFT_BUTTON && state == GLUT_UP )
exit(0);
if( btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
if( first )
{
first = !first;
xx = x;
yy = wh - y;
}
else
{
first = !first;
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_QUADS );
glVertex2f( xx, yy );
glVertex2f( xx, wh - y );
glVertex2f( x, wh - y );
glVertex2f( x, yy );
glEnd();
glFlush();
}
}
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( “How could we use from the mouse callback?” );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mouse );
init();
glutMainLoop();
return 0;
}
==========
//How could we use from the mouse callback?
//Note: we use from the single buffer.
//For double buffer we must use from another method.
#include <GL/glut.h>
#include <iostream.h>
GLint xx1, yy1, xx2, yy2;
int wh = 0;
void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );
}
void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, width, 0.0, height );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );
wh = height;
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_QUADS );
glVertex2f( xx1, yy1 );
glVertex2f( xx1, yy2 );
glVertex2f( xx2, yy2 );
glVertex2f( xx2, yy1 );
glEnd();
glFlush();
}
void mouse( int btn, int state, int x, int y )
{
static bool first = true;
static int xx, yy;
if( btn == GLUT_LEFT_BUTTON && state == GLUT_UP )
exit(0);
if( btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
if( first )
{
first = !first;
xx1 = x;
yy1 = wh - y;
}
else
{
first = !first;
xx2 = x;
yy2 = wh - y;
}
glutPostRedisplay();
}
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( “How could we use from the mouse callback?(2)” );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mouse );
init();
glutMainLoop();
return 0;
}
-Ehsan-