BROKEN's Advanced Vehicle Laboratory

Real-Time Linuxによるリアルタイム処理とOpenGL によるシミュレータの構築 (B)

index section 1 section 2 section 3 section 4 section 5 section 6
bibrography appendix A appendix B appendix C

B. 簡単なOpenGLアニメーションプログラムのサンプル

サンプルプログラムBのソースコードのダウンロード

B.1 cube.c


/*
  sample program for OpenGL animaion
  cube.c
 */

#include <glut.h>
#include <stdlib.h>

static double spin[3];

void display(void)
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glLoadIdentity();
  glTranslated(0.0, 0.0, -2.0 );

  glPushMatrix();
      glRotated( spin[0], 1.0, 0.0, 0.0 );
      glRotated( spin[1], 0.0, 1.0, 0.0 );
      glRotated( spin[2], 0.0, 0.0, 1.0 );
      glColor3d( 1.0, 1.0, 1.0 );
      glutWireCube( 1.0 );
  glPopMatrix();

  glutSwapBuffers();
}

void spinCube(void)
{
  int i;

  spin[0] += 2.0;
  spin[1] += 3.0;
  spin[2] += 5.0;

  for(i=0; i<3; i++)
    if( spin[i] > 360.0 )
      spin[i] -= 360.0;

  glutPostRedisplay();
}


void init(void)
{
  glClearColor( 0.0, 0.0, 0.0, 0.0 );
  glShadeModel( GL_SMOOTH );
}

void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei)w, (GLsizei)h );
  
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  gluPerspective( 60.0, (GLfloat)w/(GLfloat)h, 0.05, 5.0 );

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

}

void mouse(int button, int state, int x, int y)
{
  switch ( button ) {

  case GLUT_LEFT_BUTTON :
    if(state == GLUT_DOWN)
      glutIdleFunc( spinCube );
    break;

  case GLUT_MIDDLE_BUTTON :
    if(state == GLUT_DOWN)
      glutIdleFunc( NULL );
    break;

  case GLUT_RIGHT_BUTTON :
    if(state == GLUT_DOWN)
      exit(0);
    
  default :
    break;

  }

}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (250, 250); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);

   init ();

   glutDisplayFunc(display); 
   glutReshapeFunc(reshape); 
   glutMouseFunc(mouse);

   glutMainLoop();

   return 0;   /* ANSI C requires main to return int. */
}

B.2 Makefile


#
#  Makefile for simple OpenGL animation program
#

INCLUDE = -I/usr/local/include
GLLIB   = -L/usr/local/lib -lglut -lGL -lGLU
XLIB    = -L/usr/X11/lib -lX11 -lXext -lXmu -lXt -lXi -lSM -lICE

cube : cube.c
        gcc -o cube cube.c ${INCLUDE} ${GLLIB} ${XLIB}

test :
        ./cube &

clean :
        rm *~ core cube