I have a class that takes an arbitrary set of data points which represent a whole bunch of quads to make up a surface. The user gives this 3d matrix, and my class should render it. I have that much working, but now I want to make it so that proper normals are created for lighting… but I don’t know how to do it for arbitrary shapes. Alan_W gave me a start using some code he made for doing something like what I’m after, but the results don’t come out right. The current result is that it looks like I’m not calculating any normals at all. I don’t know what’s wrong, but something is Perhaps someone could help me figure out how to calculate these normals? Here is my current attempt:
public void setData (double[][][] data) {
this.data = data;
normals = new Vec3D[data.length][data[0].length][2];
for (int j = 0; j < data.length - 1; j++) {
for (int i = 0; i < data[j].length - 1; i++) {
Vec3D uu = new Vec3D();
uu.subtract(
new Vec3D(data[j+1][i][0], data[j+1][i][1], data[j+1][i][2]),
new Vec3D(data[j][i][0], data[j][i][1], data[j][i][2]));
Vec3D vv = new Vec3D();
vv.subtract(
new Vec3D(data[j+1][i+1][0], data[j+1][i+1][1], data[j+1][i+1][2]),
new Vec3D(data[j][i][0], data[j][i][1], data[j][i][2]));
normals[j][i][0] = new Vec3D();
normals[j][i][0].cross(uu, vv);
normals[j][i][0].normalize();
uu.subtract(
new Vec3D(data[j][i+1][0], data[j][i+1][1], data[j][i+1][2]),
new Vec3D(data[j+1][i+1][0], data[j+1][i+1][1], data[j+1][i+1][2]));
vv.subtract(
new Vec3D(data[j][i][0], data[j][i][1], data[j][i][2]),
new Vec3D(data[j+1][i+1][0], data[j+1][i+1][1], data[j+1][i+1][2]));
normals[j][i][1] = new Vec3D();
normals[j][i][1].cross(uu, vv);
normals[j][i][1].normalize();
}
}
}
and in my render method...
for (int j = 0; j < data.length - 1; j++) {
gl.glBegin(GL.GL_TRIANGLES);
for (int i = 0; i < data[j].length - 1; i++) {
gl.glNormal3d(normals[j][i][0].x, normals[j][i][0].y, normals[j][i][0].z);
gl.glVertex3dv(data[j][i]);
gl.glVertex3dv(data[j+1][i]);
gl.glVertex3dv(data[j+1][i+1]);
gl.glNormal3d(normals[j][i][1].x, normals[j][i][1].y, normals[j][i][1].z);
gl.glVertex3dv(data[j+1][i+1]);
gl.glVertex3dv(data[j][i+1]);
gl.glVertex3dv(data[j][i]);
}
gl.glEnd();
}