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
| void Init ()
{
// Le contexte de rendu est valide, on peut initialiser OpenGL
// Initialisation des valeurs d'effacement pour les tampons de couleur et de profondeur
glClearDepth (1.0f);
glClearColor (0.0f, 0.5f, 0.5f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Activation de la lecture et de l'écriture dans le tampon de profondeur
glEnable (GL_DEPTH_TEST);
glDepthMask (GL_TRUE);
glEnable (GL_COLOR_MATERIAL);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
GLfloat LampeDiffuse [] = {1.0f, 1.0f, 1.0f};
GLfloat LampeAmbient [] = {0.75f, 0.75f, 0.75f};
GLfloat LampePosition [] = {6.0f, 10.0f, 5.0f, 1.0f};
glLightfv (GL_LIGHT0, GL_DIFFUSE, LampeDiffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, LampeDiffuse);
glLightfv (GL_LIGHT0, GL_AMBIENT, LampeAmbient);
glLightfv (GL_LIGHT0, GL_POSITION, LampePosition);
// Initialisation du viewport
glViewport (0, 0, 800, 600);
// Initialisation de la matrice modelview à l'identité
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
// Mise en place d'une projection perspective
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60.0, 800.0 / 600.0, 0.1, 1000.0);
}
void Draw ()
{
Vector3 m_taille (10.0f, 10.0f, 10.0f);
glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glColor3f (1.0f, 0.0f, 0.0f);
glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
glColor3f (1.0f, 1.0f, 1.0f);
glColorMaterial (GL_FRONT_AND_BACK, GL_EMISSION);
glColor3f (0.0f, 0.0f, 0.0f);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
glBegin(GL_QUADS);
// Devant
glNormal3f (0.0f, 0.0f, 1.0f);
glVertex3f (-m_taille.x, -m_taille.y, m_taille.z);
glVertex3f (m_taille.x, -m_taille.y, m_taille.z);
glVertex3f (m_taille.x, m_taille.y, m_taille.z);
glVertex3f (-m_taille.x, m_taille.y, m_taille.z);
// Derrière
glNormal3f (0.0f, 0.0f, -1.0f);
glVertex3f (-m_taille.x, -m_taille.y, -m_taille.z);
glVertex3f (-m_taille.x, m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, -m_taille.y, -m_taille.z);
// Haut
glNormal3f (0.0f, 1.0f, 0.0f);
glVertex3f (-m_taille.x, m_taille.y, -m_taille.z);
glVertex3f (-m_taille.x, m_taille.y, m_taille.z);
glVertex3f (m_taille.x, m_taille.y, m_taille.z);
glVertex3f (m_taille.x, m_taille.y, -m_taille.z);
// Bas
glNormal3f (0.0f, -1.0f, 0.0f);
glVertex3f (-m_taille.x, -m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, -m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, -m_taille.y, m_taille.z);
glVertex3f (-m_taille.x, -m_taille.y, m_taille.z);
// Droite
glNormal3f (1.0f, 0.0f, 0.0f);
glVertex3f (m_taille.x, -m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, m_taille.y, -m_taille.z);
glVertex3f (m_taille.x, m_taille.y, m_taille.z);
glVertex3f (m_taille.x, -m_taille.y, m_taille.z);
// Gauche
glNormal3f (-1.0f, 0.0f, 0.0f);
glVertex3f (-m_taille.x, -m_taille.y, -m_taille.z);
glVertex3f (-m_taille.x, -m_taille.y, m_taille.z);
glVertex3f (-m_taille.x, m_taille.y, m_taille.z);
glVertex3f (-m_taille.x, m_taille.y, -m_taille.z);
glEnd ();
}
int main()
{
// On commence par créer une fenêtre et un contexte valide pour OpenGL
sfRenderWindow App (sfVideoMode (800, 600), "Frustum Culling grâce à des sphères",
false);
// Boucle principale
bool running = true;
while (running)
{
// On commence à dessiner notre monde
App.BeginOpenGL();
Init ();
gluLookAt (0.0f, 0.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
Draw ();
// End OpenGL rendering
App.EndOpenGL();
sfString Text("This is a rotating cube");
Text.SetLeft(230.f);
Text.SetTop(300.f);
Text.SetColor(sfColor(255, 0, 0));
App.Draw(Text);
App.Display();
sfEvent appEvent;
while (App.GetEvent (appEvent))
{
// Fermeture de la fenêtre
if (appEvent.Type == sfEvent::Close)
{
running = false;
}
// Appuie de la touche Echap
if ((appEvent.Type == sfEvent::KeyPressed) && (appEvent.Key.Code == sfKey::Escape))
{
running = false;
}
}
}
return EXIT_SUCCESS;
} |
Partager