Effect transparent ( toujours avec camera et spin )

Soyez le premier à donner votre avis sur cette source.

Snippet vu 4 186 fois - Téléchargée 35 fois

Contenu du snippet

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 / Exemple :


#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();
}

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.