glRotatef 旋转相机而不是物体
glRotatef rotates the camera instead of the object
我需要用 C++ 创建一个虚拟轨迹球。我已经做了所有的计算,我可以找到旋转角度和轴值。每次拖动鼠标时,我的对象都会按照我的意图旋转,但问题是每次旋转后它都会回到其初始位置。
所以我发现我需要获取当前的模型视图矩阵,将其乘以旋转矩阵,然后将结果加载回 opengl。
我试过了,但不幸的是,glRotatef 旋转了我的相机而不是物体。这是我绘制场景的函数
//--- Drawing code ---------------------------------------
/** Drawing code for one frame. */
void drawGLScene ()
{
// Real time in seconds.
GLfloat t = frameStat->frameStart( width, height );
// Clear the frame buffer and the depth buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Set current model-view transform:
glLoadIdentity();
// Looking from "camera" to the "center" point (y-axis defines the "up" vector)
gluLookAt( 0.0f, 0.0f, 10.0f,
center[ 0 ], center[ 1 ], center[ 2 ],
0.0f, 1.0f, 0.0f );
if (dragging)
glLoadMatrixf(matModelView.array());
glScalef( zoom, zoom, zoom );
#ifdef USE_VBO
// scene rendering using VBO buffers:
scene.render();
#else
// client-side vertex arrays (cube):
glVertexPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), vert ); // specify vertex data array
glColorPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), vert + 3 ); // specify vertex color array
glDrawElements( GL_QUADS, sizeof(ind) / sizeof(GLubyte), GL_UNSIGNED_BYTE, ind );
#endif
frameStat->swapBuffers( drawStat ); // SDL_GL_SwapWindow() and frame statistics
}
//--- Event handling -------------------------------------
/** Function to release/destroy our resources and restore the old desktop. */
void Quit ( int returnCode )
{
scene.deleteBuffers();
if ( glcontext )
SDL_GL_DeleteContext( glcontext );
// Clean up the window ..
SDL_Quit();
// .. and exit appropriately
exit( returnCode );
}
这是我的鼠标处理函数,我排除了释放函数,因为它很简单。
//--------------------------------------------------------
// Mouse handling:
void handleMouseMove ( SDL_Event &ev )
{
if ( ev.button.button == SDL_BUTTON_LEFT && dragging )
{
rotation.set(MI_IDENTITY);
rotation.rotate(5, 0.0f, 1.0f, 0.0f);
matModelView = matModelView * rotation;
}
}
void handleMousePress ( SDL_Event &ev )
{
if ( ev.button.button == SDL_BUTTON_LEFT )
{
dragging = true;
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)matModelView.array());
rotation.set(MI_IDENTITY);
}
}
matModelView
和 rotation
是 4x4 矩阵。在此代码中,我不包括我的轨迹球计算。在这里,我只是希望只要鼠标拖动它就可以通过 x 轴旋转 5 度。
也许很简单,但我对这一点很感兴趣。任何指导,代码示例都会很棒。
尝试更改:
matModelView = matModelView * rotation;
为此:
matModelView = rotation * matModelView;
我需要用 C++ 创建一个虚拟轨迹球。我已经做了所有的计算,我可以找到旋转角度和轴值。每次拖动鼠标时,我的对象都会按照我的意图旋转,但问题是每次旋转后它都会回到其初始位置。
所以我发现我需要获取当前的模型视图矩阵,将其乘以旋转矩阵,然后将结果加载回 opengl。
我试过了,但不幸的是,glRotatef 旋转了我的相机而不是物体。这是我绘制场景的函数
//--- Drawing code ---------------------------------------
/** Drawing code for one frame. */
void drawGLScene ()
{
// Real time in seconds.
GLfloat t = frameStat->frameStart( width, height );
// Clear the frame buffer and the depth buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Set current model-view transform:
glLoadIdentity();
// Looking from "camera" to the "center" point (y-axis defines the "up" vector)
gluLookAt( 0.0f, 0.0f, 10.0f,
center[ 0 ], center[ 1 ], center[ 2 ],
0.0f, 1.0f, 0.0f );
if (dragging)
glLoadMatrixf(matModelView.array());
glScalef( zoom, zoom, zoom );
#ifdef USE_VBO
// scene rendering using VBO buffers:
scene.render();
#else
// client-side vertex arrays (cube):
glVertexPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), vert ); // specify vertex data array
glColorPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), vert + 3 ); // specify vertex color array
glDrawElements( GL_QUADS, sizeof(ind) / sizeof(GLubyte), GL_UNSIGNED_BYTE, ind );
#endif
frameStat->swapBuffers( drawStat ); // SDL_GL_SwapWindow() and frame statistics
}
//--- Event handling -------------------------------------
/** Function to release/destroy our resources and restore the old desktop. */
void Quit ( int returnCode )
{
scene.deleteBuffers();
if ( glcontext )
SDL_GL_DeleteContext( glcontext );
// Clean up the window ..
SDL_Quit();
// .. and exit appropriately
exit( returnCode );
}
这是我的鼠标处理函数,我排除了释放函数,因为它很简单。
//--------------------------------------------------------
// Mouse handling:
void handleMouseMove ( SDL_Event &ev )
{
if ( ev.button.button == SDL_BUTTON_LEFT && dragging )
{
rotation.set(MI_IDENTITY);
rotation.rotate(5, 0.0f, 1.0f, 0.0f);
matModelView = matModelView * rotation;
}
}
void handleMousePress ( SDL_Event &ev )
{
if ( ev.button.button == SDL_BUTTON_LEFT )
{
dragging = true;
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)matModelView.array());
rotation.set(MI_IDENTITY);
}
}
matModelView
和 rotation
是 4x4 矩阵。在此代码中,我不包括我的轨迹球计算。在这里,我只是希望只要鼠标拖动它就可以通过 x 轴旋转 5 度。
也许很简单,但我对这一点很感兴趣。任何指导,代码示例都会很棒。
尝试更改:
matModelView = matModelView * rotation;
为此:
matModelView = rotation * matModelView;