Replacement for gluPerspective and gluLookAt.

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Replacement for gluPerspective and gluLookAt.

Post by D.J.Peters »

Code: Select all

sub Perspective(fov as double, ratio as double, zNear as double, zFar as double)
  dim as double fH = tan( fov / 360.0 * 3.1415926535897932384626433832795 ) * zNear
  dim as double fW = fH * ratio
  glFrustum(-fW,fW,-fH,fH,zNear,zFar)
end sub

sub LookAt(ex as double, ey as double, ez as double, _
           lx as double, ly as double, lz as double, _
           ux as double, uy as double, uz as double )
   dim as double z(2) = {ex-lx,ey-ly,ez-lz}
  dim as double l=z(0)*z(0)+z(1)*z(1)+z(2)*z(2)
  if (l) then
    l=1.0/sqr(l):z(0)*=l:z(1)*=l:z(2)*=l
  end if
  dim as double x(2)={uy*z(2)-uz*z(1), _
                       -ux*z(2)+uz*z(0), _
                        ux*z(1)-uy*z(0)}
  l=x(0)*x(0)+x(1)*x(1)+x(2)*x(2)
  if (l) then
    l=1.0/sqr(l):x(0)*=l:x(1)*=l:x(2)*=l
  end if
  dim as double y(2)={z(1)*x(2)-z(2)*x(1), _
                       -z(0)*x(2)+z(2)*x(0), _
                        z(0)*x(1)-z(1)*x(0)}

  l=y(0)*y(0)+y(1)*y(1)+y(2)*y(2)
  if (l) then
    l=1.0/sqr(l):y(0)*=l:y(1)*=l:y(2)*=l
  end if
  dim as double m4x4(15)=>{x(0),y(0),z(0),0, _
                             x(1),y(1),z(1),0, _
                             x(2),y(2),z(2),0, _
                                0,   0,   0,1}
  glMultMatrixd @m4x4(0)
  glTranslated -ex,-ey,-ez
end sub
Last edited by D.J.Peters on Apr 27, 2017 6:09, edited 1 time in total.
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Replacement for gluPerspective and gluLookAt.

Post by Roland Chastain »

Hello D.J.Peters!

Could you provide a code example, or a link to an existing one ?

And why that replacement ?

Best regards.

Roland
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Re: Replacement for gluPerspective and gluLookAt.

Post by D.J.Peters »

Often you have to link your OpenGL code to glu32.dll or libglu.so just because two commands.
gluLookAt() and gluPerspective()
I removed these dependencies from my current project.

Joshy
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Replacement for gluPerspective and gluLookAt.

Post by Roland Chastain »

D.J.Peters wrote:Often you have to link your OpenGL code to glu32.dll or libglu.so just because two commands.
gluLookAt() and gluPerspective()
Thank you for the explanation.
Post Reply