1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include "opengl.h"
void InitPixelFormat (HDC hDC) //Définir le DC
{
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
}; //Parramétrer le DC
SetPixelFormat (hDC, ChoosePixelFormat (hDC, &pfd), &pfd);//Définit le DC
};
void RePaint () //Permet de rafraichir l'écran
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Effacer
glMatrixMode (GL_MODELVIEW); //Matrice de visualisation
glLoadIdentity ();//Possibilité de modifier la matrice
GamePaint ();
SwapBuffers (DC); //Met en mémoire et l'écran et charge le précédent
};
LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE: //Création de la fenetre
DC = GetDC (hwnd); //Nouveau DC
InitPixelFormat (DC); //Initialiser le DC
RC = wglCreateContext (DC); //Définir le Rendering Context
Initialize ();
wglMakeCurrent (DC, RC);//Libérer RC et DC
break;
case WM_CLOSE: //Fermeture de la fenetre
wglMakeCurrent (NULL, NULL); //Annule la sortie courrante
wglDeleteContext (RC); //Effacer de Rendering Context
ReleaseDC (hwnd,DC); //Libérer le DC
PostQuitMessage (0); //Quitter l'appli
break;
case WM_SIZE:
glViewport (0,0,LOWORD (lParam),HIWORD (lParam)); //taille de la zone d'affichage
glMatrixMode (GL_PROJECTION); //Créer un matrice de 3D
glLoadIdentity ();//Permet de modifier la matrice
gluPerspective (45,(float)(LOWORD(lParam))/(float)(HIWORD(lParam)),1,100);//Effet de perspective a 45°
break;
case WM_PAINT:
RePaint ();//Repeindre
break;
default:
return DefWindowProc (hwnd,uMsg,wParam,lParam);//Traitement pardef des messages
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", "Fenetre OpenGL",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
350, 150, 640, 480, NULL, NULL, hInstance, NULL
);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
} |
Partager