begin process at 2012 05 27 17:34:39
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

OpenGL

 > OPENGL : GALAXIE

OPENGL : GALAXIE


 Information sur la source

Note :
10 / 10 - par 2 personnes
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :OpenGL Niveau :Débutant Date de création :08/12/2003 Vu / téléchargé :2 267 / 343

Auteur : JCDjcd

Ecrire un message privé
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note


 Description

voila un petit programme qui reprensente une pseudo-galaxie (elle a une forme d'un cube ...) il en plus il existe 3 type de planetes !! (rouge,vert,bleu)

bon elles sont reprensentees par un point (c'est ca le but de la source)

Source

  • #include <windows.h>
  • #include <gl/gl.h>
  • #include <gl/glu.h>
  • #include <gl/glaux.h>
  • #include <math.h>
  • //------------------------------------------------------------
  • #define PI 3.1415926535
  • //------------------------------------------------------------
  • #define MY_MIN_ZOOM 1
  • #define MY_MAX_ZOOM 100
  • #define NBPOINT 5000
  • #define MY_CUBE_LINE 99
  • #define MY_PLANET_1 100
  • #define MY_PLANET_2 101
  • #define MY_PLANET_3 102
  • //------------------------------------------------------------
  • double CompiloRandomDouble(double a,double b)
  • {
  • return a + ((b-a)*(double)rand())/((double)RAND_MAX);
  • }
  • //------------------------------------------------------------
  • void RePaint(HDC DC,double zoom,double theta,double phi)
  • {
  • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • // position de l'oeil
  • gluLookAt(zoom*cos(theta)*cos(phi),zoom*sin(theta)*cos(phi),zoom*sin(phi),0,0,0,cos(theta-PI)*cos(PI/2.-phi),sin(theta-PI)*cos(PI/2.-phi),sin(PI/2.-phi));
  • // on trace les lignes du cube
  • glCallList(MY_CUBE_LINE);
  • glCallList(MY_PLANET_1);
  • glCallList(MY_PLANET_2);
  • glCallList(MY_PLANET_3);
  • glPopMatrix();
  • SwapBuffers(DC);
  • }
  • //------------------------------------------------------------
  • void InitPixelFormat(HDC hdc)
  • {
  • PIXELFORMATDESCRIPTOR pfd = {sizeof (PIXELFORMATDESCRIPTOR),1,PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,16,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0};
  • SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd);
  • }
  • //------------------------------------------------------------
  • void CreatePlanet(int ID,double r,double g,double b)
  • {
  • int i;
  • glNewList(ID,GL_COMPILE);
  • glColor3d(r,g,b);
  • glBegin(GL_POINTS);
  • for(i=0;i<NBPOINT;i++)
  • {
  • double x,y,z;
  • x = CompiloRandomDouble(-10.,+10.);
  • y = CompiloRandomDouble(-10.,+10.);
  • z = CompiloRandomDouble(-10.,+10.);
  • glVertex3d(x,y,z);
  • }
  • glEnd();
  • glEndList();
  • }
  • //------------------------------------------------------------
  • void CreateNewList(void)
  • {
  • glNewList(MY_CUBE_LINE,GL_COMPILE);
  • glColor3d(1.,1.,1.);
  • glBegin(GL_LINE_LOOP);
  • glVertex3d(-10.,-10.,-10.);
  • glVertex3d(-10.,-10.,+10.);
  • glVertex3d(-10.,+10.,+10.);
  • glVertex3d(-10.,+10.,-10.);
  • glEnd();
  • glBegin(GL_LINE_LOOP);
  • glVertex3d(+10.,-10.,-10.);
  • glVertex3d(+10.,-10.,+10.);
  • glVertex3d(+10.,+10.,+10.);
  • glVertex3d(+10.,+10.,-10.);
  • glEnd();
  • glBegin(GL_LINES);
  • glVertex3d(+10.,-10.,-10.);
  • glVertex3d(-10.,-10.,-10.);
  • glVertex3d(+10.,-10.,+10.);
  • glVertex3d(-10.,-10.,+10.);
  • glVertex3d(+10.,+10.,+10.);
  • glVertex3d(-10.,+10.,+10.);
  • glVertex3d(+10.,+10.,-10.);
  • glVertex3d(-10.,+10.,-10.);
  • glEnd();
  • glEndList();
  • CreatePlanet(MY_PLANET_1,1.,0.,0.);
  • CreatePlanet(MY_PLANET_2,0.,1.,0.);
  • CreatePlanet(MY_PLANET_3,0.,0.,1.);
  • }
  • //------------------------------------------------------------
  • LRESULT CALLBACK WindowProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
  • {
  • static HDC DC;
  • static HGLRC RC;
  • static int cxClient,cyClient;
  • static double zoom,theta,phi,_theta,_phi;
  • static BOOL isMouseDown;
  • static int xMouseStart,yMouseStart;
  • // traitement des messages
  • switch (iMsg)
  • {
  • case WM_CREATE:
  • {
  • DC = GetDC(hwnd);
  • InitPixelFormat(DC);
  • RC = wglCreateContext(DC);
  • wglMakeCurrent(DC,RC);
  • glEnable(GL_DEPTH_TEST);
  • glClearColor(0,0,0,0);
  • zoom = 50.;
  • theta = 0.;
  • phi = 0.;
  • CreateNewList();
  • break;
  • }
  • case WM_SIZE:
  • {
  • cxClient = LOWORD(lParam); cxClient = (cxClient ? cxClient : 1);
  • cyClient = HIWORD(lParam); cyClient = (cyClient ? cyClient : 1);
  • glViewport(0,0,cxClient,cyClient);
  • glMatrixMode(GL_PROJECTION);
  • glLoadIdentity();
  • gluPerspective(45.,(float)(cxClient)/(float)(cyClient),0.1,1000);
  • break;
  • }
  • case WM_CHAR:
  • {
  • switch((char)wParam)
  • {
  • case 'a':
  • {
  • zoom -= 1.;
  • if(zoom < MY_MIN_ZOOM) zoom = MY_MIN_ZOOM;
  • return 0;
  • }
  • case 'q':
  • {
  • zoom += 1.;
  • if(zoom > MY_MAX_ZOOM) zoom = MY_MAX_ZOOM;
  • return 0;
  • }
  • case 'w':
  • {
  • CreateNewList();
  • return 0;
  • }
  • }
  • break;
  • }
  • case WM_LBUTTONDOWN:
  • {
  • xMouseStart = LOWORD(lParam);
  • yMouseStart = HIWORD(lParam);
  • // on sauvegarde les angles
  • _theta = theta;
  • _phi = phi;
  • isMouseDown = TRUE;
  • SetCapture(hwnd);
  • return 0;
  • }
  • case WM_LBUTTONUP:
  • {
  • if(isMouseDown)
  • {
  • ReleaseCapture();
  • isMouseDown = FALSE;
  • }
  • return 0;
  • }
  • case WM_MOUSEMOVE:
  • {
  • if(isMouseDown)
  • {
  • int x,y;
  • x = (int) (__int16) LOWORD(lParam);
  • y = (int) (__int16) HIWORD(lParam);
  • // attention, comme les coordonnees en Y
  • // client sont inversees (de haut en bas)
  • // il faut faire -, et non +
  • // comme pout le X
  • theta = _theta + ((xMouseStart - x)*2*PI)/(double)cxClient;
  • phi = _phi - ((yMouseStart - y)*2*PI)/(double)cyClient;
  • }
  • return 0;
  • }
  • case WM_PAINT:
  • {
  • RePaint(DC,zoom,theta,phi);
  • return 0;
  • }
  • case WM_DESTROY:
  • {
  • glDeleteLists(MY_CUBE_LINE,1);
  • wglMakeCurrent(NULL, NULL);
  • wglDeleteContext(RC);
  • ReleaseDC(hwnd,DC);
  • PostQuitMessage(0);
  • break;
  • }
  • }
  • return DefWindowProc(hwnd,iMsg,wParam,lParam);
  • }
  • //------------------------------------------------------------
  • int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
  • {
  • WNDCLASS wc;
  • MSG msg;
  • HWND hWnd;
  • srand((unsigned)GetTickCount());
  • wc.style = CS_OWNDC;
  • wc.lpfnWndProc = WindowProc;
  • wc.cbClsExtra = 0;
  • wc.cbWndExtra = 0;
  • wc.hInstance = hInstance;
  • wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  • wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  • wc.hbrBackground = NULL;
  • wc.lpszMenuName = NULL;
  • wc.lpszClassName = "jcd";
  • RegisterClass(&wc);
  • hWnd = CreateWindow("jcd","openGl : Une Galaxie ... (JCD)",
  • WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  • CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  • NULL,NULL,hInstance,NULL
  • );
  • while(GetMessage(&msg,NULL,0,0))
  • {
  • TranslateMessage(&msg);
  • DispatchMessage(&msg);
  • }
  • return 0;
  • }
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

#include <math.h>

//------------------------------------------------------------
#define PI							3.1415926535
//------------------------------------------------------------
#define MY_MIN_ZOOM			1
#define MY_MAX_ZOOM			100

#define NBPOINT					5000

#define MY_CUBE_LINE		99
#define MY_PLANET_1			100
#define MY_PLANET_2			101
#define MY_PLANET_3			102
//------------------------------------------------------------
double CompiloRandomDouble(double a,double b)
{
return a + ((b-a)*(double)rand())/((double)RAND_MAX);
}
//------------------------------------------------------------
void RePaint(HDC DC,double zoom,double theta,double phi)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);	
glLoadIdentity();


// position de l'oeil
gluLookAt(zoom*cos(theta)*cos(phi),zoom*sin(theta)*cos(phi),zoom*sin(phi),0,0,0,cos(theta-PI)*cos(PI/2.-phi),sin(theta-PI)*cos(PI/2.-phi),sin(PI/2.-phi));

// on trace les lignes du cube
glCallList(MY_CUBE_LINE);
glCallList(MY_PLANET_1);
glCallList(MY_PLANET_2);
glCallList(MY_PLANET_3);

glPopMatrix();

SwapBuffers(DC);
}
//------------------------------------------------------------
void InitPixelFormat(HDC hdc)
{	
PIXELFORMATDESCRIPTOR pfd = {sizeof (PIXELFORMATDESCRIPTOR),1,PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,16,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0};
SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd);
}
//------------------------------------------------------------	
void CreatePlanet(int ID,double r,double g,double b)
{
int i;

glNewList(ID,GL_COMPILE);
	glColor3d(r,g,b);
	glBegin(GL_POINTS);
		for(i=0;i<NBPOINT;i++)
			{
			double x,y,z;
			x = CompiloRandomDouble(-10.,+10.);
			y = CompiloRandomDouble(-10.,+10.);
			z = CompiloRandomDouble(-10.,+10.);
			glVertex3d(x,y,z);
			}
	glEnd();
glEndList();
}
//------------------------------------------------------------	
void CreateNewList(void)
{

glNewList(MY_CUBE_LINE,GL_COMPILE);
	glColor3d(1.,1.,1.);


	glBegin(GL_LINE_LOOP);
		glVertex3d(-10.,-10.,-10.);
		glVertex3d(-10.,-10.,+10.);
		glVertex3d(-10.,+10.,+10.);
		glVertex3d(-10.,+10.,-10.);
	glEnd();

	glBegin(GL_LINE_LOOP);
		glVertex3d(+10.,-10.,-10.);
		glVertex3d(+10.,-10.,+10.);
		glVertex3d(+10.,+10.,+10.);
		glVertex3d(+10.,+10.,-10.);
	glEnd();

	glBegin(GL_LINES);
		glVertex3d(+10.,-10.,-10.);
		glVertex3d(-10.,-10.,-10.);
		glVertex3d(+10.,-10.,+10.);
		glVertex3d(-10.,-10.,+10.);
		glVertex3d(+10.,+10.,+10.);
		glVertex3d(-10.,+10.,+10.);
		glVertex3d(+10.,+10.,-10.);
		glVertex3d(-10.,+10.,-10.);
	glEnd();

glEndList();

CreatePlanet(MY_PLANET_1,1.,0.,0.);
CreatePlanet(MY_PLANET_2,0.,1.,0.);
CreatePlanet(MY_PLANET_3,0.,0.,1.);
}
//------------------------------------------------------------	
LRESULT CALLBACK WindowProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
static HDC						DC;
static HGLRC					RC;
static int						cxClient,cyClient;
static double					zoom,theta,phi,_theta,_phi;
static BOOL						isMouseDown;
static int						xMouseStart,yMouseStart;

// traitement des messages
switch (iMsg)
	{
	case WM_CREATE:
		{
		DC	=	GetDC(hwnd);
		InitPixelFormat(DC);
		RC	= wglCreateContext(DC);
		wglMakeCurrent(DC,RC);
		glEnable(GL_DEPTH_TEST); 
		glClearColor(0,0,0,0); 

		zoom	= 50.;
		theta = 0.;
		phi		= 0.;

		CreateNewList();
		break;
		}
  case WM_SIZE:
		{
		cxClient = LOWORD(lParam);	cxClient = (cxClient ? cxClient : 1);
		cyClient = HIWORD(lParam);	cyClient = (cyClient ? cyClient : 1);
    glViewport(0,0,cxClient,cyClient);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.,(float)(cxClient)/(float)(cyClient),0.1,1000);
   	break;
		}
	case WM_CHAR:
		{
		switch((char)wParam)
			{
			case 'a':
				{
				zoom -= 1.;
				if(zoom < MY_MIN_ZOOM)	zoom = MY_MIN_ZOOM;
				return 0;
				}
			case 'q':
				{
				zoom += 1.;
				if(zoom > MY_MAX_ZOOM)	zoom = MY_MAX_ZOOM;
				return 0;
				}
			case 'w':
				{
				CreateNewList();
				return 0;
				}
			}
		break;
		}
	case WM_LBUTTONDOWN:
		{
		xMouseStart = LOWORD(lParam);
		yMouseStart = HIWORD(lParam);
		// on sauvegarde les angles
		_theta	= theta;
		_phi		= phi;
		isMouseDown = TRUE;
		SetCapture(hwnd);
		return 0;
		}
	case WM_LBUTTONUP:
		{
		if(isMouseDown)
			{
			ReleaseCapture();
			isMouseDown = FALSE;
			}
		return 0;
		}
	case WM_MOUSEMOVE:
		{
		if(isMouseDown)
			{
			int x,y;
			x = (int) (__int16) LOWORD(lParam);
			y = (int) (__int16) HIWORD(lParam);

			// attention, comme les coordonnees en Y
			// client sont inversees (de haut en bas)
			// il faut faire -, et non +
			// comme pout le X
			theta = _theta + ((xMouseStart - x)*2*PI)/(double)cxClient;
			phi		= _phi	 - ((yMouseStart - y)*2*PI)/(double)cyClient;
			}
		return 0;
		}
  case WM_PAINT:
		{
		RePaint(DC,zoom,theta,phi);
		return 0;
		}
	case WM_DESTROY:
		{
		glDeleteLists(MY_CUBE_LINE,1);

    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(RC);
    ReleaseDC(hwnd,DC);
		PostQuitMessage(0);
		break;
		}
	}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}	
//------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
WNDCLASS	wc;
MSG				msg;
HWND			hWnd;

srand((unsigned)GetTickCount());

wc.style					= CS_OWNDC;
wc.lpfnWndProc		= WindowProc;
wc.cbClsExtra			= 0;
wc.cbWndExtra			= 0;
wc.hInstance			= hInstance;
wc.hIcon					= LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor				= LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground	= NULL;
wc.lpszMenuName		= NULL;
wc.lpszClassName	= "jcd";

RegisterClass(&wc);

hWnd = CreateWindow("jcd","openGl : Une Galaxie ... (JCD)",
										WS_OVERLAPPEDWINDOW | WS_VISIBLE,
										CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
										NULL,NULL,hInstance,NULL
										);
	
while(GetMessage(&msg,NULL,0,0))
	{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
	}
	
return 0;
}


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture COLORATION SYNTAXIQUE
Source avec Zip Source avec une capture ORBITES DES SATELLITES GPS
Source avec Zip Source avec une capture DESSIN D'ARBRES
Source avec Zip Source avec une capture PROGRAMMATION LINEAIRE
Source avec Zip EXTENSION DE CORPS (MATH)

 Sources de la même categorie

Source avec Zip Source avec une capture AFFICHER DES COURBES DE BEZIER par shorzy
Source avec Zip Source avec une capture BASE/MOTEUR 3D EN QT/OPENGL (COMPLET ET FONCTIONNEL!) POUR U... par envi33
Source avec Zip Source avec une capture CLASSE AVEC OPENGL - OBJETS 3D ET ANIMATIONS par rasta63
Source avec Zip Source avec une capture LETTRES 3D AVEC OPENGL ET QT par opossum_farceur
Source avec Zip CUBE 3D GLUT32 VC++ ET DEVC++ par bobby03

Commentaires et avis

Commentaire de djl le 08/12/2003 21:07:47

ca c pas mal comme effet, fallait y penser!
en tout cas ta sources d'avant sur sinusoide 3d m'a donner idée;) (ouai ca m'arrive)

Commentaire de vecchio56 le 08/12/2003 22:04:13 administrateur CS

moi je pensais que quand on regardait une galaxie on voyait des étoiles avant de voir des planètes...

Commentaire de Viper31 le 09/12/2003 00:02:52

rooooh faut pas jouer sur les mot ... c un galaxie , au sens moins astronomique du terme ---&gt; un gros tas diforme de truc qui brille :)

Commentaire de vecchio56 le 13/12/2003 18:53:02 administrateur CS

tu essaies d'être drôle?

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,733 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales