Debut d'un moteur3d open gl (utilisant glut)

Description

Soyer indulgent je debute ! :-)

Compiler avec Visual C++ 6

Source / Exemple :


//******************************************
// Moteur 3d Opengl par Dufour Christophe **
//******************************************

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>

#define	PI	3.1415926535898

//***************************
// Prototype des fonctions  *
//***************************

void Init();
void Display();
void Reshape(int w, int h);
void Clavier(unsigned char key, int x, int y);

int LoadBMP(char *File);

double a=0,b=-5,angle=0;	// Pour la mise au point camera (mouvement)

GLuint		Mur,Sol;

//***************************
// Programme Principal      *
//***************************

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Moteur 3D");
	Init();
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Clavier);
 	glutMainLoop();
	return 0;
}

//***************************
// Definition des fonctions *
//***************************

////////////////////////////////////////////////////////
//LoadBMP : charge une image 24bpp
#define EXIT {fclose(fichier);return -1;}
#define CTOI(C) (*(int*)&C)	//récupère en int un nombre pointé par un char*

int LoadBMP(char *File)
{
	unsigned char	*Data;
	FILE			*fichier;
	unsigned char	Header[0x36];
	GLuint			DataPos,DataSize;
	GLint			Components;
	GLsizei			Width,Height;
	GLenum			Format,Type;
	GLuint			Name[1];
	
//Lit le fichier et son header
	fichier = fopen(File,"rb");if (!fichier) return -1;
	if (fread(Header,1,0x36,fichier)!=0x36) EXIT;
	if (Header[0]!='B' || Header[1]!='M')	EXIT;
	if (CTOI(Header[0x1E])!=0)				EXIT;
	if (CTOI(Header[0x1C])!=24)				EXIT;

//Récupère les infos du fichier
	DataPos			= CTOI(Header[0x0A]);
	DataSize		= CTOI(Header[0x22]);
//Récupère les infos de l'image
	Width			= CTOI(Header[0x12]);
	Height			= CTOI(Header[0x16]);	
	Type = GL_UNSIGNED_BYTE;
	Format = GL_RGB;
	Components = 3;
	
	//!!!!
	if (DataSize==0) DataSize=Width*Height*Components;
	if (DataPos==0)  DataPos=0x36;

//Charge l'image
	fseek(fichier,DataPos,0);
	Data = new unsigned char[DataSize];
	if (!Data) EXIT;

	if (fread(Data,1,DataSize,fichier)!=DataSize) 
	{
		delete Data;
		fclose(fichier);
		return -1;
	}
	
	fclose(fichier);

//Inverse R et B
	unsigned char t;
	for (int x=0;x<Width*Height;x++) 
	{
		t=Data[x*3];
		Data[x*3]=Data[x*3+2];
		Data[x*3+2]=t;
	}

//Envoie la texture à OpenGL
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	glGenTextures(1, Name);
	glBindTexture(GL_TEXTURE_2D, Name[0]);
	
	
	glTexImage2D
	( 	
		GL_TEXTURE_2D, 	//target
		0,				//mipmap level
		Components,		//nb couleurs
		Width,			//largeur
		Height,			//hauteur
		0,			 	//largeur du bord
		Format,			//type des couleurs
		Type,			//codage de chaque composante
		Data			//Image
	); 
	
	return Name[0];
}

///////////////////////////

void Init()
{
	glClearColor(0.0,0.0,0.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR);
	glEnable(GL_TEXTURE_2D);
} 

void Display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	gluLookAt(a, 6, b, a+(sin(angle)), 6, b+(cos(angle)), 0.0, 1.0, 0.0); // point de vue visé grace a l'angle

	
	Mur = LoadBMP("Texture.bmp");
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	
	/*glRotated(a,0,1,0);
	glRotated(b,1,0,0);	
	a+=2;
	b+=1.6;

  • /
glBegin(GL_QUADS); glColor3d(1,0,0);glVertex3i(1,5,1); glColor3d(0,1,0);glVertex3i(1,3,1); glColor3d(0,0,1);glVertex3i(-1,3,1); glColor3d(1,0,1);glVertex3i(-1,5,1); glColor3d(1,1,0);glVertex3i(1,5,-1); glColor3d(0,1,1);glVertex3i(1,3,-1); glColor3d(1,0,0);glVertex3i(-1,3,-1); glColor3d(1,1,1);glVertex3i(-1,5,-1); glColor3d(1,0,0);glVertex3i(1,5,1); glColor3d(0,1,0);glVertex3i(1,3,1); glColor3d(0,1,1);glVertex3i(1,3,-1); glColor3d(1,1,0);glVertex3i(1,5,-1); glColor3d(1,0,1);glVertex3i(-1,5,1); glColor3d(0,0,1);glVertex3i(-1,3,1); glColor3d(1,0,0);glVertex3i(-1,3,-1); glColor3d(1,1,1);glVertex3i(-1,5,-1); glColor3d(1,1,1);glVertex3i(-1,5,-1); glColor3d(1,0,1);glVertex3i(-1,5,1); glColor3d(1,0,0);glVertex3i(1,5,1); glColor3d(1,1,0);glVertex3i(1,5,-1); glColor3d(1,0,0);glVertex3i(-1,3,-1); glColor3d(0,0,1);glVertex3i(-1,3,1); glColor3d(0,1,0);glVertex3i(1,3,1); glColor3d(0,1,1);glVertex3i(1,3,-1); glEnd(); // Dessin de la scene /*glBegin(GL_QUADS); // 1ere face glColor3d(1,0,0); glVertex3i(0,2,0); glVertex3i(1,2,0); glVertex3i(1,3,0); glVertex3i(0,3,0); //2eme face glColor3d(0,1,0); glVertex3i(0,3,0); glVertex3i(0,3,1); glVertex3i(1,3,1); glVertex3i(1,3,0); //3eme face glColor3d(0,0,1); glVertex3i(1,3,0); glVertex3i(1,3,1); glVertex3i(1,2,1); glVertex3i(1,2,0); //4eme face glColor3d(1,0,1); glVertex3i(0,2,0); glVertex3i(0,2,1); glVertex3i(1,2,1); glVertex3i(1,2,0); //5eme face glColor3d(1,1,0); glVertex3i(0,2,1); glVertex3i(0,3,1); glVertex3i(1,3,1); glVertex3i(1,2,1); //6eme face glColor3d(0,1,1); glVertex3i(0,2,0); glVertex3i(0,3,0); glVertex3i(0,3,1); glVertex3i(0,2,1);
  • /
glBegin(GL_QUADS); // Les 4 murs glColor3d(0.5,0.5,0.5); glTexCoord2i(0,0);glVertex3i(-30,0,-30); glTexCoord2i(5,0);glVertex3i(-30,0,30); glTexCoord2i(5,5);glVertex3i(-30,30,30); glTexCoord2i(0,5);glVertex3i(-30,30,-30); glTexCoord2i(0,0);glVertex3i(-30,0,30); glTexCoord2i(5,0);glVertex3i(30,0,30); glTexCoord2i(5,5);glVertex3i(30,30,30); glTexCoord2i(0,5);glVertex3i(-30,30,30); glTexCoord2i(0,0);glVertex3i(30,0,30); glTexCoord2i(5,0);glVertex3i(30,0,-30); glTexCoord2i(5,5);glVertex3i(30,30,-30); glTexCoord2i(0,5);glVertex3i(30,30,30); glTexCoord2i(0,0);glVertex3i(-30,0,-30); glTexCoord2i(5,0);glVertex3i(30,0,-30); glTexCoord2i(5,5);glVertex3i(30,30,-30); glTexCoord2i(0,5);glVertex3i(-30,30,-30); glEnd(); Sol = LoadBMP("Sol.bmp"); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glBegin(GL_QUADS); // Le sol glColor3d(0.5,0.5,0.5); glTexCoord2i(0,0);glVertex3i(-50,0,-50); glTexCoord2i(5,0);glVertex3i(-50,0,50); glTexCoord2i(5,5);glVertex3i(50,0,50); glTexCoord2i(0,5);glVertex3i(50,0,-50); glEnd(); glutSwapBuffers(); glutPostRedisplay(); } void Reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Vide la matrice gluPerspective(45,float(w)/float(h),0.01,100); glMatrixMode(GL_MODELVIEW); // Pour revenir toujours a ModelView } void Clavier(unsigned char key, int x, int y) { // sin et cos marche pas en degré mais en radian // marche avant if (key==27) exit(0); // Touche echap if (key=='t') { a=a-0.3*cos((PI/2)+angle); // D'apres trigo : x=-1*cos a b=b+0.3*sin((PI/2)+angle); // D'apres trigo : y=1*sin a } // marche arriere if (key=='g') { a=a+0.3*cos((PI/2)+angle); // Inverse de marche avant b=b-0.3*sin((PI/2)+angle); } // tourner a gauche if (key=='f') { angle=angle+PI/60; // Angle permet de calculer sin et cos du point de visé } // tourner a droite if (key=='h') { angle=angle-PI/60; } }

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.