PDA

View Full Version : GLU deprecated



MuteClown
21-03-2011, 07:41 PM
I heard that GLU shouldn't be used any more since it contains a lot of deprecated code now. What do you guys think?
Does anyone have other alternative to such things as gluLookAt?

JSoftware
21-03-2011, 09:22 PM
Just do some matrix math. Matrices are fun :D

Needs the math unit for the CoTan function


procedure gluPerspective(fovy, aspect, znear, zfar: single);
var res: TMatrix;
f: single;
begin
{ Oblique projection matrix - Got formulas from OpenGL docs :) }
f := CoTan(fovy/2);

FillChar(res[0], sizeof(TMatrix), 0);

res[0] := f/aspect;
res[5] := f;
res[10] := (zfar+znear)/(znear-zfar);
res[11] := (2*zfar*znear)/(znear-zfar);
res[14] := -1;

glMultMatrixf(@res[0]);
end;

procedure Cross3x3(out ox,oy,oz: single; a,b,c,x,y,z: single);
begin
{
calculate determinant - i,j,k are the new basis

i j k
a b c
x y z

i*b*z+j*c*x+k*a*y-k*x*b-i*y*c-j*z*a
}
ox := b*z-y*c;
oy := c*x-z*a;
oz := a*y-x*b;
end;

function Norm2(x,y,z: single): single;
begin
result := sqrt(x*x+y*y+z*z);
end;

procedure gluLookAt(eyeX, eyeY, eyez, centerX, centerY, centerZ, upX, upY, upZ: single);
var dx,dy,dz, sx,sy,sz, ux,uy,uz, mag: single;
res: TMatrix;
begin
{ Create projector from orthonormal basis }
FillChar(res[0], sizeof(TMatrix), 0);

{ Get direction - the new "z" basis }
dx := centerX-eyeX; dy := centerY-eyeY; dz := centerZ-eyeZ;
{ Normalize direction }
mag := Norm2(dx,dy,dz);
dx := dx/mag; dy := dy/mag; dz := dz/mag;

{ Normalize up vector }
mag := Norm2(upX,upY,upZ);
upX := upX/mag; upY := upY/mag; upZ := upZ/mag;

{ Find vector orthogonal to "z" and up - the new "x" basis
Since direction and up are normalized, the output will be normalized too }
Cross3x3(sx,sy,sz, dx,dy,dz, upX,upY,upZ);

{ Find vector orthogonal to "x" and "z" - the new "y" basis }
Cross3x3(upX,upY,upZ, sx,sy,sz, dx,dy,dz);

res[0] := dx; res[1] := dy; res[2] := dz;
res[4] := upX; res[5] := upY; res[6] := upZ;
res[8] := -dx; res[9] := -dy; res[10] := -dz;
res[15] := 1;

glMultMatrixf(@res[0]);
glTranslatef(eyeX,eyeY,eyeZ);
end;

Ingemar
22-09-2011, 04:20 AM
Writing your own gluLookAt is a standard exam question in my CG course. I don't really like gluPerspective, it makes frustum culling confusing.

But I really don't like that everything is deprecated. Replacing standard code with nonstandard, what's the point? Moving everything to shaders, I guess.

paul_nicholls
22-09-2011, 05:15 AM
Hi JSoftware,
I just noticed something about a comment in your code:

{ Find vector orthogonal to "z" and up - the new "x" basis
Since direction and up are normalized, the output will be normalized too }
Cross3x3(sx,sy,sz, dx,dy,dz, upX,upY,upZ);

Well, I believe a cross-product returns a vector whos length is the area (or 1/2 area?) inside of the two input vectors...so this means that the resulting vector is NOT guaranteed to be normalized even though the inputs are :)

Feel free to correct me if you wish, I don't mind being proven wrong :D

cheers,
Paul

Ingemar
22-09-2011, 06:12 AM
Hi JSoftware,
I just noticed something about a comment in your code:

{ Find vector orthogonal to "z" and up - the new "x" basis
Since direction and up are normalized, the output will be normalized too }
Cross3x3(sx,sy,sz, dx,dy,dz, upX,upY,upZ);

Well, I believe a cross-product returns a vector whos length is the area (or 1/2 area?) inside of the two input vectors...so this means that the resulting vector is NOT guaranteed to be normalized even though the inputs are :)

Feel free to correct me if you wish, I don't mind being proven wrong :D

cheers,
Paul

Yes, if the two input vectors are orthonormal, orthogonal and normalized, only then you know that the cross product be a unit vector. And the three vector will then be an orthonormal base.

I always have a solid set of linear algebra functions around to do things like these. Replacing gluLookAt is particularly straight-forward, but you need these vector and matrix functions for pretty much everything. My algebra unit includes vector addition/subtraction, norm, normalization, dot and cross products, matrix-matrix and matrix-vector multiplication, vector splitting into orthogonal components, matrix inverse, making rotation matrices around arbitrary axis... OpenGL incudes much of that but I often need to do those things on the CPU too.

paul_nicholls
22-09-2011, 06:30 AM
ok, cool :)

cheers,
Paul

Ingemar
22-09-2011, 06:50 AM
And like you say, the length is indeed the area of a rhombus/diamond where the two vectors are two of the sides. But I rarely use that property, the direction of the cross product is what I use the most.