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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| #include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "resource.h"
#define MAIN 0
#define DLG_1 1
#define OGL 2
LRESULT CALLBACK MainWndProc(HWND hFrame,UINT Message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK OpenGlProc(HWND hOgl,UINT Message,WPARAM wParam,LPARAM lParam);
INT_PTR CALLBACK Dlg1Proc(HWND hChild,UINT Message,WPARAM wParam,LPARAM lParam);
HINSTANCE hInst;
HWND hwnd[3];
HDC hDC = NULL; // Private GDI Device Context
HGLRC hRC = NULL; // Permanent Rendering Context
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
hInst = hInstance;
WNDCLASSEX wc;
MSG msg;
wc.cbSize=sizeof(wc);
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.style=0;
wc.hInstance=hInst;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc=MainWndProc;
wc.lpszMenuName=NULL;
wc.lpszClassName="Main Frame";
wc.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE + 1);
if (!RegisterClassEx(&wc))
return 1;
hwnd[MAIN]= CreateWindowEx(
WS_EX_CLIENTEDGE,
"Main Frame",
"Main Window",
WS_OVERLAPPEDWINDOW,
100,
75,
540,
360,
NULL,
NULL,
hInst,
NULL);
if (!hwnd[MAIN])
return 1;
ShowWindow(hwnd[MAIN], nCmdShow);
while( GetMessage(&msg,NULL,0,0) != NULL )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hDC && hwnd[OGL])
ReleaseDC(hwnd[OGL],hDC);
return msg.wParam;
}
LRESULT CALLBACK MainWndProc(HWND hFrame,UINT Message,
WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
hwnd[DLG_1] = CreateDialog(hInst, MAKEINTRESOURCE(DIALOG_1),
hFrame, Dlg1Proc);
if (hwnd[DLG_1])
ShowWindow(hwnd[DLG_1], SW_SHOW);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hFrame, Message, wParam, lParam);
}
return DefWindowProc(hFrame, Message, wParam, lParam);
}
INT_PTR CALLBACK Dlg1Proc(HWND hChild,UINT Message,
WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
WNDCLASS WindowClass = {0, //Style
OpenGlProc, //WndProc
0, //octets supplémentaires à allouer à la classe
0, //octets supplémentaires à allouer à la fenêtre
hInst, //instance
0, //icone
0, //curseur
0, //couleur d'arrière-plan
NULL, //menu
"OpenGl" //Nom de la classe fenêtre
};
if (!RegisterClass(&WindowClass))
{
MessageBox(NULL, "Can't register OpenGl window", "exit(1)", NULL);
exit(1);
}
hwnd[OGL] = CreateWindow
("OpenGl",
"Fenêtre OpenGL",
WS_CHILD | WS_VISIBLE | WS_BORDER,
0, // X
0, // Y
400, // Width
300, // Height
hChild, // Mother
0, // Child id
hInst, // instance
NULL // WM_CREATE
);
if (!hwnd[OGL])
{
MessageBox(NULL, "OpenGl handle is NULL", "exit(1)", NULL);
exit(1);
}
ShowWindow(hwnd[OGL], SW_SHOW);
break;
}
return FALSE;
}
void SetupPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //taille du descripteur de format
1, //version
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
PFD_DOUBLEBUFFER, //Propriété
PFD_TYPE_RGBA, //Mode de couleurs
16, //Bits de couleur
0, 0, 0, 0, 0, 0, //Paramètres des couleurs
0,0, //Paramètres alpha
0,0, 0, 0, 0, //Paramètres du buffer d'accumulation
32, //Bits de profondeur
0, //Bits du buffer stencil
0, //Nombre de buffers auxiliaires
0, //ignoré (obsolète)
0, //réservé/code>
0, //ignoré (obsolète)
0, //Couleur de transparence
0 //Ignoré (obsolète)
};
int pixelFormat;
pixelFormat = ChoosePixelFormat(hDC, &pfd);
if (!pixelFormat)
{
MessageBox
(
WindowFromDC(hDC),
"Mode graphique non supporté",
"Problème",
MB_ICONERROR | MB_OK
);
exit(1); /*Vérifie si un PixelFormat du type demandé existe*/
}
if (!SetPixelFormat(hDC, pixelFormat, &pfd))
{
MessageBox
(
WindowFromDC(hDC),
"Mode graphique non supporté",
"Problème",
MB_ICONERROR | MB_OK
);
exit(1); /*Applique le PixelFormat. Arrête si erreur*/
}
}
void Reshape(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,float(width)/float(height),0.1,100);
}
void InitGL()
{
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Efface le frame buffer et le Z-buffer
glMatrixMode(GL_MODELVIEW); //Choisit la matrice MODELVIEW
glLoadIdentity(); //Réinitialise la matrice
gluLookAt(0,0,-10,0,0,0,0,1,0);
glBegin(GL_TRIANGLES);
glVertex2i(0,1);
glVertex2i(-1,0);
glVertex2i(1,0);
glEnd();
//Placer ici tout le code de transformation et de dessin
SwapBuffers(hDC);
}
LRESULT CALLBACK OpenGlProc(HWND hOgl,UINT Message,
WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CLOSE:
wglMakeCurrent(NULL, NULL);
if (hRC)
wglDeleteContext(hRC);
ReleaseDC(hOgl,hDC); // à faire dans le main
PostQuitMessage(0);
break; //Libère le DC et ferme le programme
case WM_CREATE:
hDC = GetDC(hOgl); // We get the Device Context
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC); // We create the Rendering Context
if (!hRC)
SendMessage(hOgl,WM_CLOSE,0,0); // If we can't, then we close the handle
wglMakeCurrent(hDC, hRC); // else we make it the current RC for the OpenGl Output
InitGL();
break;
case WM_SIZE:
Reshape(
LOWORD(lParam),
HIWORD(lParam)
);
break;
default:
return DefWindowProc(
hOgl,
Message,
wParam,
lParam);
break; //Sinon, fait le truc habituel
}
return FALSE;
} |
Partager