Opengl Lightning from object

219 views Asked by At

I want to draw some light from an object (sphere in my case, its a pole with a handle and sphere on the top).

The light should draw on the other objects too but from the only sphere as I mentioned.

Currently, I am only getting ambient light only.

I have set the L key to show lightning but it only shows ambient light(darkens the scene) when I press the key.

here is the code

    /// Lights 
    float ambientLight[4] = { 0.35f, 0.35f, 0.35f, 1.0f }; // colour
    float ambientLightOff[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; // colour
    
    float sunLightPos[4] = { 6.f, 15.5f, 5.0f, 0.0f }; // distant above
    float sunLightDif[4] = { 0.8f,0.8f,0.8f,0.8f };
    float sunLightSpec[4] = { 0.8f,0.8f,0.8f,0.8f }; // colour
    float sunLightAmb[4] = { 0.1f,0.1f,0.1f,0.0f }; // colour
    
    
    // materials 
    
    
    float   wooddif1[] = { 0.647059f, 0.164706f, 0.164706f , 1.0f };
    float   woodamb1[] = { 0.41f, 0.164706f, 0.164706f, 1.0f };
    float   woodspe1[] = { 0.547059f, 0.364706f, 0.264706f, 1.0f };
    float   woodshiny1 = 60;
    
    bool showNormals = TRUE;
    bool lighting = false;
    bool texture = false;
    bool ambientlighting = true;
    bool sunlighting = false;
    bool buildinglighting = false;
    static GLfloat  lightIntensity = 0.5;
    bool whereLights = true;

    void drawpole()
    
    {
        
        glColor3f(0.5f, 0.35f, 0.05f);
        glTranslatef(0, 1, 0);
        drawCylinder3();
        glColor3f(0.5f, 0.35f, 0.05f);
        glTranslatef(0, 1, 0);
        drawCylinder3();
    
        glPushMatrix();
        //glTranslatef(light2Pos[0],0, light2Pos[2]);
        //glTranslatef(25, 0, 13);
        glColor3f(0.3, 0.4, 0.1);
        if (lighting) { setMaterial(4); }
    
        drawSphere();
        glPopMatrix();
        
        
    }

 void setLights()
    {
        if (lighting)
        {
            glEnable(GL_LIGHTING);
            glEnable(GL_NORMALIZE);
            //setShademodel();
        }
        else glDisable(GL_LIGHTING);
        if (sunlighting)
        {
        }
        else glDisable(GL_LIGHT0);
        //if (buildinglighting)

        if (ambientlighting)
        {
        }
        else
        {
        }
    }
    void drawLightPlaces()
    {
        //draw the lights at position

        glDisable(GL_LIGHTING);
        if (lighting) glEnable(GL_LIGHTING);
    }

Edit (related to comment).

void drawpole()

    {

        glColor3f(0.5f, 0.35f, 0.05f);
        glTranslatef(0, 1, 0);
        drawCylinder3();
        glColor3f(0.5f, 0.35f, 0.05f);
        glTranslatef(0, 1, 0);
        drawCylinder3();

        glPushMatrix();
        glColor3f(0.5f, 0.35f, 0.05f);
        if (buildinglighting) {
            glDisable(GL_LIGHTING);
            
            glColor3f(1.0, 1.0, 1.0);
            setMaterial(4);
        }
        

        drawSphere();
        glPopMatrix();

    }
   
    void setLights()
    {
        if (lighting)
        {
            glEnable(GL_LIGHTING); 

        }
       else glDisable(GL_LIGHTING);


        if (sunlighting)
        {
            sunlighting;
        }
        else glDisable(GL_LIGHT0);
        
        
        if (buildinglighting)
        {
           // glLightfv(GL_LIGHT0, GL_POSITION, light_position);
            //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
            glShadeModel(GL_SMOOTH);
            glEnable(GL_LIGHTING);
            glEnable(GL_LIGHT0);
            //glEnable(GL_DEPTH_TEST);
            


        }
        if (ambientlighting)
        {
            glEnable(GL_LIGHTING);
            
            glEnable(GL_COLOR_MATERIAL);
            glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
            glEnable(GL_NORMALIZE);
            setShademodel();
            //glDisable(GL_LIGHTING);
        }
        else 
        {
        }
    }


float   wooddif1[] = { 0.647059f, 0.164706f, 0.164706f , 1.0f };
float   woodamb1[] = { 0.41f, 0.164706f, 0.164706f, 1.0f };
float   woodspe1[] = { 0.547059f, 0.364706f, 0.264706f, 1.0f };
float   woodshiny1 = 60;

    void setMaterial(int m) // 1=plane 2= 3=ground 4=wood 5=roof 6=wood2 7=
    {
        if (m == 1)
        {
            glMaterialfv(GL_FRONT, GL_AMBIENT, woodamb1);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, wooddif1);
            glMaterialfv(GL_FRONT, GL_SPECULAR, woodspe1);
            glMaterialf(GL_FRONT, GL_SHININESS, woodshiny1);
        }
        if (m == 2) //  2
        {
        }
    }


    
1

There are 1 answers

11
Viktor Latypov On

A wild guess: the glEnable(GL_LIGHT0) is missing in your code (at least in the posted part).

Just in case, the glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); might also be useful to be sure that back-facing triangles are lit.

After that, the GL_SHININESS and GL_SPECULAR parameters should be tweaked to get this kind of highlights.