如何将opengl形状堆叠到std :: vector中

时间:2012-01-08 23:08:06

标签: c++ opengl stl

如何将这两个opengl形状叠加到容器类中? 有人可以告诉我 我不介意模板。

void myTriangle()
 {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity();                                   
glTranslatef(-1.5f,0.0f,-6.0f);                     
glBegin(GL_TRIANGLES);                      
    glColor3f(1.0f,0.0f,0.0f);                      
    glVertex3f( 0.0f, 1.0f, 0.0f);                  
    glColor3f(0.0f,1.0f,0.0f);                      
    glVertex3f(-1.0f,-1.0f, 0.0f);                  
    glColor3f(0.0f,0.0f,1.0f);                      
    glVertex3f( 1.0f,-1.0f, 0.0f);                  
glEnd();
glutSwapBuffers ( );
  }

void myQuad()
 {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
glColor3f(0.5f,0.5f,1.0f);                      
    glBegin(GL_QUADS);                              
      glVertex3f(-1.0f, 1.0f, 0.0f);                    
      glVertex3f( 1.0f, 1.0f, 0.0f);                    
      glVertex3f( 1.0f,-1.0f, 0.0f);                    
      glVertex3f(-1.0f,-1.0f, 0.0f);                    
    glEnd();
glutSwapBuffers ( );
     }

3 个答案:

答案 0 :(得分:2)

不确定你想做什么,但你可以这样做:

typedef void (*ShapeFct_t)();
typedef std::vector<ShapeFct_t> ShapeArray_t;

ShapeArray_t shapes;
shapes.push_back(myTriangle);
shapes.push_back(myQuad);

答案 1 :(得分:1)

您可以定义Shape基类,并使QuadTriangle从该类继承,并使用重载draw函数。这是简化不完整 示例

/* Define these; use tuples, your own class, 3rd party, whatever */
class Position;
class Color;

/* Shape class is abstract base class */
class Shape {
public:
    Shape(const Position &pos) : position(pos) {}
    virtual void draw() = 0;
protected:
    Position position;
};

/* Triangle and Quad classes are Shapes */
class Triangle : public Shape {
public:
    Triangle(const Position &pos,
             const Position &p1, const Color &c1, 
             const Position &p2, const Color &c2, 
             const Position &p3, const Color &c3)
        : Shape(pos),
          pos1(p1), col1(c1),
          pos2(p2), col2(c2),
          pos3(p3), col3(c3)
    {}

    virtual void draw() {
        glTranslatef(position.x, position.y, position.z);
        glBegin(GL_TRIANGLES);                      
        glColor3f(col1.r, col1.g, col1.b);                      
        glVertex3f(pos1.x, pos1.y, pos1.z);              
        glColor3f(col2.r, col2.g, col2.b);                      
        glVertex3f(pos2.x, pos2.y, pos2.z);             
        glColor3f(col3.r, col3.g, col3.b);                      
        glVertex3f(pos3.x, pos3.y, pos3.z);                    
        glEnd();
    }
private:
    Position pos1, pos2, pos3;
    Color col1, col2, col3;
};

class Quad : public Shape {
    /* Your implementation here */
};

void draw_all_shapes() {

    std::vector<Shape*> shapes;
    shapes.push_back(new Triangle(Position(-1.5f, 0f, -6f),
        Position(0f, 1f, 0f), Color(1f, 0f, 0f),
        Position(-1f, -1f, 0f), Color(0f, 1f, 0f),
        Position(0f, 0f, 1f),  Color(0f, 0f, 1f)));

    shapes.push_back(new Quad(/* Arguments */));

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();  
    for(auto it = shapes.begin(); it != shapes.end(); it++) {
        (*it)->draw();
    }
    glutSwapBuffers();
}

注意:正如我所说,这个例子不完整。它也没有经过测试,有点乱(内存泄漏等)但这些问题超出了这个问题的范围。我试图在这里演示的主要观点是,您可以将函数转换为类,这可以利用继承和多态来简化代码。

答案 2 :(得分:1)

您不确定在创建形状后是否需要编辑形状,但是您可以将其放在显示列表中,然后将每个显示列表存储在您喜欢的任何容器中:

GLuint triangleList = glGenLists(1);
glNewList(triangleList, GL_COMPILE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity();                                   
glTranslatef(-1.5f,0.0f,-6.0f);                     
glBegin(GL_TRIANGLES);                      
    glColor3f(1.0f,0.0f,0.0f);                      
    glVertex3f( 0.0f, 1.0f, 0.0f);                  
    glColor3f(0.0f,1.0f,0.0f);                      
    glVertex3f(-1.0f,-1.0f, 0.0f);                  
    glColor3f(0.0f,0.0f,1.0f);                      
    glVertex3f( 1.0f,-1.0f, 0.0f);                  
glEnd();
glEndList();

GLuint quadList = glGenList(1);
glNewList(quadList, GL_COMPILE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
glColor3f(0.5f,0.5f,1.0f);                      
    glBegin(GL_QUADS);                              
      glVertex3f(-1.0f, 1.0f, 0.0f);                    
      glVertex3f( 1.0f, 1.0f, 0.0f);                    
      glVertex3f( 1.0f,-1.0f, 0.0f);                    
      glVertex3f(-1.0f,-1.0f, 0.0f);                    
    glEnd();
glEndList();

此时,您可以将quadList和triangleList存储在您喜欢的任何容器中。您可以使用glCallList()函数告诉OpenGL显示它们。要删除列表,请致电glDeleteList()