====== 003 点、線、四角形の描画 ====== {{:wiki:opengl:tips:opengl_drawpoint.png?200|}} ===== 概要 ===== 点、線、四角形を何か表示してみます。 ==== 点 ==== #include // 頂点情報。 struct FloatVertex { float x; float y; float z; }; // 色情報。 struct FloatColor { float r; float g; float b; float a; }; // 頂点配列情報 static const FloatVertex Vertices[] = { { -0.5f, 0.5f, 0.0 }, // 左上 }; // カラー配列情報 static const FloatColor Colors[] = { { 1.0f, 0.0f, 0.0f, 1.0f }, }; // 描画順 static const GLuint Indices[] = { 0, }; // ディスプレイコールバック void displayCallBack() { // クリアカラーの設定。 glClearColor( 1.0, 1.0, 1.0, 1.0 ); // 画面クリア。 glClear( GL_COLOR_BUFFER_BIT ); // 点の大きさ指定。 glPointSize( 10.0f ); // 描画。 glDrawElements( GL_POINTS, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_INT, Indices ); // ダブルバッファ交換。 glutSwapBuffers(); } // メイン int main( int argc, char *argv[] ) { // GLUTの初期化 glutInit( &argc, argv ); // ウインドウの表示モード指定 glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA ); // ウインドウ生成 glutCreateWindow( argv[0] ); // 頂点、カラーのセットアップ。 { // 頂点配列のメモリアドレスの指定 glVertexPointer( 3, GL_FLOAT, 0, Vertices ); glEnableClientState( GL_VERTEX_ARRAY ); // カラー配列のメモリアドレスの指定 glColorPointer( 4, GL_FLOAT, 0, Colors ); glEnableClientState( GL_COLOR_ARRAY ); } // ディスプレイ コールバックの登録。 glutDisplayFunc( displayCallBack ); // メインループ glutMainLoop(); return 0; } ==== 線 ==== {{:wiki:opengl:tips:opengl_drawline.png?200|}} // 頂点配列情報 static const FloatVertex Vertices[] = { { -0.5f, -0.5f, 0.0 }, // 左下 { 0.5f, -0.5f, 0.0 }, // 右下 { 0.5f, 0.5f, 0.0 }, // 右上 { -0.5f, 0.5f, 0.0 }, // 左上 }; // カラー配列情報 static const FloatColor Colors[] = { { 1.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f, 1.0f }, }; // 描画順 static const GLuint Indices[] = { 0, 1, 2, 3, }; // ディスプレイコールバック void displayCallBack() { // クリアカラーの設定。 glClearColor( 1.0, 1.0, 1.0, 1.0 ); // 画面クリア。 glClear( GL_COLOR_BUFFER_BIT ); // 描画。 glDrawElements( GL_LINE_LOOP, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_INT, Indices ); // ダブルバッファ交換。 glutSwapBuffers(); } ==== 四角形 ==== {{:wiki:opengl:tips:opengl_drawsqare.png?200|}} // 頂点配列情報 static const FloatVertex Vertices[] = { { -0.5f, 0.5f, 0.0 }, // 左上 { -0.5f, -0.5f, 0.0 }, // 左下 { 0.5f, 0.5f, 0.0 }, // 右上 { 0.5f, -0.5f, 0.0 }, // 右下 }; // カラー配列情報 static const FloatColor Colors[] = { { 1.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f, 1.0f }, }; // 描画順 static const GLuint Indices[] = { 0, 1, 2, 3, }; // ディスプレイコールバック void displayCallBack() { // クリアカラーの設定。 glClearColor( 1.0, 1.0, 1.0, 1.0 ); // 画面クリア。 glClear( GL_COLOR_BUFFER_BIT ); // 描画。 glDrawElements( GL_TRIANGLE_STRIP, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_INT, Indices ); // ダブルバッファ交換。 glutSwapBuffers(); }