System solaire avec lighting

Contenu du snippet

Voila mon premier programme en OpenGL qui utilise glRotate(), glTranslate(), etc pour illustré le mouvement de la terre autour du soleil et de la lune autour de la terre .... j'ai ajoute du lighting pour donner un effect plus réel mais c pas encore ca ...

PS: desole pour les commentaires en anglais

Source / Exemple :


#include <windows.h>
#include <gl\glut.h>

float g_LightPosition[3] = {0, 0, -300};
float g_bLight = true;	

void RenderScene (void)
{
	//angle of revolution for moon/earth
	static float fMoonRot = 0.0f;
	static float fEarthRot = 0.0f;

	//clear the screen with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

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

	glPushMatrix();

	
	glLightfv( GL_LIGHT0, GL_POSITION, g_LightPosition );	

	// Translate view 300 units 'into' the screen
	glTranslatef(0.0f, 0.0f, -300.0f);

	//Set material color, Red
	//Sun
	glColor3ub(255, 255, 0);

	glutSolidSphere(18.0f, 15, 15);

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

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

	// Moon Rotation from earth-based coordinated
	glColor3ub(200,200,200);
	glRotatef(fMoonRot, 0.0f, 1.0f, 0.0f);
	glTranslatef(30.0f, 0.0f, 0.0f);

	fMoonRot += 1.0f;
	if (fMoonRot > 360.0f )
		fMoonRot = 0.0f;

	glutSolidSphere(5.0f, 15, 15);

	//Restore the matrix
	glPopMatrix();

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

	glutSwapBuffers();
	glutPostRedisplay();

}

void SetupRC (void)
{

	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	float ambience[4] = {0.5f, 0.5f, 0.5f, 1.0};		
	float diffuse[4] = {0.5f, 0.5f, 0.5f, 1.0};

	glLightfv( GL_LIGHT0, GL_AMBIENT,  ambience );
	
	glLightfv( GL_LIGHT0, GL_DIFFUSE,  diffuse );	
	
	glLightfv( GL_LIGHT0, GL_POSITION, g_LightPosition );

	glEnable(  GL_LIGHT0   );

	glEnable(  GL_LIGHTING );

	glEnable(GL_COLOR_MATERIAL);

}

void ChangeSize (GLsizei w, GLsizei h)
{
	GLfloat fAspect;

	//prevent a divide by zero
	if ( h == 0)
		h = 1;

	// Set viewport to window dimensions
	glViewport(0, 0, w, h);

	// Calculate the ratio of aspect
	fAspect = (GLfloat)w / (GLfloat)h;

	//Set the perspective coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(45.0f, fAspect, 1.0, 450.0 );

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

void main (void)
{
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutCreateWindow("Solar System");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);

	SetupRC();

	glutMainLoop();
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.