Original Post
Hey all, Quick question, ive compiled my code with no errors, its ment to render a rotating a lime green triangle on a black background. All that is displayed is a flash of a lime green triangle, then a plane black screen and nothing else happens. winmain.cpp CGfxOpenGL.h CGfxOpenGL.cpp Cheers for any help :D ~Matthew()
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
//
// INCLUDES and GLOBAL VARIABLES
//
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "CGfxOpenGL.h"
bool exiting = false; // is the application exiting?
long windowWidth = 800; // window width
long windowHeight = 600; // window height
long windowBits = 32; // window bits per pixel
bool fullscreen = false; // is the application running in fullscreen mode?
HDC hDC; // WINDOW DEVICE CONTEXT
//
// GLOBAL POINTER to the CGfxOpenGL rendering class
//
CGfxOpenGL *g_glRender = NULL;
//
// FUNCTION: SetupPixelFormat(HDC hDC) || Setup DEVICE CONTEXT Pixel Format
//
void SetupPixelFormat(HDC hDC)
{
int pixelFormat;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size
1, // version
PFD_SUPPORT_OPENGL | // OpenGL window
PFD_DRAW_TO_WINDOW | // render to window
PFD_DOUBLEBUFFER, // support double-buffering
PFD_TYPE_RGBA, // colour type
32, // prefered colour depth
0, 0, 0, 0, 0, 0, // colour bits
0, // no alpha buffer
0, // alpha bits
0, // no accumulation buffer
0, 0, 0, 0, // accum bits
16, // depth buffer
0, // no stencil buffer
0, // no auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0, // no layer, visible, damage masks
};
pixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelFormat, &pfd);
}
//
// MainWindowProc()
//
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int height, width;
// dispatch messages
switch (uMsg)
{ // window creation
case WM_CREATE:
hDC = GetDC(hWnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
break;
case WM_DESTROY: // window destroy
case WM_QUIT:
case WM_CLOSE: // window is closing
// deselect RENDERING CONTEXT and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
// send WM_QUIT to message queue
PostQuitMessage(0);
break;
case WM_SIZE:
height = HIWORD(lParam); // retrieve height
width = LOWORD(lParam); // retrieve width
g_glRender->SetupProjection(width, height);
break;
case WM_KEYDOWN:
int fwKeys;
LPARAM keyData;
fwKeys = (int)wParam; // virtual-key code
keyData = lParam; // key data
switch(fwKeys)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
default:
break;
}
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//
// WinMain()
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass; // window class
HWND hwnd; // window handle
MSG msg; // message
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT windowRect;
g_glRender = new CGfxOpenGL; // g_glRender new CGfxOpenGL class
windowRect.left = (long)0;
windowRect.right = (long)windowWidth;
windowRect.top = (long)0;
windowRect.bottom = (long)windowHeight;
// window class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = MainWindowProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "GLClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
// register the windows class
if (!RegisterClassEx(&windowClass))
return 0;
if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = windowWidth;
dmScreenSettings.dmPelsHeight = windowHeight;
dmScreenSettings.dmBitsPerPel = windowBits;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
// fullscreen not successful
MessageBox(NULL, "Display Mode Failed", NULL, MB_OK);
fullscreen = false;
}
}
if (fullscreen)
{
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(false);
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle);
// class registered, now create window
hwnd = CreateWindowEx(NULL,
"GLClass", // class name
"My OpenGL Application", // application name
dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0, // x, y coordinate
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top, // width, height
NULL,
NULL,
hInstance, // application instance
NULL);
hDC = GetDC(hwnd);
// check window creation
if (!hwnd)
return 0;
ShowWindow(hwnd, SW_SHOW); // display the window
UpdateWindow(hwnd); // update the window
g_glRender->Init();
while (!exiting)
{
g_glRender->Prepare(0.0f);
g_glRender->Render();
SwapBuffers(hDC);
while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage (&msg, NULL, 0, 0))
{
exiting = true;
break;
}
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
delete g_glRender;
if (fullscreen)
{
ChangeDisplaySettings(NULL, 0); // if still in fullscreen mode, switch back to desktop
ShowCursor(true); // show mouse cursor
}
return (int)msg.lParam;
}
class CGfxOpenGL
{
private:
int m_windowWidth;
int m_windowHeight;
float m_angle;
public:
CGfxOpenGL();
virtual ~CGfxOpenGL();
bool Init();
bool Shutdown();
void SetupProjection(int width, int height);
void Prepare(float dt);
void Render();
};
#ifdef _WINDOWS
#include <windows.h>
#endif
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "CGfxOpenGL.h"
CGfxOpenGL::CGfxOpenGL()
{
}
CGfxOpenGL::~CGfxOpenGL()
{
}
bool CGfxOpenGL::Init()
{
// clear background to 0.0, 0.0, 0.0, 0.0 (black)
glClearColor(0.0, 0.0, 0.0, 0.0);
m_angle = 0.0f;
return true;
}
bool CGfxOpenGL::Shutdown()
{
return true;
}
void CGfxOpenGL::SetupProjection(int width, int height)
{
if (height == 0)
{
height = 1;
}
glViewport(0, 0, width, height); // reset viewport
glMatrixMode(GL_PROJECTION); // CURRENT MATRIX : projection matrix
glLoadIdentity(); // reset projection matrix
// GLU : calculate window aspect ratio
gluPerspective(52.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // CURRENT MATRIX : modelview matrix
glLoadIdentity(); // reset modelview matrix
m_windowWidth = width;
m_windowHeight = height;
}
void CGfxOpenGL::Prepare(float dt)
{
m_angle += 0.1;
}
void CGfxOpenGL::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer
glLoadIdentity;
glTranslatef(0.0, 0.0, -5.0f); // move back 5.0 units on Z axis
glRotatef(m_angle, 1.0f, 0.0f, 0.0f); // rotate about all 3 axis
glRotatef(m_angle, 0.0f, 1.0f, 0.0f);
glRotatef(m_angle, 0.0f, 0.0f, 1.0f);
glColor3f(0.7f, 1.0f, 0.3f); // change colour to LIME
glBegin(GL_TRIANGLES); // draw triangle with central rotation point
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
}