• Advertisement
Sign in to follow this  

Working with lighting.

This topic is 3480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I've been melting my brain with lighting textures and I'm not sure my normals are all calculating quite right. Here's my code for normal calculation and rendering, does this look wrong to anyone? First the init of lighting glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)WINX/(GLfloat)WINY,0.50f,75.0f); glEnable(GL_DEPTH_TEST); // glEnable(GL_NORMALIZE); glEnable (GL_TEXTURE_2D); // Somewhere in the initialization part of your program… glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // Create light components GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f }; GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat position[] = { -1.5f, 1.0f, -4.0f, 0.0f }; // Assign created components to GL_LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT0, GL_POSITION, position); // enable color tracking glEnable(GL_COLOR_MATERIAL); // set material properties which will be assigned by glColor glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled) then the normal calculations void Poly3d::normalize(LinkedList *vertices) { Vec3f *p1; p1=(Vec3f*)(*vertices)[p[0]]; Vec3f *p2; p2=(Vec3f*)(*vertices)[p[1]]; Vec3f *p3; p3=(Vec3f*)(*vertices)[p[2]]; Vec3f v1,v2,out; v1=(*p2)-(*p1); v2=(*p3)-(*p1); out=v1.cross(v2); out.normalize(); normal = out; //normal = normal * *p1; //normal.normalize(); } void Model::NormalizeFaces() { int x,y; Poly3d* poly; for(x=0;x<polygon_qty;x++) { poly=(Poly3d*)polygons[x]; poly->normalize(&vertices); } //patchy figure out something better later. Vec3f norm,*vert; for(x=0;x<vertex_qty;x++) { vert=(Vec3f*)vertices[x]; for(y=0;y<polygon_qty;y++) { poly=(Poly3d*)polygons[y]; if(*(*poly)[0]==x || *(*poly)[1]==x || *(*poly)[2]==x) norm=(norm)+(*(*poly).facenorm()); } norm.normalize(); (*vert).nx=norm[0]; (*vert).ny=norm[1]; (*vert).nz=norm[2]; } } //end normalizefaces Then the drawing routines, they may also be drawing the normals, that's what I was trying for anyway. void Model::drawgl() { glPushMatrix(); Poly3d *pTemp; //printf("%d %d %d\n",(*pTemp)[0],(*pTemp)[1],(*pTemp)[2]); fflush(stdout); Vec3f *vTemp; //printf("%f %f %f\n",(*vTemp)[0],(*vTemp)[1],(*vTemp)[2]); fflush(stdout); UV2f *uvTemp; int i1,i2,x; //printf("f,%f,%f",vTemp[0],vTemp[1],vTemp[2]); x=pqty(); float n1; float n2; float n3; float *u1; float *v1; int *p; glBindTexture( GL_TEXTURE_2D, texture_id[0] ); glBegin(GL_TRIANGLES); for(i1=0;i1<x;i1++) { pTemp=(Poly3d*)polygons[i1]; for(i2=0;i2<3;i2++) { p=(*pTemp)[i2]; vTemp=(Vec3f*)vertices[*p]; n1=(*vTemp).nx; n2=(*vTemp).ny; n3=(*vTemp).nz; uvTemp=(UV2f*)textcoords[*p]; glNormal3f(n1,n2,n3); glTexCoord2d(*(*uvTemp)[0],*(*uvTemp)[1]); glVertex3f((*vTemp)[0],(*vTemp)[1],(*vTemp)[2]); } } glEnd(); glBegin(GL_LINES); for(i1=0;i1<x;i1++) { pTemp=(Poly3d*)polygons[i1]; for(i2=0;i2<3;i2++) { p=(*pTemp)[i2]; vTemp=(Vec3f*)vertices[*p]; n1=(*vTemp).nx; n2=(*vTemp).ny; n3=(*vTemp).nz; glColor3f(1,1,1); glVertex3f((*vTemp)[0],(*vTemp)[1],(*vTemp)[2]); glVertex3f(n1,n2,n3); } } glEnd(); glPopMatrix(); } Then when I render the scene Does that seem right to everyone?

Share this post


Link to post
Share on other sites
Advertisement
When drawing normal vectors, you need to add the normals to the corresponding vertex position to get the end position of the line.


glVertex3f(v.pos.x,v.pos.y,v.pos.z);
glVertex3f(v.pos.x+v.nor.x,v.pos.y+v.nor.y,v.pos.z++v.nor.z);


You code is not easy to read (or more honest: it is chaotic ...), but I can't find the above addition and suppose it is missing.

Alexander

Share this post


Link to post
Share on other sites
Yep didn't have that one in there, got rid of the terrain model because it is just hard to look at, so here's a cube with some normals that's just a bit less ugly. I'm pretty sure that my normals are not calculating right, but with your hint at least I'll be able to better see what is going on, Thanks.




(I'll clean up the code when I get time, right now chaos reigns through many projects)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement