Results 1 to 3 of 3

Thread: Drawing C++ classes

  1. #1
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Drawing C++ classes

    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.
    Code :
     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)*sizeof(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(unsigned short)*3,vertexes,GL_STATIC_DRAW);
       glDrawArrayElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,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?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: Drawing C++ classes

    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.

  3. #3
    Member Newbie
    Join Date
    Jan 2011
    Location
    Brazil
    Posts
    44

    Re: Drawing C++ classes

    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.

Similar Threads

  1. can we use Classes?
    By qasim1981 in forum OpenGL ES
    Replies: 1
    Last Post: 11-11-2004, 09:01 AM
  2. OpenGL classes & wrapper classes
    By biot023 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-04-2003, 08:10 AM
  3. Any OpenGL classes
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-19-2001, 10:02 AM
  4. classes
    By dirtmaster in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-15-2001, 05:34 AM
  5. C++ and Classes
    By drakaza in forum OpenGL: Advanced Coding
    Replies: 37
    Last Post: 09-22-2000, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean