3

OpenGLで現在のマウスの位置を拡大するタスクに絶望しています。私はさまざまなことを試し、これに関する他の投稿を読みましたが、特定の問題に可能な解決策を適応させることができませんでした。私が理解している限りでは、マウスカーソルの現在のウィンドウ座標を取得し、それらを投影解除してワールド座標を取得し、最後にそれらのワールド座標に変換する必要があります。

現在のマウス位置を見つけるために、マウスの右ボタンがクリックされるたびに、GLUTマウスコールバック関数で次のコードを使用します。

if(button == 2)
{
    mouse_current_x = x;
    mouse_current_y = y;
...

次に、ModelViewマトリックスとProjectionマトリックスを設定する前に、表示関数で現在のマウスの位置を投影解除します。これも完全に正常に機能しているようです。

// Unproject Window Coordinates
float mouse_current_z;
glReadPixels(mouse_current_x, mouse_current_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouse_current_z);
glm::vec3 windowCoordinates = glm::vec3(mouse_current_x, mouse_current_y, mouse_current_z);
glm::vec4 viewport = glm::vec4(0.0f, 0.0f, (float)width, (float)height);
glm::vec3 worldCoordinates = glm::unProject(windowCoordinates, modelViewMatrix, projectionMatrix, viewport);
printf("(%f, %f, %f)\n", worldCoordinates.x, worldCoordinates.y, worldCoordinates.z);

今、翻訳は問題が始まるところです。現在、寸法(dimensionX、dimensionY、dimensionZ)で立方体を描画しており、その立方体の中心に移動しているため、ズームは中心点にも行われます。z方向(ドリー)に移動することでズームを実現しています:

// Set ModelViewMatrix
modelViewMatrix = glm::mat4(1.0); // Start with the identity as the transformation matrix
modelViewMatrix = glm::translate(modelViewMatrix, glm::vec3(0.0, 0.0, -translate_z)); // Zoom in or out by translating in z-direction based on user input 
modelViewMatrix = glm::rotate(modelViewMatrix, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the whole szene in x-direction based on user input
modelViewMatrix = glm::rotate(modelViewMatrix,  rotate_y, glm::vec3(0.0f, 1.0f, 0.0f)); // Rotate the whole szene in y-direction based on user input
modelViewMatrix = glm::rotate(modelViewMatrix, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the camera by 90 degrees in negative x-direction to get a frontal look on the szene
modelViewMatrix = glm::translate(modelViewMatrix, glm::vec3(-dimensionX/2.0f, -dimensionY/2.0f, -dimensionZ/2.0f)); // Translate the origin to be the center of the cube
glBindBuffer(GL_UNIFORM_BUFFER, globalMatricesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(modelViewMatrix));
glBindBuffer(GL_UNIFORM_BUFFER, 0);

worldCoordinatesベクトルに変換して、立方体の中心への変換を置き換えようとしましたが、これは機能しませんでした。また、ベクトルを幅または高さでスケーリングしようとしました。私はここでいくつかの重要なステップを逃していますか?

4

1 に答える 1

0

たぶん、これはあなたの場合にはうまくいきません。しかし、私にはこれがこれを処理する最良の方法のように思えます。glulookat() を使用して、既に見つけたマウス クリックの xyz 位置を調べます。次に、gluPerspective() をより小さな視野角に変更して、実際のズームを実現します。

于 2013-02-03T13:37:15.823 に答える