views:

367

answers:

2
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

ifstream myFile("Coordinates.txt");

if (!myFile.is_open())
{
 cout << "Unable to open file";
 exit(1); // terminate with error
}

// Light values and coordinates
float ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
float diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
float lightPos[] = { 0.0f, -150.0f, -150.0f, 1.0f };

glEnable(GL_CULL_FACE);  // Do not calculate inside of jet
glFrontFace(GL_CCW);  // Co unter clock-wise polygons face

// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Light values and coordinates

float specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine 
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specref);
glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,128);


while (! myFile.eof()) 
{
 glPushMatrix();
 myFile>>plot[0];
 myFile>>plot[1];
 myFile>>plot[2];
 myFile>>plot[3];  //this data will not be used

 glColor3f(0.60f/1.5,0.80f/1.5,0.90f/1.5);

 glTranslatef((plot[0]-1.15)*26, (plot[2]-0.51)*45, (plot[1]-1)*30);
 glutSolidSphere(2, 12, 12);
 glLoadIdentity();
 glPopMatrix();

 axes += 0.00005f;

}

glRotatef(axes, 0.0f, 1.0f, 0.0f);   

myFile.close();
glFlush();
glutSwapBuffers();

This is my 1st time playing with lighting.

My problem is that after i place all the light effect code from a tutorial the objects seem only exist in one plane which is the xy-plane thought my data have coordinated in all xyz and the reflection seems a bit off..

can anyone tell me why and how to fix it?

+1  A: 

Have a look-see here: Avoiding 16 Common OpenGL Pitfalls

epatel
+1  A: 

You haven't given enough information. What values are in your file? Why are you loading plot[3] when it goes unused? Do you mean that the glutSphere is rendering as a flat 2d object in the xy plane?

I'd recommend you familiarise yourself with the core OpenGL functionality before using the in-built lighting, this problem probably has nothing to do with lighting. I also wouldn't recommend using GL's inbuilt lighting for any thing other than testing and tiny projects anyway... its not very flexible and has lots of limitations too.

jheriko
plot[3] is to take the 4th dimension data its not to be used here that's all.
noob88