PDA

View Full Version : normal problems? or maybe light



zauron
05-11-2002, 01:38 AM
Well i am trying to get lights into my scene, but it looks just plain veird. http://home.no.net/zauron/opengl.jpg .

Here is some of my code:



glTranslatef(x, y, z);
glRotatef(rx, 1.0f, 0.0f, 0.0f);
glRotatef(ry, 0.0f, 1.0f, 0.0f);
glRotatef(rz, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES); // Start Drawing Triangles
for (int i = 0; i < numtriangles; i++){
glNormal3f( normal[i].x, normal[i].y, normal[i].z);
glTexCoord2f(triangle[i].vertex[0].u,triangle[i].vertex[0].v);
glVertex3f(triangle[i].vertex[0].x, triangle[i].vertex[0].y, triangle[i].vertex[0].z); // Set The TexCoord And Vertice

glTexCoord2f(triangle[i].vertex[1].u,triangle[i].vertex[1].v);
glVertex3f(triangle[i].vertex[1].x, triangle[i].vertex[1].y, triangle[i].vertex[1].z); // Set The TexCoord And Vertice

glTexCoord2f(triangle[i].vertex[2].u,triangle[i].vertex[2].v);
glVertex3f(triangle[i].vertex[2].x, triangle[i].vertex[2].y, triangle[i].vertex[2].z); // Set The TexCoord And Vertice
}
glEnd();

Here is how i load models



int ReadVtxString(char *b, FILE *f){
int tmp;
do{
fgets(b, 255, f);
/* nasty hack to get number of vertexes */
if(b[0] == '/'){
tmp = atoi(&amp;b[3]);
if(tmp !=0){
return tmp;
}
}
}while(b[0] == '/' &amp;#0124; &amp;#0124; b[0] == ' ' &amp;#0124; &amp;#0124; b[0] == '\n' &amp;#0124; &amp;#0124; b[0] == '.');
return -1;
}

int LoadSectorVtx(SECTOR *sec, char *filename){
int faces; // nimber of faces
int *indexarray; // array to hold the faces or vertex indexes
int verts; // number of vertices(sp)
VERTEX *vertarray; // array to hold the vertexdata
VERTEX *normalarray; // array to hold the normal data
char buff[255]; // readbuffer
float x, y, z, u, v; // temporary xyzuv vars
float nx, ny, nz; // temporary xyz normal vars
int ix, iy, iz; // temporary face vars
FILE *file = fopen(filename, "r");
if(!file){
return FALSE;
}
verts = ReadVtxString(buff, file); // get number of vertices
vertarray = new VERTEX[verts]; // make room for them all in the array
normalarray = new VERTEX[verts]; // there are justas many normals as vertices
for (int i = 0; i < verts; i++) // Loop Through All The verts
{
ReadVtxString(buff, file); // Read String To Work With
sscanf(buff, "%f %f %f %f %f %f %f %f", &amp;x, &amp;y, &amp;z, &amp;nx, &amp;ny, &amp;nz, &amp;u, &amp;v);
// Store Values Into Respective arrays
vertarray[i].x = x;
vertarray[i].y = y;
vertarray[i].z = z;
vertarray[i].u = u;
vertarray[i].v = v;
normalarray[i].x = nx;
normalarray[i].y = ny;
normalarray[i].z = nz;
}
/* read faces section */
faces = ReadVtxString(buff, file); // get number of faces
indexarray = new int[faces*3]; // make room for all the data in the faces
// there are 3 ints per face
for(i = 0; i < faces; i++){ // loop trough all the faces
ReadVtxString(buff, file); // read one line
sscanf(buff, "%d %d %d", &amp;ix, &amp;iy, &amp;iz);
indexarray[i*3+0] = ix; // store the data in their place
indexarray[i*3+1] = iy;
indexarray[i*3+2] = iz;
}
sec->numtriangles = faces; // set number of triangles aka faces in the sector
sec->triangle = new TRIANGLE[faces]; // make room for them all
sec->normal = new VERTEX[faces]; // the normals as well
for(i = 0; i < faces; i++){ // loop trough all the faces
for(int c = 0; c < 3; c++){ // for every face there are 3 coords
/* read the data into the sector */
sec->triangle[i].vertex[c].x = vertarray[indexarray[i*3+c]].x;
sec->triangle[i].vertex[c].y = vertarray[indexarray[i*3+c]].y;
sec->triangle[i].vertex[c].z = vertarray[indexarray[i*3+c]].z;
sec->triangle[i].vertex[c].u = vertarray[indexarray[i*3+c]].u;
sec->triangle[i].vertex[c].v = vertarray[indexarray[i*3+c]].v;
sec->normal[i].x = vertarray[indexarray[i*3+c]].x;
sec->normal[i].y = vertarray[indexarray[i*3+c]].y;
sec->normal[i].z = vertarray[indexarray[i*3+c]].z;
}
}
/* cleanup */
delete [] indexarray;
delete [] vertarray;
delete [] normalarray;
fclose(file);
return TRUE;
}