Hej. Jeg får ikke sat noget på på skærmen, når jeg bruger glVertex kaldene. Kan I se, hvad der er galt?
main.cpp (sikkert ikke relevant):
#include <windows.h>
#include "wndproc.h"
int WINAPI WinMain (HINSTANCE Instance, HINSTANCE, PSTR, int) {
WNDCLASSEX wcl;
memset (&wcl, 0, sizeof (wcl));
wcl.cbSize = sizeof (WNDCLASSEX);
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hInstance = Instance;
wcl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wcl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor (NULL, IDC_ARROW);
wcl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wcl.lpszMenuName = NULL;
wcl.lpszClassName = TEXT ("MAINWINDOW");
wcl.lpfnWndProc = WndProc;
if (!RegisterClassEx (&wcl))
return 0;
HWND Window = CreateWindow (
TEXT ("MAINWINDOW"),
TEXT ("OpenGL"),
WS_POPUP,
0, 0, 0, 0,
GetDesktopWindow (),
NULL, Instance, NULL
);
ShowCursor (0);
ShowWindow (Window, SW_SHOWMAXIMIZED);
UpdateWindow (Window);
int Run = 1;
MSG Message;
while (Run) {
if (PeekMessage (&Message, NULL, 0, 0, PM_REMOVE) > 0) {
if (Message.message == WM_QUIT) {
Run = 0;
break;
}
else {
TranslateMessage (&Message);
DispatchMessage (&Message);
}
}
}
return Message.wParam;
}
opengl.cpp:
#include <windows.h>
#include <gl/gl.h>
#include "opengl.h"
void glEnable (HWND Window, HDC *hdc, HGLRC *hrc) {
PIXELFORMATDESCRIPTOR pf;
memset (&pf, 0, sizeof (pf));
pf.nSize = sizeof (pf);
pf.nVersion = 1;
pf.cColorBits = 24;
pf.cDepthBits = 16;
pf.iPixelType = PFD_TYPE_RGBA;
pf.iLayerType = PFD_MAIN_PLANE;
pf.dwFlags = PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL;
int iFormat = ChoosePixelFormat (*hdc, &pf);
SetPixelFormat (*hdc, iFormat, &pf);
*hrc = wglCreateContext (*hdc);
wglMakeCurrent (*hdc, *hrc);
}
void glDisable (HWND Window, HDC *hdc, HGLRC *hrc) {
wglDeleteContext (*hrc);
wglMakeCurrent (NULL, NULL);
ReleaseDC (Window, *hdc);
}
void glRender () {
glClearColor (0.2f, 0.6f, 0.9f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
//glPolygonMode (GL_FRONT, GL_LINE);
}
void glDrawScene () {
glBegin (GL_TRIANGLES);
glVertex2f (0.5f, 1.0f);
glVertex2f (0.0f, 0.0f);
glVertex2f (1.0f, 0.0f);
glEnd ();
}
opengl.h:
void glEnable (HWND, HDC *, HGLRC *);
void glDisable (HWND, HDC *, HGLRC *);
void glRender ();
void glDrawScene ();
wndproc.cpp:
#include <windows.h>
#include "wndproc.h"
#include "opengl.h"
// Event handlers
void OnKey (int iKey) {
switch (iKey) {
case VK_ESCAPE:
PostQuitMessage (0);
break;
}
}
// Device- and rendercontext
HDC hdc;
HGLRC hrc;
LRESULT CALLBACK WndProc (HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) {
switch (Message) {
case WM_CREATE:
hdc = GetDC (Window);
glEnable (Window, &hdc, &hrc);
SetTimer (Window, 0, 1000, 0);
break;
case WM_PAINT:
glRender ();
glDrawScene ();
SwapBuffers (hdc);
break;
case WM_KEYDOWN:
OnKey (wParam);
break;
case WM_QUIT:
case WM_CLOSE:
case WM_DESTROY:
glDisable (Window, &hdc, &hrc);
PostQuitMessage (0);
break;
default:
return DefWindowProc (Window, Message, wParam, lParam);
}
}
wndproc.h:
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
// Event handlers
void OnKey (int);
Resultatet er en blå skærm, som jeg sætter den med glClearColor.