Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: lighting problem

Hybrid View

  1. #1
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52

    lighting problem

    Hi, I have a problem using openGL light.

    I draw two 3D .obj model. Loader works fine, vertex and normals are correct. The problem is that when I enable light the effect on one of the model is bad:

    good model with light:
    Click image for larger version. 

Name:	Schermata 05-2456062 alle 18.44.47.png 
Views:	25 
Size:	11.2 KB 
ID:	719

    bad model with light:
    Click image for larger version. 

Name:	Schermata 05-2456062 alle 18.44.56.png 
Views:	24 
Size:	12.6 KB 
ID:	720

    bad model without light
    Click image for larger version. 

Name:	Schermata 05-2456062 alle 18.45.43.png 
Views:	24 
Size:	9.2 KB 
ID:	721


    Here is the light code in render function:

    Code :
    [COLOR=#4700de][COLOR=#490085]gluLookAt[/COLOR][COLOR=#000000]([/COLOR]20.0f[COLOR=#000000], [/COLOR]5.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]1.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000]);[/COLOR][/COLOR]
     
    [COLOR=#7d4725][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHT0[COLOR=#000000]);
    [/COLOR][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHTING[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#ca00a5]float[/COLOR] lightPos[] = {[COLOR=#4700de]20.0[/COLOR], [COLOR=#4700de]10[/COLOR], [COLOR=#4700de]1[/COLOR], [COLOR=#4700de]0.0[/COLOR]};
    [COLOR=#7d4725][COLOR=#490085]glLightfv[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHT0[COLOR=#000000], [/COLOR]GL_POSITION[COLOR=#000000], lightPos);
     
    // here draw model[/COLOR][/COLOR]

    Any suggestions? Light params are incorrect?

  2. #2
    Junior Member Newbie stefkos's Avatar
    Join Date
    May 2012
    Location
    Warsaw
    Posts
    7
    I had similar problem, and i fixed it by setting znear parameter to 1.0.
    Check My thread, maybe My code from it will help you to find solution.
    http://www.opengl.org/discussion_boa...67#post1237367

  3. #3
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by stefkos View Post
    I had similar problem, and i fixed it by setting znear parameter to 1.0.
    Check My thread, maybe My code from it will help you to find solution.
    http://www.opengl.org/discussion_boa...67#post1237367
    Thank you, I've already read your topic but my znear value is 1.0 in reshape func


    Code :
    [COLOR=#490085]glViewport[/COLOR]([COLOR=#4700DE]0[/COLOR], [COLOR=#4700DE]0[/COLOR], ([COLOR=#7D1BAE]GLsizei[/COLOR])width, ([COLOR=#7D1BAE]GLsizei[/COLOR])height);
    [COLOR=#008C00][COLOR=#490085]glMatrixMode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7D4725]GL_PROJECTION[/COLOR][COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#008C00][COLOR=#490085]glLoadIdentity[/COLOR][COLOR=#000000]();[/COLOR]  [/COLOR]
     
    [COLOR=#490085]gluPerspective[/COLOR]([COLOR=#4700DE]60[/COLOR], ([COLOR=#7D1BAE]GLfloat[/COLOR])width / ([COLOR=#7D1BAE]GLfloat[/COLOR])height, [COLOR=#4700DE]1.0[/COLOR], [COLOR=#4700DE]100.0[/COLOR]);

    I've also tried with different light position params but I didn't solve the problem

  4. #4
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Another information.

    In my code I draw at first some objects without light, then I set up light and draw my 3D model.
    Here is the code:

    Code :
    [COLOR=#7d4725][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_NORMALIZE[COLOR=#000000]);[/COLOR][/COLOR]
     
     
    [COLOR=#008c00][COLOR=#000000]   [/COLOR]// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);[/COLOR]
     
    [COLOR=#008c00][COLOR=#000000]   [/COLOR]// gluLookAt(0.0f, 0.0f, 7.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f);[/COLOR]
    [COLOR=#4700de][COLOR=#000000]    [/COLOR][COLOR=#490085]gluLookAt[/COLOR][COLOR=#000000]([/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]2.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]1.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]1.0f[COLOR=#000000], -[/COLOR]1.0f[COLOR=#000000]);[/COLOR][/COLOR]
     
     
     
    [COLOR=#008c00][COLOR=#000000]    [/COLOR]/**** init & render arena *****/[/COLOR]
     
    [COLOR=#008c00][COLOR=#000000]   [/COLOR]// gluOrtho2D(-2.0,2.0,-2.0,2.0);[/COLOR]
    [COLOR=#490085][COLOR=#000000]    [/COLOR]glPushMatrix[COLOR=#000000]();[/COLOR][/COLOR]
            [COLOR=#490085]glScalef[/COLOR]([COLOR=#4700de]0.08[/COLOR], [COLOR=#4700de]0.08[/COLOR], [COLOR=#4700de]0.08[/COLOR]);
            [COLOR=#490085]glTranslatef[/COLOR]([COLOR=#4700de]11.0[/COLOR], [COLOR=#4700de]8.8[/COLOR], [COLOR=#4700de]0.0[/COLOR]);
            [COLOR=#ca00a5]if[/COLOR](![COLOR=#438288]arenas[/COLOR][[COLOR=#438288]currArena[/COLOR]].[COLOR=#275a5e]init[/COLOR]()) {
    [COLOR=#de0000][COLOR=#000000]            [/COLOR][COLOR=#7d1bae]cout[/COLOR][COLOR=#000000] << [/COLOR]"Impossibile init arena"[COLOR=#000000] << [/COLOR][COLOR=#490085]endl[/COLOR][COLOR=#000000];[/COLOR][/COLOR]
                [COLOR=#ca00a5]return[/COLOR];
            }
    [COLOR=#438288][COLOR=#000000]        [/COLOR]arenas[COLOR=#000000][[/COLOR]currArena[COLOR=#000000]].[/COLOR][COLOR=#275a5e]render[/COLOR][COLOR=#000000]();[/COLOR][/COLOR]
    [COLOR=#490085][COLOR=#000000]    [/COLOR]glPopMatrix[COLOR=#000000]();[/COLOR][/COLOR]
     
     
     
    [COLOR=#008c00][COLOR=#000000] 	[/COLOR]//gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -5.0f);[/COLOR]
     
     
     
    [COLOR=#008c00][COLOR=#000000]   [/COLOR]// m_Background->render();[/COLOR]
     
     
     
     
     
     
        [COLOR=#490085]glColor3f[/COLOR]([COLOR=#4700de]1.0[/COLOR], [COLOR=#4700de]1.0[/COLOR], [COLOR=#4700de]1.0[/COLOR]);
     
        [COLOR=#ca00a5]if[/COLOR]([COLOR=#438288]currCycle[/COLOR]>[COLOR=#4700de]0[/COLOR]) {
            [COLOR=#438288]left1[/COLOR]->[COLOR=#275a5e]render[/COLOR]();
        }
    [COLOR=#438288][COLOR=#000000]    [/COLOR][COLOR=#ca00a5]if[/COLOR][COLOR=#000000]([/COLOR]currCycle[COLOR=#000000]<[/COLOR]numCycles[COLOR=#000000]-[/COLOR][COLOR=#4700de]1[/COLOR][COLOR=#000000]) {[/COLOR][/COLOR]
            [COLOR=#438288]right1[/COLOR]->[COLOR=#275a5e]render[/COLOR]();
        }
     
        [COLOR=#ca00a5]if[/COLOR]([COLOR=#438288]currArena[/COLOR]>[COLOR=#4700de]0[/COLOR]) {
            [COLOR=#438288]left2[/COLOR]->[COLOR=#275a5e]render[/COLOR]();
        }
    [COLOR=#438288][COLOR=#000000]    [/COLOR][COLOR=#ca00a5]if[/COLOR][COLOR=#000000]([/COLOR]currArena[COLOR=#000000]<[/COLOR]numArena[COLOR=#000000]-[/COLOR][COLOR=#4700de]1[/COLOR][COLOR=#000000]) {[/COLOR][/COLOR]
            [COLOR=#438288]right2[/COLOR]->[COLOR=#275a5e]render[/COLOR]();
        }
     
        [COLOR=#438288]play[/COLOR]->[COLOR=#275a5e]render[/COLOR]();
     
    [COLOR=#000000]    [/COLOR][COLOR=#275a5e]writeParams[/COLOR][COLOR=#000000]();[/COLOR]
    [COLOR=#7D4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glBegin[/COLOR][COLOR=#000000]([/COLOR]GL_QUADS[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#000000]        [COLOR=#490085]glColor3f[/COLOR]([COLOR=#4700de]0.1[/COLOR],[COLOR=#4700de]0.1[/COLOR],[COLOR=#4700de]0.4[/COLOR]);[/COLOR]
    [COLOR=#000000]        [COLOR=#490085]glVertex3f[/COLOR](-[COLOR=#4700de]2.3[/COLOR],[COLOR=#4700de]0.5[/COLOR],-[COLOR=#4700de]1.0[/COLOR]);[/COLOR]
    [COLOR=#000000]        [COLOR=#490085]glVertex3f[/COLOR](-[COLOR=#4700de]0.1[/COLOR],[COLOR=#4700de]0.5[/COLOR],-[COLOR=#4700de]1.0[/COLOR]);[/COLOR]
    [COLOR=#000000]        [COLOR=#490085]glVertex3f[/COLOR](-[COLOR=#4700de]0.1[/COLOR],[COLOR=#4700de]1.65[/COLOR],-[COLOR=#4700de]1.0[/COLOR]);[/COLOR]
    [COLOR=#000000]        [COLOR=#490085]glVertex3f[/COLOR](-[COLOR=#4700de]2.3[/COLOR],[COLOR=#4700de]1.65[/COLOR],-[COLOR=#4700de]1.0[/COLOR]);[/COLOR]
    [COLOR=#000000]    [COLOR=#490085]glEnd[/COLOR]();
     
    //here new gluLookAt
    // set up light
    // draw model[/COLOR]

  5. #5
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    I'm trying to test light and my .obj loader in a simple scene using a simple .obj polygon ( a parallelepiped).
    If I don't enable light the polygon seems ok.
    If I enable light I've noticed that one face is correct and other faces show the opposite not-illuminated face. Here are the images:

    The right face
    Click image for larger version. 

Name:	Schermata 05-2456064 alle 22.07.50.jpg 
Views:	14 
Size:	5.4 KB 
ID:	726

    wrong faces:
    Click image for larger version. 

Name:	Schermata 05-2456064 alle 22.07.39.jpg 
Views:	17 
Size:	6.2 KB 
ID:	727

    Click image for larger version. 

Name:	Schermata 05-2456064 alle 22.07.55.jpg 
Views:	14 
Size:	5.9 KB 
ID:	728

    Click image for larger version. 

Name:	Schermata 05-2456064 alle 22.07.46.jpg 
Views:	12 
Size:	4.9 KB 
ID:	729


    I don't understand if the normals are wrong or vertices or some other thing.
    I printed several times data laoded from my objLoader and it seems ok

  6. #6
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Solved my problem! I didn't put GLUT_DEPTH in my glut window!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •