begin process at 2012 05 27 15:58:09
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

OpenGL

 > EFFECT TRANSPARENT ( TOUJOURS AVEC CAMERA ET SPIN )

EFFECT TRANSPARENT ( TOUJOURS AVEC CAMERA ET SPIN )


 Information sur la source

Note :
Aucune note
Catégorie :OpenGL Niveau :Débutant Date de création :04/05/2003 Date de mise à jour :04/05/2003 06:14:00 Vu :2 993

Auteur : BenjaminRare

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

Ce programme dessine 2 cubes l'un dans l'autre celui de l'exterieur (le plus "gros" ) est donner un effect transparent pour que l'on puisse voir le petit cube a l'interieur .. j'ai garde le camera movement and ajouter un touche 'Spin' qui accelere le taux de spin des 2 cubes ... enjoy, @+

Source

  • #include <windows.h>
  • #include <GL\glut.h>
  • #include <stdio.h>
  • // variable pour distance de camera
  • static float Distance = -30.0f;
  • // variable pour le spin du cube
  • static float Spin = 0.0f;
  • typedef unsigned char uchar;
  • void RenderScene ( void )
  • {
  • // angle de spin pour le cube
  • static float fElect1 = 0.0f;
  • // clear l'ecran
  • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  • // Retourner a (0,0,0);
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • // Controle la distance de camera
  • glTranslatef(0.0f, 0.0f, Distance);
  • // Controle le spin des cubes
  • glRotatef(Spin, 1.0f, 0.0f, 0.0f);
  • // "Enregistre" le current-matrix
  • glPushMatrix();
  • // spin le cube sur l'axe x
  • glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
  • //spin le cube sur l'axe y
  • glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
  • // Dessine le petit cube
  • glBegin (GL_QUADS);
  • // Front Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.0f, 2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.0f, -2.0f, 2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.0f, -2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-2.0f, 2.0f, 2.0f);
  • // Back Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.0f, 2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-2.0f, 2.0f, -2.0f);
  • // Right Side
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.0f, 2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.0f, 2.0f, -2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(2.0f, -2.0f, 2.0f);
  • // Left Side
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-2.0f, 2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(-2.0f, 2.0f, -2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-2.0f, -2.0f, 2.0f);
  • // Top Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-2.0f, 2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.0f, 2.0f, -2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(2.0f, 2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-2.0f, 2.0f, 2.0f);
  • // Bottom Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.0f, -2.0f, -2.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(2.0f, -2.0f, 2.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-2.0f, -2.0f, 2.0f);
  • glEnd();
  • glPopMatrix();
  • // Ce qui donne l'effet transparent
  • glEnable(GL_BLEND);
  • // Meme rotation que le cube precedent
  • glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
  • glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
  • // Dessine le grand cube
  • glBegin (GL_QUADS);
  • // Front Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(5.0f, 5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(5.0f, -5.0f, 5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-5.0f, -5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-5.0f, 5.0f, 5.0f);
  • // Back Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(5.0f, 5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-5.0f, 5.0f, -5.0f);
  • // Right Side
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(5.0f, 5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(5.0f, 5.0f, -5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(5.0f, -5.0f, 5.0f);
  • // Left Side
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-5.0f, 5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(-5.0f, 5.0f, -5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-5.0f, -5.0f, 5.0f);
  • // Top Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-5.0f, 5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(5.0f, 5.0f, -5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(5.0f, 5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-5.0f, 5.0f, 5.0f);
  • // Bottom Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(5.0f, -5.0f, -5.0f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(5.0f, -5.0f, 5.0f);
  • glColor3f(0.0f, 1.0f, 1.0f);
  • glVertex3f(-5.0f, -5.0f, 5.0f);
  • glEnd();
  • // Rapport 1:1
  • glBlendFunc(GL_ONE, GL_ONE);
  • // Disable !! apres avoir dessiner tout object
  • glDisable(GL_BLEND);
  • // augment angle de rotation
  • fElect1 += 1.0f;
  • // swap les buffers
  • glutSwapBuffers();
  • // "update" notre scene
  • glutPostRedisplay();
  • }
  • void SetupRC ( void )
  • {
  • // Couleur noir pour le font d'ecran
  • glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  • // enable le depth test
  • glEnable(GL_DEPTH_TEST);
  • }
  • void ReSize ( GLsizei w, GLsizei h )
  • {
  • // rapport fovy
  • GLfloat fAspect;
  • // au cas ou la window est resizer a une hauteur < 0
  • if ( h == 0 )
  • h = 1;
  • // Viewport
  • glViewport(0, 0, w, h);
  • // rapport fove calculer
  • fAspect = (GLfloat)w / (GLfloat)h;
  • glMatrixMode(GL_PROJECTION);
  • // La vue que l'on a choisi
  • gluPerspective(60.0f, fAspect, 1.0, 400.0);
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • }
  • void OnKey ( uchar Key, int X, int Y)
  • {
  • switch (Key)
  • {
  • // au cas ou on tape 'i' ou 'I' zoom In
  • case 'i': case 'I':
  • Distance += 1.0f;
  • break;
  • case'o':case'O': // au cas ou on tape 'o' ou 'O' zoom Out
  • Distance -= 1.0f;
  • break;
  • case 's':case'S':// si on tape 's' ou 'S' augment le taux de spin
  • Spin += 5.0f;
  • if ( Spin > 360.0f)
  • {
  • Spin = 0.0f;
  • }
  • break;
  • }
  • }
  • void main ( void )
  • {
  • // notre display mode
  • glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
  • // créer notre fenêtre
  • glutCreateWindow("Transparency");
  • // Dessine note Scene
  • glutDisplayFunc(RenderScene);
  • // notre Resize function
  • glutReshapeFunc(ReSize);
  • // prends en charge l'input du user
  • glutKeyboardFunc(OnKey);
  • // font d'ecran
  • SetupRC();
  • // loop
  • glutMainLoop();
  • }
#include <windows.h>
#include <GL\glut.h>
#include <stdio.h>

// variable pour distance de camera
static float Distance = -30.0f;

// variable pour le spin du cube
static float Spin = 0.0f;

typedef unsigned char uchar;

void RenderScene ( void )
{

	// angle de spin pour le cube
	static float fElect1 = 0.0f;

	// clear l'ecran
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// Retourner a (0,0,0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Controle la distance de camera
	glTranslatef(0.0f, 0.0f, Distance);

	// Controle le spin des cubes
	glRotatef(Spin, 1.0f, 0.0f, 0.0f);

	// "Enregistre" le current-matrix
	glPushMatrix();

	// spin le cube sur l'axe x
	glRotatef(fElect1, 1.0f, 0.0f, 0.0f);

	//spin le cube sur l'axe y
	glRotatef(fElect1, 0.0f, 1.0f, 0.0f);


	// Dessine le petit cube 
	glBegin (GL_QUADS);

		// Front Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(2.0f, 2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(2.0f, -2.0f, 2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-2.0f, 2.0f, 2.0f);

		// Back Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(2.0f, 2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-2.0f, 2.0f, -2.0f);

		// Right Side
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(2.0f, 2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(2.0f, 2.0f, -2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(2.0f, -2.0f, 2.0f);

		// Left Side
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-2.0f, 2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(-2.0f, 2.0f, -2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, 2.0f);

		// Top Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-2.0f, 2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(2.0f, 2.0f, -2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(2.0f, 2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-2.0f, 2.0f, 2.0f);

		// Bottom Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(2.0f, -2.0f, -2.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(2.0f, -2.0f, 2.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, 2.0f);

		glEnd();

		
		glPopMatrix();

		// Ce qui donne l'effet transparent
		glEnable(GL_BLEND);

		// Meme rotation que le cube precedent
		glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
		glRotatef(fElect1, 0.0f, 1.0f, 0.0f);

		// Dessine le grand cube
		glBegin (GL_QUADS);

		// Front Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(5.0f, 5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(5.0f, -5.0f, 5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-5.0f, -5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-5.0f, 5.0f, 5.0f);

		// Back Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(5.0f, 5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-5.0f, 5.0f, -5.0f);

		// Right Side
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(5.0f, 5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(5.0f, 5.0f, -5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(5.0f, -5.0f, 5.0f);

		// Left Side
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-5.0f, 5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(-5.0f, 5.0f, -5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-5.0f, -5.0f, 5.0f);

		// Top Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-5.0f, 5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(5.0f, 5.0f, -5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(5.0f, 5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-5.0f, 5.0f, 5.0f);

		// Bottom Face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(5.0f, -5.0f, -5.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(5.0f, -5.0f, 5.0f);

		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(-5.0f, -5.0f, 5.0f);

		glEnd();

		// Rapport 1:1
		glBlendFunc(GL_ONE, GL_ONE);

		// Disable !! apres avoir dessiner tout object
		glDisable(GL_BLEND);

		// augment angle de rotation
		fElect1 += 1.0f;

		// swap les buffers
		glutSwapBuffers();

		// "update" notre scene
		glutPostRedisplay();

}


void SetupRC ( void )
{
	// Couleur noir pour le font d'ecran
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	// enable le depth test
	glEnable(GL_DEPTH_TEST);
}

void ReSize ( GLsizei w, GLsizei h )
{
	// rapport fovy
	GLfloat fAspect;

	// au cas ou la window est resizer a une hauteur < 0
	if ( h == 0 )
		h = 1;

	// Viewport
	glViewport(0, 0, w, h);

	// rapport fove calculer
	fAspect = (GLfloat)w / (GLfloat)h;


	glMatrixMode(GL_PROJECTION);

	// La vue que l'on a choisi
	gluPerspective(60.0f, fAspect, 1.0, 400.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

void OnKey ( uchar Key, int X, int Y)
{
	switch (Key)
	{
		// au cas ou on tape 'i' ou 'I' zoom In
	case 'i': case 'I':
		Distance += 1.0f;
		break;

	case'o':case'O': // au cas ou on tape 'o' ou 'O' zoom Out
		Distance -= 1.0f;
		break;

	case 's':case'S':// si on tape 's' ou 'S' augment le taux de spin
		Spin += 5.0f;

		if ( Spin > 360.0f)
		{
			Spin = 0.0f;
		}
		break;
	}
}

void main ( void )
{
	// notre display mode
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);

	// créer notre fenêtre
	glutCreateWindow("Transparency");

	// Dessine note Scene
	glutDisplayFunc(RenderScene);

	// notre Resize function
	glutReshapeFunc(ReSize);

	// prends en charge l'input du user
	glutKeyboardFunc(OnKey);

	// font d'ecran
	SetupRC();

	// loop
	glutMainLoop();
}



 Sources du même auteur

CUBE TOURNANT AVEC CAMERA MOVEMENT ET ECLAIRAGE
SYSTEM SOLAIRE AVEC LIGHTING
PASSWORD MASKING
EQUATION QUADRATIQUE

 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

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

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,608 sec (3)

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