Hello, I have been attempting to create a fast class for OpenGL's Vertex Buffer Objects. I finally got all the libraries to work and now i am getting a sigseg but cant see why. Can someone find my error? (I will keep looking too)

#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
class glModel
{
    private:
    int numv;
    unsigned int model;
    struct glVertexStructure
    {
        float vert[3];
        float text[2];
        float norm[3];
    };
    glVertexStructure *vars;
    public:
    glModel():numv(0),model(0),vars(0){}
    glModel &Vertex(float tx, float ty, float x, float y, float z);
    glModel &Compile();
    glModel &Draw();
    glModel &Move(float x, float y, float z){glTranslatef(x,y,z);return *this;}
};




glModel &glModel::Vertex(float tx, float ty, float x, float y, float z)
{
    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);
    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
    return *this;
}
glModel &glModel::Draw()
{
    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, 3*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, 3*numt);
    #endif
    return *this;
}
#endif
#endif

The error comes from lines 105 and 114, you should not multiply the numt by 3. The "count" given to the glDrawArrays is the number of vertices to draw, so it should be numt. As so:

glDrawArrays(GL_TRIANGLES, 0, numt);

Also, your class is missing several very important basic things, that is, the destructor (to delete vars, avoiding a leak), the copy-constructor and assignment operator (or disable them). Read my tutorial on that subject.

But, frankly, I would recommend you store your buffer "vars" using a std::vector<glVertexStructure>, it will be much easier to implement your "vertex" function correctly (although it is already correct).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.