====== 010 図形の描画 ====== ===== 概要 ===== GLUTを使うとちょっとした図形が簡単に表示できます。 ==== 立方体の描画 ==== {{:wiki:opengl:tips:opengl_drawcube.png?200|}} #include #include #pragma comment(lib, "glew32.lib") // 色 GLfloat color[] = { 1.0f, 0.0f, 0.0f, 1.0f }; //ライトの位置 GLfloat light_pos[] = { 200.0f, // X 150.0f, // Y -500.0f, // Z 1.0f // W }; void displayCallBack() { // 画面の初期色設定。 glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); // 画面クリア。 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); int window_width = glutGet( GLUT_INIT_WINDOW_WIDTH ); int window_height = glutGet( GLUT_INIT_WINDOW_HEIGHT ); gluPerspective( 30.0f, (double)window_width / (double)window_height, 1.0f, 1000.0f ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); // 視点の設定 gluLookAt( 150.0f, 100.0f, -150.0f, //カメラの座標 0.0f, 0.0f ,0.0f , // 注視点の座標 0.0f, 1.0f, 0.0f ); // 画面の上方向を指すベクトル // ライトの設定 glLightfv( GL_LIGHT0, GL_POSITION, light_pos ); // マテリアルの設定 glMaterialfv( GL_FRONT, GL_DIFFUSE, color ); // 立方体の描画。 glutSolidCube( 60.0f ); glutSwapBuffers(); } void reshapeCallBack( int _width, int _height ) { glViewport( 0, 0, _width, _height ); } void idleCallBack() { glutPostRedisplay(); } int main( int argc, char *argv[] ) { // GLUTの初期化 glutInit( &argc, argv ); // ウィンドウの位置を指定。 glutInitWindowPosition( 100, 100 ); // ウインドウサイズ指定 glutInitWindowSize( 640, 480 ); // ウインドウの表示モード指定 glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA ); // ウインドウ生成 glutCreateWindow( argv[0] ); // コールバック関数の登録。 glutDisplayFunc( displayCallBack ); // ディスプレイ。 glutIdleFunc( idleCallBack ); // アイドル。 glutReshapeFunc( reshapeCallBack ); // ウインドウリサイズ時コールバック。 // 深度有効。 glEnable( GL_DEPTH_TEST ); // ライト有効。 glEnable( GL_LIGHTING ); // ライト01番有効。 glEnable( GL_LIGHT0 ); // メインループ glutMainLoop(); return 0; } ==== 球体の描画 ==== {{:wiki:opengl:tips:opengl_drawsphere.png?200|}} glutSolidSphere(40.0,16,16); ==== トーラスの描画 ==== {{:wiki:opengl:tips:opengl_drawtorus.png?200|}} GLdouble innerRadius = 20.0f; // 内側の円の半径。 GLdouble outerRadius = 40.0f; // 外側の円の半径。 GLint sides = 16; // 断面の分割数。 GLint rings = 16; // 円の分割数。 glutSolidTorus( innerRadius, outerRadius, sides, rings ); ==== 円錐の描画 ==== {{:wiki:opengl:tips:opengl_drawcone.png?200|}} GLdouble base = 20.0f; // 底辺の円の半径。 GLdouble height = 60.0f; // 外側の円の半径。 GLint slices = 16; // 断面の分割数。 GLint stacks = 16; // 円の分割数。 glutSolidCone( base, height, slices, stacks ); ==== ティーポットの描画 ==== {{:wiki:opengl:tips:opengl_drawteapot.png?200|}} glutSolidTeapot( 40.0 );