Jump to content
  • Advertisement
Sign in to follow this  
k_ounu_eddy

OpenGL Lighting

This topic is 4254 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

I started work with lighting in Opengl.This simple program draws a triangle,but I have a error and I don't know why,the triangle colour isn't red,as I want.I think the error may be the normal vectors,but I am not sure.Here it is: #include <windows.h> #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> #include <gl\glut.h> #include <math.h> void normalize(float v[3]) {GLfloat d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); if (d == 0.0) return; v[0] /= d; v[1] /= d; v[2] /= d;} void normcrossprod(float v1[3], float v2[3], float out[3]) {out[0] = v1[1]*v2[2] - v1[2]*v2[1]; out[1] = v1[2]*v2[0] - v1[0]*v2[2]; out[2] = v1[0]*v2[1] - v1[1]*v2[0]; normalize(out);} void init(void) {glClearColor(1,1,1,0); glColor3f(1,0,0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10,10,-10,10,-10,10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluPerspective(45,1,1,5); glTranslatef(0,0,-5); float position[]={0,0,-2}; float ambient[]={1,1,1}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightfv(GL_LIGHT0, GL_AMBIENT,ambient); glEnable(GL_DEPTH_TEST); } void redisplay(int width,int height) {glViewport(0,0,width,height);} void display(void) {glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); float out[3],v1[]={-5,-5,0},v2[]={5,-5,0}; glBegin(GL_TRIANGLES); glNormal3fv(out); glVertex3f(-5,-5,0); glNormal3fv(out); glVertex3f(5,-5,0); glNormal3fv(out); glVertex3f(0,5,0); glEnd(); glFlush(); glutSwapBuffers();} void main(int argc,char**argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("Exercise"); init(); glutDisplayFunc(display); glutReshapeFunc(redisplay); glutMainLoop(); } Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I found some errors:I forgot to call normcrossprod for out vector and I didn't soustract two points from that triangle to obtain a vector.But my program stil doesn't work.

Share this post


Link to post
Share on other sites
The reason your triangle isn't red is because when you enable lighting glColor* has no effect unless you enable GL_COLOR_MATERIAL.

Share this post


Link to post
Share on other sites
I didn't know that.Thanks.
[EDIT]:One more question.Let's supose that we have a triangle ,and the vertices are v1,v2,v3.I make the crossproduct (v2-v1)*(v3-v1).after that I normalize the result vector.Now comes the question:for each vertex of triangle is the same normal?For example is corect:

float normal[3];
normcrossprod(vector1[3],vector2[3],normal[3])
/*we supose that normcrossprod is a function that calculate the cross product of vector vector1(vector1=v2-v1) and vector2(vector2=v3-v1).After that ,the function normalize the product.The result is stored in normal.*/
glBegin(GL_TRIANGLES);
glNormal3fv(normal);
glVertex(v1);
glNormal3fv(normal);
glVertex(v2);
glNormal3fv(normal);
glVertex(v3);
glEnd();
If I write this I get the proper results?

[Edited by - k_ounu_eddy on April 23, 2007 1:00:25 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by k_ounu_eddy
I didn't know that.Thanks.
[EDIT]:One more question.Let's supose that we have a triangle ,and the vertices are v1,v2,v3.I make the crossproduct (v2-v1)*(v3-v1).after that I normalize the result vector.Now comes the question:for each vertex of triangle is the same normal?For example is corect:

float normal[3];
normcrossprod(vector1[3],vector2[3],normal[3])
/*we supose that normcrossprod is a function that calculate the cross product of vector vector1(vector1=v2-v1) and vector2(vector2=v3-v1).After that ,the function normalize the product.The result is stored in normal.*/
glBegin(GL_TRIANGLES);
glNormal3fv(normal);
glVertex(v1);
glNormal3fv(normal);
glVertex(v2);
glNormal3fv(normal);
glVertex(v3);
glEnd();
If I write this I get the proper results?


If you're only drawing one triangle, yeah. If you're drawing a mesh, doing normals this way will result in lighting seams between triangles--each triangle will be flat shaded, so your mesh will look faceted. In that case, you usually want to compute a normal for each vertex, based on the normals of every facet triangle that contains that vertex.

Share this post


Link to post
Share on other sites
I try enable GL_COLOR_MATERIAL and it works.I understand also what lancekt said.Thanks both.
[EDIT]:Altough I enable GL_LIGHTING and GL_COLOR_MATERIAL,I don't see any differnce between using lighting and not using lithing(for that triangle).It's possible that I don't compute correct the normals or this difference is rather imperceptible?

[Edited by - k_ounu_eddy on April 23, 2007 2:43:12 PM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!