开发者

Help with camera in OpenGL

开发者 https://www.devze.com 2023-01-15 16:45 出处:网络
Right now my application rotates on camera.rotationX, Y Z and does a -translation of camera.x,y,z on the modelview matrix. How could I equivocate this to a call togluLookAt?

Right now my application rotates on camera.rotationX, Y Z and does a -translation of camera.x,y,z on the modelview matrix. How could I equivocate this to a call to gluLookAt?

Thanks

basically how could I get this:

    glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();

//starts here
     glRotatef(Camera.rotx,1,0,0);
     glRotatef(Camera.roty,0,1,0);
     glRotatef(Camera.rotz,0,0,1);

     glTranslatef(-Camera.x , -Camera.y - 4.5,-Camera.z );

Instead into a call开发者_如何学编程 to gluLookAt which would make it look the same.

Thanks

The reason I want to do this is because I found a fustrum culling class that works with gluLookAt as seen below

void FrustumG::setCamDef(Vec3 &p, Vec3 &l, Vec3 &u) {

    Vec3 dir,nc,fc,X,Y,Z;

    Z = p - l;
    Z.normalize();

    X = u * Z;
    X.normalize();

    Y = Z * X;

    nc = p - Z * nearD;
    fc = p - Z * farD;

    ntl = nc + Y * nh - X * nw;
    ntr = nc + Y * nh + X * nw;
    nbl = nc - Y * nh - X * nw;
    nbr = nc - Y * nh + X * nw;

    ftl = fc + Y * fh - X * fw;
    ftr = fc + Y * fh + X * fw;
    fbl = fc - Y * fh - X * fw;
    fbr = fc - Y * fh + X * fw;

    pl[TOP].set3Points(ntr,ntl,ftl);
    pl[BOTTOM].set3Points(nbl,nbr,fbr);
    pl[LEFT].set3Points(ntl,nbl,fbl);
    pl[RIGHT].set3Points(nbr,ntr,fbr);
    pl[NEARP].set3Points(ntl,ntr,nbr);
    pl[FARP].set3Points(ftr,ftl,fbl);
}


Don't use gluLookAt, glRotate or glOrtho there all deprecated in the OpenGL 3 spec. Go grab the glm library http://glm.g-truc.net/ and do this instead...

Camera example.

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform2.hpp>

glm::vec4 position = glm::vec4( 1.0f, 0.0f, 0.0f, 1.0f );
glm::mat4 view = glm::lookAt( glm::vec3(0.0,0.0,5.0),
glm::vec3(0.0,0.0,0.0),
glm::vec3(0.0,1.0,0.0) );
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate( model, 90.0f, glm::vec3(0.0f,1.0f,0.0) );
glm::mat4 mv = view * model;
glm::vec4 transformed = mv * position;

Deprecated function replacements

http://glm.g-truc.net/api-0.9.2/a00003.html


Well your eye position E will be (Camera.x, Camera.y + 4.5, Camera.z). The default view vector V is (0, 0, -1) in OpenGL, if I remember correctly and default up vector U is (0, 1, 0).

So you rotate these two vectors with your camera rotation. See http://en.wikipedia.org/wiki/Rotation_matrix or http://en.wikipedia.org/wiki/Quaternion. Center is then the rotated view vector View vector + Camera vector. Up vector is simply the rotated up vector.

gluLookAt(Camera.x, Camera.y + 4.5, Camera.z, Camera.x + V.x, Camera.y + V.y, Camera.z + V.z, U.x, U.y, U.z)
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz : glDouble);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号