PDA

View Full Version : lighting question



alanr
11-17-2011, 05:56 PM
Hi eveyrone,

I have a question on lighting.
I have an open cube inside another open cube.
In the inner cube, there's a spotlight.
Everything is draw with glMaterialfv.
Light from the spotlight goes through the inner cube into the outer cube.
Is there a way prevent light from bleeding through? i.e., how do I make the walls completely opaque?

-Alan


#include <GL/glut.h>

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float fovy = 90.0;
float aspect = 1.0;
float zNear = 1.0;
float zFar = 100.0;
gluPerspective(fovy, aspect, zNear, zFar);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float eyex = 2.0f;
float eyey = 5.0f;
float eyez = 6.0f;
gluLookAt(eyex, eyey, eyez,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);

glViewport(0, 0, 640, 480);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}


void room(float width = 10)
{
// floor
glPushMatrix();
glScalef(width, 0.1, width);
glutSolidCube(1);
glPopMatrix();

// walls
glPushMatrix();
glTranslatef(0, width/2, 0);

// left
glPushMatrix();
glTranslatef(-width/2, 0, 0);
glScalef(0.1, width, width);
glutSolidCube(1);
glPopMatrix();

// right
glPushMatrix();
glTranslatef(width/2, 0, 0);
glScalef(0.1, width, width);
glutSolidCube(1);
glPopMatrix();

// back
glPushMatrix();
glTranslatef(0, 0, -width/2);
glScalef(width, width, 0.1);
glutSolidCube(1);
glPopMatrix();

glPopMatrix();

// ceiling
glPushMatrix();
glTranslatef(0, width, 0);
glScalef(width, 0.1, width);
glutSolidCube(1);
glPopMatrix();

}


void display()
{
float lightx = 0;
float lighty = 2;
float lightz = 0;
GLfloat light_position[] = {lightx, lighty, lightz, 1.0f};
GLfloat dir[] = {1.0, 0, 1.0};
float cutoff_angle = 90.0;
float spot_exp = 5.0f;
GLfloat light_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff_angle);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, spot_exp);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez,
0.0, 0.25, 0.0,
0.0, 1.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f}; // gray
GLfloat mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

// spotlight
glPushMatrix();
glTranslatef(lightx, lighty, lightz);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();

room(4);
room(5);

glutSwapBuffers();
}



int main(int argc, char ** argv)
{
glutInit(&amp;argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("");

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

_arts_
11-18-2011, 01:36 AM
Yes, simply enable your light for the geometry you want to lit, and disable it for the geometry that should not receive its lighting.