PDA

View Full Version : Need help with my solar system



angel120
10-09-2009, 08:22 AM
I'm attempting to write a solar system using the C++ OpenGL API. I have a planet class, and I'm able to show all the planets, their orbits, etc. I also have a switch where I can change from the geocentric solar system to the heliocentric solar system. However, I'm having trouble controlling the lighting.

I'm able to get the heliocentric model to look (somewhat) correctly, by positioning GL_LIGHT0 to the origin

GLfloat LightPos[]={0,0,0,1};//Sun as light source
(which is where the Sun is placed).
However, since the light is located in the origin, no light is shining on the sun itself, so it looks severely unnatural (think of a light bulb lighting up a room even though it's off).

My next issue is to get the light source to change from GL_LIGHT0 to GL_LIGHT1 when I switch from heliocentric to geocentric. I'm able to get the planets to change position, but here is how I'm switching between my light sources:


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'l':
geocentric=!geocentric;
for(int i=0; i<7; i++)
planet[i]=new Planet(i,geocentric); //memory leak, I know
if(geocentric==true)
{
glDisable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}//if
else if(geocentric==false)
{
glDisable(GL_LIGHT1);
glEnable(GL_LIGHT0);
}//else if
break;
}//switch
glutPostRedisplay();
}//keyboard

and I have the following in my main function:


glEnable(GL_LIGHTING);
if(!geocentric)glEnable(GL_LIGHT0);
else if(geocentric)glEnable(GL_LIGHT1);

and the following in display:


if(!geocentric)
glLightfv(GL_LIGHT0,GL_POSITION,LightPos);
else if(geocentric)
glLightfv(GL_LIGHT1,GL_POSITION,LightPos2); //LightPos2 is set to an arbitrary location on the x-axis for now, {20,0,0,1}


What am I doing wrong? I'll gladly post my source code if anyone needs any more info. Would've done so in the first place, but I'm not 100% sure if this forum allows that sort of thing.

Thanks for any and all help in advance,
-Angel.

angel120
10-09-2009, 10:56 AM
Update:

I managed to get the light source switching to work by simply changing the coordinates of GL_LIGHT0 when I enable/disable the geocentric model. However, GL_LIGHT0 stays in place when the planets start revolving. I'm going to look up tutorials on how to make rotating/revolving lights, but if anyone can add any input/tips/etc, it would be greatly appreciated.

I'm still having my other issue, unfortunately, which is my Sun not being illuminated even though it is giving off light. Basically, the light source is located within the Sun, and the light goes through the Sun's surface, but since no light is shining on its surface, the Sun appears dim.

My heliocentric solar system. The order of planets: Sun (center), Mercury, Venus, Earth, Mars, Jupiter, Saturn. Notice the Sun is not illuminated, but is giving off light.
http://i77.photobucket.com/albums/j43/doublebass120/Solar%20System/Heliocentric.png

My geocentric solar system. The order of planets: Earth (center), Mercury, Venus, Sun, Mars, Jupiter, Saturn. Notice that the Sun is not illuminated, but it is giving off light.
http://i77.photobucket.com/albums/j43/doublebass120/Solar%20System/Geocentric.png

My geocentric solar system, with the planets revolving around the Earth. Notice that the Sun is now illuminated from a light source that started in the Sun's initial position.
http://i77.photobucket.com/albums/j43/doublebass120/Solar%20System/GeocentricRevolution.png

dukey
10-09-2009, 11:27 AM
Manchester computer science ? :)

angel120
10-09-2009, 11:33 AM
lol, no, I wish I went to a better school!

Queens College (in NYC) Computer Science. I'm actually creating this solar system for a Physics presentation I have to do on Tuesday (on Copernicus), which is why I really need this thing to look good... I need all the points I can get!

dukey
10-09-2009, 11:39 AM
The reason the sun isn't lit is because you are defining the light source light 0 or whatever inside the sun, at the origin. And the normals of the glut solid sphere point away from the sun. If you position the camera inside the sun you'll probably find the inside is lit. There are a number of ways around this.

Since the sun is supposed to be emitting light you could disable lighting entirely and just draw it full bright. This is perfectly valid.

You could just flip the normals so they face inwards instead of outwards.

Or the easiest way might just be to enable two sided lighting :p

GL_LIGHT_MODEL_TWO_SIDE should do the trick .. I think

Aleksandar
10-09-2009, 12:14 PM
Notice that the Sun is now illuminated from a light source that started in the Sun's initial position.
The Sun should not be illuminated from the light source, because the Sun SHOULD LOOK LIKE IT IS A LIGHT SOURCE.
So, forget about changing normals, and set special material for the Sun, which must have a very high emission component.

angel120
10-09-2009, 06:57 PM
I'm really new to OpenGL. I only started OpenGL coding in July, and I haven't done all that much. Unfortunately, I'm not too sure what you mean by setting a special material with a very high emission component. :o

Aleksandar
10-10-2009, 01:34 AM
In order to define the lighting effect on the surface of the objects, you have to define their material properties. How did you define the colors of your planets?

A material property is defined by calling function glMaterialfv() with proper parameters. You can define several properties of material:
- ambient color
- diffuse color
- specular color
- shininess and
- emission color

To set emission color for your Sun, you can do something like this:

GLfloat mat_em[] = (1.0, 0.9, 0.9, 0.0);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_em);

More information you can find in the "Red Book" (OpenGL Programming Guide) in the chapter 5 - Lighting, or in any other OpenGL book...

dletozeun
10-10-2009, 02:24 AM
In addition, take a look to this tutorial about materials:
http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial14.php

remdul
10-10-2009, 06:00 AM
Another good reference:
http://www.sjbaker.org/steve/omniv/opengl_lighting.html

angel120
10-12-2009, 07:15 PM
Just as an update, I finally figured out what I was doing wrong with GL_EMISSION. As it turns out, I was supposed to apply the emission within the Sun's matrix.

Next, to get the light to revolve with the sun, simply call:
glLightfv(GL_LIGHT0,GL_POSITION,sunPosition);
within the sun's matrix, and poof! Rotating light!

Thanks to all who have replied to this thread, all of your help is greatly appreciated!