I am still unable to get OpenGL Vertex Buffer Objects to work. I will just show you the code I have written so far:
3dglgui.h (I think this should be fine):
#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"
//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, int bpp, 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, int bpp, 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=bpp;
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,bpp,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,0.5f);
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
models.h (I think the sigseg is here):
#ifdef LAB3DGLGUI_H
#ifndef GLMODELS_H
#define GLMODELS_H
#ifndef GOODGL
typedef void (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
PFNGLGENBUFFERSARBPROC glGenBuffersD=(PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
PFNGLBINDBUFFERARBPROC glBindBufferD=(PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
PFNGLBUFFERDATAARBPROC glBufferDataD=(PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersD=(PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");
#else
#include <gl/glext.h>
#endif
#include <stdio.h>
struct glVertexStructure
{
float vert[3];
float text[2];
float norm[3];
}NULLV;
/* Model File Format (.mdl)
4 bytes (int) -> numv
32*numv bytes (glVertexStructure)-> vars
-> 12 bytes (float[3]) -> vert
-> 8 bytes (float[2]) -> text
-> 12 bytes (float[3]) -> norm
*/
int HIGH_ID=0;
int COMPILED_ID=0;
class glModel
{
private:
int id;
unsigned int numv;
unsigned int model;
glVertexStructure *vars;
glModel &Compile();
public:
glModel():numv(0),model(0),vars(0),id(++HIGH_ID){}
glModel(const glModel&);
glModel(const char*);//Load from .mdl
~glModel()
{
if (numv>1)
delete[]vars;
else if (numv>0)
delete vars;
//Clean up VBOs??? Research needed!
}
glModel &operator=(const glModel&);
glModel &operator=(const char*);//Load from .mdl
glModel operator+(const glVertexStructure&)const;//Add Vertices
glModel operator+(const glModel&)const;
glModel &operator+=(const glVertexStructure&);//Append Vertices
glModel &operator+=(const glModel&);
glModel &Load(const char*);//Load from .mdl
glModel &Save(const char*);//Save to .mdl
glModel &Vertex(float tx, float ty, float x, float y, float z);
glModel &Draw();
glModel &Move(float x, float y, float z){glTranslatef(x,y,z);return *this;}
int len()const{return numv;}
glVertexStructure &operator[](int x)const{if (x<numv){return vars[x];}else{return NULLV;}}
};
glModel::glModel(const glModel &source)
{
numv=source.len();
vars=new glVertexStructure[numv];
for (int i=0; i<numv; i++)
{
vars[i]=source[i];
}
model=0;
id=++HIGH_ID;
}
glModel::glModel(const char *filename)
{
FILE *file=fopen(filename,"rb");
if (file)
{
fread(&numv, sizeof(numv), 1, file);
vars=new glVertexStructure[numv];
for (unsigned int i=0; i<numv; i++)
fread(&(vars[i].vert[0]), sizeof(float), 8, file);//I hope this works :P
}
fclose(file);
id=++HIGH_ID;
}
glModel &glModel::operator=(const glModel &source)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
if (numv>0)
delete[]vars;
numv=source.len();
vars=new glVertexStructure[numv];
for (int i=0; i<numv; i++)
{
vars[i]=source[i];
}
model=0;//Must I clean up the old model?
return *this;
}
glModel &glModel::operator=(const char *filename)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
if (numv>0)
delete[]vars;
FILE *file=fopen(filename,"rb");
if (file)
{
fread(&numv, sizeof(numv), 1, file);
vars=new glVertexStructure[numv];
for (unsigned int i=0; i<numv; i++)
fread(&(vars[i].vert[0]), sizeof(float), 8, file);//again I hope this works ><
}
fclose(file);
return *this;
}
glModel glModel::operator+(const glVertexStructure& add)const
{
if (COMPILED_ID==id)
COMPILED_ID=0;
glModel ret=*this;
ret+=add;
return ret;
}
glModel glModel::operator+(const glModel&add)const
{
if (COMPILED_ID==id)
COMPILED_ID=0;
glModel ret=*this;
ret+=add;
return ret;
}
glModel &glModel::operator+=(const glVertexStructure& add)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
glVertexStructure *temp=new glVertexStructure[numv];
for (int i=0; i<numv; i++)
temp[i]=vars[i];
vars[numv]=add;
delete[]vars;
vars=temp;
numv++;
return *this;
}
glModel &glModel::operator+=(const glModel& add)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
for (int i=0; i<add.len(); i++)
(*this)+=add[i];
return *this;
}
glModel &glModel::Load(const char *fname)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
if (numv>1)
delete[]vars;
else if (numv>0)
delete vars;
FILE *file=fopen(fname,"rb");
if (file)
{
fread(&numv,sizeof(numv), 1, file);
vars=new glVertexStructure[numv];
for (unsigned int i=0; i<numv; i++)
fread(&(vars[i].vert[0]),sizeof(float), 8, file);
}
fclose(file);
return *this;
}
glModel &glModel::Save(const char *fname)
{
FILE *file=fopen(fname,"wb");
fwrite(&numv, sizeof(unsigned int), 1, file);
fwrite(vars, sizeof(float), numv, file);//Again.... I hope this works!!!
fclose(file);
return *this;
}
glModel &glModel::Vertex(float tx, float ty, float x, float y, float z)
{
if (COMPILED_ID==id)
COMPILED_ID=0;
glVertexStructure *temp=new glVertexStructure[numv+1];
for (int i=0; i<numv; i++)
temp[i]=vars[i];
delete[]vars;
vars=temp;
temp=NULL;
vars[numv].vert[0]=x;
vars[numv].vert[1]=y;
vars[numv].vert[2]=z;
vars[numv].text[0]=tx;
vars[numv].text[1]=ty;
vars[numv].norm[0]=0.0;
vars[numv].norm[1]=0.0;
vars[numv].norm[2]=0.0;
numv++;
if (numv%3==0)
{
float &zz0=vars[numv-3].vert[2];
float &zz1=vars[numv-2].vert[2];
float &zz2=vars[numv-1].vert[2];
float &yy0=vars[numv-3].vert[1];
float &yy1=vars[numv-2].vert[1];
float &yy2=vars[numv-1].vert[1];
float &xx0=vars[numv-3].vert[0];
float &xx1=vars[numv-2].vert[0];
float &xx2=vars[numv-1].vert[0];
float ox=(yy1-yy0)*(zz2-zz0)-(zz1-zz0)*(yy2-yy0);
float oy=(zz1-zz0)*(xx2-xx0)-(xx1-xx0)*(zz2-zz0);
float oz=(xx1-xx0)*(yy2-yy0)-(yy1-yy0)*(xx2-xx0);
float mag=sqrt(ox*ox+oy*oy+oz*oz);
ox/=mag;
oy/=mag;
oz/=mag;
vars[numv-1].norm[0]=vars[numv-2].norm[0]=vars[numv-3].norm[0]=ox;
vars[numv-1].norm[1]=vars[numv-2].norm[1]=vars[numv-3].norm[1]=oy;
vars[numv-1].norm[2]=vars[numv-2].norm[2]=vars[numv-3].norm[2]=oz;
}
return *this;
}
glModel &glModel::Compile()
{
#ifndef GOODGL
glGenBuffersD(1,&model);//1st sigseg here
glBindBufferD(GL_ARRAY_BUFFER, model);
glBufferDataD(GL_ARRAY_BUFFER, numv*sizeof(glVertexStructure), vars, GL_STATIC_DRAW);
#else
glGenBuffers(1,&model);
glBindBuffer(GL_ARRAY_BUFFER,model);
glBufferData(GL_ARRAY_BUFFER, numv*sizeof(glVertexStructure), vars, GL_STATIC_DRAW);
#endif
COMPILED_ID=id;
return *this;
}
glModel &glModel::Draw()
{
if (COMPILED_ID!=id){Compile();}//1st sigseg comes from here
int numt=numv-numv%3;
if (numt==0)
return *this;
#ifndef GOODGL
glBindBufferD(GL_ARRAY_BUFFER,model);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(glVertexStructure),0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,sizeof(glVertexStructure),(void*)((char*)(NULL)+sizeof(float)*(&(vars[0].text[0])-&(vars[0].vert[0]))));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,sizeof(glVertexStructure),(void*)((char*)(NULL)+sizeof(float)*(&(vars[0].norm[0])-&(vars[0].vert[0]))));
glDrawArrays(GL_TRIANGLES, 0, numt);
#else
glBindBuffer(GL_ARRAY_BUFFER,model);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(glVertexStructure),0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,sizeof(glVertexStructure),(void*)((char*)(NULL)+sizeof(float)*(&(vars[0].text[0])-&(vars[0].vert[0]))));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,sizeof(glVertexStructure),(void*)((char*)(NULL)+sizeof(float)*(&(vars[0].norm[0])-&(vars[0].vert[0]))));
glDrawArrays(GL_TRIANGLES, 0, numt);
#endif
return *this;
}
#endif
#endif
test.cpp (I used this to test the headers):
#include "3dglgui.h"
int main()
{
GL.Init("TEST",500,500,24,false,0,0);
glModel model;
model.Vertex(0,0,0,0,0);
model.Vertex(1,1,1,1,1);
model.Vertex(1,0,1,1,0);
model.Draw();
do{
GL.Update();
}while(!GL.window.key[VK_ESCAPE]);
return 0;
}
Can anybody figure out how to get this to work???