This is my own implemetation of the gluLookAt and gluPerspective function:
[b]public static unsafe void LookAtd(double eyex, double eyey, double eyez, double centerx, double centery, double centerz, double upx, double upy, double upz)
{
var forward = new double[3];
var side = new double[3];
var up = new double[3];
var m = new float[4, 4];
forward[0] = eyex - centerx;
forward[1] = eyey - centery;
forward[2] = eyez - centerz;
forward = normalize(forward);
up[0] = upx;
up[1] = upy;
up[2] = upz;
//crossf(forward, up, side)
side[0] = forward[1]*up[2] - forward[2]*up[1];
side[1] = forward[2]*up[0] - forward[0]*up[2];
side[2] = forward[0]*up[1] - forward[1]*up[0];
//fin crossf(forward, up, side)
//crossf(side, forward, up)
up[0] = side[1] * forward[2] - side[2] * forward[1];
up[1] = side[2] * forward[0] - side[0] * forward[2];
up[2] = side[0] * forward[1] - side[1] * forward[0];
//fin crossf(side, forward, up)
side = normalize(side);
up = normalize(up);
//__identf(&m[0][0])
m[0, 3] = m[1, 3] = m[2, 3] = m[3, 0] = m[3, 1] = m[3, 2] = 0;
m[3, 3] = 1;
//fin __identf(&m[0][0])
m[0, 0] = (float)side[0];
m[1, 0] = (float)side[1];
m[2, 0] = (float)side[2];
m[0, 1] = (float)up[0];
m[1, 1] = (float)up[1];
m[2, 1] = (float)up[2];
m[0, 2] = (float)forward[0];
m[1, 2] = (float)forward[1];
m[2, 2] = (float)forward[2];
fixed (float* punterom = m)
{
gl.MultMatrixf(punterom);
}
gl.Translatef((float)-eyex, (float)-eyey, (float)-eyez);
}
private static double[] normalize(double[] arrayIn)
{
var arrayOut = new double[3];
var r = Math.Sqrt(arrayIn[0] * arrayIn[0] + arrayIn[1] * arrayIn[1] +
arrayIn[2] * arrayIn[2]);
if (r == 0)
throw new Exception("Vector nulo");
arrayOut[0] = arrayIn[0] / r;
arrayOut[1] = arrayIn[1] / r;
arrayOut[2] = arrayIn[2] / r;
return arrayOut;
}
public static unsafe void Perspectivef(float fovy, float aspect, float n, float f)
{
var m = new float[4,4];
float s;
float cot;
var dz = f - n;
var rad = fovy / 2.0f * __PI / 180.0f;
s = (float) Math.Sin(rad);
if (dz == 0 || s == 0 || aspect == 0)
return;
cot = (float) (Math.Cos(rad)/s);
//__identf(&m[0][0]);
m[0, 1] = m[0, 2] = m[0, 3] = m[1, 0] = m[1, 2] = m[1, 3] = m[2, 0]
= m[2, 1] = m[3, 0] = m[3, 1] = 0;
//fin __identf(&m[0][0]);
m[0, 0] = cot/aspect;
m[1, 1] = cot;
m[2, 2] = -(f + n)/dz;
m[2, 3] = -1.0f;
m[3, 2] = -2.0f * f * n / dz;
m[3, 3] = 0.0f;//0.0f;
fixed (float* punterom = m)
{
gl.MultMatrixf(punterom);
}
}
[/b]
First, i do a OpenGL inizialization:
[b]gl.EnableClientState(gl.GL_VERTEX_ARRAY);
gl.ShadeModel(gl.GL_FLAT);
gl.BlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
gl.ClearColor(_clearColor.Red, _clearColor.Green, _clearColor.Blue, _clearColor.Alpha);
gl.MatrixMode(gl.GL_PROJECTION);
gl.LoadIdentity();
gl.Viewport(0, 0, _openGLControl.Width, _openGLControl.Height);
gluLibrary.Perspectivef(45,Width/Height, 1f, 100.0f);
gl.MatrixMode(gl.GL_MODELVIEW);
gl.LoadIdentity();[/b]
Then, the program temporarly run a Draw function:
void DrawOpenGL()
{
DrawEnvironment();
ViewGenerator();
}
in detail, ViewGenerator:
[b]gl.MatrixMode(gl.GL_MODELVIEW);
gl.LoadIdentity(); glu.LookAtd(ex,ey,ez,cx,cy,cz,ux,uy,uz); [/b]
I hope this information is helpful. Thank you!