Système solaire open gl avec bibliothèque glut : 5 planetes / lumières / espace

Description

Voici un petit système solaire que j'ai fait en TP à l'école.
C'est une représentation partielle du système solaire (le Soleil, Mercure, Vénus, la Terre et la Lune.
Toutes ces planètes sont sur le même plan et gravitent autour du soleil, la Lune gravitant elle autour de la terre.

J'espère qu'il vous servira.

Touches :
q : Quitter
+ : se rapprocher
- : s�éloigner
l : activer/désactiver les lumières

Source / Exemple :

//***************************************************************************************

// G@BuLe - 2008

//***************************************************************************************





#include <stdio.h>

#include <stdlib.h>



#include"glut.h"

#include<GL/glu.h>



bool light =true;

GLubyte* data;

int eyex=0,eyey=25,eyez=12;

int width=800,height=800;



// Initialisation des angles

float angle0=0.0;

float angle1=0.0;

float angle2=0.0;

float angle3=0.0;

float angle4=0.0;

float angle5=0.0;

float angle6=0.0;

float angle7=0.0;

float angle8=0.0;





GLUquadricObj *obj = gluNewQuadric();

GLUquadricObj *obj_reverse = gluNewQuadric();



//Texture :

GLuint		Nom[6];



//LoadBMP : charge une image 24bpp

#define EXIT {fclose(fichier);return -1;}

#define CTOI(C) (*(int*)&C)	//récupère en int un nombre pointé par un char*



void inc_angles(){

	(angle0 == 360.0)? angle0=0.0 : angle0 +=3.0/15;

	(angle1 == 360.0)? angle1=0.0 : angle1 +=3.0;

	(angle2 == 0.0)? angle2=360.0 : angle2 -=3.0;

	(angle3 == 360.0)? angle3=0.0 : angle3 +=3;

	(angle4 == 360.0)? angle4=0.0 : angle4 +=1.0;

	(angle5 == 360.0)? angle5=0.0 : angle5 +=(0.088*4);

	(angle6 == 360.0)? angle6=0.0 : angle6 +=(0.224*4);

	(angle7 == 360.0)? angle7=0.0 : angle7 +=(3.0/365.0*40.0);

	(angle8 == 360.0)? angle8=0.0 : angle8 +=2.0;

}





int LoadBMP(char *File)

{

	unsigned char	*Data;

	FILE			*fichier;

	unsigned char	Header[0x36];

	GLuint			DataPos,DataSize;

	GLint			Components;

	GLsizei			Width,Height;

	GLenum			Format,Type;

	GLuint			Name[1];



	//Lit le fichier et son header

	fichier = fopen(File,"rb");if (!fichier) return -1;

	if (fread(Header,1,0x36,fichier)!=0x36) EXIT;

	if (Header[0]!='B' || Header[1]!='M')	EXIT;

	if (CTOI(Header[0x1E])!=0)				EXIT;

	if (CTOI(Header[0x1C])!=24)				EXIT;



	//Récupère les infos du fichier

	DataPos			= CTOI(Header[0x0A]);

	DataSize		= CTOI(Header[0x22]);

	//Récupère les infos de l'image

	Width			= CTOI(Header[0x12]);

	Height			= CTOI(Header[0x16]);	

	Type = GL_UNSIGNED_BYTE;

	Format = GL_RGB;

	Components = 3;



	//!!!!

	if (DataSize==0) DataSize=Width*Height*Components;

	if (DataPos==0)  DataPos=0x36;



	//Charge l'image

	fseek(fichier,DataPos,0);

	Data = new unsigned char[DataSize];

	if (!Data) EXIT;



	if (fread(Data,1,DataSize,fichier)!=DataSize) 

	{

		delete Data;

		fclose(fichier);

		return -1;

	}



	fclose(fichier);



	//Inverse R et B

	unsigned char t;

	for (int x=0;x<Width*Height;x++) 

	{

		t=Data[x*3];

		Data[x*3]=Data[x*3+2];

		Data[x*3+2]=t;

	}



	//Envoie la texture à OpenGL

	//	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	glGenTextures(1, Name);

	glBindTexture(GL_TEXTURE_2D, Name[0]);





	glTexImage2D

		( 	

		GL_TEXTURE_2D, 	//target

		0,				//mipmap level

		Components,		//nb couleurs

		Width,			//largeur

		Height,			//hauteur

		0,			 	//largeur du bord

		GL_RGB,			//type des couleurs

		GL_UNSIGNED_BYTE,			//codage de chaque composante

		Data			//Image

		); 



	return Name[0];

}





void init(void) 

{

	// initialise la buffer ou va etre rendu la scene 

	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );

	// initialisation de la fenetre (taille 500x500) 

	glutInitWindowSize (800, 800); 

	// on positionne la fenetre sur l'ecran

	glutInitWindowPosition (100, 100);

	// on cree la fenetre 

	glutCreateWindow ("Système Planetaire .:: G@BuLe ::. || q : Quitter || + : s'approcher || - : s'éloigner || l : lumières On/OFF");

	// couleur de remplissage lors de l'effacement du buffer

	glClearColor (0.0, 0.0, 0.0, 0.0);

	// model d'eclairage 



	// Crée une source de lumière

	GLfloat position1 [] = { 0, 0.0F, 0.0F, 1.0F };

	glLightfv(GL_LIGHT0, GL_POSITION, position1);

	glEnable(GL_LIGHTING);

	glEnable(GL_LIGHT0);

	glShadeModel (GL_SMOOTH);



	// Z Buffer pour la suppression des parties cachées

	glEnable(GL_DEPTH_TEST);



	glEnable(GL_COLOR_MATERIAL);



	glEnable(GL_TEXTURE_2D);

	glMatrixMode (GL_MODELVIEW);	

	glLoadIdentity ();  





	Nom[0] = LoadBMP("sun.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);





	Nom[1] = LoadBMP("mercure.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);



	Nom[2] = LoadBMP("venus.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);



	Nom[3] = LoadBMP("earth.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);



	Nom[4] = LoadBMP("moon.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);





	Nom[5] = LoadBMP("space.bmp");

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);



	glDepthFunc(GL_LESS);

	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}





void display(void)

{

	

	

	//materiaux

	GLfloat ambiant [4] = { 0.2, 0.2, 0.2, 1.0 };

	GLfloat diffuse [4] = { 0.6, 0.6, 0.6, 1.0 };

	GLfloat specular [4] = { 1.0, 1.0, 1.0, 1.0 };

	GLfloat emission [4] = { 0.5, 0.5, 0., 0.0 };

	GLfloat no_material [4] = {0.0,0.0,0.0,0.0};

	GLfloat shininess []= {50.0};







	inc_angles();



	// Affiche l'espace

	glLoadIdentity();

	gluLookAt(eyex,eyey,eyez,0,0,0,0.0,0.0,1.0);

	glClear (GL_COLOR_BUFFER_BIT);

	glClear (GL_DEPTH_BUFFER_BIT);

	glBindTexture(GL_TEXTURE_2D, Nom[5]);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);



	glTranslatef(-0.5,-0.5,0);

	glBegin(GL_QUADS);

	glTexCoord2f(0.0, 0.0); glVertex3f(100.0, -10.0, -10.0);

	glTexCoord2f(0.0, 1.0); glVertex3f(100.0, -10.0, 10.0);

	glTexCoord2f(1.0, 1.0); glVertex3f(-100.0, -10.0, 10.0);

	glTexCoord2f(1.0, 0.0); glVertex3f(-10.0, -10.0, -30.0);

	glEnd();



	// Definit les propriétés matérielles de la couleur spéculaire et du

	// degré de brillance. 

	if (light){

		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambiant);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); 

		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);

		glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);	

		glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);

	}



	// le soleil

	glPushMatrix();

	glBindTexture(GL_TEXTURE_2D, Nom[0]);

	gluQuadricTexture(obj, GL_TRUE );

	gluQuadricDrawStyle(obj, GLU_FILL);

	glTranslatef(0,0.0,0.0);	

	glRotatef(angle0,0.0,0.0,1.0);

	gluSphere(obj,3.0,100.0,100.0);

	glPopMatrix();



	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambiant);

	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); 

	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);

	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);	

	glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, no_material);



	// mercure

	glPushMatrix();

	glBindTexture(GL_TEXTURE_2D, Nom[1]);

	gluQuadricTexture(obj, GL_TRUE );

	gluQuadricDrawStyle(obj, GLU_FILL);

	glRotatef(10,1,0,0);

	glRotatef(angle5,0.0,0.0,1.0);

	glTranslatef(5.0,0.0,0.0);

	glRotatef(angle1,0.0,0.0,1.0);

	gluSphere(obj,0.55,100.0,100.0);

	glPopMatrix();



	// venus

	glPushMatrix();

	glBindTexture(GL_TEXTURE_2D, Nom[2]);

	gluQuadricTexture(obj, GL_TRUE );

	gluQuadricDrawStyle(obj, GLU_FILL);



	//glColor3f (1.0, 0.0, 0.0);

	glRotatef(340,1,0,0);

	glRotatef(angle6,0.0,0.0,1.0);

	glTranslatef(7.0,0.0,0.0);

	glRotatef(angle2,0.0,0.0,1.0);

	gluSphere(obj,0.815,100.0,100.0);

	glPopMatrix();



	// terre

	glPushMatrix();

	glBindTexture(GL_TEXTURE_2D, Nom[3]);

	gluQuadricTexture(obj, GL_TRUE );

	gluQuadricDrawStyle(obj, GLU_FILL);

	glRotatef(angle7,0.0,0.0,1.0);

	glRotatef(30,1,0,0);

	glTranslatef(9.5,0.0,0.0);

	glRotatef(360-angle7,0.0,0.0,1.0);

	glRotatef(angle3,0.0,0.0,1.0);

	glRotatef(23.0,0.0,0.0,1.0);

	gluSphere(obj,1.0,100.0,100.0);



	// lune

	glPushMatrix();

	glBindTexture(GL_TEXTURE_2D, Nom[4]);

	gluQuadricTexture(obj, GL_TRUE );

	gluQuadricDrawStyle(obj, GLU_FILL);

	glColor3f (1.0,1.0,1.0);

	glRotatef(30,1,0,0);

	glRotatef(angle8,0.0,0.0,1.0);

	glTranslatef(1.3,0.0,0.0);

	glRotatef(angle4,0.0,0.0,1.0);

	gluSphere(obj,0.05,100.0,100.0);

	glPopMatrix();

	glPopMatrix();



	glutSwapBuffers();

	glutPostRedisplay();

}





void reshape (int width,int height){

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);

	glLoadIdentity();

	gluPerspective(45.0,width/height,1.0,3000.0);

	glMatrixMode(GL_MODELVIEW);

}



// fonction de gestion du clavier 

void keyboard(unsigned char key, int x, int y)

{

	switch (key) {

	  case 'l':

		  if (light){

			  light=false;

			  glDisable(GL_LIGHT0);

		  }else{

			  light=true;

			  glEnable(GL_LIGHT0);

		  }

		  break;

	  case 'q':exit(0); break;

	  case '-' : eyex +=5.0 ;break;

	  case '+' : eyex -=5.0 ;break;

	  default:printf ("La touche %d n&#65533;est pas active.\n", key); break;

	}

	glMatrixMode(GL_MODELVIEW);

	glutPostRedisplay();

}



int main(int argc, char** argv)

{

	glutInit(&argc, argv);



	// on initialise la scene

	init ();



	// definition des fonctions "callback"

	// les 3 fonctions qui suivent permettent d'indiquer les fonctions a utilisé :

	// lors de l'affichage

	glutDisplayFunc(display); 

	// lors du redimensionnement de la fentre 

	glutReshapeFunc(reshape);

	// lors d'un appui sur un touche du clavier

	glutKeyboardFunc(keyboard); 



	// la librairie glut gere elle meme la boucle d'evenement

	glutMainLoop();  



	return 0;

}

Codes Sources

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.