3

I have written the following program to display a teapot on a table in a room with 2side walls and a floor.

#include <stdio.h>
#include <glut.h>


void wall1(float thickness)
{
    glPushMatrix();
    glTranslatef(100,100,0);
    glRotatef(90,1,0,0);
    glScalef(thickness,1,1);
    glutSolidCube(100);
    glPopMatrix();
}
void wall2(float thickness)
{
    glPushMatrix();
    glTranslatef(150,100,-50);
    glScalef(1,1,thickness);
    glutSolidCube(100);
    glPopMatrix();
}
void floor(float thickness)
{
    glPushMatrix();
    glTranslatef(150,50,0);
    glScalef(1,thickness,1);
    glutSolidCube(100);
    glPopMatrix();
}
void leg(float thickness)
{
    glPushMatrix();
    glScalef(thickness,.5,thickness);
    glutSolidCube(100);
    glPopMatrix();
}
void tableTop(float thickess)
{
    glPushMatrix();
    glTranslatef(150,100,0);
    glScalef(.5,thickess,.5);
    glutSolidCube(100);
    glPopMatrix();
}
void table()
{
    tableTop(.05);

    glPushMatrix();
    glTranslatef(125,75,-25);
    leg(.05);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(175,75,-25);
    leg(.05);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(175,75,25);
    leg(.05);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(125,75,25);
    leg(.05);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(150,110,0);
    glScalef(.1,.1,.1);
    glutSolidTeapot(100);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    float pos[] = {200,200,0};
    float dif[] = {.3,.3,.3,3};
    float spe[] = {1,1,1,1};
    float amb[] = {1,1,1,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,dif);
    glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
    glLightfv(GL_LIGHT0,GL_SPECULAR,spe);

    glTranslatef(50,50,0);
    glRotatef(30,1,0,0);
    glRotatef(-30,0,1,0);
    wall1(.05);
    wall2(.05);
    floor(0.05);
    table();

    glFlush();
}
void reshape(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,400,0,400,-400,400);
    glMatrixMode(GL_MODELVIEW);
}

void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(400,400);
    glutCreateWindow("woot");
    glClearColor(1,1,1,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);


    glShadeModel(GL_SMOOTH);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMainLoop();
}

The problem with this is my lighting part is not working as expected. Its not illuminating evenly all my objects... What am i missing? This making even the teapot hard to sea. My lighting part is in display function.

3 Answers 3

5

I was missing

glEnable(GL_NORMALIZE);

in the main function, and thus opengl was not rendering it properly! Alse @Christian's answer of using ambient only worked.

:)

4
  • But you know, that ambient is no light but a consant color? Jun 2, 2011 at 12:58
  • 3
    Ah, I see what happened right now: Scaling also affects the normals. Also try glEnable(GL_RESCALE_NORMAL); if it's available it's a little bit faster than normalizing.
    – datenwolf
    Jun 2, 2011 at 13:24
  • @christian oh! i didnt know that.. though it was a light source... It will be added to the acual material color right
    – footy
    Jun 2, 2011 at 14:36
  • 1
    The light's ambient color is multiplied by the meterial's ambient color. But as it doesn't take any normals or other things into account, it is just a constant color without any impression of space. Jun 2, 2011 at 14:45
3

OpenGL fixed function pipeline lighting is evaluated at the vertices only. glutSolidCube just creates vertices at the corners and nowhere else. So your lighting is calculated only very coarse. You could either switch to per fragment lighting by using a shader, or tesselate your objects. The latter requires you don't use glutSolidCube and instead load objects modelled in a 3D modeller or write your own primitive generators that offer a higher tesselation. I strongly recommend the first option.

glutSolidCube is just a very crude stand in function BTW.

1
  • ok :) This is just for practice with lighting functions i am writing the program for. I will keep that in mind the next time i write program
    – footy
    Jun 2, 2011 at 12:55
0

If you want to evenly light all objects, only use ambient lighting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.