PDA

View Full Version : normals?



colinisinhere
02-17-2003, 02:45 PM
i wrote a program for read a file format and here is the code:




struct Vertex
{
GLfloat unknown_0;
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat u;
GLfloat v;
GLfloat unknown_1;
};

struct Triangle
{
GLfloat unknown_0;
int indices[3];
int normals[3];
GLfloat unknown_1;
};

struct Normal
{
GLfloat x;
GLfloat y;
GLfloat z;
};

class Mesh
{
public:
unsigned short num_Vertex;
Vertex* vertices;
unsigned short num_Triangle;
Triangle* tris;
unsigned short num_Normal;
Normal* norms;
void Load(char* filename);
void Render(void);
};
void Mesh::Load(char* filename)
{
ifstream ifs(filename);
ifs >> num_Vertex;
vertices = new Vertex [num_Vertex];
for(int i = 0; i < num_Vertex; i++)
{
ifs >> vertices[i].unknown_0;
ifs >> vertices[i].x;
ifs >> vertices[i].y;
ifs >> vertices[i].z;
ifs >> vertices[i].u;
ifs >> vertices[i].v;
ifs >> vertices[i].unknown_1;
}
ifs >> num_Normal;
norms = new Normal [num_Normal];
for(int c = 0; c < num_Normal; c++)
{
ifs >> norms[c].x;
ifs >> norms[c].y;
ifs >> norms[c].z;
}
ifs >> num_Triangle;
tris = new Triangle [num_Triangle];
for(int j = 0; j < num_Triangle; j++)
{
ifs >> tris[j].unknown_0;
ifs >> tris[j].indices[0];
ifs >> tris[j].indices[1];
ifs >> tris[j].indices[2];
ifs >> tris[j].normals[0];
ifs >> tris[j].normals[1];
ifs >> tris[j].normals[2];
ifs >> tris[j].unknown_1;
}
return;
}
void Mesh::Render(void)
{
glBegin(GL_TRIANGLES);
for(int i = 0; i < num_Triangle; i++)
{
glNormal3f(norms[tris[i].normals[0]].x, norms[tris[i].normals[0]].y, norms[tris[i].normals[0]].z);
glVertex3f(vertices[tris[i].indices[0]].x, vertices[tris[i].indices[0]].y, vertices[tris[i].indices[0]].z);
glNormal3f(norms[tris[i].normals[1]].x, norms[tris[i].normals[1]].y, norms[tris[i].normals[1]].z);
glVertex3f(vertices[tris[i].indices[1]].x, vertices[tris[i].indices[1]].y, vertices[tris[i].indices[1]].z);
glNormal3f(norms[tris[i].normals[2]].x, norms[tris[i].normals[2]].y, norms[tris[i].normals[2]].z);
glVertex3f(vertices[tris[i].indices[2]].x, vertices[tris[i].indices[2]].y, vertices[tris[i].indices[2]].z);
}
glEnd();
return;
}


it loads fine and renders fine but i dont think the normals are right... how exactly do normals work? and whats wrong with the normals in this code?


thanks

Miguel_dup1
02-17-2003, 03:31 PM
Well, I dont know exactly what format you are reading, so I cannot help you with your code.
But as far as normal goes though. A normal are unit vector that are perpendicular to a surface; mathematical terms is also known as an orthnormal vector http://www.opengl.org/discussion_boards/ubb/smile.gif Simple. How do you get them???
You need to do some nice cross product of the vectors you need the normal of.

Unless the file you are reading gives you the normals already, you will have to compute them yourself with the vertices you get from the file. Also, normals are commonly used for lighting, so you wont see those normals working until you enable lighting.


Regards,
Miguel Castillo

Gavin
02-19-2003, 07:10 AM
Normals are the information that your computer uses so that it know in which direction each polygon is facing.