#include<windows.h>
#include"glut.h"
#include<math.h>
#define PI 3.141592654f
#define R 50
#define RADIUS 500
GLfloat mx , my , mz;
bool mFlag;
void init()
{
glClearColor( 0.0f , 0.0f , 0.0f , 0.0f );
glPolygonMode( GL_FRONT , GL_LINE );
glPolygonMode( GL_BACK , GL_LINE );
}
void Resize( int w , int h )
{
if(0==w || 0==h)
return;
glViewport( 0 , 0 , w , h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( w > h )
glOrtho( -100*w/h , 100*w/h , -100 , 100 , 100 , -100 );
else
glOrtho( -100 , 100 , -100*h/w , 100*h/w , 100 , -100 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
void Display()
{
GLfloat z , r , angle;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );
glMatrixMode(GL_MODELVIEW);
for( angle = 0; angle <= 2*PI; angle += (PI/18) )
{
glBegin( GL_TRIANGLE_STRIP );
for( z = -R; z <= R; z += 5 )
{
r = sqrt( R*R - z*z );
glVertex3f( r*cos(angle) , r*sin(angle) , z );
glVertex3f( r*cos(angle+(PI/18)) , r*sin(angle+(PI/18)) , z );
}//for
glEnd();
}//for
glutSwapBuffers();
}
void Motion( int x , int y )
{
if( false == mFlag )
return;
x = x-400;
y = y-300;
GLfloat z = sqrt( (GLfloat)(RADIUS*RADIUS - x*x - y*y ));
//GLfloat Rot = 100 * acos( (mx*x + my*y + mz*z) / ( sqrt(mx*mx + my*my + mz*z) * sqrt(x*x + y*y + z*z) ) );
GLfloat Rot = sqrt( (GLfloat)((x-mx)*(x-mx) + (y-my)*(y-my)) ) / 3;
GLfloat lastMatrix[ 16 ];
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX,lastMatrix);
glLoadIdentity();
glRotatef( Rot , (my*z-y*mz) , (mx*z-x*mz) , (mx*y-x*my) );
::glMultMatrixf(lastMatrix);
mx = x;
my = y;
mz = z;
glutPostRedisplay();
}
void Mouse( int button , int state , int x , int y )
{
if( GLUT_LEFT_BUTTON == button )
{
if( GLUT_DOWN == state )
{
mx = x-400;
my = y-300;
mz = sqrt( RADIUS*RADIUS - mx*mx - my*my );
mFlag = true;
}//if
else
{
mFlag = false;
}//else
}//if
}
int main( int argc , char** argv )
{
glutInit( &argc , argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 800 , 600 );
glutCreateWindow( "Tom" );
init();
glutDisplayFunc( Display );
glutReshapeFunc( Resize );
glutMotionFunc( Motion );
glutMouseFunc( Mouse );
glutShowWindow();
glutMainLoop();
return 0;
}