0

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 Answers1

0

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.

Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
  • Thanks for reply, yeah that was missing but now i added to it and its showing light but not like the picture (a vertex of light to goes above warehouse)..guide me please – BeingHumayun Sep 30 '20 at 09:14
  • GL light model has a number of parameters, fallofs among them. Show us the picture you are getting right now, so we may guess the parameters. By the way, the setMaterial code would be useful. You may be using 'ambientLightOff' parameters incorrectly – Viktor Latypov Sep 30 '20 at 09:16
  • Where are the glMaterialfv() calls ? And what are their parameters. – Viktor Latypov Sep 30 '20 at 09:32
  • Here is the video...basically i wanted to add some lights when i trigger B button..it is bascially a building lights function..i want to light it up so that it looks like it is emmiting from the pole which i added as a white sphere when ambient light is activated. it works but not like what i wanted in picture. – BeingHumayun Sep 30 '20 at 09:33
  • I have seen the video and it obviously shows no specular highlights (which is the thing you want), so for further investigation the setMaterial function source is needed and the exact usage of your light parameters – Viktor Latypov Sep 30 '20 at 09:35
  • Try playing around with GL_SHININESS and GL_SPECULAR in glMaterialfv parameters for the ground object, that is all I can say for now. Right now they seem to be zeroes. – Viktor Latypov Sep 30 '20 at 09:37
  • I guess it is not zeroes...i have added float values which i used in edit section – BeingHumayun Sep 30 '20 at 09:39
  • I see only the if (m == 1) branch in setMaterial to be non-empty, why should the _ground_ have non-zero shininess parameters ? You only call setMaterial(4) above. – Viktor Latypov Sep 30 '20 at 09:41
  • so should i try m=2? – BeingHumayun Sep 30 '20 at 09:43
  • Try removing if(m==1) and using something like (0,1,0,1) for shininess - this should be bright green for all the objects. Don't be shy, try experimenting yourself, you are in control. – Viktor Latypov Sep 30 '20 at 09:44
  • Excellent, now use some smaller green values for the (m==4) case - get those if's in place. I don't know what you want (and nobody but you does), but you have found a way to set lighting parameters. Tweaking the shininess and specular color has little to do with programming, add some sliders or bind more buttons to adjust these values in real-time (say, 'u' key makes a += on green component, 'd' make a -=) – Viktor Latypov Sep 30 '20 at 09:51