All pastes #124236 Raw Edit

Someone

public text v1 · immutable
·published 2006-08-09 22:21 UTC
rendered paste body
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>


float	angle		= 0.0,
	delta_ngle	= 0.0,
     	ratio,
     	x		= 0.0,
     	y		= 1.75,
     	z		= 5.0,
     	lx		= 0.0,
     	ly		= 0.0,
     	lz		= -1.0;

int	delta_move	= 0;

GLint	scene_display_list;


void change_size ( int w , int h )
{
	if ( h == 0 )
		h = 1;

	ratio = 1.0 * w / h;

	glMatrixMode ( GL_PROJECTION );
	glLoadIdentity ( );
	
	glViewport ( 0 , 0 , w , h );

	gluPerspective ( 45 , ratio , 1 , 1000 );

	glMatrixMode ( GL_MODELVIEW );
	glLoadIdentity ( );
	gluLookAt ( x , y , z ,
		    x + lx , y + ly , z + lz ,
		    0 , 1.0 , 0 );
}


GLuint create_display_list ( void )
{
	GLuint snowman_display_list;

	// Create the id for the list
	snowman_display_list = glGenLists ( 1 );

	// Start list
	glNewList ( snowman_display_list , GL_COMPILE );

		// Call rendering function
		draw_snowman ( );

	glEndList ( );

	
	return ( snowman_display_list );
}


void draw_snowman ( void )
{
	glColor3f ( 1.0 , 1.0 , 1.0 );

	// Body
	glTranslatef ( 0 , 0.75 , 0 );
	glutSolidSphere ( 0.75 , 20 , 20 );

	// Head
	glTranslatef ( 0 , 1.0 , 0 );
	glutSolidSphere ( 0.25 , 20 , 20 );

	// Eyes
	glPushMatrix ( );
	glColor3f ( 0 , 0 , 0 );
	glTranslatef ( 0.05 , 0.10 , 0.18 );
	glutSolidSphere ( 0.05 , 10 , 10 );
	glTranslatef ( -0.1 , 0 , 0 );
	glutSolidSphere ( 0.05 , 10 , 10 );
	glPopMatrix ( );

	// Nose
	glColor3f ( 1.0 , 0.5 , 0.5 );
	glRotatef ( 0 , 1.0 , 0 , 0 );
	glutSolidCone ( 0.08 , 0.5 , 10 , 2 );
}


void init_scene ( void )
{
	glEnable ( GL_DEPTH_TEST );
	scene_display_list = create_display_list ( );
}


void move_camera_angle ( float angle )
{
	lx =  sin ( angle );
	lz = -cos ( angle );

	glLoadIdentity ( );
	gluLookAt ( x , y , z ,
		    x + lx , y + ly , z + lz ,
		    0 , 1 , 0 );
}


void move_camera_vertical ( int direction )
{
	x = x + direction * lx * 0.1;
	z = z + direction * lz * 0.1;

	glLoadIdentity ( );
	gluLookAt ( x , y , z ,
		    x + lx , y + ly , z + lz ,
		    0 , 1 , 0 );
}


void process_normal_keys ( unsigned char key , int x , int y )
{
	switch ( key ) {
		case 27 :
			exit ( 0 );
			break;
	}
}


void process_special_keys ( int key , int x , int y )
{
	switch ( key ) {
		case GLUT_KEY_LEFT :
			angle -= 0.01;
			move_camera_angle ( angle );
			break;

		case GLUT_KEY_RIGHT :
			angle -= 0.01;
			move_camera_angle ( angle );
			break;

		case GLUT_KEY_UP :
			move_camera_vertical ( 1 );
			break;

		case GLUT_KEY_DOWN :
			move_camera_vertical ( -1 );
			break;
	}
}


void render_scene ( void )
{
	int	i, j;


	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// Draw ground
	glColor3f ( 0.9 , 0.9 , 0.9 );
	glBegin ( GL_QUADS );
		glVertex3f ( -100.0 , 0 , -100.0 );
		glVertex3f ( -100.0 , 0 ,  100.0 );
		glVertex3f (  100.0 , 0 ,  100.0 );
		glVertex3f (  100.0 , 0 , -100.0 );
	glEnd ( );

	// Draw 36 snowmen
	for ( i = -3; i < 3; i++ )
		for ( j = -3; j < 3; j++ ) {
			glPushMatrix ( );
			glTranslatef ( i * 10.0 , 0 , j * 10.0 );
			glCallList ( scene_display_list );
			glPopMatrix ( );
		}

	glutSwapBuffers ( );
}


int main ( int argc , char **argv )
{
	glutInit ( &argc , argv );
	glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
	glutInitWindowPosition ( 100 , 100 );
	glutInitWindowSize ( 640 , 480 );
	glutCreateWindow ( "Camera" );

	init_scene ( );

	glutDisplayFunc ( render_scene );
	glutIdleFunc ( render_scene );
	glutReshapeFunc ( change_size );
	glutKeyboardFunc ( process_normal_keys );
	glutSpecialFunc ( process_special_keys );

	glutMainLoop ( );


	return 0;
}