I'm drawing a sphere and some ground. I used GL_POLYGON to draw a rectangle and colored it green. When I added a sphere, my ground just turned black. Is this because the sphere is a 3d object and the polygon is 2d or is there something else? Here is the code for my sphere and my ground, which are just in my display func.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw sphere
   glPushMatrix();
   glTranslatef (-4.0, 5.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(.5, 50, 50);
   glPopMatrix();

//ground
	glMatrixMode(GL_PROJECTION);
	glColor3f(0.0,1.0,0.0);
	glBegin(GL_POLYGON);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();

Recommended Answers

All 8 Replies

Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.

Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.

What do you mean by my normals? It worked fine and was green until until I added my sphere. Now my ground and sphere is just gray.

Did you call glNormalize in your init function ?

By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).

Try this :

glBegin(GL_POLYGON);
           glNormal(0.0f,0.0f,1.0f);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();

Did you call glNormalize in your init function ?

By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).

Try this :

glBegin(GL_POLYGON);
           glNormal(0.0f,0.0f,1.0f);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();

Ok, I tried that, didn't do anything. I put glEnable(GL_NORMALIZE); in my init() function. Everything is still gray.

wanna send me the file so I can take a look at it. Make sure you
have enabled lighting and LIGHT0

Here is my code. It is supposed to be a sky background, with a green floor and a sphere in the air. Doesn't really matter what color the sphere is. I think it is blue now. I really appreciate your help!

#define GLUT_DISABLE_ATEXIT_HACK
#include <stdlib.h>
#include <GL/glut.h>


// Globals
void display();
void myInit();
//Set up material
	GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
	GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	GLfloat mat_ambient_color[] = { 1,1,1,1 };
	GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat no_shininess[] = { 0.0 };
	GLfloat low_shininess[] = { 5.0 };
	GLfloat high_shininess[] = { 100.0 };
	GLfloat mat_emission[] = {1,1,1,1};

//Set up lighting
	GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
	
	GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
	GLfloat ambient0[]={1.0, 1.0, 1.0, 1.0};
	GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};

	GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
   GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
   GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
   GLfloat local_view[] = { 0.0 };
   GLfloat global_ambient[]={0,0,0,0};

void myReshape(int w, int h);
void drawSphere();
GLsizei wh=800, ww=800;




void myInit()
{

  
   glClearColor(0.7f, 0.9f, 1.0f, 1.0f);	// Set background color to sky blue.
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);

   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
   glLightfv(GL_LIGHT0, GL_POSITION, position);
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_NORMALIZE);	
}


void display(void)
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw sphere
   glPushMatrix();
   glTranslatef (-4.0, 5.0, 0.0);
  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  glutSolidSphere(.5, 50, 50);
   glPopMatrix();

//ground
	glMatrixMode(GL_PROJECTION);
	glColor3f(0.0,1.0,0.0);
	glBegin(GL_POLYGON);
	glNormal3f(0.0f,0.0f,1.0f);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();
	glFlush();
	glutReshapeFunc(myReshape);
}


void myReshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= (h * 2))
      glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 
         3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 
         6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   ww=w;
   wh=h;
}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (800, 800);
   glutCreateWindow("Sphere");
   myInit();
   glutReshapeFunc(myReshape);
   glutDisplayFunc(display);
   glutMainLoop();
   return 0; 
}

You forgot to enable GL_COLOR_MATERIAL, when using lighting. I
changed it a little, take a look at the comments.

#define GLUT_DISABLE_ATEXIT_HACK
#include <stdlib.h>
#include <GL/glut.h>


// Globals
void display();
void myInit();
//Set up material
	GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
	GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	GLfloat mat_ambient_color[] = { 1,1,1,1 };
	GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat no_shininess[] = { 0.0 };
	GLfloat low_shininess[] = { 5.0 };
	GLfloat high_shininess[] = { 100.0 };
	GLfloat mat_emission[] = {1,1,1,1};

//Set up lighting
	GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
	
	GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
	GLfloat ambient0[]={0.4, 1.0, 1.0, 1.0};
	GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};

	GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
   GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
   GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
   GLfloat local_view[] = { 0.0 };
   GLfloat global_ambient[]={0,0,0,0};

void myReshape(int w, int h);
void drawSphere();
GLsizei wh=800, ww=800;

void myInit()
{
  
   glClearColor(0.7f, 0.9f, 1.0f, 1.0f);	// Set background color to sky blue.
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);

   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
   glLightfv(GL_LIGHT0, GL_POSITION, position);

   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_NORMALIZE);	
	//NEW
   glEnable(GL_COLOR_MATERIAL);

  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
}


void display(void)
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

//Draw sphere
   glPushMatrix();
   glTranslatef (-4.0, 5.0, 0.0);
   glColor3f(1.0f,1.0f,0.0f);
		glutSolidSphere(.5, 50, 50);
   glPopMatrix();

//ground		
	glBegin(GL_POLYGON);
	glColor3f(0.0,1.0,0.0);
		glNormal3f(0.0f,0.0f,1.0f);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();
	
	//dont use glFlush()
	//glFlush();
	
	//use these two instead
	glutSwapBuffers();
	glutPostRedisplay();

	//just call this once in your main
	glutReshapeFunc(myReshape);
}


void myReshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= (h * 2))
      glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 
         3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 
         6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   ww=w;
   wh=h;
}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   //use GLUT_DOUBLE not GLUT_SINGLE
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (800, 800);
   glutCreateWindow("Sphere");
   myInit();
   glutReshapeFunc(myReshape);
   glutDisplayFunc(display);
   glutMainLoop();
   return 0; 
}

Thank you so much! I never would had thought to enable GL_COLOR_MATERIAL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.