又是高分求指导
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "glut32.lib")
#pragma comment( lib, "glew32.lib")
#pragma comment( lib, "glaux.lib")
#pragma comment( lib, "vfw32.lib" )
/********************************************************************************************************************************/
#include <windows.h> // Windows的头文件
#include "glew.h" // 包含最新的gl.h,glu.h库
#include "glut.h" // 包含OpenGL实用库
#include "gl.h"
#include "glaux.h"
// Modified By jiangcaiyang
void init()
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0);
glShadeModel( GL_FLAT );
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode( GL_PROJECTION );// 新添加的
glLoadIdentity();
/*gluPerspective( 90.0, ( GLfloat)w/ ( GLfloat)h, 0.1, 100.0f);*/
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
/*glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0);*/
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadidentity();
glPushMatrix( );// 新添加的
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin( GL_POLYGON );// 下面四条语句的坐标为了符合顺时针的顺序而更改
glVertex3f( 0.5f, 0.5f, -4.0f );
glVertex3f( 0.5f, -0.5f, -4.0f );
glVertex3f( -0.5f, -0.5f, -4.0f );
glVertex3f( -0.5f, 0.5f, -4.0f );
glEnd();
glPopMatrix( );// 新添加的
glFlush();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(400, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow("hehe");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
int viewport[4];
double modelMatrix[16];
double projMatrix[16];
double nx, ny, nz;
GLdouble mx, my, mz;
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels((GLint)0, GLint(viewport[3]-0), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &nz/*distancePixels*/);
gluUnProject((GLdouble)0, (GLdouble)(viewport[3] - 0), (GLdouble)nz/*distancePixels[0][0]*/, modelMatrix, projMatrix, viewport, &mx, &my, &mz);
double c = nz;
glutMainLoop( );
return 0;
}
我写的从二维坐标转换为三维的坐标,先要求得一个点的深度比如我求的是(0,0)点, 然后用函数gluUnProject()求得他的三维坐标。但是为什么我总是求的不对呀。 mx = -1.0, my = 1.0, mz= -9.255e64次方。而且读取的深度值也是不对的。趋于无穷小了。为什么呀?帮忙看下,万分感谢。