c++ - 获取旋转顶点的当前顶点位置

标签 c++ opengl 3d transformation vertex

我有一个非常简单的程序,里面有我介绍的 OpenGL 的东西。我在屏幕上有 3d 顶点,整个系统正在沿 y 轴旋转。

float rotation =0;
//update function
rotation = rotation +1;

绘图函数看起来像这样:

glPushMatrix();
        glTranslatef(500,390,0);
        glRotatef(rotation,0.0,1.0,0.0);
        glVertex3d(365,50,0);
glPopMatrix();

在旋转 glVertex 时,它的位置在屏幕上 发生了明显的变化。但是点本身的坐标不是。当屏幕上被 glRotatef 旋转的点的“实际”坐标静止时,如何获取其当前 坐标?

最佳答案

How do I get the current coordinates of the point on the screen which has been rotated by glRotatef, when their 'actual' coordinates are stationary?

您必须模仿 OpenGL 转换管道。由于您使用的是陈旧的固定函数 OpenGL,我将向您展示如何使用陈旧的 GLU 执行此操作:

首先我们需要当前视口(viewport)和投影矩阵

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

GLdouble projection[16];
glMatrixMode(GL_PROJECTION);
glGetDoublev(GL_PROJECTION_MATRIX, projection);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
    glTranslatef(500,390,0);
    glRotatef(rotation,0.0,1.0,0.0);

此时模型 View 矩阵包含应用的模型 View 变换,所以也得到这个。

GLdouble modelview[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

现在我们拥有了投影它的一切。

GLdouble obj_x, obj_y, obj_z; // point coordinates
GLdouble win_x, win_y, win_z; // receives window coordinates
gluProject( obj_x, obj_y, obj_z,
            modelview, projection, viewport,
            &win_x, &win_y, &win_z );

给你。我建议查看 MesaGL 提供的 gluProject 的源代码。但基本上是这些步骤:

clip = Projection · Modelview · obj{x,y,z,w}
clip = clip / clip.w;
win{x,y} = viewport{x,y} + viewport{width,height} * (clip{x,y} + 1)/2

关于c++ - 获取旋转顶点的当前顶点位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424055/

相关文章:

c++ - 无法从不是原点的其他地方旋转对象

python - 表示 3D 多面体的库

c++ - boost property tree add_child 指定路径

c++ - 如何使用 Material 的环境色代替灯光的全局环境色? (OpenGL 1)

c++ - 线路循环的背面剔除

linux - 在 Linux 上安装 Freeglut

javascript - WebGL 中的时空立方体

c++ - 如何释放在子对话框中创建的 CWin 对象以避免内存泄漏

c++ - 有没有办法 "statically"将共享的 .so(或 .o)库插入可执行文件?

c++ - 对 WinRT 接口(interface)的动态调用