glGetDoublev(GL_MODELVIEW_MATRIX)
时间: 2023-09-20 20:05:40 浏览: 84
This function retrieves the current modelview matrix as a 4x4 matrix of double-precision floating-point values. The modelview matrix is used to transform vertex coordinates from object space to eye space, where they are then further transformed by the projection matrix to produce clip coordinates. The resulting clip coordinates are then divided by the clip coordinate's fourth component to produce normalized device coordinates, which are then transformed by the viewport matrix to produce window coordinates.
The retrieved matrix can be used for various purposes, such as calculating the direction of the camera, transforming normals, or performing custom transformations in the vertex shader.
相关问题
glGetDoublev(GL_MODELVIEW_MATRIX)解释
glGetDoublev(GL_MODELVIEW_MATRIX)是一个OpenGL函数,它用于获取当前OpenGL上下文中的模型视图矩阵。该函数接受一个参数,即一个指向双精度浮点数数组的指针,用于存储返回的矩阵数据。该函数返回值为void。
模型视图矩阵是OpenGL中的一个重要概念,它是用于将场景中的对象从世界坐标系转换到相机坐标系的矩阵。该矩阵包含了相机的位置和方向,以及场景中物体的位置和方向等信息。在OpenGL中,模型视图矩阵通常是由glMatrixMode(GL_MODELVIEW)和glLoadMatrix*()系列函数来设置的。而glGetDoublev(GL_MODELVIEW_MATRIX)函数则用于获取当前设置的模型视图矩阵的值,以便在需要时进行后续的操作,比如进行照明计算或投影变换等。
需要注意的是,glGetDoublev(GL_MODELVIEW_MATRIX)函数返回的矩阵是一个4x4的矩阵,其中包含了位移、旋转和缩放等变换信息。在使用返回的矩阵时,需要进行适当的转换和计算,以确保正确地应用到场景中的对象上。
glLineWidth(2.0f); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_LINE_STRIP); float r = 1.0f; for (float theta = 0.0f; theta <= 2 * PI; theta += PI / 180.0f) { float value = std::cos(2 * PI * spacing * std::cos(theta) / wavelength); value *= std::cos(beamwidth * std::sin(theta)); drawPolarLine(r * value, theta); } glEnd();修该为根据世界坐标绘制而不是屏幕坐标
这段代码使用OpenGL库绘制了一个极坐标图形,但是绘制的是屏幕坐标。如果要根据世界坐标绘制,可以通过以下步骤进行修改:
1. 设置投影矩阵,将屏幕坐标转换为世界坐标。可以使用gluOrtho2D函数设置正交投影矩阵,将屏幕的左下角坐标映射为世界坐标系的左下角,右上角坐标映射为世界坐标系的右上角。例如:
```
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, screenWidth, 0, screenHeight);
glMatrixMode(GL_MODELVIEW);
```
2. 修改drawPolarLine函数,将屏幕坐标转换为世界坐标。可以通过gluUnProject函数将屏幕坐标转换为世界坐标。例如:
```
void drawPolarLine(float r, float theta) {
float x = r * std::cos(theta);
float y = r * std::sin(theta);
GLdouble worldX, worldY, worldZ;
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject(screenWidth / 2 + x, screenHeight / 2 + y, 0.0,
modelview, projection, viewport, &worldX, &worldY, &worldZ);
glVertex2d(worldX, worldY);
}
```
这样就可以将屏幕坐标转换为世界坐标,并通过glVertex2d函数绘制世界坐标系中的点。
修改后的代码:
```
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, screenWidth, 0, screenHeight);
glMatrixMode(GL_MODELVIEW);
glLineWidth(2.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
float r = 1.0f;
for (float theta = 0.0f; theta <= 2 * PI; theta += PI / 180.0f) {
float value = std::cos(2 * PI * spacing * std::cos(theta) / wavelength);
value *= std::cos(beamwidth * std::sin(theta));
drawPolarLine(r * value, theta);
}
glEnd();
void drawPolarLine(float r, float theta) {
float x = r * std::cos(theta);
float y = r * std::sin(theta);
GLdouble worldX, worldY, worldZ;
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject(screenWidth / 2 + x, screenHeight / 2 + y, 0.0,
modelview, projection, viewport, &worldX, &worldY, &worldZ);
glVertex2d(worldX, worldY);
}
```