I’m a little hesitant about asking this because last time I asked something here, I was given a curt answer that wasn’t what I was looking for and the thread was closed …
So … please go easy.
It’s just a simple program that’s supposed to display a square that you can rotate using the numpad. Nothing comes up. I get a sky blue screen, and that’s it. I had a much more sophisticated version of this program, but then my hard drive crashed ( :mad: ) and I have not been able to get this to work since.
/MAIN.CPP
/**************************
* Includes
*
**************************/
#include "OpenGL.h"
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
OpenGL *gl = new OpenGL;
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
MSG msg;
BOOL bQuit = FALSE;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
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 (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
gl->hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_POPUPWINDOW | WS_VISIBLE,
300, 100, 400, 400,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
gl->EnableOpenGL ();
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.75f, 1.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
gl->DrawTriangle();
SwapBuffers (gl->hDC);
Sleep (1);
}
}
/* shutdown OpenGL */
gl->DisableOpenGL ();
/* destroy the window explicitly */
DestroyWindow (gl->hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
case VK_NUMPAD7:
gl->dDelta +=.05;
return 0;
case VK_NUMPAD1:
gl->dDelta -=.05;
return 0;
case VK_NUMPAD2:
gl->eDelta -=.1;
return 0;
case VK_NUMPAD8:
gl->eDelta +=.1;
return 0;
case VK_NUMPAD4:
gl->aDelta +=.1;
return 0;
case VK_NUMPAD6:
gl->aDelta -=.1;
return 0;
case VK_NUMPAD5:
gl->dDelta = 0.0;
gl->eDelta = 0.0;
gl->aDelta = 0.0;
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/OPENGL.H
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#define PI 3.1415926535
#define DEG2RAD(x) x*(PI/180)
class OpenGL {
public:
HWND hWnd;
HDC hDC;
HGLRC hRC;
float e, a, d;
float eDelta, aDelta, dDelta;
float eyex, eyey, eyez;
float upx, upy, upz;
void EnableOpenGL();
void DisableOpenGL();
void DrawTriangle();
};
/*******************
* Enable OpenGL
*
*******************/
void OpenGL::EnableOpenGL ()
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (hDC, &pfd);
SetPixelFormat (hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );
aDelta = eDelta = dDelta = 0.0;
a = 45.0;
e = 30.0;
d = 10.0;
}
/******************
* Disable OpenGL
*
******************/
void OpenGL::DisableOpenGL ()
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
void OpenGL::DrawTriangle() {
glPushMatrix ();
eyey = d * sin(DEG2RAD(e));
eyex = (cos(DEG2RAD(e)) * d) * cos(DEG2RAD(a));
eyez = (cos(DEG2RAD(e)) * d) * sin(DEG2RAD(a));
upy = d * sin(DEG2RAD(e + 1));
upx = (cos(DEG2RAD(e + 1)) * d) * cos(DEG2RAD(a));
upz = (cos(DEG2RAD(e + 1)) * d) * sin(DEG2RAD(a));
gluLookAt(eyex, eyey, eyez, 0.0, 0.0, 0.0, upx, upy, upz);
glColor3f(0.0f, 0.75f, 0.0f);
glBegin (GL_QUADS);
glVertex3f( 2.0f, 0.0f, 2.0f);
glVertex3f(-2.0f, 0.0f, 2.0f);
glVertex3f(-2.0f, 0.0f, -2.0f);
glVertex3f( 2.0f, 0.0f, -2.0f);
glEnd ();
glPopMatrix ();
e += eDelta;
a += aDelta;
d += dDelta;
}