Still getting the Errors with GOODGL enabled, and a clear screen without it. I have decided to put my entire 3dglgui.h in here so...
#ifndef LAB2DGLGUI_H
#ifndef CLASS_NAME
#define CLASS_NAME "OpenGL Windows Class"//This can be overwritten to specify a unique class name
#endif
//Header Guards and version number
#ifndef LAB3DGLGUI_H
#define LAB3DGLGUI_H 1.0
//Error codes
#define ERR_NO_ERROR 0x0000
#define ERR_CLASS_REGISTRY_FAILED 0x0001
#define ERR_FULLSCREEN_FAILED 0x0002
#define ERR_WINDOW_CREATION_FAILED 0x0004
#define ERR_GET_DC_FAILED 0x0008
#define ERR_PIXEL_FORMAT_CHOICE_FAILED 0x0010
#define ERR_NO_VALID_PIXEL_FORMAT 0x0020
#define ERR_GET_RC_FAILED 0x0040
#define ERR_MAKE_CURRENT_FAILED 0x0080
#define ERR_OPENGL_INITIALIZATION_FAILED 0x0100
#define ERR_RELEASE_DC_RC_FAILED 0x0200
#define ERR_RELEASE_RC_FAILED 0x0400
#define ERR_RELEASE_DC_FAILED 0x0800
#define ERR_RELEASE_WINDOW_FAILED 0x1000
#define ERR_RELEASE_WINDOW_CLASS_FAILED 0x2000
#define APART(X) ((float)((char)((X)>>24))/255.0)
#define RPART(X) ((float)((char)((X)>>16))/255.0)
#define GPART(X) ((float)((char)((X)>>8))/255.0)
#define BPART(X) ((float)((char)((X)))/255.0)
#define DEGTORAD(X) ((X)*0.0174532925)
//Includes
#include <math.h>//for trig ratios and sqrt
#include <windows.h>//for windows
#include <gl/gl.h>
#include <gl/glu.h>
#define GL_GLEXT_PROTOTYPES
#include <gl/glext2.h>
#include "models.h"
#include "random.h"
RND Rand;
//typedefs
typedef GLuint glTexture;//Storage for one (or more) textures
typedef unsigned int glColour;//Stores a colour as 0xAARRGGBB
//Unique main function
#define main() WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevhInstance, LPSTR cmdline, int cmdshow)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
class glFont
{
private:
unsigned int letters;
GLYPHMETRICSFLOAT chars[256];
public:
void Load(const char *fname, int height, int weight, bool italic, bool underline, bool strikethrough, float depth);
void operator()(const char *str);
~glFont(){glDeleteLists(letters,255);}
};
class OpenGLInstance
{
private:
GLUquadricObj *quadricobject;
HDC hDC;
HGLRC hRC;
HWND hWnd;
HINSTANCE hInstance;
LONGLONG tm;
public:
short ERRORCODE;
struct windowdatastructure
{
bool key[256]; bool active; bool fullscreen; int w; int h; int fps;
}window;
struct mousedatastructure
{
int x; int y; int scroll; bool l; bool r; bool m;
}mouse;
struct positiondatastructure
{
float x; float y; float z; float h; float v; float t;
}pos;
OpenGLInstance &Perpective(double angle, double near, double far);
OpenGLInstance &Resize(int height, int width);
OpenGLInstance &Init(const char *title, int width, int height, bool fullscreen, int posx, int posy);
OpenGLInstance &Kill();
OpenGLInstance &LoadTexture(const char *fname, glTexture *tex, bool linear);
OpenGLInstance &SetTexture(glTexture tex);
OpenGLInstance &LightPosition(unsigned int light, float x, float y, float z);
OpenGLInstance &AmbientLight(unsigned int light, glColour col);
OpenGLInstance &DiffuseLight(unsigned int light, glColour col);
OpenGLInstance &SpecularLight(unsigned int light, glColour col);
OpenGLInstance &Move(float rl, float fb, float z);
OpenGLInstance &Rotate(float rl, float ud, float cc);
OpenGLInstance &Mouse(int x, int y);
OpenGLInstance &Fog(unsigned char quality, glColour col, float density, float start, float end);
OpenGLInstance &Draw(glModel &model);
OpenGLInstance &Draw(const char *text, glFont font);
OpenGLInstance &Update();
OpenGLInstance &DrawCylinder(double baseradius, double topradius, double height, int quality);
OpenGLInstance &DrawDisk(double innerradius, double outerradius, int quality);
OpenGLInstance &DrawDisk(double innerradius, double outerradius, double startangle, double deltaangle, int quality);
OpenGLInstance &DrawSphere(double radius, int quality);
bool operator()(unsigned char ch){return window.key[ch];}
HDC &GetHDC(){return hDC;}
}GL;
void glFont::operator()(const char *str)
{
for (int i=0; str[i]!=0; i++)
glCallList(letters+str[i]);
}
void glFont::Load(const char *fname, int height, int weight, bool italic, bool underline, bool strikethrough, float depth)
{
HFONT fnt,ofnt;
letters=glGenLists(256);
fnt=CreateFont(-height,0,0,0,weight,italic,underline,strikethrough,ANSI_CHARSET,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,FF_DONTCARE|DEFAULT_PITCH,fname);
ofnt=(HFONT)SelectObject(GL.GetHDC(),fnt);
wglUseFontOutlines(GL.GetHDC(),0,255,letters,0.0f,depth,WGL_FONT_POLYGONS,chars);
}
/*IMPLEMENTATIONS*/
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch (msg)
{
case WM_ACTIVATE:
GL.window.active=!HIWORD(wparam);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_KEYDOWN:
GL.window.key[wparam]=true;
return 0;
case WM_KEYUP:
GL.window.key[wparam]=false;
return 0;
case WM_SIZE:
GL.Resize(LOWORD(lparam),HIWORD(lparam));
return 0;
case WM_MOUSEMOVE:
POINT pnt;
GetCursorPos(&pnt);
GL.mouse.x=pnt.x;
GL.mouse.y=pnt.y;
return 0;
case WM_MOUSEWHEEL:
GL.mouse.scroll=GET_WHEEL_DELTA_WPARAM(wparam);
return 0;
case WM_RBUTTONDOWN:
GL.mouse.r=true;
return 0;
case WM_RBUTTONUP:
GL.mouse.r=false;
return 0;
case WM_LBUTTONDOWN:
GL.mouse.l=true;
return 0;
case WM_LBUTTONUP:
GL.mouse.l=false;
return 0;
case WM_MBUTTONDOWN:
GL.mouse.m=true;
return 0;
case WM_MBUTTONUP:
GL.mouse.m=false;
return 0;
default:
return DefWindowProc(hwnd,msg,wparam,lparam);
}
}
OpenGLInstance &OpenGLInstance::Perpective(double angle, double vnear, double vfar)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(angle,
((double)((*this).window.w)/(double)((*this).window.h)),vnear,vfar);
glMatrixMode(GL_MODELVIEW);
return *this;
}
OpenGLInstance &OpenGLInstance::Resize(int height, int width)
{
(height==0)?height=1:height=height;
glViewport(0,0,width,height);
window.w=width;
window.h=height;
return *this;
}
OpenGLInstance &OpenGLInstance::Init(const char *title, int width, int height, bool fullscreen, int posx, int posy)
{
DEVMODE tmp;
EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&tmp);
window.fps=tmp.dmDisplayFrequency;
unsigned int PixelFormat;
DWORD dwExStyle,dwStyle;
RECT WindowRect={(long)0,(long)0,(long)width,(long)height};
window.fullscreen=fullscreen;
hInstance=GetModuleHandle(NULL);
WNDCLASS wc={CS_HREDRAW|CS_VREDRAW|CS_OWNDC,(WNDPROC)WndProc,0,0,hInstance,LoadIcon(NULL,IDI_WINLOGO),LoadCursor(NULL,IDC_ARROW),NULL,NULL,CLASS_NAME};
(!RegisterClass(&wc))?ERRORCODE|=ERR_CLASS_REGISTRY_FAILED:ERRORCODE=ERRORCODE;
if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth=width;
dmScreenSettings.dmPelsHeight=height;
dmScreenSettings.dmBitsPerPel=32;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSHEIGHT|DM_PELSWIDTH;
(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)?ERRORCODE|=ERR_FULLSCREEN_FAILED:ERRORCODE=ERRORCODE;
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);
if (!(hWnd=CreateWindowEx(dwExStyle,CLASS_NAME,title,WS_CLIPSIBLINGS|WS_CLIPCHILDREN|dwStyle,posx,posy,WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,NULL,NULL,hInstance,NULL)))
{
Kill();
ERRORCODE|=ERR_WINDOW_CREATION_FAILED;
}
static PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,32,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0};
if (!(hDC=GetDC(hWnd)))
{
Kill();
ERRORCODE|=ERR_GET_DC_FAILED;
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
{
Kill();
ERRORCODE|=ERR_PIXEL_FORMAT_CHOICE_FAILED;
}
if (!SetPixelFormat(hDC,PixelFormat,&pfd))
{
Kill();
ERRORCODE|=ERR_NO_VALID_PIXEL_FORMAT;
}
if (!wglMakeCurrent(hDC,hRC))
{
Kill();
ERRORCODE|=ERR_MAKE_CURRENT_FAILED;
}
ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
Resize(width,height);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_COLOR_MATERIAL);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
quadricobject=gluNewQuadric();
gluQuadricNormals(quadricobject,GLU_SMOOTH);
gluQuadricTexture(quadricobject,GL_TRUE);
return *this;
}
OpenGLInstance &OpenGLInstance::Kill()
{
if (window.fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(TRUE);
}
if (hRC)
{
(!wglMakeCurrent(NULL,NULL))?ERRORCODE|=ERR_RELEASE_DC_RC_FAILED:ERRORCODE=ERRORCODE;
(!wglDeleteContext(hRC))?ERRORCODE|=ERR_RELEASE_RC_FAILED:ERRORCODE=ERRORCODE;
hRC=NULL;
}
if (hDC&&!ReleaseDC(hWnd,hDC))
{
ERRORCODE|=ERR_RELEASE_DC_FAILED;
hDC=NULL;
}
if (hWnd&&!DestroyWindow(hWnd))
{
ERRORCODE|=ERR_RELEASE_WINDOW_FAILED;
hWnd=NULL;
}
if (!UnregisterClass(CLASS_NAME,hInstance))
{
ERRORCODE|=ERR_RELEASE_WINDOW_CLASS_FAILED;
hInstance=NULL;
}
return *this;
}
OpenGLInstance &OpenGLInstance::LoadTexture(const char *fname, glTexture *tex, bool linear)
{
if (!tex)
return *this;
glTexture &out=*tex;
HBITMAP hBMP;BITMAP BMP;
glTexture texid;
glGenTextures(1, &texid);
hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),fname,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
if (!hBMP)
return *this;
GetObject(hBMP, sizeof(BMP), &BMP);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear?GL_LINEAR:GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear?GL_LINEAR:GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
DeleteObject(hBMP);
out=texid;
return *this;
}
OpenGLInstance &OpenGLInstance::SetTexture(glTexture tex)
{
glBindTexture(GL_TEXTURE_2D,tex);
return *this;
}
OpenGLInstance &OpenGLInstance::LightPosition(unsigned int light, float x, float y, float z)
{
glLightfv(light, GL_POSITION, (float[4]){x,y,z,1.0f});
return *this;
}
OpenGLInstance &OpenGLInstance::AmbientLight(unsigned int light, glColour col)
{
glLightfv(light,GL_AMBIENT,(float[4]){RPART(col),GPART(col),BPART(col),APART(col)});
return *this;
}
OpenGLInstance &OpenGLInstance::DiffuseLight(unsigned int light, glColour col)
{
glLightfv(light,GL_DIFFUSE,(float[4]){RPART(col),GPART(col),BPART(col),APART(col)});
return *this;
}
OpenGLInstance &OpenGLInstance::SpecularLight(unsigned int light, glColour col)
{
glLightfv(light,GL_SPECULAR,(float[4]){RPART(col),GPART(col),BPART(col),APART(col)});
return *this;
}
OpenGLInstance &OpenGLInstance::Move(float rl, float fb, float z)
{
if (rl>0)
{
pos.x-=(float)sin(DEGTORAD(pos.h-90.0f))*rl;
pos.y-=(float)cos(DEGTORAD(pos.h-90.0f))*rl;
}
else
{
pos.x+=(float)sin(DEGTORAD(pos.h+90.0f))*rl;
pos.y+=(float)cos(DEGTORAD(pos.h+90.0f))*rl;
}
pos.x-=(float)sin(DEGTORAD(pos.h))*fb;
pos.y-=(float)cos(DEGTORAD(pos.h))*fb;
pos.z+=z;
return *this;
}
OpenGLInstance &OpenGLInstance::Rotate(float rl, float ud, float cc)
{
pos.h+=rl;
pos.v+=ud;
pos.t+=cc;
return *this;
}
OpenGLInstance &OpenGLInstance::Mouse(int x, int y)
{
RECT windim;
GetWindowRect(hWnd,&windim);
SetCursorPos(x+windim.left,y+windim.top);
return *this;
}
OpenGLInstance &OpenGLInstance::Fog(unsigned char quality, glColour col, float density, float start, float end)
{
unsigned int mode,hint;
switch (quality)
{
case 0:
mode=GL_EXP;
hint=GL_FASTEST;
break;
case 1:
mode=GL_EXP;
hint=GL_DONT_CARE;
break;
case 2:
mode=GL_EXP;
hint=GL_NICEST;
break;
case 3:
mode=GL_EXP2;
hint=GL_FASTEST;
break;
case 4:
mode=GL_EXP2;
hint=GL_DONT_CARE;
break;
case 5:
mode=GL_EXP2;
hint=GL_NICEST;
break;
case 6:
mode=GL_LINEAR;
hint=GL_FASTEST;
break;
case 7:
mode=GL_LINEAR;
hint=GL_DONT_CARE;
break;
case 8:
default:
mode=GL_LINEAR;
hint=GL_NICEST;
}
glFogi(GL_FOG_MODE,mode);
float colour[]={RPART(col),GPART(col),BPART(col),APART(col)};
glFogfv(GL_FOG_COLOR,colour);
glFogf(GL_FOG_DENSITY,density);
glHint(GL_FOG_HINT,hint);
glFogf(GL_FOG_START,start);
glFogf(GL_FOG_END,end);
return *this;
}
OpenGLInstance &OpenGLInstance::Draw(glModel &model)
{
model.Draw();
return *this;
}
OpenGLInstance &OpenGLInstance::Draw(const char *text, glFont font)
{
font(text);
return *this;
}
OpenGLInstance &OpenGLInstance::Update()
{
SwapBuffers(hDC);
MSG temp;
PeekMessage(&temp,NULL,0,0,PM_REMOVE);
TranslateMessage(&temp);
DispatchMessage(&temp);
//wait off to force the fps
LARGE_INTEGER tmp;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&tmp);
int dtime=tmp.QuadPart-tm;
double deltatime=((double)dtime/(double)freq.QuadPart)*(double)1000.0;
Sleep(((double)window.fps/(double)1000.0)-deltatime);
return *this;
}
OpenGLInstance &OpenGLInstance::DrawCylinder(double baseradius, double topradius, double height, int quality)
{
gluCylinder(quadricobject, baseradius, topradius, height, quality, quality);
return *this;
}
OpenGLInstance &OpenGLInstance::DrawDisk(double innerradius, double outerradius, int quality)
{
gluDisk(quadricobject, innerradius, outerradius, quality, quality);
return *this;
}
OpenGLInstance &OpenGLInstance::DrawDisk(double innerradius, double outerradius, double startangle, double deltaangle, int quality)
{
gluPartialDisk(quadricobject, innerradius, outerradius, quality, quality, startangle, deltaangle);
return *this;
}
OpenGLInstance &OpenGLInstance::DrawSphere(double radius, int quality)
{
gluSphere(quadricobject, radius, quality, quality);
return *this;
}
#endif
#endif