Hi there,
i got some trouble with the rendering of md3 models.
Some days ago, i tried the md2 format and everything was just fine.
So, I tried the md3 format. But the rendered md3 model looks strange, or let's say "not very smooth":
Take a look for yourself:
md2 head:
md3 head:
I have no idea why is that, but - assuming my model loading is right - i could imagine that my calculation of the normals is not correct. (For md2 I'm using the precalculated normals in quake's anorms.h, btw.)
The data structures
typedef struct {
md3_mesh_header header;
md3_skin *skins;
md3_triangle *triangles;
md3_tex_coords *tex_coords;
md3_vertex *vertices;
GLuint texture;
bool set_texture;
} md3_mesh;
typedef struct {
short coords[3];
char normal[2];
} md3_vertex;
typedef struct {
int vertex[3];
} md3_triangle;
float anorms[256][256][3];
calculating the normals:
float a, b;
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
a = i * 2.0 * 3.14159265358979 / 255.0;
b = j * 2.0 * 3.14159265358979 / 255.0;
anorms[j][0] = (float) (cos(b) * sin(a));
anorms[j][1] = (float) (sin(b) * sin(a));
anorms[j][2] = (float) cos(a);
}
}
Don't know where I got that from, but another way is that:
vector norm_coords;
char *norm = meshes[0].vertices[index].normal;
double pi = 3.1415927;
float lat = (norm[0] & 255) * (2.0 * pi) / 255.0;
float lng = (norm[1] & 255)* ( 2.0 * pi) / 255.0;
norm_coords[0] = cos(lng) * sin(lat);
norm_coords[1] = sin(lng) * sin(lat);
norm_coords[2] = cos(lat);
glNormal3fv(norm_coords);
Pretty the same and the results are - of course - the same...
The rendering:
glBegin(GL_TRIANGLES);
float scale = 1.0 / 64.0;
for (int i = 0; i < meshes[0].header.num_triangles; i++) {
for (int j = 0; j < 3; j++) {
int index = meshes[0].triangles.vertex[j];
glVertex3f((GLfloat) meshes[0].vertices[index].coords[0] * scale,
(GLfloat) meshes[0].vertices[index].coords[1] * scale,
(GLfloat) meshes[0].vertices[index].coords[2] * scale);
int u = meshes[0].vertices[index].normal[0];
int v = meshes[0].vertices[index].normal[1];
glNormal3f(anorms[v][0], anorms[v][1], anorms[v][2]);
}
}
glEnd();
It's just drawing the model's first frame without any textures or animation.
It's kind of messy. Sorry for that :) (But I'm still testing)
Maybe it's also important ... my lighting:
GLfloat amb[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat diff[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT1, GL_AMBIENT, amb);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diff);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
So, what am I doing wrong? Did I forget anything?
regards