PDA

View Full Version : Drawing C++ classes



Wasabi
01-25-2011, 10:17 AM
From what I've found on the web, I'm thinking the answer to this is a negatory, but can one "draw a C++ class"?

i.e.

struct AAA
{
float x,y,z,r,g,b;
void setPos(float _x, float _y, float _z);
void setColor(float _r, float _g, float _b);
}
...
void Display()
{
unsigned short indexes[3] = {0,1,2};
AAA vertexes = new AAA[3]
vertexes[0].setPos(0,0,0);
vertexes[0].setColor(0,0,1);
//... initializing vertexes[1] and [2]
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertexes)*size of(AAA),&vertexes[0],GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof (AAA),BUFFER_OFFSET(0));
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof (AAA),BUFFER_OFFSET(3*sizeof(float)));

glGenBuffers(1,&VIO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VIO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(unsign ed short)*3,vertexes,GL_STATIC_DRAW);
glDrawArrayElements(GL_TRIANGLES,3,GL_UNSIGNED_SHO RT,BUFFER_OFFSET(0));

I forgot to define some variables there, but you get the jist of it. My question is: won't the SetColor() function mess up the object's size? Do vertex-objects (defining position, color, texture, etc) need to be pure data?

Alfonse Reinheart
01-25-2011, 10:33 AM
My question is: won't the SetColor() function mess up the object's size? Do vertex-objects (defining position, color, texture, etc) need to be pure data?

... There are two ways to answer that question.

According to the Standard for Programming Language C++, doing what you are doing is not guaranteed to work as you expect it to. The next version of the C++ standard will allow what you're doing to work because...

According to pretty much every actual C++ compiler on the planet, what you're doing will be just fine. As long as you don't have inheritance or virtual functions (and all member variables have the same protection class), a class/struct with members will work exactly like a class/struct without members.

So take from that what you will. Personally, I've never been a big fan of defining vertex formats with structs. But mostly that's because I load vertex data from files which define their vertex attributes locally.

Wasabi
01-27-2011, 10:10 AM
Indeed, at least with VC++ 2008 Express, compiling classes with members works just fine. I'd seen that it compiled before, but thought that perhaps there was some problems that might appear later. It's good to know you can't have inheritance and different variable protections.