PDA

View Full Version : OpenGL ES 1.x and LookAt problem



LucasEscucha
03-17-2010, 06:05 AM
Hi, I'm Lucas. I'm working with a PXA320 processor, based in the ARMV4i core. I'm developing an aplication that uses Opengl ES 1.x. When I use the function Perspective and LookAt, of Glu library, the Opengl doesn't display correctly. The screen shows figures of different colors, but when I look on GetError function, it allways returns zero. After a while, the image freezes. I don`t know what is the problem. Please help me, I need to emulate a 3d enviroment.
Please look at the following images I captured

Top view (correct):
http://i40.tinypic.com/t670no.jpg

From an angle view(incorrect):
http://i43.tinypic.com/29kxbw6.jpg

Render error:
http://i44.tinypic.com/1608v2x.jpg

yooyo
03-18-2010, 04:38 AM
Can you post how do you use gluLookAt?

dorbie
03-18-2010, 01:26 PM
Bear in mind you may also have limited numeric precision on your device that falls well short of what you can get on the desktop.

This will not necessarily produce a gl error state.

LucasEscucha
03-19-2010, 06:27 AM
This is my own implemetation of the gluLookAt and gluPerspective function:

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);
}
}

First, i do a OpenGL inizialization:

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();

Then, the program temporarly run a Draw function:

void DrawOpenGL()
{
DrawEnvironment();
ViewGenerator();
}

in detail, ViewGenerator:

gl.MatrixMode(gl.GL_MODELVIEW);
gl.LoadIdentity(); glu.LookAtd(ex,ey,ez,cx,cy,cz,ux,uy,uz);

I hope this information is helpful. Thank you!

yooyo
03-19-2010, 07:49 AM
This is from MESA gluLookAt impl.


static void __gluMakeIdentityf(GLfloat m[16])
{
m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
}

static void normalize(float v[3])
{
float r;

r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
if (r == 0.0) return;

v[0] /= r;
v[1] /= r;
v[2] /= r;
}

static void cross(float v1[3], float v2[3], float result[3])
{
result[0] = v1[1]*v2[2] - v1[2]*v2[1];
result[1] = v1[2]*v2[0] - v1[0]*v2[2];
result[2] = v1[0]*v2[1] - v1[1]*v2[0];
}

void GLAPIENTRY
gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
GLdouble upz)
{
float forward[3], side[3], up[3];
GLfloat m[4][4];

forward[0] = centerx - eyex;
forward[1] = centery - eyey;
forward[2] = centerz - eyez;

up[0] = upx;
up[1] = upy;
up[2] = upz;

normalize(forward);

/* Side = forward x up */
cross(forward, up, side);
normalize(side);

/* Recompute up as: up = side x forward */
cross(side, forward, up);

__gluMakeIdentityf(&m[0][0]);
m[0][0] = side[0];
m[1][0] = side[1];
m[2][0] = side[2];

m[0][1] = up[0];
m[1][1] = up[1];
m[2][1] = up[2];

m[0][2] = -forward[0];
m[1][2] = -forward[1];
m[2][2] = -forward[2];

glMultMatrixf(&m[0][0]);
glTranslated(-eyex, -eyey, -eyez);
}


and gluPerspective:


#define __glPi 3.14159265358979323846

void GLAPIENTRY
gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble m[4][4];
double sine, cotangent, deltaZ;
double radians = fovy / 2 * __glPi / 180;

deltaZ = zFar - zNear;
sine = sin(radians);
if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
return;
}
cotangent = COS(radians) / sine;

__gluMakeIdentityd(&m[0][0]);
m[0][0] = cotangent / aspect;
m[1][1] = cotangent;
m[2][2] = -(zFar + zNear) / deltaZ;
m[2][3] = -1;
m[3][2] = -2 * zNear * zFar / deltaZ;
m[3][3] = 0;
glMultMatrixd(&m[0][0]);
}

LucasEscucha
03-22-2010, 08:39 AM
Hi. These impl. are equivalent to those which I wrote, but mines are writtes in C# language. I do not think that these functions are the problem. What else do you think could cause problems?

LucasEscucha
03-29-2010, 07:59 AM
Please,help me. I have not been able to solve my problem and I do not know what to do. Thank you!!

dorbie
04-01-2010, 02:20 PM
Please re-read my reply.

LucasEscucha
04-05-2010, 06:01 AM
Dear dorbie. I understand what you say. But so it does not find a way to solve my problem. Please be more specific or givme any way to start the way to solve my problem.

Regards