opengl gluLookAt函数实现

标签: gluLookAt opengl
17人阅读 评论(0) 收藏 举报
分类:

函数的原型如下:

 gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,GLdouble centerx,GLdouble centery,GLdouble centerz,GLdouble upx,GLdouble upy,GLdouble upz);

前三个参数的含义是摄像机的位置,四到六个参数是被观察物体的方向,最后面的三个参数是垂直于摄像机向上的向量(也就是摄像机可以扭个角度在同一位置,看同一个地方)

c++实现

#include <iostream>
#include <cstring>
#include <GL/glut.h>
#include <cmath>
using namespace std;

struct point
{
    GLdouble x, y, z;
    point()
    {
        x = y = z = 0;
    }
    point(GLdouble _x, GLdouble _y, GLdouble _z)
    {
        x = _x;
        y = _y;
        z = _z;
    }
};
typedef point Vector;
void Normalize( Vector &V)
{
    GLdouble tmp = sqrt(V.x*V.x + V.y*V.y + V.z*V.z);
    V.x = V.x / tmp;
    V.y = V.y / tmp;
    V.z = V.z / tmp;
}
Vector Cross(Vector a, Vector b)
{
    return Vector(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
void MyLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ)
{
    GLdouble Mat[16];
    memset(Mat, 0, sizeof(Mat));
    Mat[15] = 1;
    Vector forward(centerX - eyeX, centerY - eyeY, centerZ - eyeZ);
    Vector up(upX, upY, upZ);
    Normalize(forward);
    Vector side = Cross(forward, up);
    Normalize(side);
    up = Cross(side, forward);
    Mat[0] = side.x;
    Mat[4] = side.y;
    Mat[8] = side.z;
    Mat[1] = up.x;
    Mat[5] = up.y;
    Mat[9] = up.z;
    Mat[2] = -forward.x;
    Mat[6] = -forward.y;
    Mat[10] = -forward.z;
    cout<<Mat[10]<<" ";
    cout<<endl;
    glLoadMatrixd(Mat);
    glTranslated(-eyeX, -eyeY, -eyeZ);
}
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //背景黑色  
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0); //画笔白色  

    glLoadIdentity();  //加载单位矩阵  

//  gluLookAt(0, 0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    MyLookAt(0, 0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glutWireTeapot(2);
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
//  gluLookAt(0, 0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    //  
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
查看评论

OpenGL 实现Google地图瓦片的绘制,漫游

-
  • 1970年01月01日 08:00

实现opengl 中gluLookAt函数

opengl Distilled 中介绍了view 坐标转换的原理,先列一下原著中的介绍: The view transformation transforms vertices into eye ...
  • liuzhidejll
  • liuzhidejll
  • 2012-01-18 17:59:56
  • 2328

OpenGL: gluLookAt函数详解

下面的一段摘自百度百科:视点转换函数原型void gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,GLdouble centerx,GLdoub...
  • Augusdi
  • Augusdi
  • 2014-03-04 16:48:31
  • 8579

OpenGL: gluPerspective和gluLookAt的关系

看了Opengl的相关程序,发现有些东西还是特别迷茫,尤其是gluLookAt的函数做啥用的,而gluPerspective又有什么功能.在网上查看到了这篇: 终于搞明白gluPerspective和...
  • Augusdi
  • Augusdi
  • 2014-02-25 22:10:50
  • 4663

OpenGL es gluLookAt 函数

OpenGL es 没有gluLookAt 函数,从外网找了一个 附链接 gluLookAt函数 为了方便不会墙的同志,附代码 已调试使用,正常! object c 和 c++混编的同志在使...
  • layject
  • layject
  • 2016-01-22 21:36:58
  • 959

《高效学习OpenGL》之视图变换 gluLookAt()

注意:视图变换函数必须在调用任何模型变换函数之前调用,确保首先作用于物体的是模型变换 void gluLookAt ( GLdouble eyex, GLdouble e...
  • huangbangqing12
  • huangbangqing12
  • 2014-02-18 15:38:39
  • 1425

gluPerspective and gluLookAt 在OpenGL-ES中的替代

gluPerspective and gluLookAt 在OpenGL-ES中的替代
  • iversonyang
  • iversonyang
  • 2010-08-16 10:16:00
  • 2263

OpenGL(六) gluLookAt和gluPerspective函数解析

在调用gluLookAt和gluPerspective函数之前一般要先调用一下glLoadIdentity函数,先说一下这个函数是做什么的。 glLoadIdentity glLo...
  • dcrmg
  • dcrmg
  • 2016-11-09 23:38:39
  • 4971

linux中 OpenGL 出现 undefined reference to `gluLookAt' 报错

出错状况 lh@ubuntu:~$ gcc -lglut -o example example.c /tmp/ccT7NQY2.o: In function `init': example.c:(....
  • u013383042
  • u013383042
  • 2015-12-17 18:16:37
  • 958

小白学opengl之gluLookAt函数例子

#include #include #include #include using namespace std; GLdouble wx,wy,wz;/*returned world x,...
  • yangkaikwill
  • yangkaikwill
  • 2014-12-24 01:19:21
  • 678
    最新评论