begin process at 2012 05 27 15:20:33
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

OpenGL

 > CUBE TOURNANT AVEC CAMERA MOVEMENT ET ECLAIRAGE

CUBE TOURNANT AVEC CAMERA MOVEMENT ET ECLAIRAGE


 Information sur la source

Note :
Aucune note
Catégorie :OpenGL Niveau :Débutant Date de création :03/05/2003 Date de mise à jour :03/05/2003 05:21:05 Vu :3 712

Auteur : BenjaminRare

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

 Description

Ce programme dessine un cube et utilise la fonction glRotate() pour le faire tourner, la fonction OnKey() maitrise le camera movement et un simple eclairage ....
PS: mon second prog =)

Source

  • #include <windows.h>
  • #include <gl\glut.h>
  • // variable for camera Distance
  • float Distance = -30.0f;
  • //float g_LightPosition[] = {0, 10, (Distance)+30};
  • float g_bLight = true;
  • typedef unsigned char uchar;
  • void RenderScene (void)
  • {
  • // angle of revolution
  • static float fElect1 = 0.0f;
  • //clears screen with current clearing color
  • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  • //Loads Identity
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • // variable for the z-axis in glTranslate
  • // controlled by the camera distance
  • glTranslatef(0.0f, 0.0f, Distance);
  • // Rotation controlled by variable (angle of revolution)
  • glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
  • glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
  • glBegin(GL_QUADS);
  • // Front face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, 2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, -2.5f, 2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.5f, -2.5f, 2.5f);
  • glColor3f(1.0f, 1.0f ,0.0f);
  • glVertex3f(-2.5, 2.5f ,2.5f);
  • // Back Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, -2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, -2.5f, -2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.5f, -2.5f, -2.5f);
  • glColor3f(1.0f, 1.0f ,0.0f);
  • glVertex3f(-2.5, 2.5f ,-2.5f);
  • // Right-Side Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, 2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, -2.5f, 2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(2.5f, -2.5, -2.5f);
  • glColor3f(1.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, -2.5f);
  • //Left Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(-2.5f, 2.5f, 2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(-2.5f, -2.5f, 2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.5f, -2.5, -2.5f);
  • glColor3f(1.0f, 1.0f, 0.0f);
  • glVertex3f(-2.5f, 2.5f, -2.5f);
  • // Top Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, 2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, 2.5f, -2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.5f, 2.5f, -2.5);
  • glColor3f(1.0f,1.0f, 0.0f);
  • glVertex3f(-2.5f, 2.5f, 2.5f);
  • //Bottom Face
  • glColor3f(1.0f, 0.0f, 0.0f);
  • glVertex3f(2.5f, -2.5f, 2.5f);
  • glColor3f(0.0f, 1.0f, 0.0f);
  • glVertex3f(2.5f, -2.5f, -2.5f);
  • glColor3f(0.0f, 0.0f, 1.0f);
  • glVertex3f(-2.5f, -2.5f, -2.5);
  • glColor3f(1.0f,1.0f, 0.0f);
  • glVertex3f(-2.5f, -2.5f, 2.5f);
  • glEnd();
  • // Rotate by 10 degrees
  • fElect1 += 0.5f;
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • // Swap the buffers
  • glutSwapBuffers();
  • // Update our scene
  • glutPostRedisplay();
  • }
  • void SetupRC( void )
  • {
  • // Clearing color
  • glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  • GLfloat ambient[] = {0.8f, 0.8f, 0.8f, 1.0f};
  • // Enable depth test
  • glEnable(GL_DEPTH_TEST);
  • glEnable( GL_LIGHTING );
  • glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambient);
  • glEnable(GL_COLOR_MATERIAL);
  • glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  • }
  • void ChangeSize (GLsizei w, GLsizei h)
  • {
  • // Aspect ratio variable
  • GLfloat fAspect = 0.0f;
  • // prevent a divide by zero
  • if ( h == 0 )
  • h = 1;
  • // Sets the viewport to window dimensions
  • glViewport(0, 0, w, h);
  • // calculates the aspect ration
  • fAspect = (GLfloat)w / (GLfloat)h;
  • glMatrixMode(GL_PROJECTION);
  • glLoadIdentity();
  • // Perspective with angle 70 degrees, aspect ration
  • // Near and Far clip
  • gluPerspective(70.0f, fAspect, 1.0f, 400.0f);
  • glMatrixMode(GL_MODELVIEW);
  • glLoadIdentity();
  • }
  • void OnKey( uchar Key, int X, int Y )
  • {
  • // Key input for switching zoom IN/OUT
  • switch( Key )
  • {
  • case 'i': case 'I':
  • Distance += 1.0f;
  • break;
  • case 'o': case 'O':
  • Distance -= 1.0f;
  • break;
  • }
  • }
  • void main (void)
  • {
  • glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
  • glutCreateWindow("Teapot");
  • SetupRC();
  • glutDisplayFunc(RenderScene);
  • glutReshapeFunc(ChangeSize);
  • glutKeyboardFunc(OnKey);
  • glutMainLoop();
  • }
#include <windows.h>
#include <gl\glut.h>

// variable for camera Distance
float Distance = -30.0f;


//float g_LightPosition[] = {0, 10, (Distance)+30};
float g_bLight = true;	

typedef unsigned char uchar;

void RenderScene (void)
{
	// angle of revolution
	static float fElect1 = 0.0f;

	//clears screen with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//Loads Identity
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	

	// variable for the z-axis in glTranslate 
	// controlled by the camera distance
	glTranslatef(0.0f, 0.0f, Distance);

	// Rotation controlled by variable (angle of revolution)
	glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
	glRotatef(fElect1, 0.0f, 1.0f, 0.0f);

	glBegin(GL_QUADS);

		// Front face
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(2.5f, 2.5f, 2.5f);

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

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

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

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

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

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

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

		// Right-Side Face

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

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

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

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

		//Left Face

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

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

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

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

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

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

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

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

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

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

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

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

	glEnd();

	// Rotate by 10 degrees
	fElect1 += 0.5f;


	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Swap the buffers
	glutSwapBuffers();

	// Update our scene
	glutPostRedisplay();

	
}

void SetupRC( void )
{
	// Clearing color
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	GLfloat ambient[] = {0.8f, 0.8f, 0.8f, 1.0f};

	// Enable depth test
	glEnable(GL_DEPTH_TEST);
	glEnable(  GL_LIGHTING );

	glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambient);	


	glEnable(GL_COLOR_MATERIAL);

	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

}

void ChangeSize (GLsizei w, GLsizei h)
{
	// Aspect ratio variable
	GLfloat fAspect = 0.0f;

	// prevent a divide by zero
	if ( h == 0 )
		h = 1;

	// Sets the viewport to window dimensions
	glViewport(0, 0, w, h);

	// calculates the aspect ration
	fAspect = (GLfloat)w / (GLfloat)h;

	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// Perspective with angle 70 degrees, aspect ration
	// Near and Far clip
	gluPerspective(70.0f, fAspect, 1.0f, 400.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void OnKey( uchar Key, int X, int Y )
{
	// Key input for switching zoom IN/OUT
	switch( Key )
	{
		case 'i': case 'I':
		Distance += 1.0f;
		break;

		case 'o': case 'O':
		Distance -= 1.0f;
		break;
	}
}


void main (void)
{
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
	glutCreateWindow("Teapot");

	SetupRC();

	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);
	glutKeyboardFunc(OnKey);

	glutMainLoop();

}



 Sources du même auteur

EFFECT TRANSPARENT ( TOUJOURS AVEC CAMERA ET SPIN )
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

Commentaire de trinitacs le 04/05/2003 11:33:14

Je ne comprend pas pourquoi tu dis que tu parle de caméra. Tu fais une simple rotation pour le cube.

Après avoir dessiner tous tes rectangles ton prog créer une matrice identité. C'est inutil puisqu'il n'y a plus rien a dessiner!

Tu devrais créer une class boxe. Tu pourras faire une scène avec pleins de cube en peu de code. Si ça t'interresse je te passe la mienne.

Et enfin je te conseil d'utiliser glfw au lieu de glut car cette dernière n'est plus supporté depuis 1997. Glfw permet de créer des programmes très portables: http://hem.passagen.se/opengl/glfw/
De plus elle est simple d'utilisation et offre une bonne documentation simple d'accès.

PS: bonne chance pour ton troisième prog ;)

Commentaire de slashbeastleo93 le 17/05/2003 10:08:52

j'ai essayé ton programme, mais le compilateur dit qu'il y a une erreur dans "#include &lt;glglut.h&gt;", je suis allez dans le site http://hem.passagen.se/opengl/glfw/ mais ca existe meme pô !!!

Commentaire de BenjaminRare le 19/05/2003 11:11:59

essaye plutot #include &lt;glglut.h&gt; et si ca marche pas download le file glut.h et met le dans Include-&gt;GL .... n'oublie pas de faire ton LINK avec les librairies glu32.lib glut32.lib et opengl32.lib

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

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

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