1

画面上に作成したボタンをマウスでクリックしたときに、プログラムで多角形を描画したいと考えています。ポリゴン描画コマンドはどこに配置すればよいですか? 次回ディスプレイ コールバックが実行されたときにその効果が失われ、それがディスプレイ関数に含まれている必要があるため、マウス関数に入れることができないことを理解しています。しかし、表示関数で if 条件を設定できますか?

4

1 に答える 1

0

これが私を助けたのと同じように役立つことを願っています。マウス座標を 3D オブジェクトが使用できるものに変換し (以下のコードを参照)、どこかに保存すると、変換を使用して保存された座標でループ内のオブジェクトを描画できます。OpenGL プログラムの init 関数で形状、色などを初期化しましたが、キーボードで数字を選択してからビューポートのどこかをクリックしたときにのみ描画しました。これらの手順を繰り返すと、そのオブジェクトが新しい座標に移動/変換されます。

void MouseButton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
    leftmousebutton_down = (state == GLUT_DOWN) ? TRUE : FALSE;     
    if(leftmousebutton_down)
    {
        cout << "LEFT BUTTON DOWN" << endl;
        //pTransInfo[0].vTranslate  = Vector3( 0.5f, 0.5f, 0.5f ); // center of scene
        GLint viewport[4]; //var to hold the viewport info
        GLdouble modelview[16]; //var to hold the modelview info
        GLdouble projection[16]; //var to hold the projection matrix info
        GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
        GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates

        glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
        glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
        glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info

        winX = (float)x;
        winY = (float)viewport[3] - (float)y;
        winZ = 0;

        //get the world coordinates from the screen coordinates
        gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);

        cout << "coordinates: worldX = " << worldX << " worldY = " << worldY << " worldZ = " <<  worldZ << endl; //THIS IS WHAT YOU WANT, STORE IT IN AN ARRAY OR OTHER STRUCTURE
    }
}

}

于 2012-05-25T05:46:40.930 に答える