Saturday, December 27, 2008

gluPerspective to glFrustrumf

gluPerspective is a nice, intuitive helper function for setting the projection matrix in OpenGL. However, it's part of the glu library, which doesn't seem to be (readily) available for the iPhone (OpenGL ES).

Here is how to call glFrustrumf with the values you would give to gluPerspective:

float fov = 45;
float near = 0.01, far = 10;
float aspect = (float)windowWidth / (float)windowHeight;

// glPerspective(fov, aspect, near, far);

top = tan(toRad(fov)) * near;
bottom = -top;
left = aspect * bottom;
right = aspect * top;

glFrustumf(left, right, bottom, top, near, far);

Saturday, December 20, 2008

"error: syntax error before '*' token"

I recently began porting Jumper to the iPhone as a winter project. After adding another source file (Level.c in this case), I started getting thousands of build errors, along the lines of:

error: syntax error before '@' token
error: syntax error before '*' token

These were from files in /Developer/Platforms/iPhoneSimulator.platform/..., which was not encouraging. Something weird was going on.

Actually, it was really simple. Turns out that should have been Level.m, not .c. Whoops.

Saturday, December 13, 2008

Drawing triangles for height maps

Two ways of drawing triangles for HeightMap h1:

Simple, like this:

----
|/|/
----



int ndx = 0;
for (int r = 0; r < h1->rows-1; r ++) {
glBegin(GL_TRIANGLE_STRIP); {
for (int c = 0; c < h1->cols; c ++, ndx ++) {
int cols = h1->cols;
glVertex3fv(h1->verts[ndx+cols].xyz);
glVertex3fv(h1->verts[ndx].xyz);
}
glEnd();
}
}



More complex, but lends itself to simplification over distance, like this:

----
|\/|
----
|/\|
----



for (int r = 1; r < h1->rows-1; r += 2) {
for (int c = 1; c < h1->cols-1; c += 2) {
int cols = h1->cols;
int mid = r*cols + c;
glBegin(GL_TRIANGLE_FAN); {
glVertex3fv(h1->verts[mid].xyz);
glVertex3fv(h1->verts[mid-1].xyz);
glVertex3fv(h1->verts[mid-cols-1].xyz);
glVertex3fv(h1->verts[mid-cols].xyz);
glVertex3fv(h1->verts[mid-cols+1].xyz);
glVertex3fv(h1->verts[mid+1].xyz);
glVertex3fv(h1->verts[mid+cols+1].xyz);
glVertex3fv(h1->verts[mid+cols].xyz);
glVertex3fv(h1->verts[mid+cols-1].xyz);
glVertex3fv(h1->verts[mid-1].xyz);
}
glEnd();
}
}

Tuesday, December 9, 2008

Keep ssh connections open

While in college, I've had to do a lot of work by sshing into school machines. Unfortunately, they allow a rather short "idle" time before they kick me out of whatever I was doing (littering my code folders with .swp files in the process :-P)

While one way around this is to run all your sessions in screen, it's still annoying to me to have my sessions freeze every time I go for a snack.

Fortunately, you can keep your sessions open by putting the following into ~/.ssh/config on your (client) machine:

ServerAliveInterval 60

According to the man page:
"Sets a timeout interval in seconds after which if no data has been received from the server, ssh will send a message through the encrypted channel to request a response from the server."