Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: normals?

  1. #1

    normals?

    i wrote a program for read a file format and here is the code:

    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
    Programming makes my head burn.

  2. #2
    Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    296

    Re: normals?

    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 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

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Cardiff University
    Posts
    668

    Re: normals?

    Normals are the information that your computer uses so that it know in which direction each polygon is facing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •