Reflection d'obj (opengl)

Description

Voila ceci est un effet de relfection d'objet sur une surface.

j'avait chercher sur le net des cours et des sources sur comment realiser cette effet et j'ai trouver que tous les sources presente utilise le meme principe qui est un peut trop long et moin satisfesant.
Alors j'ai pensé de creé mon propre algo sans utiliser le stencil buffer ni le glcolormask et d'esseyer de reduire le code.
ben j'ai constater que mon algo personel est plus rapide et c plus JOLI.

Le zip conteint deux exe

Reflecton General.exe : utilise le stencil buffer et glcolormask ansi que le blending (la methode que tous le monde utilise)

Reflect.exe = utilise que le blending (c celui que j'ai realiser et qui est livré avec ces sources )

je pense que mon algo et mieux que l'algo courant , si je me trompe sur ce point veuillez vous me le dire en m'expliquant pourquoi

Merci d'avance
j'espere que j'ai pas commi bcp de fote ;D
----------------------
Merci a platon179
-----------------------

Source / Exemple :


Finalement voila deux sources des deux effets :
constater vous meme :D 

Reflection : Methode Courant 
---------------------------------------

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);// Clears the screen.
   glLoadIdentity();                   //Reset modelview matrix for new frame.

   // Here we just set the camera's position.
   gluLookAt(0.0f, 1.5f, 2.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);

   // Since our reflections uses the stencil buffer we must enable it.  Just like if we
   // are enabling depth testing.
   glEnable(GL_STENCIL_TEST);

   // Next we must disable depth testing and turn off color modifications on all colors.
   glDisable(GL_DEPTH_TEST);
   glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

   // Next two functions will set up the stencil buffer and stencil test.
   glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
   glStencilFunc(GL_ALWAYS, 1, 1);  // Test all.

   // Now we will draw everything that can reflect the surroundings.  This will not
   // actually draw the ground just the "area" that makes up it.
   DrawGround();

   // Now we enable depth testing back and enable the color mask.
   glEnable(GL_DEPTH_TEST);
   glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);   

   // Next we will set the stencil test to pixels equal to 1.  What this means is that
   // every pixel on the object that reflects if it has a 1 set to it then we draw on it.
   // Above we set the stencil test to always 1 so when we rendered the ground every pixel
   // of the ground was given the ability to be drawn on.  Now we draw everything that we
   // want reflection onto the "ground stencil area".
   glStencilFunc(GL_EQUAL, 1, 1);
   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

   // Draw the reflection.
   glPushMatrix();

      // We scale to invert the object onto the ground.
      glScalef(1.0, -1.0, 1.0);

      // Draw the reflected cube.
      DrawCube();

   glPopMatrix();

   // Now we are done.  We can disable the stencil buffer so what we draw from here on out
   // is not drawn on the ground "area".
   glDisable(GL_STENCIL_TEST);

   // Now we can draw the ground.  We will blend the ground with the stencil "area"and give
   // it alpha so we can see the reflection.
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   // Set the alpha to 50%.
   glColor4f(1.0f, 1.0f, 1.0f, 0.5f);

   // Draw the ground.
   DrawGround();

   // Stop the blending.
   glDisable(GL_BLEND);

   // Well we are done.  Now we draw the cube normally.
   DrawCube();

   // Increase the objects rotation.
   Rotation += 0.5;

   // Reset it if the rotation goes over 360.
   if(Rotation > 360.0f)
      Rotation = 0.0f;

 
   glFlush();
-------------------------------------------------------

Reflection propre algo
---------
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
    glLoadIdentity();								

//Place la camera

gluLookAt(0.0f, 2.8f, -4.0f, 0.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f);

glPushMatrix();

    // Inversé l'obj sur le sol
    glScalef(1.0, -1.0, 1.0);

    // Dessine le cube reflecté
    DrawCube();

glPopMatrix();
   
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   glColor4f(1.0f, 1.0f, 1.0f, 0.7f);

   // Dessine le sol
   DrawGround();

   glDisable(GL_BLEND);
  //Le Cube Normal

	DrawCube();

// Rotation
   rot += 0.5;

   if(rot > 360.0f)
      rot = 0.0f;

   glFlush();
	return TRUE;

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.