小白学opengl之gluLookAt函数例子

时间:2022-09-10 21:23:05
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glu.h>

using namespace std;

GLdouble wx,wy,wz;/*returned world x,y,z coords */
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void display(void)
{
//printf("use display\n");

glClear(GL_COLOR_BUFFER_BIT);
// float mat[16];
// glGetFloatv(GL_MODELVIEW_MATRIX, mat);
// glColor3f(1,1,0);
// glPointSize(10);
// glVertex2f(0.1,0.1);
// for(int i=0;i<16;++i)
// {
// printf("%f ",mat[i]);
// }
// printf("\n");
//glGetFloatv(GL_PROJECTION_MATRIX, mat);
// for(int i=0;i<16;++i)
// {
// printf("%f ",mat[i]);
// }
// printf("\n");
//glTranslatef(-0.5f,-0.5f,-1.0f);
glColor3f(0,1,0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3f(0,1.1,0);//任意一个>1的分量都不会被显示
glEnd();


glutSwapBuffers();

}

void reshape(int w,int h)
{
//printf("use reshape\n");
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//如果注释,每次调用reshape都会累加结果
//gluPerspective( 45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0 );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();//如果注释,每次调用reshape都会累加结果
gluLookAt(0,0,0,0,1.1,0,0,1,0);

}


// void mouse(int button, int state, int x, int y)
// {
// GLint viewport [4];
// GLdouble mvmatrix [16],projmatrix [16];
// GLint realy;/*OpenGL y coordinate position */

// switch (button)
// {
// case GLUT_LEFT_BUTTON:
// {
// if (state == GLUT_DOWN)
// {
// glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
// glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
// glGetIntegerv(GL_VIEWPORT,viewport);
// note viewport [3] is height of window in pixels
// realy =viewport [3] -(GLint)y -1;
// printf("Coordinates at cursor are (%4d,%4d)\n " , x,realy);
// gluUnProject((GLdouble)x,(GLdouble)realy,0.1,
// mvmatrix,projmatrix,viewport,&wx,&wy,&wz);
// printf("World coords at z=0.0 are (%f,%f,%f)\n ",
// wx,wy,wz);



// }
// break;
// }

// // Start of drag.


// }

// // redraw.
// glutPostRedisplay();
// }

int main(int argc,char* argv[])
{

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);//(0,0)表示窗口的左上角对齐屏幕的左上角,(0,100)窗会向下移100
glutCreateWindow("test");
init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutMouseFunc(mouse);
glutMainLoop();

return 0;
}

////////////////////////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glu.h>

using namespace std;
GLdouble mvmatrix [16],projmatrix [16];

GLdouble wx,wy,wz;/*returned world x,y,z coords */
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void display(void)
{
//printf("use display\n");

glClear(GL_COLOR_BUFFER_BIT);
float mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat);


for(int i=0;i<16;++i)
{
printf("%f ",mat[i]);
}
printf("\n");
glGetFloatv(GL_PROJECTION_MATRIX, mat);
for(int i=0;i<16;++i)
{
printf("%f ",mat[i]);
}
printf("\n");

glColor3f(0,1,0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3f(0,0.5,0.5);//任意一个>1的分量都不会被显示

glColor3f(1,1,0);
glPointSize(10);
glVertex3f(0,1,-1);
glEnd();


glutSwapBuffers();

}

void reshape(int w,int h)
{
//printf("use reshape\n");
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//如果注释,每次调用reshape都会累加结果
glOrtho(-1,1,-1,1,-1,1);//照相机在坐标原点时默认的视景体


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();//如果注释,每次调用reshape都会累加结果
gluLookAt(0,0,0,0,1,0,0,1,0);
}


// void mouse(int button, int state, int x, int y)
// {
// GLint viewport [4];
// GLdouble mvmatrix [16],projmatrix [16];
// GLint realy;/*OpenGL y coordinate position */

// switch (button)
// {
// case GLUT_LEFT_BUTTON:
// {
// if (state == GLUT_DOWN)
// {
// glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
// glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
// glGetIntegerv(GL_VIEWPORT,viewport);
// note viewport [3] is height of window in pixels
// realy =viewport [3] -(GLint)y -1;
// printf("Coordinates at cursor are (%4d,%4d)\n " , x,realy);
// gluUnProject((GLdouble)x,(GLdouble)realy,0.1,
// mvmatrix,projmatrix,viewport,&wx,&wy,&wz);
// printf("World coords at z=0.0 are (%f,%f,%f)\n ",
// wx,wy,wz);



// }
// break;
// }

// // Start of drag.


// }

// // redraw.
// glutPostRedisplay();
// }

int main(int argc,char* argv[])
{

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);//(0,0)表示窗口的左上角对齐屏幕的左上角,(0,100)窗会向下移100
glutCreateWindow("test");
init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutMouseFunc(mouse);
glutMainLoop();

return 0;
}

得到 投影矩阵和模型视图矩阵后  算出两者乘积 {(0,0,0,0),(0,0,-1,0),(0,0,0,0),(0,0,0,1)} ,取空间任意两点的 与 这个矩阵相乘  ,得到的列向量 第一个分量和第三个分量都是0,此时与镜头朝向(0,1,0)在同一直线上,打印到屏幕上显示 两个点的位置重合 只能看到一个点