PDA

View Full Version : Fast question about lights.



Anonymous Coward
08-07-2000, 12:49 AM
Ok, I have a solar system with the sun at (0.0.0). I want to have a light that simule the real world sun over the planets, moons,etc. The problem is that I tried using a positional light at (0.0.0) but it is lighting ALL the scene and not only the front faces of the planets facing the sun (the scenes has rotating planets). What can you tell me?

Relic
08-07-2000, 01:10 AM
Sounds as if you have specified the ambient component of the light too bright.
Try to set the ambient to a very low value like {0.1, 0.1, 0.1, 1.0} and the diffuse component to bright values like {0.9, 0.9, 0.7, 1.0} for some yellowish appearance.

Or perhaps you have enabled the ambient light model?

Ah, forgot one. Watch out for the position vector it has to be specified as homogenous coordinate so the origin is at {0.0, 0.0, 0.0, 1.0}. Setting the w component to zero means a vector specifieing a directional light. With the null-vector this is not meaningful.

Post code if that is not helping.


[This message has been edited by Relic (edited 08-07-2000).]

Anonymous Coward
08-07-2000, 02:23 AM
I tried that, but no success, look at my code, maybe is a materials problem, but I dont know...
/////////LIGHT///////////////////////////
float light_ambient[] = { 0.1,0.1,0.1,1.0 };
float light_diffuse[] = { 0.9,0.9,0.4,1.0 };
float light_specular[] = { 0.0,0.0,0.0,0.0 };
float light_pos[] = { 0.0,0.0,0.0,1.0 };
///////MATERIAL////////////////////////
float mat_earth_amb_dif[] = { 0.0,0.5,0.8 };
float mat_earth_emission[] = { 0.0,0.0,0.0 };
float mat_earth_specular[] = {0.0,0.0,0.0};

float mat_sun_amb_dif[] = { 1.0,0.9,0.0 };
float mat_sun_emission[] = { 0.2,0.2,0.0 };
float mat_sun_specular[] = { 0.0,0.0,0.0 };
///////////////////////////////////////

in init(),
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient)
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse)
glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular)
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

/// End Of Code ///////////

Sun is at (0,0,0), Earth start at (40,0,0) and rotates.

The light keep lighting all the scene...

Hope you can help me, thx!

Relic
08-07-2000, 02:57 AM
The materials have to contain four components, add alpha = 1.0.

The rest looks fine.

Please post more code for following topics
- glMaterial code
- modelview and projection matrices
- when the light position is set
- usage of textures? (GL_MODULATE (!) or GL_DECAL?)
- light model(?)

Anonymous Coward
08-07-2000, 04:53 AM
I have no light model, I tried with glLightModel functions but no success. There are no textures actually.

/////// Code ///////////

::Init()
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

//////////////I`ll resume this//////
: http://www.opengl.org/discussion_boards/ubb/biggrin.gifisplay()

PushMatrix();
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_s un_amb_dif);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_sun_specular );
glMaterialfv(GL_FRONT,GL_EMISSION,mat_sun_emission );
glutSolidSphere(10.0,30.0,10.0); // the sun

PushMatrix();
glRotatef(RotEarthSun,0.0f,1.0f,0.0f);
glTranslatef(40.0f,0.0f,0.0f);
glRotatef(RotEarth,0.0f,1.0f,0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_tierra_amb_dif);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_tierra_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_tierra_emission);
glutSolidSphere(3.0f,15.0f,7.0f); // earth

Pop();
Pop();

//////////////////////////////////

I tried taking the sun out (0.0.0), I tried enabling the light after the sun, but no way. The light should work as a bulb...

Thx!

Anonymous Coward
08-07-2000, 04:57 AM
Ups I did a mistake in the last post. In the glMaterial*() lines its supposed to be
"mat_earth_amb_dif" and not "mat_tierra_amb_dif" so dont think thats the problem!

08-07-2000, 05:47 AM
I combined some stuff above with another program and i created i working example of a sun two planets and a moon. All with correct lightning

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

static int year = 0, day =0;
float mat_earth_amb_dif[] ={ 0.0,0.5,0.8 };
float mat_earth_emission[] = { 0.0,0.0,0.1 };
float mat_earth_specular[] = {1.0,1.0,1.0};

float mat_sun_amb_dif[] = { 0.9,0.9,0.0 };
float mat_sun_emission[] = { 0.4,0.4,0.2 };
float mat_sun_specular[] = { 0.0,0.0,0.0 };
float light_pos2[] = {0, 0.0, 0.0, 0 };

void init(void)
{

/////////LIGHT///////////////////////////
float light_ambient[] = { 1.0,1.0,1.0,1.0 };
float light_diffuse[] = { 1.0,1.0,1.0,1.0 };
float light_specular[] = { 0.0,0.0,0.0,1.0 };
float light_pos[] = { 0.0,0.0,0.0,1 };



///////MATERIAL////////////////////////
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_sun_emission);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_sun_amb_dif);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_sun_specular);
///////////////////////////////////////
glClearColor (0.1, 0.1, 0.1, 0.1);
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT1,GL_SPECULAR, light_specular);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// glEnable(GL_LIGHT1);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
/// End Of Code ///////////

glShadeModel (GL_SMOOTH);
}

void display(void)
{

glColor3f (1.0, 1.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_earth_emission);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_earth_amb_dif);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_earth_specular);



glPushMatrix();
glDisable(GL_LIGHT0);
if(sin(6.28*((year-90)%360)/360)>0.1 &#0124; &#0124; sin(6.28*((year-90)%360)/360) < -0.1)
light_pos2[0] = sin(6.28*((year-90)%360)/360);
else
light_pos2[0]=0;

if(cos(6.28*((year-90)%360)/360)>0.1 &#0124; &#0124; cos(6.28*((year-90)%360)/360) < -0.1)
light_pos2[2] = cos(6.28*((year-90)%360)/360);
else
light_pos2[2]=0;
glLightfv(GL_LIGHT1,GL_POSITION,light_pos2);
glEnable(GL_LIGHT1);
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glPushMatrix();
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutSolidSphere(0.2, 10, 8); /* draw smaller planet */
glPopMatrix();
glPopMatrix();

glPushMatrix();
glDisable(GL_LIGHT0);
if(sin(6.28*((year-90)%360)/360)>0.1 &#0124; &#0124; sin(6.28*((year-90)%360)/360) < -0.1)
light_pos2[0] = sin(6.28*((-year-90)%360)/360);
else
light_pos2[0]=0;

if(cos(6.28*((year-90)%360)/360)>0.1 &#0124; &#0124; cos(6.28*((year-90)%360)/360) < -0.1)
light_pos2[2] = - cos(6.28*((year-90)%360)/360);
else
light_pos2[2]=0;
glLightfv(GL_LIGHT1,GL_POSITION,light_pos2);
glEnable(GL_LIGHT1);
glRotatef ((GLfloat) -year, 0.0, 1.0, 0.0);
glPushMatrix();

glTranslatef (3.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutSolidSphere(0.2, 10, 8); /* draw smaller planet */
glTranslatef(0.5, 0.0, 0.0);
glutSolidSphere(0.1, 10, 8); /* draw smaller planet */
glPopMatrix();
glPopMatrix();


glDisable(GL_LIGHT1);
glEnable(GL_LIGHT0);
mat_sun_amb_dif[0]=0.9 + 0.1 * cos(6.28 * year/360);
mat_sun_amb_dif[1]=0.9 - 0.1 * sin(6.28 * year/360);
mat_sun_amb_dif[2]=0.1 + 0.1 * sin(6.28 * year/360);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_sun_emission);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_sun_amb_dif);
glPushMatrix();
glutSolidSphere(1.0, 20, 16); /* draw sun */
glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 0.1, 250.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (10.0, 0.0, 0.0, 0.0, 0.0, 0.0,0,1, 0.0);
}

void spin()
{
day = (day + 40) % 360;
year = (year + 1) % 360;
glutPostRedisplay();
}

/* ARGSUSED1 */
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd':
day = (day + 40) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 40) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 1) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 1) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutIdleFunc(spin);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Anonymous Coward
08-07-2000, 09:11 AM
It seems like your code use a directional light that keeps rotating to light all the planets...

Isīnt there other way to achieve what I want easily? Is there something like a bulb light?

Thx!

ngill
08-07-2000, 11:44 PM
there is a superbible program that does EXACTLY what you are trying to do to demonstrate push/pop matrixes!! do you want the source?

ngill
08-07-2000, 11:58 PM
i took like 20 lines of code out...




// Solar.c
// OpenGL SuperBible, Chapter 5
// Demonstrates OpenGL nested coordinate transformations
// and perspective
// Program by Richard S. Wright Jr.

#include <gl/glut.h>
#include <math.h>
#include <stdio.h>

// Lighting values
GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };


// Called to draw scene
void RenderScene(void)
{
// Earth and Moon angle of revolution
static float fMoonRot = 0.0f;
static float fEarthRot = 0.0f;

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Save the matrix state and do the rotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Set light position before viewing transformation
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

// Translate the whole scene out and into view
glTranslatef(0.0f, 0.0f, -300.0f);

// Set material color, Red
// Sun
glColor3ub(255, 255, 0);
glutSolidSphere(15.0f, 15, 15);

// Move the light after we draw the sun!
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

// Rotate coordinate system
glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);

// Draw the Earth
glColor3ub(0,0,255);
glTranslatef(105.0f,0.0f,0.0f);
glutSolidSphere(15.0f, 15, 15);


// Rotate from Earth based coordinates and draw Moon
glColor3ub(200,200,200);
glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
glTranslatef(30.0f, 0.0f, 0.0f);
fMoonRot+= 15.0f;
if(fMoonRot > 360.0f)
fMoonRot = 0.0f;

glutSolidSphere(6.0f, 15, 15);

// Restore the matrix state
glPopMatrix(); // Modelview matrix


// Step earth orbit 5 degrees
fEarthRot += 5.0f;
if(fEarthRot > 360.0f)
fEarthRot = 0.0f;

// Show the image
glutSwapBuffers();
glutPostRedisplay();
}


// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Light values and coordinates
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// Black blue background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

// Set Viewport to window dimensions
glViewport(0, 0,300, 300);

// Set the perspective coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// field of view of 45 degrees, near and far planes 1.0 and 425
gluPerspective(45.0f, 1, 1.0, 425.0);

// Modelview matrix reset
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Earth/Moon/Sun System");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();

return 0;
}

zed
08-09-2000, 01:10 PM
just a note should the earth/moon have any emission?