#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <vector>
using namespace std;
typedef struct Point3D
{
double objx;
double objy;
double objz;
}POINT_3D, *PPOINT_3D;
;
;
;
;
vector<POINT_3D> vec3dPoint;
vector<POINT_3D>::iterator it;
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
/*THE FUNCTION TO DRAW THE STUFF ON THE SCREEN*/
void display( )
{
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
//The big green floor like polygon
glBegin( GL_POLYGON );
glColor3f( );
glVertex3f( - );
glVertex3f( - );
glVertex3f( );
glVertex3f( );
glEnd( );
for (it = vec3dPoint.begin(); it != vec3dPoint.end(); it++)
{
//The red cube to be drawn at clicked position
glPushMatrix( );
glColor3f( );
glTranslatef( it->objx,it->objy,it->objz );
glRotatef(angle, );
angle++;
)
{
angle = ;
}
glutSolidCube( );
glPopMatrix( );
}
glFlush( );
glutSwapBuffers( );
}
void mouse( int button, int state, int x, int y)
{
];
];
;
/*Read the projection, modelview and viewport matrices
using the glGet functions.*/
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetIntegerv( GL_VIEWPORT, viewport );
//Read the window z value from the z-buffer
, GL_DEPTH_COMPONENT, GL_FLOAT, &z );
//Use the gluUnProject to get the world co-ordinates of
//the point the user clicked and save in objx, objy, objz.
]-y, 0.0, modelview, projection, viewport, &objnx, &objny, &objnz );
gluUnProject( x, viewport[]-y, z, modelview, projection, viewport, &objx, &objy, &objz );
gluUnProject( x, viewport[, modelview, projection, viewport, &objfx, &objfy, &objfz );
POINT_3D objxyz;
objxyz.objx = objx;
objxyz.objy = objy;
objxyz.objz = objz;
vec3dPoint.push_back(objxyz);
}
void initVector()
{
vec3dPoint.clear();
POINT_3D objxyz;
objxyz.objx = objx;
objxyz.objy = objy;
objxyz.objz = objz;
vec3dPoint.push_back(objxyz);
}
void init( int width, int height )
{
glViewport( , width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( );
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
//The most important part specify the things your
//glut window should provide
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( );
glutCreateWindow("gluUnProject Demo");
glClearColor( );
//enable z buffer
glEnable( GL_DEPTH_TEST );
//set the value in z-buffer as 1.0
);
initVector();
init( );
glutDisplayFunc( display );
glutReshapeFunc( init );
glutIdleFunc( display );
glutMouseFunc( mouse );
glutMainLoop( );
}