//main.cpp
#include "main.h"
void setCamera( void ) {
glViewport( 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 30.f, ( GLdouble ) WINDOW_WIDTH / ( GLdouble ) WINDOW_HEIGHT, 0.1f, 100.f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.f, 0.f, 20.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f );
}
void setLight( void )
{
//点光源
GLfloat light_position[ 4 ] = { 5.f, 5.f, 5.f, 1.f };
GLfloat light_ambient[ 4 ] = { 0.3f, 0.3f, 0.3f, 0.f };
GLfloat light_diffuse[ 4 ] = { 1.f, 1.f, 1.f, 1.f };
GLfloat light_specular[ 4 ] = { 1.f, 1.f, 1.f, 1.f };
glLightfv( GL_LIGHT0, GL_POSITION, light_position );
glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular );
}
void displaySphere( float radius, float dr, float dg, float db, float sr, float sg, float sb, float shininess )
{
float diffuse[ 4 ] = { dr, dg, db, 1.f };
float specular[ 4 ] = { sr, sg, sb, 1.f };
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse );
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
glutSolidSphere( radius, 100, 100 );
}
void displayAxis( float length )
{
glBegin( GL_LINES );
glColor3f( 1.f, 0.f, 0.f );
glVertex3f( 0.f, 0.f, 0.f );
glVertex3f( length, 0.f, 0.f );
glColor3f( 0.f, 1.f, 0.f );
glVertex3f( 0.f, 0.f, 0.f );
glVertex3f( 0.f, length, 0.f );
glColor3f( 0.f, 0.f, 1.f );
glVertex3f( 0.f, 0.f, 0.f );
glVertex3f( 0.f, 0.f, length );
glEnd();
}
//速度と位置の初期化
void initSphere( void )
{
vx[ 0 ] = 1.f;
vy[ 0 ] = 1.f;
vx[ 1 ] = 0.f;
vy[ 1 ] = 0.f;
px[ 0 ] = 0.f;
py[ 0 ] = 0.f;
px[ 1 ] = 3.f;
py[ 1 ] = 3.f;
}
void keyboardFunction( unsigned char key, int x, int y )
{
switch( key ) {
case 'q':
exit( 0 );
break;
default:
break;
}
glutPostRedisplay();
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
setCamera();
setLight();
displayAxis( 10.f );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glPushMatrix();
glTranslatef( px[ 0 ], py[ 0 ], 0.f );
displaySphere( 1.f, 0.f, 0.3f, 1.f, 0.3f, 0.3f, 0.3f, 30.f );
glPopMatrix();
glPushMatrix();
glTranslatef( px[ 1 ], py[ 1 ], 0.f );
displaySphere( 1.f, 1.f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 30.f );
glPopMatrix();
glDisable( GL_LIGHT0 );
glDisable( GL_LIGHTING );
glutSwapBuffers();
}
void update( void )
{
for( int i = 0; i < numSpheres; i++ ) {
px[ i ] += vx[ i ] * dt;
py[ i ] += vy[ i ] * dt;
}
glutPostRedisplay();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
glutCreateWindow( "OpenGL Based Renderer" );
glClearColor( 0.f, 0.f, 0.f, 0.f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
initSphere();
glutDisplayFunc( display );
glutKeyboardFunc( keyboardFunction );
glutIdleFunc( update );
glutMainLoop();
return 0;
}
Ly9tYWluLmNwcAojaW5jbHVkZSAibWFpbi5oIgoKdm9pZCBzZXRDYW1lcmEoIHZvaWQgKSB7CglnbFZpZXdwb3J0KCAwLCAwLCBXSU5ET1dfV0lEVEgsIFdJTkRPV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