Another problem with lighting

Started by
8 comments, last by k_ounu_eddy 16 years, 11 months ago
Hello guys.I started recently to work with lighting in opengl.In the following code ,I want to draw a polygon and a trianlge,and use the lighting.The problem is it doesn't works.the surface of polygon has the same color in every point.the same thing to the triangle.please help.thanks in advance.and sorry if you consider this a stupid question,but I'm a begginer and,I don't understand. Here is the code: #include <windows.h> #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> #include <gl\glut.h> #include<math.h> GLfloat position[]={-1,-1,-1,0}; void normalize(float *v) {float length=sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); v[0]/=length; v[1]/=length; v[2]/=length;} void NormCrossProd(float *v1,float *v2,float* result) {result[0]=v1[1]*v2[2]-v2[1]*v1[2]; result[1]=v2[0]*v1[2]-v1[0]*v2[2]; result[2]=v1[0]*v2[1]-v2[0]*v1[1]; normalize(result);} void init() { //light glLightfv(GL_LIGHT0,GL_POSITION,position); glShadeModel (GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glClearColor(1,1,1,0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10,10,-10,10,-10,10); glMatrixMode(GL_MODELVIEW); glLoadIdentity();} void reshape(int w,int h) {gluPerspective(6,w/h,0,10); glViewport(0,0,w,h);} void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); float v1[]={10,0,0}; //v1 and v2 are the vector necessary for compute the normal vector for the polygon float v2[]={0,0,10}; float out[3]; //vector out store the result of normcrossprod float v3[]={10,0,0}; //v3 and v4 are the vector necessary for compute the normal vector for triangle float v4[]={5,5,0}; float out2[3]; NormCrossProd(v1,v2,out); NormCrossProd(v3,v4,out2); glLightfv(GL_LIGHT0,GL_POSITION,position); float ambient[]={1,0,0}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); glBegin(GL_POLYGON); glNormal3fv(out); glVertex3f(-5,-5,-20); glNormal3fv(out); glVertex3f(5,-5,-20); glNormal3fv(out); glVertex3f(5,-5,-10); glNormal3fv(out); glVertex3f(-5,-5,-10); glEnd(); float ambient2[]={0,0,1}; glBegin(GL_TRIANGLES); glNormal3fv(out2); glVertex3f(-5,-5,-10); glNormal3fv(out2); glVertex3f(5,-5,-10); glNormal3fv(out2); glVertex3f(0,0,-10); glEnd(); glFlush(); glutSwapBuffers(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("Exercitiu..."); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }
Advertisement
You havent set the light color, it is the default for GL_LIGHT0 which is (from what i remember) 1, 1, 1, 1, that is very strong, and could be the reason why your faces end up completely lit.

Another small detail, has nothing to do with your problem though:

Your code

void reshape(int w,int h)
{gluPerspective(6,w/h,0,10);
glViewport(0,0,w,h);}


should be written as:

void reshape(int w,int h)
{gluPerspective(6,(GLfloat) w / (GLfloat) h,0,10);
glViewport(0,0,w,h);}


Notice the type casting. A view of 640*480 will have an ratio of 1,333, but you dont cast, and end up with a ratio of 1....
But isn't supose that my light is white light(like in reality)?
I don't understand why it doesn't work.It's aboslutely neccesary to set the diffuse,ambient and specular light?It's necessary to set all the material properties(in my code I set only the GL_AMBIENT)?Where is the mistake?What should I do to get proper results?
You are setting both orthographic and perspective projections. Is this intentional?

Quote:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10,10,-10,10,-10,10);


Then, again you are specifying a perspective matrix

Quote:
gluPerspective(6,w/h,0,10);


Moreover, please gothrough the documantations on projection transformation. I think, you have mis-understood the near and far parameters. They are actually the distance from the viewing position to the near & far clipping planes respectively. Not the Z co-ordinates. Look at the fovy as well; its just 6 degrees!!!


Hope this helps you,
Regards,
Kumgame
Best Regards,KumGame07
I was thinking that 0 and 10 are the Z co-ordination in eye coordinates.
If the fovy was bigger than 6,the image is disproportionate.
Yes,KumGame07,you were right.I was thinking that always I must set the Ortho projection and perspective projection,which are 2 different things(in perspective the farther objects appears small but in ortographic this doesn't matter).I'm trying to make the program runs corectly(the light is the problem).
it Seems to me that you got the wrong Normal Vectors

Where did you get those normal vectors?

I would recommend Vector(0,0,1) for the Triangle to be Normal Vector

One more things :) to speed up your program
If all the Vertices of a triangle have the same Normal
just use glNormal3fv one time before all of the glVertex3f commands until you wanna change the Normal for another vertices
---------------
If anything I give above is not right, Im very glad to get comments or corrected


If I set the normal vector(0,0,1) is the same thing.
[EDIT]:Does anyone know where is the mistake?I have been trying since then and I don't find anything bad.I think that I don't compute the normal vector correct.Maybe is this,maybe no.If someone find the mistake ,thousands of thanks.Or if you don't have patience to correct my program,can someone write a short program where a triangle appears corectly(using the lighting,of course)?

[Edited by - k_ounu_eddy on May 5, 2007 7:52:18 AM]
#include <gl\glut.h>#include <gl\gl.h> #include <gl\glu.h> #include<math.h>GLfloat position[]={10,10,10,1};float MSpec[]={1,1,1,1};void init(){//lightglFrontFace(GL_CCW);glShadeModel (GL_LINE);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);glClearColor(1,1,1,0);}void reshape(int w,int h){glMatrixMode(GL_PROJECTION);glLoadIdentity();    float k=float(w)/float(h);gluPerspective(45,k,1,1000);    glViewport(0,0,w,h);}void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(60,40,50,0,0,0,0,0,1);glLightfv(GL_LIGHT0,GL_POSITION,position);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, MSpec);glMateriali(GL_FRONT_AND_BACK, GL_SHININESS,64 );glEnable(GL_LIGHTING);glBegin(GL_TRIANGLES);glNormal3f(0,0,1);glVertex3f(-40,-60,0);glVertex3f(20,-20,0);glVertex3f(20,20,0);glEnd();glDisable(GL_LIGHTING);glBegin(GL_LINES);{    glColor3f(1,0,0);    glVertex3f(100,0,0);    glVertex3f(0,0,0);    glColor3f(0,1,0);    glVertex3f(0,100,0);    glVertex3f(0,0,0);    glColor3f(0,0,1);    glVertex3f(0,0,100);    glVertex3f(0,0,0);}glEnd();glutSwapBuffers();}int main(int argc,char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);glutInitWindowSize(400,400);glutInitWindowPosition(100,100);glutCreateWindow("Exercitiu...");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return 0;}


Here is an example

Take a look at Light Position and compare to yours
Yours was directional light ,mine is Positional light ,In general every thing which is in directional light usually doesnt have shade ,you should take a look at OPENGL red book at light section for the lighting fomular

that is very detail and comprehensive book

Another thing should be considered is gluPerspective(6,w/h,0,10);

fovy = 6 should be 45 or more , and w/h generate integer
should use w*1.0/h
-------------
its just a quick code !
Every thing for Setting Light and material should be put in Init() for performance sake ,except something we wish to manipulate during rendering process
Thanks,I appreciate this.I found also the errors in my program.It's look like I was initializing firstly the GL_PROJECTION and after that i was initializing
GL_MODELVIEW.After that the function reshape() is called.Function reshape() contains the gluPerspective() ,and this affect the GL_MODELVIEW instead of GL_PROJECTION

[Edited by - k_ounu_eddy on May 6, 2007 6:37:32 AM]

This topic is closed to new replies.

Advertisement