OpenGL Another problem with lighting

Recommended Posts

k_ounu_eddy    122
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; }

Share on other sites
gardin    122
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:

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

Share on other sites
k_ounu_eddy    122
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?

Share on other sites
KumGame07    145
You are setting both orthographic and perspective projections. Is this intentional?

Quote:

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

Share on other sites
k_ounu_eddy    122
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.

Share on other sites
k_ounu_eddy    122
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).

Share on other sites
PenguinGLX    100
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

Share on other sites
k_ounu_eddy    122
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]

Share on other sites
PenguinGLX    100
#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

Share on other sites
k_ounu_eddy    122
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]

Create an account

Register a new account

• Similar Content

• I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.

• There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window.
I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.

Thanks
• By cebugdev
hi all,

i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
thank you, and looking forward to positive replies.

• I have a few beginner questions about tesselation that I really have no clue.
The opengl wiki doesn't seem to talk anything about the details.

What is the relationship between TCS layout out and TES layout in?
How does the tesselator know how control points are organized?
e.g. If TES input requests triangles, but TCS can output N vertices.
What happens in this case?
http://www.informit.com/articles/article.aspx?p=2120983
the isoline example TCS out=4, but TES in=isoline.
And gl_TessCoord is only a single one.
So which ones are the control points?
How are tesselator building primitives?
• By Orella
I've been developing a 2D Engine using SFML + ImGui.
Here you can see an image
The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine.
I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui.
3D Editor preview
But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
If you can provide code will be better. And if you want me to provide any specific code tell me.
Thanks!

• 9
• 9
• 14
• 24
• 37