| |
| OpenGL en Visual Studio Express 2008 Win32 versie (Deel 2) |
© 2010 Hein Pragt
Het is ook mogelijk om gebruik te maken van de Microsoft OpenGL implementatie, deze is wel enigszins windows
afhankelijk maar de basis van OpgenGL blijft hetzelfde waardoor u wel grafische programma's kunt ontwikkelen die
met een paar kleine aanpassingen ook op een ander platform kunnen draaien. Het is aan uzelf om zo weinig mogelijk
Win32 code te gebruiken en de Windows code en de OpenGL code zo goed mogelijk te scheiden van elkaar. Dit programma
maakt gebruik van de Microsoft OpenGL methodes en zal dan ook een een Microsoft Windows venster draaien. Kopieer het
vorige project van Deel 1 en kopieer deze broncode er in. Bestudeer de broncode en ontdek hoe u met Win32 OpenGL
kunt programmeren.
// -----------------------------------------------------------------
// Een voorbeeld va een minimaal Win32 en OpenGL programma.
// Maakt gebruik van een MS Windows venster.
// Het werkt alleen in 16 bit kleuren mode of hoger.
// (c) Hein Pragt 2010 vrij te gebruiken.
// -----------------------------------------------------------------
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
// OpenGL code
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0, 0.8);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.8, -0.8);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.8, -0.8);
glEnd();
glFlush();
}
// Minimale windows interface
#define WIN_CLASS_NAME "MyGraphClass"
// Forward declarations
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND CreateOpenGLWindow(HINSTANCE hInstance,char* title, int x, int y,
int width, int height, BYTE type, DWORD flags);
// Minimal Win32 main function
int APIENTRY WinMain(HINSTANCE hCurrentInst,HINSTANCE hPreviousInst,LPSTR lpszCmd,int nCmdShow)
{
HDC hDC; /* device context */
HGLRC hRC; /* opengl context */
HWND hWnd;
MSG msg;
BOOL quit = FALSE;
hWnd = CreateOpenGLWindow(hCurrentInst,"Mini OpenGl Demo", 0, 0, 400, 400, PFD_TYPE_RGBA, 0);
if (hWnd == NULL)
exit(1);
hDC = GetDC(hWnd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
ShowWindow(hWnd, nCmdShow);
// Windows message loop
while (!quit) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
quit = TRUE;
}
else {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
}
wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd,hDC);
wglDeleteContext(hRC);
DestroyWindow(hWnd);
return msg.wParam;
}
HWND CreateOpenGLWindow(HINSTANCE hInstance,char* title, int x, int y,
int width, int height, BYTE type, DWORD flags)
{
int pf;
HDC hDC;
HWND hWnd;
WNDCLASS wc;
PIXELFORMATDESCRIPTOR pfd;
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = WIN_CLASS_NAME;
if (!RegisterClass(&wc)) {
MessageBox(NULL, "RegisterClass() failed!", "Error", MB_OK);
return NULL;
}
hWnd = CreateWindow(WIN_CLASS_NAME, title,
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
x, y, width, height, NULL, NULL, hInstance, NULL);
if (hWnd == NULL) {
MessageBox(NULL, "CreateWindow() failed!","Error", MB_OK);
return NULL;
}
hDC = GetDC(hWnd);
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
pfd.iPixelType = type;
pfd.cColorBits = 32;
pf = ChoosePixelFormat(hDC, &pfd);
if (pf == 0) {
MessageBox(NULL, "ChoosePixelFormat() failed!","Error", MB_OK);
return NULL;
}
if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
MessageBox(NULL, "SetPixelFormat() failed!","Error", MB_OK);
return NULL;
}
DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
ReleaseDC(hWnd,hDC);
return hWnd;
}
// Minimal windows proc handler
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PAINTSTRUCT ps;
switch(uMsg) {
case WM_PAINT:
display();
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0L;
case WM_SIZE:
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
PostMessage(hWnd, WM_PAINT, 0, 0);
return 0L;
case WM_CHAR:
switch (wParam) {
case 27: /* ESC */
PostQuitMessage(0);
break;
}
return 0L;
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Druk op F7 om te compileren en daarna op F5 om het uit te voeren, u heeft nu uw tweede OpenGl programma gemaakt.
Last update: 20-04-2010
|
Wilt u deze site steunen dan kunt u dit doen door VIA deze site iets te bestellen op:

Programmeren
|