Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a OpenGL program that shows a cube.
But the light is not correct because when I rotate the cube, not all the faces are correctly lighted: there are two faces that appears black.

Here's the code:

#include<GL\glut.h><br />
#include<GL\GL.h>º<br />
<br />
float rotX = 0.0, rotY = 0.0;<br />
<br />
void light ()<br />
{<br />
	static float Position [] = {0.0, 0.0, 1.0, 0.0};<br />
	static float Diffuse  [] = {0.8, 0.8, 0.8, 1.0};<br />
	static float Ambient  [] = {0.3, 0.3, 0.3, 1.0};<br />
	static float Specular [] = {1.0, 1.0, 1.0, 1.0};<br />
<br />
	glLightfv(GL_LIGHT0, GL_POSITION, Position);<br />
	glLightfv(GL_LIGHT0, GL_DIFFUSE,  Diffuse);<br />
	glLightfv(GL_LIGHT0, GL_AMBIENT,  Ambient);<br />
	glLightfv(GL_LIGHT0, GL_SPECULAR, Specular);<br />
<br />
	glEnable(GL_LIGHTING);<br />
	glEnable(GL_LIGHT0);<br />
}<br />
<br />
void drawCube(void)<br />
{<br />
	int i;<br />
<br />
	GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */<br />
	{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},<br />
	{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };<br />
	<br />
	GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */<br />
	{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},<br />
	{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };<br />
	<br />
	GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */<br />
<br />
	v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;<br />
	v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;<br />
	v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;<br />
	v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;<br />
	v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;<br />
	v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;<br />
<br />
	for (i = 0; i < 6; i++) {<br />
		glBegin(GL_QUADS);<br />
			glNormal3fv(&n[i][0]);<br />
			glVertex3fv(&v[faces[i][0]][0]);<br />
			glVertex3fv(&v[faces[i][1]][0]);<br />
			glVertex3fv(&v[faces[i][2]][0]);<br />
			glVertex3fv(&v[faces[i][3]][0]);<br />
		glEnd();<br />
	}<br />
}<br />
<br />
void display ()<br />
{<br />
	glClear(GL_COLOR_BUFFER_BIT);<br />
	glClear(GL_DEPTH_BUFFER_BIT);<br />
		<br />
	glMatrixMode(GL_MODELVIEW);<br />
	glLoadIdentity();<br />
<br />
	float diffuse [] = {1.0, 1.0, 0.0};<br />
	glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);<br />
	<br />
	glTranslatef(0.0,0.0,-5.0);<br />
	glRotatef(rotX, 0.0, 1.0, 0.0);<br />
	glRotatef(rotY, 1.0, 0.0, 0.0);<br />
	<br />
	drawCube();<br />
<br />
	glFlush();<br />
}<br />
<br />
void reshape (int w, int h)<br />
{<br />
	float aspect = (float)w/(float)h;<br />
	<br />
	glViewport(0, 0, w, h);<br />
<br />
	glMatrixMode(GL_PROJECTION);<br />
	glLoadIdentity();<br />
<br />
	gluPerspective(60.0, aspect, 0.1, 10.0);<br />
}<br />
<br />
void special (int key, int, int)<br />
{<br />
	switch (key)<br />
	{<br />
	case GLUT_KEY_UP:		rotY -= 1.0; break;<br />
	case GLUT_KEY_DOWN:		rotY += 1.0; break;<br />
	case GLUT_KEY_LEFT:		rotX -= 1.0; break;<br />
	case GLUT_KEY_RIGHT:	rotX += 1.0; break;<br />
	default: return;<br />
	}<br />
<br />
	glutPostRedisplay();<br />
}<br />
<br />
int main (int argc, char* argv [])<br />
{<br />
	glutInit(&argc, argv);<br />
	glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH);<br />
	glutCreateWindow("Cube");<br />
	glutReshapeWindow(640,480);	<br />
<br />
	glutDisplayFunc(display);<br />
	glutReshapeFunc(reshape);<br />
	glutSpecialFunc(special);<br />
<br />
	glClearColor(0.0, 0.0, 0.0, 1.0);<br />
<br />
	light();<br />
	<br />
	glEnable(GL_DEPTH_TEST);<br />
	<br />
	glutMainLoop();<br />
<br />
	return 0;<br />
}


Can someone help me?

What I have tried:

I tried to correct normals, and I read the CUBE.C tutorial from OpenGL.
Posted
Updated 22-Jul-17 7:37am

1 solution

Change the coordinates of the missed surfaces and/or try other light position. Maybe the coordinates of your skin are bottom up or have some sign issues.

Your code is malformatted with some HTML tags.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900