Effet radar en opengl

Description

C'est un effet de radar tout bete en vert avec degradé regardez la capture
Le systeme pour le rendu du radar est tout bete, il est expliqué dans le lisezmoi dans le zip.
+ pour augmenter la taille du radar
- pour le diminuer
esc pour quitter

Source / Exemple :


/*-----------------------------------------------------------------------------------------------*
//
//                                          RadarGL
//Fait par         : MaegisInstinct
//
//le               : 25/06/2003 à 09:37:25
//
//Description      : Un super radar en opengl
//
/*----------------------------------------------------------------------------------------------*/

//Lire le lisez moi pour une description de la methode d'affichage du radar

//Inclure les libs  opengl32.lib  et glu32.lib 

#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>

#define PI			3.14159
#define INCSTEP		0.0007853

//Vars globales
int X,Y,midx,midy;

//Declarations de fonction
void Draw(HDC,double);
void Reshape(int,int);
void SetupPixelFormat(HDC);
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);

int APIENTRY WINAPI WinMain(HINSTANCE hInstance,  
							HINSTANCE hPrevInstance,   
							LPSTR lpCmdLine,   
							int nCmdShow)
{
	MSG msg;
	WNDCLASS wc;
	HWND hWnd;

	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	wc.hCursor = NULL;
	wc.hIcon = NULL;
	wc.hInstance = hInstance;
	wc.lpfnWndProc = &WinProc;
	wc.lpszClassName = "Radar";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClass(&wc); 

	//on cree la fenetre
	hWnd = CreateWindow("Radar",
                        "Radar GL",
                         WS_POPUP,
                         0,0,
                         GetSystemMetrics(SM_CXSCREEN),
                         GetSystemMetrics(SM_CYSCREEN),
                         NULL,
					     NULL,
                         hInstance,
                         NULL);

	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);
	ShowCursor(FALSE);		//on cache le curseur

	 while (GetMessage(&msg,NULL,0,0))
	 {
		 TranslateMessage(&msg);
		 DispatchMessage(&msg);
		 InvalidateRect(hWnd,NULL,FALSE); /*Je fait un redraw tout le temps car le calcul est long
										  à faire ca doit etre la faute des cos et des sin et
										  des nombres a virgules*/
	 }

	return msg.wParam;
}

//----------------------------------------------------
//Met l'affichage en 32bits et specifie le backbuffer
//----------------------------------------------------
void SetupPixelFormat(HDC hDC)
{
	int pixelFormat;
    PIXELFORMATDESCRIPTOR pfd = {
        sizeof(PIXELFORMATDESCRIPTOR),
		1,
        PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0, 0,	
        0, 0, 0, 0, 0,
        32,
        0,
        0,
        0,
        0,
        0,
        0,
        0
	};

    pixelFormat = ChoosePixelFormat(hDC, &pfd);
    if (pixelFormat == 0)
	{
        MessageBox(NULL,"Mode graphique non supporté","Erreur",MB_OK);
        exit(1);
    }

    if (SetPixelFormat(hDC,pixelFormat,&pfd) != TRUE)
	{
		MessageBox(NULL,"Mode graphique non supporté","Erreur",MB_OK);
        exit(1);
    }
}

//-------------------------------
//Fonction de gesin des messages
//-------------------------------
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	HDC				hdc;
	PAINTSTRUCT		ps;
	static HGLRC	hrc;
	static double	radarstep = PI/4/255; //le pas entre chaque ligne

	switch(msg)
	{
	case WM_CREATE:
		hdc=GetDC(hWnd);
		SetupPixelFormat(hdc); //on set le mode d'affichage
		hrc = wglCreateContext(hdc);		//On cree un rendering context
		if (!hrc)
			DestroyWindow(hWnd);
		wglMakeCurrent(hdc,hrc);		//on l'associe au DC
		break;
	case WM_DESTROY:
		wglMakeCurrent(NULL,NULL);		//on libere le RC
		if (hrc)
			wglDeleteContext(hrc);		//on le delete
		PostQuitMessage(0);
		break;
	case WM_SIZE:
		Reshape(LOWORD(lParam),HIWORD(lParam));		//on met a jour l'affichage
		X = GetSystemMetrics(SM_CXSCREEN);			//on les coordonnees
		Y = GetSystemMetrics(SM_CYSCREEN);
		midx = X/2;
		midy = Y/2;
		break;
	case WM_KEYDOWN :
		switch(wParam)
		{
		case VK_ESCAPE :
			DestroyWindow(hWnd);
			break;
		case VK_ADD :
			if(radarstep < 10.0*INCSTEP)
				radarstep += INCSTEP;		//augmente le step et donc la taille du radar
			break;
		case VK_SUBTRACT :
			if(radarstep > INCSTEP)
				radarstep -= INCSTEP;		//dec le step
			break;
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd,&ps);
		Draw(hdc,radarstep);  //on Draw la scene
		EndPaint(hWnd,&ps);
		break;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}

//-------------------
//Fonction de dessin
//-------------------
void Draw(HDC hdc,double radarstep)
{
	static double				angle = 0;
	static unsigned char		taille = 0;		//le nombre de lignes calcules (pour l'effet de depart)
	static POINT				lignes[255];	//les fameuses lignes
	unsigned char				i;				//controle
	static unsigned char		indexmax = 0;

	angle = angle + radarstep;		//on increment l'angle
	if (angle >= 2*PI)
		angle = 0;

	if (taille == 255)
	{
		for(i=0;i<=indexmax;i++)		//si toute les lignes sont deja calcules, on les decale
			lignes[i] = lignes[i+1];   //d'un cran 
	}

	lignes[indexmax].x = (int)(cos(angle)*((double)midx))+midx; //et on calcule la derniere ligne
	lignes[indexmax].y = (int)(sin(angle)*((double)midy))+midy;
		

	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		//on efface tout
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

		glBegin(GL_TRIANGLES);
		for(i=0;i<indexmax;i++)		//on dessine les 255 lignes
		{
			glColor3ub(0,i,0);		//la couleur en fonction de la position dans le tableau
			glVertex2i(midx,midy);
			glVertex2i(lignes[i].x,lignes[i].y);
			glVertex2i(lignes[i+1].x,lignes[i+1].y);
		}
		glEnd();
		SwapBuffers(hdc);

		if (taille != 255)
		{
			taille++;				//on incremente la taille
			if (indexmax == 254)		//si taille = 255 indexmax serai a 255 et il n'y aurait
				indexmax = taille-1;	//pas le calcul de la 255 ligne et par le decalage
			else						//une ligne non calculee serai affichée
				indexmax =taille;
		}
}

void Reshape(int width,int height)
{
	glViewport(0,0,width,height);	//on defini la taille
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,width,height,0);	//on defini les coords de la matrice de projection
}

Conclusion :


Y'a un petit probleme au niveau de la vitesse d'affichage il n'y a pas de timer pour gere le redraw, il redraw des qu'il peut et pourtant c'est pas tres rapide
ca vient des cos des sin ou bien ?

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.