Animation [dev-c++]

Soyez le premier à donner votre avis sur cette source.

Snippet vu 7 672 fois - Téléchargée 36 fois

Contenu du snippet

IL FAUT AVOIR GLAUX.DLL ET NE PAS OUBLIER DE METTRE DES FICHIERS BMP...
By SoftPower

http://www.softpower.t2u.com

Source / Exemple :


////////////////////////////////////////////////////////////////////////////////////////
// MAIN.CPP
///////////////////////////////////////////////////////////////////////////////////////

#include "main.h"

///////////////////////////////////////////////////////////////////////////////
//                            SoftPower Productions                         //
/////////////////////////////////////////////////////////////////////////////

void RePaint ()
{     
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();

	gluLookAt(x,0,z,x,0,z+10,0,1,0);
    glRotated(rot,0,1,0);
    
    if (zbuff) glEnable (GL_DEPTH_TEST);
		else glDisable (GL_DEPTH_TEST);

    if (tex) glEnable(GL_TEXTURE_2D);
		else glDisable (GL_TEXTURE_2D);
  
   
    glTranslatef(0, 0, 0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-15, 1, -15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(-15, -1, -15);

		glTexCoord2f(12.0f, 0.0f);	glVertex3f(15, -1, -15);

		glTexCoord2f(12.0f, 1.0f);	glVertex3f(15, 1, -15);
    glEnd();
    
    glTranslatef(0, 0, 0);
	glBindTexture(GL_TEXTURE_2D, texture[0]); 
	// Display a 4 long quads to represent walls
	glBegin(GL_QUADS);

	////////////////////// BACK WALL ///////////////////////

		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-15, 1, -15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(-15, -1, -15);

		glTexCoord2f(12.0f, 0.0f);	glVertex3f(15, -1, -15);

		glTexCoord2f(12.0f, 1.0f);	glVertex3f(15, 1, -15);

	////////////////////// FRONT WALL ///////////////////////

		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-15, 1, 15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(-15, -1, 15);

		glTexCoord2f(12.0f, 0.0f);	glVertex3f(15, -1, 15);

		glTexCoord2f(12.0f, 1.0f);	glVertex3f(15, 1, 15);

	////////////////////// LEFT WALL ///////////////////////

		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-15, 1, -15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(-15, -1, -15);

		glTexCoord2f(12.0f, 0.0f);	glVertex3f(-15, -1, 15);

		glTexCoord2f(12.0f, 1.0f);	glVertex3f(-15, 1, 15);

	////////////////////// RIGHT WALL ///////////////////////

		glTexCoord2f(0.0f, 1.0f);	glVertex3f(15, 1, -15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(15, -1, -15);

		glTexCoord2f(12.0f, 0.0f);	glVertex3f(15, -1, 15);

		glTexCoord2f(12.0f, 1.0f);	glVertex3f(15, 1, 15);

	// Stop drawing the walls
	glEnd();

	
	glTranslatef(0, 0, 0);	
	// Bind this texture to the floor quad
	glBindTexture(GL_TEXTURE_2D, texture[2]);

	// Display a huge quad to represent a floor
	glBegin(GL_QUADS);

		glTexCoord2f(0.0f, 16.0f);	glVertex3f(-15, -1, -15);

		glTexCoord2f(0.0f, 0.0f);	glVertex3f(-15, -1, 15);

		glTexCoord2f(16.0f, 0.0f);	glVertex3f(15, -1, 15);

		glTexCoord2f(16.0f, 16.0f);	glVertex3f(15, -1, -15);

	// Stop drawing the floor
	glEnd();
 

    
if (a <= -5) ecranComplet = 0; 
if (a >= 5) ecranComplet = 1; 
if (!ecranComplet) a += 0.1; 
if (ecranComplet) a -= 0.1; 
glTranslated (0,0,a); 
glScaled (.5,.5,.5);  

         glBindTexture(GL_TEXTURE_2D, texture[8]);
       glBegin(GL_QUADS);
        glTexCoord2i(1,0);glVertex3i (-1,-1,1);
		glTexCoord2i(1,1);glVertex3i (-1,1,1);
		glTexCoord2i(0,1);glVertex3i (1,1,1);
		glTexCoord2i(0,0);glVertex3i (1,-1,1);

    glEnd();
 
    glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER , 0.9f); 

	glTranslatef(-1-t, 0, -5.75-g);
	glBindTexture(GL_TEXTURE_2D, texture[5]);
  
	glBegin (GL_QUADS);
		glTexCoord2i(1,0);glVertex3i (-1,-1,1);
		glTexCoord2i(1,1);glVertex3i (-1,1,1);
		glTexCoord2i(0,1);glVertex3i (1,1,1);
		glTexCoord2i(0,0);glVertex3i (1,-1,1);
	glEnd();
 	glDisable(GL_ALPHA_TEST);   
  	SwapBuffers (DC);
}

void InitPixelFormat (HDC hDC)
{
      PIXELFORMATDESCRIPTOR pfd =
      {
           sizeof (PIXELFORMATDESCRIPTOR),
           1,
           PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,
           16,
           0, 0, 0, 0, 0, 0, 0, 0,	0, 0, 0, 0, 0,
           16,
           0, 0, 0, 0, 0, 0, 0
      };

      SetPixelFormat (hDC, ChoosePixelFormat (hDC, &pfd), &pfd);
}

LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

   	switch (uMsg)
	{
	case WM_CREATE:
		DC=GetDC (hwnd);
        InitPixelFormat (DC);
        RC = wglCreateContext (DC);
        wglMakeCurrent (DC, RC);
        glEnable (GL_DEPTH_TEST); 
        glFogfv(GL_FOG_COLOR, fogcolor);
        
        glClearColor (0,0,1,0);
		glFogfv(GL_FOG_COLOR, fogcolor);
		glFogi(GL_FOG_MODE, GL_EXP);
		glHint(GL_FOG_HINT, GL_NICEST);
		glFogf(GL_FOG_DENSITY, 0.3f);
		glFogf(GL_FOG_START, 1.0);
		glFogf(GL_FOG_END, 5.0);
        glEnable(GL_TEXTURE_2D);
		//On charge les textures
        LoadTexture("data/mur.bmp", &texture[0]);
		LoadTexture("data/floor1.bmp", &texture[2]); 
		LoadTexture("data/envwall.bmp", &texture[3]); 
		LoadTexture("data/perso.bmp", &texture[5]); 
        LoadTexture("data/plafond.bmp", &texture[6]); 
        LoadTexture("data/crate.bmp", &texture[7]);
        LoadTexture("data/porte.bmp", &texture[8]);
		
		break;
	case WM_CLOSE:
        wglMakeCurrent (NULL, NULL);
        wglDeleteContext (RC);
        ReleaseDC (hwnd,DC);
		PostQuitMessage (0);
		break;
    case WM_SIZE:
    	glViewport (0,0,LOWORD (lParam),HIWORD (lParam));
    	glMatrixMode (GL_PROJECTION);
    	glLoadIdentity ();
    	gluPerspective (45,(float)(LOWORD(lParam))/(float)(HIWORD(lParam)),1,100);
    	break;
    case WM_PAINT:
    	RePaint ();
    	break;
	default:
		return DefWindowProc (hwnd,uMsg,wParam,lParam);
		break;
  case WM_KEYDOWN:
		switch (wParam) {
		case VK_DOWN:
			z--;
			g++;
            break;
		case VK_UP:
			z++;
			g--;
            break;
		case VK_RIGHT:
			x--;
			t++;
            break;
		case VK_LEFT:
			x++;
			t--;
            break;
		case 'B':
			fog=!fog;
			break;
   		case 'A':
            LoadTexture("data/perso2.bmp", &texture[5]);
         	break;
   		/*case 'A':
			rot-- -rot--;
			break;
 		case 'Z':
			rot++ +rot++;
			break;*/
        }
        

    }
	return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
      
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "OGL";
	
	RegisterClass(&wc);

	hWnd = CreateWindow
	("OGL", "l HADJ SALAH Adrien l SoftPower Productions",
	WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE | WS_MAXIMIZEBOX,
	0, 0, 640, 480, NULL, NULL, hInstance, NULL
	);

	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN.h
/////////////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

#define	LIFE 100	

WNDCLASS wc;
MSG msg;
HWND hWnd;
HDC	DC;
HGLRC RC;

double a;
int ecranComplet;

bool fullscreen=TRUE;	
unsigned texture[10];
double x=0, y, n ,z=-5,filt,tex=1,zbuff=1, fog, rot, hb=0, aa=0, gd=0, g, t;
// x pour les cotes et z pour l' avant/arrière le reste effet
//test de y pour modifier l' angle
// complement de 3D
float fogcolor[4] = {-1,-1,-1,-1};
// couleur lorsque "b" est enclanché

GLUquadricObj *Quadric1 = gluNewQuadric();

void LoadTexture(char *Filename, unsigned *TextureName) 
{

    AUX_RGBImageRec *Texture;
	Texture = auxDIBImageLoad(Filename);
	unsigned char *ImageData = Texture->data;
	glGenTextures(2, TextureName);
	glBindTexture(GL_TEXTURE_2D, *TextureName);
    
	unsigned char *RgbaData = new unsigned char[Texture->sizeX * Texture->sizeY * 4];
	
	int i;
	for (i=0; i< (Texture->sizeX * Texture->sizeY); i++)
	{
		RgbaData[i*4] = ImageData[i*3];
		RgbaData[i*4+1] = ImageData[i*3+1];
		RgbaData[i*4+2] = ImageData[i*3+2];
		if (RgbaData[i*4] == 255 && RgbaData[i*4+1] == 255 && RgbaData[i*4+2] == 255) //Si c'est du blanc
			RgbaData[i*4+3] = 0;	//alors la valeur alpha = 0 (transparent)
		else
			RgbaData[i*4+3] = 255;	//alors la valeur alpha = 255 (opaque)
	}

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, Texture->sizeX, Texture->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, RgbaData);
	delete[] RgbaData;
}

Conclusion :


Voila 2 fichiers cpp & un h

http://www.softpower.t2u.com

A voir également

Ajouter un commentaire

Commentaires

Messages postés
23
Date d'inscription
lundi 15 avril 2002
Statut
Membre
Dernière intervention
8 décembre 2008

lol, oui je vais le mettre mais je suis un peu fénéant... en ce moment.
Messages postés
53
Date d'inscription
samedi 24 novembre 2001
Statut
Membre
Dernière intervention
25 janvier 2003

Pourquoi tu mets pas pas tout simplement un zip?

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.