k1llrogg

12-12-2017, 04:47 AM

Greetings!

I'm trying to make a Cornell Box in c++ OpenGL GLUT, but having trouble with the light.

I have 3 sources, all of them are in GL_POSITION 'mode'

What i currently have is shown on the screen below. My trouble is that the ceiling is much darker than other walls, although they are all have same color. Floor is also lighter than walls, but its not that noticeable.

Lighting code:

static GLfloat lightpos0[] = { 0.f, 50.f, -400.f, 1.f };

static GLfloat lightpos1[] = { 20.f, 50.f, -400.f, 1.f };

static GLfloat lightpos2[] = { -20.f, 50.f, -400.f, 1.f };

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glEnable(GL_LIGHT1);

glEnable(GL_LIGHT2);

glLightfv(GL_LIGHT0, GL_POSITION, lightpos0);

glLightfv(GL_LIGHT1, GL_POSITION, lightpos1);

glLightfv(GL_LIGHT2, GL_POSITION, lightpos2);

glEnable(GL_DEPTH_TEST);

glShadeModel(GL_SMOOTH);

//создание перспективы на сцене

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-100, 100, -100, 100, 320., 640.);

glMatrixMode(GL_MODELVIEW);

Walls code:

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

GLfloat x, y, z;

/* пол */

glBegin(GL_QUADS);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, beige_material);

glNormal3f(0.f, 1.f, 0.f);

x = -100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x + j, y, z);

glVertex3f(x + j, y, z - i);

glVertex3f(x, y, z - i);

}

x = -100.f;

}

/* левая стена */

glNormal3f(1.f, 0.f, 0.f);

x = -100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y, z - j);

glVertex3f(x, y + i, z - j);

glVertex3f(x, y + i, z);

}

z = -320.f;

}

/* правая стена */

glNormal3f(-1.f, 0.f, 0.f);

x = 100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y + j, z);

glVertex3f(x, y + j, z - i);

glVertex3f(x, y, z - i);

}

y = -100.f;

}

/* потолок */

glNormal3f(0.f, -1.f, 0.f);

x = -100.f, y = 100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y, z - j);

glVertex3f(x + i, y, z - j);

glVertex3f(x + i, y, z);

}

z = -320.f;

}

/* задняя стена */

glNormal3f(0.f, 0.f, 1.f);

x = -100.f, y = -100.f, z = -520.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x + j, y, z);

glVertex3f(x + j, y + i, z);

glVertex3f(x, y + i, z);

}

x = -100.f;

}

glEnd();

Room_items code:

glPushMatrix();

glTranslatef(-34.f, -75.f, -430.f);

glRotatef(20.f, 0.f, 1.f, 0.f);

glutSolidCube(50.0);

glPopMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, brown_material);

glPushMatrix();

glTranslatef(40.f, -85.f, -370.f);

glRotatef(-25.f, 0.f, 1.f, 0.f);

glutSolidCube(30.0);

glPopMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_material);

glPushMatrix();

glTranslatef(0.f, -80.f, -470.f);

glutSolidSphere(20.0, 100, 100);

glPopMatrix();

Main method:

glutInit(&argc, argv);

glutInitWindowSize(800, 800);

glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM | GLUT_DOUBLE);

(void)glutCreateWindow("Cornell Box");

init();

glutDisplayFunc(display);

GLfloat plane[4];

GLfloat v0[3], v1[3], v2[3];

v0[0] = -100.f;v0[1] = -100.f; v0[2] = -320.f;

v1[0] = 100.f; v1[1] = -100.f; v1[2] = -320.f;

v2[0] = 100.f; v2[1] = -100.f; v2[2] = -520.f;

findplane(plane, v0, v1, v2);

shadowmatrix(floorshadow, plane, lightpos1);

glReadBuffer(GL_BACK);

glutMainLoop();

I have a shadows method and MSAA method(which is display func), which I have not included, but i can if needed.

Already tried glLightModeli or switch from only GL_POSITION lights to GL_AMBIENT, GL_SPECULAR, GL_DIFFUSE and GL_POSITION lights.

Any help are much appreciated!

Screenshot:

https://puu.sh/yEKTu/9a7fb6e1de.png

I'm trying to make a Cornell Box in c++ OpenGL GLUT, but having trouble with the light.

I have 3 sources, all of them are in GL_POSITION 'mode'

What i currently have is shown on the screen below. My trouble is that the ceiling is much darker than other walls, although they are all have same color. Floor is also lighter than walls, but its not that noticeable.

Lighting code:

static GLfloat lightpos0[] = { 0.f, 50.f, -400.f, 1.f };

static GLfloat lightpos1[] = { 20.f, 50.f, -400.f, 1.f };

static GLfloat lightpos2[] = { -20.f, 50.f, -400.f, 1.f };

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glEnable(GL_LIGHT1);

glEnable(GL_LIGHT2);

glLightfv(GL_LIGHT0, GL_POSITION, lightpos0);

glLightfv(GL_LIGHT1, GL_POSITION, lightpos1);

glLightfv(GL_LIGHT2, GL_POSITION, lightpos2);

glEnable(GL_DEPTH_TEST);

glShadeModel(GL_SMOOTH);

//создание перспективы на сцене

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-100, 100, -100, 100, 320., 640.);

glMatrixMode(GL_MODELVIEW);

Walls code:

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

GLfloat x, y, z;

/* пол */

glBegin(GL_QUADS);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, beige_material);

glNormal3f(0.f, 1.f, 0.f);

x = -100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x + j, y, z);

glVertex3f(x + j, y, z - i);

glVertex3f(x, y, z - i);

}

x = -100.f;

}

/* левая стена */

glNormal3f(1.f, 0.f, 0.f);

x = -100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y, z - j);

glVertex3f(x, y + i, z - j);

glVertex3f(x, y + i, z);

}

z = -320.f;

}

/* правая стена */

glNormal3f(-1.f, 0.f, 0.f);

x = 100.f, y = -100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y + j, z);

glVertex3f(x, y + j, z - i);

glVertex3f(x, y, z - i);

}

y = -100.f;

}

/* потолок */

glNormal3f(0.f, -1.f, 0.f);

x = -100.f, y = 100.f, z = -320.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x, y, z - j);

glVertex3f(x + i, y, z - j);

glVertex3f(x + i, y, z);

}

z = -320.f;

}

/* задняя стена */

glNormal3f(0.f, 0.f, 1.f);

x = -100.f, y = -100.f, z = -520.f;

for (int i = 1; i <= 200; i++) {

for (int j = 1; j <= 200; j++) {

glVertex3f(x, y, z);

glVertex3f(x + j, y, z);

glVertex3f(x + j, y + i, z);

glVertex3f(x, y + i, z);

}

x = -100.f;

}

glEnd();

Room_items code:

glPushMatrix();

glTranslatef(-34.f, -75.f, -430.f);

glRotatef(20.f, 0.f, 1.f, 0.f);

glutSolidCube(50.0);

glPopMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, brown_material);

glPushMatrix();

glTranslatef(40.f, -85.f, -370.f);

glRotatef(-25.f, 0.f, 1.f, 0.f);

glutSolidCube(30.0);

glPopMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_material);

glPushMatrix();

glTranslatef(0.f, -80.f, -470.f);

glutSolidSphere(20.0, 100, 100);

glPopMatrix();

Main method:

glutInit(&argc, argv);

glutInitWindowSize(800, 800);

glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM | GLUT_DOUBLE);

(void)glutCreateWindow("Cornell Box");

init();

glutDisplayFunc(display);

GLfloat plane[4];

GLfloat v0[3], v1[3], v2[3];

v0[0] = -100.f;v0[1] = -100.f; v0[2] = -320.f;

v1[0] = 100.f; v1[1] = -100.f; v1[2] = -320.f;

v2[0] = 100.f; v2[1] = -100.f; v2[2] = -520.f;

findplane(plane, v0, v1, v2);

shadowmatrix(floorshadow, plane, lightpos1);

glReadBuffer(GL_BACK);

glutMainLoop();

I have a shadows method and MSAA method(which is display func), which I have not included, but i can if needed.

Already tried glLightModeli or switch from only GL_POSITION lights to GL_AMBIENT, GL_SPECULAR, GL_DIFFUSE and GL_POSITION lights.

Any help are much appreciated!

Screenshot:

https://puu.sh/yEKTu/9a7fb6e1de.png