Création du message
10-09-2010 14:38:56
nico
attends tout à l'heure je te montre où j'en suis..... j'ai toujours pas fini mais j'ai beaucoup gagné en performances....
edit: voilà il y a des fichiers où c'est encore le bordel notamment win.h c'est le bronx lol.
main.cpp scene.h scene.cpp noeud.h camera.h lumiere.h forme.h sphere.h rendu.h rendu.cpp math.h dispositif.h win.h animation.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "dispositif.h"//device pour les intimes ;)
#include "rendu.cpp"//raytracer
#include "scene.cpp"
#include "animation.h"
int main()
{ //______________________________________MOTEUR
Animation *animation;
C_Ecran* ecran = new C_Ecran(800,600);
C_Rendu* rendu = new C_Rendu(800,600);
C_Scene* scene = new C_Scene();
//__________________________________________________________________________SCENE
C_Camera* macamera = scene->ajouterCamera(Vecteur(0.0, 1.0, -10000.0));
macamera->cibler(Vecteur(0.0, 0.0, 1.0));
C_Lumiere* soleil = scene->ajouterLumiere(Vecteur( 0.0, 240.0, -1000.0));
soleil->colorer(1.0, 1.0, 1.0);
C_Lumiere* lumiere = scene->ajouterLumiere(Vecteur(640.0, 240.0 ,-100.0));
lumiere->colorer(0.3, 0.3, 0.3);
I_Forme* lune=new C_Sphere(Vecteur( 50.0, 290.0, 0.0));
lune->redimensionner(40);
lune->definirMateriau(0);
scene->ajouterForme(lune);
I_Forme* planeteTerre=new C_Sphere(Vecteur( 350.0, 290.0, 0.0));
planeteTerre->redimensionner(100);
planeteTerre->definirMateriau(1);
scene->ajouterForme(planeteTerre);
//__________________________________________________________________BOUCLE PRINCIPALE
while (!quitter())
{
lune->positioner(animation->orbite(planeteTerre->Position()));
ecran->diffuser(rendu->lancerDeRayons(scene->parametres()));
}
//____________________AU REVOIR
delete lune;
delete planeteTerre;
delete scene;
delete rendu;
delete ecran;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#ifndef Scene_loaded
#include "lumiere.h"
#include "camera.h"
#include "sphere.h"
struct S_Scene//***********************************************************
{//structure contenant les paramètres de la scene à envoyer au raytracer
Materiau listeMateriaux[3];
I_Forme* listeFormes[3];
C_Lumiere listeLumieres[3];
C_Camera* cameraActuel;
int nbformes, nblumieres;
S_Scene(): nbformes(0), nblumieres(0){}
};//***********************************************************************
class C_Scene
{
private://** ***************************
int nbcameras, idCameraActuel;
C_Camera listeCameras[3];
S_Scene* parametresScene;
public://***** *********************************************
C_Scene();
C_Camera* ajouterCamera(vecteur3f position);
void ajouterForme(I_Forme* forme);
C_Lumiere* ajouterLumiere(vecteur3f position);
S_Scene& parametres()const {return *parametresScene;}
~C_Scene() {delete parametresScene;}
};
#define Scene_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "scene.h"
//______________________________________________________________________________________INIT
C_Scene::C_Scene():nbcameras(0),idCameraActuel(0)
{
parametresScene=new S_Scene;
parametresScene->listeMateriaux[0].rouge=0.0;
parametresScene->listeMateriaux[0].vert=1.0;
parametresScene->listeMateriaux[0].bleu=1.0;
parametresScene->listeMateriaux[0].reflection=0.0f;
parametresScene-> listeMateriaux[1].rouge=1.0;
parametresScene->listeMateriaux[1].vert=0.0;
parametresScene->listeMateriaux[1].bleu=1.0;
parametresScene-> listeMateriaux[1].reflection=1.0;
}//_____________________________________________________________________________________AJOUTER_CAMERA
C_Camera* C_Scene::ajouterCamera(vecteur3f position)
{ nbcameras++;
listeCameras[idCameraActuel].positioner(position);
parametresScene->cameraActuel=&listeCameras[idCameraActuel];
return &listeCameras[idCameraActuel];
}//_____________________________________________________________________________________AJOUTER_FORME
void C_Scene::ajouterForme(I_Forme* forme)
{
parametresScene->listeFormes[++parametresScene->nbformes-1]=forme;
}//_____________________________________________________________________________________AJOUTER_LUMIÈRE
C_Lumiere* C_Scene::ajouterLumiere(vecteur3f position)
{
parametresScene->listeLumieres[++parametresScene->nblumieres-1].positioner(position);
return ¶metresScene->listeLumieres[parametresScene->nblumieres-1];
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef Noeud_loaded
#include "math.h"
class I_Noeud //Interface pour manipuler cameras, lumieres et formes dans la scene
{
protected:
vecteur3f position;
public:
virtual void positioner(const vecteur3f& Position){this->position = Position;}
virtual vecteur3f Position()const{return position;}
};
#define Noeud_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef Camera_loaded
#include "noeud.h"
class C_Camera : public I_Noeud
{
private:
vecteur3f cible;
public:
void cibler(const vecteur3f Cible){this->cible = Cible;}
vecteur3f direction()const{return cible;}
};
#define Camera_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef Lumiere_loaded
#include "noeud.h"
class C_Lumiere : public I_Noeud
{
private:
float rouge, vert, bleu;
public:
void colorer(const float Rouge,const float Vert,const float Bleu)
{
this->rouge=Rouge;
this->vert=Vert;
this->bleu=Bleu;
}
float Rouge()const {return rouge;}
float Vert()const {return vert; }
float Bleu()const {return bleu; }
};
#define Lumiere_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#ifndef Sphere_loaded
#include "forme.h"
class C_Sphere : public I_Forme
{
public:
C_Sphere(vecteur3f Position)
{
this->position=Position;
}
bool collision(const Rayon &rayon, float &profondeur)
{
// intersection rayon/sphere
vecteur3f distance = this->position - rayon.depart;
float B = rayon.direction * distance;
float delta = B*B - distance * distance + this->taille * this->taille;
if (delta < 0.0f)return false;
float profondeur0 = B - sqrtf(delta);
bool colision = false;
if ((profondeur0 > 0.1f) && (profondeur0 < profondeur))
{
profondeur = profondeur0;
colision = true;
}
float profondeur1 = B + sqrtf(delta);
if ((profondeur1 > 0.1f) && (profondeur1 < profondeur))
{
profondeur = profondeur1;
colision = true;
}
return colision;
}
~C_Sphere()
{
}
};
#define Sphere_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifndef Rendu_loaded
#include <iostream>
#include "math.h"
#include "scene.h"
#include "time.h"
class C_Rendu
{
private:
PerfChrono chrono;
unsigned char* pixelsRVB;
int largeur, hauteur, niveau;
float rouge, vert, bleu, coef, profondeur;
public:
C_Rendu(int Largeur, int Hauteur):pixelsRVB((unsigned char *) malloc( Largeur * Hauteur * 3 )),
largeur(Largeur),
hauteur(Hauteur)
{
}
unsigned char* lancerDeRayons(const S_Scene& scene);
~C_Rendu(){delete pixelsRVB;}
};
#define Rendu_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include "rendu.h"
unsigned char* C_Rendu::lancerDeRayons(const S_Scene& scene)
{
std::cout<<"Frequence de l'horloge : "<<chrono.GetFreq()<<" Hz."<<std::endl;
chrono.Start();
int z=-1;
vecteur3f positionCamera =scene.cameraActuel->Position();
vecteur3f directionCamera=scene.cameraActuel->direction();
for (int y = 0; y <hauteur; ++y)//boucle de parcours des lignes
{
for (int x = 0; x < largeur; ++x)//boucle de parcours des pixels
{
rouge = 0, vert = 0, bleu = 0, coef = 1.0f, niveau = 0, profondeur = 20000.0f;
//on initialise le rayon en fonction de la position de la camera et de sa direction
Rayon rayonActuel = { positionCamera + Vecteur( float(x), float(y), 0.f ) , directionCamera};
do//boucle de reflexion
{
//Recherche de formes se trouvant dans le champ du rayon
int indexForme= -1;
for (int i = 0; i < scene.nbformes; ++i)
if (scene.listeFormes[i]->collision(rayonActuel, profondeur))//en cas d'intersection on definit sa distance dans "profondeur"
indexForme = i;
if (indexForme == -1) break;
//en cas d'intersection avec un objet, on va defininir un nouveau rayon partant de cet objet
vecteur3f nouveauDepart = rayonActuel.depart + rayonActuel.direction * profondeur;
//on calcule la normale au point d'intersection
vecteur3f normale = nouveauDepart.calculerNormale(scene.listeFormes[indexForme]->Position());
if( normale == 0)break;
//on recupere le materiau
Materiau materiauActuel = scene.listeMateriaux[scene.listeFormes[indexForme]->Materiau()];
//calcul de la valeur d'éclairement au point
for (int j = 0; j < scene.nblumieres; ++j)
{
C_Lumiere lumiereActuel = scene.listeLumieres[j];
vecteur3f distance = lumiereActuel.Position() - nouveauDepart;
if (normale * distance <= 0.0f)
continue;
float profondeur = sqrtf(distance * distance);
if ( profondeur <= 0.0f )
continue;
Rayon rayonDeLumiere;
rayonDeLumiere.depart = nouveauDepart;
rayonDeLumiere.direction = distance * (1/profondeur) ;
//calcul des ombres
bool ombre = false;
for (int i = 0; i < scene.nblumieres; ++i)
{
if (scene.listeFormes[i]->collision(rayonDeLumiere, profondeur))
{
ombre = true;
break;
}
}
if (!ombre)
{
// lambert
float lambert = (rayonDeLumiere.direction * normale) * coef;
rouge += lambert * lumiereActuel.Rouge() * materiauActuel.rouge;
vert += lambert * lumiereActuel.Vert() * materiauActuel.vert;
bleu += lambert * lumiereActuel.Bleu() * materiauActuel.bleu;
}
}
//on itére sur la prochaine reflexion
coef *= materiauActuel.reflection;
float reflet = 2.0f * (rayonActuel.direction * normale);
rayonActuel.depart = nouveauDepart;
rayonActuel.direction = rayonActuel.direction - normale * reflet;
niveau++;
}
while ((coef > 0.0f) && (niveau < 10));
pixelsRVB[z++]=(unsigned char)std::min(rouge*255.0f, 255.0f);
pixelsRVB[z++]=(unsigned char)std::min(vert *255.0f, 255.0f);
pixelsRVB[z++]=(unsigned char)std::min(bleu *255.0f, 255.0f);
}
}
std::cout<<(chrono.GetDiffNs()/1000000)<<std::endl;
return pixelsRVB;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#ifndef Math_loaded
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
template<typename T> struct vecteur3//**************************************************************************VECTEUR3
{
T X, Y, Z;
vecteur3<T> operator + (const vecteur3<T>& v2)const {return (vecteur3<T>){X +v2.X, Y +v2.Y, Z +v2.Z};}
vecteur3<T> operator - (const vecteur3<T>& v2)const {return (vecteur3<T>){X -v2.X, Y -v2.Y, Z -v2.Z};}
vecteur3<T> operator * (const float& c) const {return (vecteur3<T>){X *c, Y *c, Z *c };}
float operator * ( vecteur3<T>& v2) const {return X *v2.X +Y *v2.Y +Z *v2.Z ;}
bool operator ==(const int& nb) const {return (X==nb &&Y==nb &&Z==nb ) ;}
vecteur3<T> calculerNormale( vecteur3<T> direction)
{
vecteur3<T> normale= *this - direction;
float temp = normale * normale;
if (temp == 0.0f)return (vecteur3<T>){0,0,0};
return normale * (1.0f / sqrtf(temp)) ;
}
};//*************************************************************************************************************
typedef vecteur3<float> vecteur3f;
vecteur3f Vecteur(float x, float y, float z){return (vecteur3f){x,y,z};}
struct Rayon{//**************************************************************************************************RAYON
vecteur3f depart, direction;
};//*************************************************************************************************************
struct Materiau {//**********************************************************************************************MATERIAU
float rouge, vert, bleu, reflection;
};
#define Math_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifndef Dispositif_loaded
#include <gl/gl.h>
#if (defined WIN32 || defined WIN64)
#include "win.h"
#else
#include "linux.h"
#endif
class C_Ecran
{
protected:
unsigned int largeur, hauteur;
public:
C_Ecran(unsigned int Largeur,unsigned int Hauteur): largeur(Largeur),
hauteur(Hauteur)
{
}
void diffuser(unsigned char* cellulesRVB)const
{
glDrawPixels( largeur, hauteur, GL_RGB, GL_UNSIGNED_BYTE, cellulesRVB);
swap();
}
};
#define Dispositif_loaded
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#include <windows.h>
#define main Moteur
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);
using namespace std;
MSG msg;
HDC hDC;
int Moteur();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
HWND hwnd;
HGLRC hRC;
/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GLSample";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
if (!RegisterClassEx(&wcex))
return 0;
/* create main window */
hwnd = CreateWindowEx(0,
"GLSample",
"Raytracer",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);
/* fake main*/
Moteur();
/* shutdown OpenGL */
DisableOpenGL(hwnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow(hwnd);
return msg.wParam;
}
bool quitter()
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
return true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return false;
}
void swap()
{
SwapBuffers(hDC);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC(hwnd);
/* set the pixel format for the DC */
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat(*hDC, &pfd);
SetPixelFormat(*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext(*hDC);
wglMakeCurrent(*hDC, *hRC);
}
void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hwnd, hDC);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#ifndef Animation_loaded
class Animation
{
public:
Animation();
vecteur3f orbite(vecteur3f centre)const
{
static float alpha=0;
alpha+=0.01;
if(alpha>360)alpha=0;
float distance = 200.0f;
return Vecteur(distance * cos(alpha)+centre.X, centre.Y, distance * sin(alpha)+centre.Z);
}
};
#define Animation_loaded
#endif
Si vous ne pouvez pas activer javacsript, clique ici