PDA

View Full Version : look! i don't need glu.dll anymore!



JernejL
11-08-2005, 11:56 AM
don't want to link to glu.dll but still need some functions from there? no problem, here is a "no-glu" solution:


// these functions are from http://oss.sgi.com/projects/ogl-sample/
procedure PickMatrix(x, y, deltax, deltay: GLfloat; viewport: array of integer);
procedure Perspective(fovy, aspect, zNear, zFar: GLfloat);
procedure LookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz: single);


// this is gluPickMatrix equivalent translated from ogl-sample.

procedure PickMatrix(x, y, deltax, deltay: GLfloat; viewport: array of integer);
begin
if &#40;deltax <= 0&#41; and &#40;deltay <= 0&#41; then exit;

&#40;* Translate and scale the picked region to the entire window *&#41;
glTranslatef&#40;
&#40;viewport&#91;2&#93; - 2 * &#40;x - viewport&#91;0&#93;&#41;&#41; / deltax,
&#40;viewport&#91;3&#93; - 2 * &#40;y - viewport&#91;1&#93;&#41;&#41; / deltay,
0&#41;;

glScalef&#40;viewport&#91;2&#93; / deltax, viewport&#91;3&#93; / deltay, 1.0&#41;;
end;

// this is gluPerspective equivalent translated from ogl-sample.
// matrix should be double but i use single precision

procedure Perspective&#40;fovy, aspect, zNear, zFar&#58; single&#41;;
var
m&#58; TMatrix4f;
sine, cotangent, deltaZ, radians&#58; single;

begin
radians&#58;= fovy / 2 * Pi / 180;

deltaZ&#58;= zFar - zNear;
sine&#58;= sin&#40;radians&#41;;
if &#40;&#40;deltaZ = 0&#41; and &#40;sine = 0&#41; and &#40;aspect = 0&#41;&#41; then exit;

cotangent&#58;= COS&#40;radians&#41; / sine;

m&#58;= IdentityHmgMatrix;
m&#91;0&#93;&#91;0&#93;&#58;= cotangent / aspect;
m&#91;1&#93;&#91;1&#93;&#58;= cotangent;
m&#91;2&#93;&#91;2&#93;&#58;= -&#40;zFar + zNear&#41; / deltaZ;
m&#91;2&#93;&#91;3&#93;&#58;= -1;
m&#91;3&#93;&#91;2&#93;&#58;= -2 * zNear * zFar / deltaZ;
m&#91;3&#93;&#91;3&#93;&#58;= 0;
glMultMatrixf&#40;@m&#91;0&#93;&#91;0&#93;&#41;;
end;

// this is gluPerspective equivalent translated from ogl-sample.
// matrix should be double but i use single precision

procedure normalize&#40;var v&#58; array of single&#41;;
var
r&#58; single;
begin
r&#58;= sqrt&#40;v&#91;0&#93; * v&#91;0&#93; + v&#91;1&#93; * v&#91;1&#93; + v&#91;2&#93; * v&#91;2&#93;&#41;;

if &#40;r = 0&#41; then exit;

v&#91;0&#93;&#58;= v&#91;0&#93; / r;
v&#91;1&#93;&#58;= v&#91;1&#93; / r;
v&#91;2&#93;&#58;= v&#91;2&#93; / r;
end;

procedure crossp&#40;var v1, v2, result&#58; array of single&#41;;
begin
result&#91;0&#93;&#58;= v1&#91;1&#93;*v2&#91;2&#93; - v1&#91;2&#93;*v2&#91;1&#93;;
result&#91;1&#93;&#58;= v1&#91;2&#93;*v2&#91;0&#93; - v1&#91;0&#93;*v2&#91;2&#93;;
result&#91;2&#93;&#58;= v1&#91;0&#93;*v2&#91;1&#93; - v1&#91;1&#93;*v2&#91;0&#93;;
end;

procedure LookAt&#40;eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz&#58; single&#41;;
var
i&#58; integer;
forw, side, up&#58; array&#91;0..2&#93; of single;
m&#58; TMatrix4f;
begin
forw&#91;0&#93;&#58;= centerx - eyex;
forw&#91;1&#93;&#58;= centery - eyey;
forw&#91;2&#93;&#58;= centerz - eyez;

up&#91;0&#93;&#58;= upx;
up&#91;1&#93;&#58;= upy;
up&#91;2&#93;&#58;= upz;

normalize&#40;forw&#41;;

&#40;* Side&#58;= forw x up *&#41;
crossp&#40;forw, up, side&#41;;
normalize&#40;side&#41;;

&#40;* Recompute up as&#58; up&#58;= side x forw *&#41;
crossp&#40;side, forw, up&#41;;

m&#58;= IdentityHmgMatrix;
m&#91;0&#93;&#91;0&#93;&#58;= side&#91;0&#93;;
m&#91;0&#93;&#91;1&#93;&#58;= side&#91;1&#93;;
m&#91;0&#93;&#91;2&#93;&#58;= side&#91;2&#93;;

m&#91;1&#93;&#91;0&#93;&#58;= up&#91;0&#93;;
m&#91;1&#93;&#91;1&#93;&#58;= up&#91;1&#93;;
m&#91;1&#93;&#91;2&#93;&#58;= up&#91;2&#93;;

m&#91;2&#93;&#91;0&#93;&#58;= -forw&#91;0&#93;;
m&#91;2&#93;&#91;1&#93;&#58;= -forw&#91;1&#93;;
m&#91;2&#93;&#91;2&#93;&#58;= -forw&#91;2&#93;;

glMultMatrixf&#40;@m&#41;;
glTranslated&#40;-eyex, -eyey, -eyez&#41;;
end;

and maybe you will need to have this defined:



type
TVector4f= array &#91;0..3&#93; of Single;
TMatrix4f= array &#91;0..3&#93; of TVector4f;

Sly
11-08-2005, 11:01 PM
You forgot to declare IdentityHmgMatrix. :)

But yes, removing dependencies on external libraries is a good thing.

BenBE
12-08-2005, 11:40 AM
Why aren't you using TGLfloat for function and parameter input types? Thus this library could be ported much more easyly.

But all the other stuff is a good beginning. Maybe you add some other often used functions.

JernejL
12-08-2005, 01:55 PM
Why aren't you using TGLfloat for function and parameter input types? Thus this library could be ported much more easyly.

But all the other stuff is a good beginning. Maybe you add some other often used functions.

tglfloat? what? i use my own delphi opengl headers i translated myself and has types just like original delphi and C header. is TGLfloat from glscene header? i separate gl and other code and types very seriously and separately

oh sorry about missing identityhmgmatrix, here it is:

IdentityHmgMatrix&#58; TMatrix4f = &#40;&#40;1, 0, 0, 0&#41;,
&#40;0, 1, 0, 0&#41;,
&#40;0, 0, 1, 0&#41;,
&#40;0, 0, 0, 1&#41;&#41;;

also, i can't remember any other "oftenly used" calls in glu.dll, these 3 are all what i use.

i'm going to soon release my opengl header and this mini glapp framework for which i did this.

JSoftware
12-08-2005, 03:09 PM
glfloat is specified in the opengl.pas unit. it's just a single

type
glfloat = single;
:wink:

nice work, jernejl. i couldn't get my projectivematrix to work until i realized by looking at your code that i had the code from a directx article on msdn :oops: Had to transpose first...

EDIT: gluproject, gluunproject, gluortho2d, glubuild2dmipmaps, gluscaleimage enough to work on :wink:

JernejL
13-08-2005, 09:31 PM
glfloat is specified in the opengl.pas unit. it's just a single

type
glfloat = single;
:wink:

nice work, jernejl. i couldn't get my projectivematrix to work until i realized by looking at your code that i had the code from a directx article on msdn :oops: Had to transpose first...

EDIT: gluproject, gluunproject, gluortho2d, glubuild2dmipmaps, gluscaleimage enough to work on :wink:

i don't really know how that matrix glulookat works ;) i took the opengl sample implementation source code from http://oss.sgi.com/projects/ogl-sample/ and translated it, i might consider translating some other functions as well now.

i had done some delphi-glu translating before using mesa code but that was much different and wierder and never worked :D