opengl gluLookAt函数实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tengfei461807914/article/details/79969992

函数的原型如下:

 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;
}
阅读更多

MFC OpenGL gluLookAt

05-26

想做一个基于MFC的漫游,在OnKeyDown中响应改变gulLookAt视角和视点从而实现漫游功能,但是发现按键时,视角并没有改变,不知道是什么原因,程序如下:rnvoid CWanderView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) rnrn // TODO: Add your message handler code here and/or call defaultrn float speed=0.2f; //步长rn if(VK_SHIFT==nChar) speed=speed*4; //按下SHIFT键,步长乘以4rn rn if (VK_LEFT==nChar)rn rn g_angle-=speed*2; //左转rn rn if(VK_RIGHT==nChar) rn rn g_angle+=speed*2; //右转 rn rn rad_xz =float(pi*g_angle/180.0f); //计算SIN和COS函数中需要的参数。rn ///////////////////////观察点的设置/////////////////////rn if(VK_UP==nChar)rn rn g_eye[2]+=(float)(sin(rad_xz)*speed); // 前进rn g_eye[0]+=(float)(cos(rad_xz)*speed);rn rn rn if(VK_DOWN==nChar)rn rn g_eye[2]-=(float)(sin(rad_xz)*speed); // 后退rn g_eye[0]-=(float)(cos(rad_xz)*speed);rn rn g_eye[1]=1.8f;rn /////////////////////////设置摄像机,即目标点的位置/////////////rn g_look[0]=(float)(g_eye[0]+100*cos(rad_xz));rn g_look[2]=(float)(g_eye[2]+100*sin(rad_xz));rn g_look[1]=g_eye[1];rnrn// DrawScene(); //场景函数,画网格地面等rn glLoadIdentity();rn gluLookAt(g_eye[0],g_eye[1],g_eye[2],g_look[0],g_look[1],g_look[2],0.0,1.0,0.0);rn CView::OnKeyDown(nChar, nRepCnt, nFlags);rnrn 程序应该没有问题,基本上都是唐明里那本书里面的,但是我不明白为什么我的gluLookAt函数没有作用呢?我调试过,OnKeyDown消息处理没有问题,对应的按键按下时,会有g_eye和g_look的改变,希望高手给我解答一下,我新手,也没有分。。等有分了一定给大家。。。谢谢

没有更多推荐了,返回首页