有没有办法用变量表示glVertex3f?

时间:2014-09-30 19:43:03

标签: opengl graphics types

有没有办法用变量表示glVertex3f? 例如

one = glVertex3f(xL, yB, zF);

如果是这样,那会是什么类型? 此外,您可以使用创建的变量构建多边形吗? 例如:

glBegin(GL_POLYGON);
       one;
       glVertex3f(xR-45, yB, zF);
       glVertex3f(xR-45, yB, zB+55);
       glVertex3f(xL, yB, zB+55);

glEnd();

2 个答案:

答案 0 :(得分:2)

对于C来说,三十二上校已经在对OP的评论中回答了这个问题 如果需要C ++,这是一种利用强制的好方法。
Point3D类

class Pt3d
{
public:
    // omitting constructors/setters/getters etc
    //
    //
    // ____________________________
    // automatic coercion: 
    // wherever a float* is needed for coordinates, this will kick-in
    operator float* ()const  {  return (float *)m_co; }
private:
        float m_co[3];
};

<强>用法

Pt3D p1(/*set coords here*/); 
Pt3D p2(/*set coords here*/);
glBegin(GL_POLYGON);
    glVertex3fv(p1); 
    glVertex3fv(p2); 
    // so on
glEnd();

答案 1 :(得分:2)

在C ++中,您可以使用如下类来封装顶点:

class Vertex3f {
public:
    Vertex3f(GLfloat x, GLfloat y, GLfloat z)
        : m_x(x), m_y(y), m_z(z) {
    }

    void apply() const {
        glVertex3f(m_x, m_y, m_z);
    }

private:
    GLfloat m_x, m_y, m_z;
};

按照问题中的示例,您将实例化此类型的对象:

Vertex3f one(xL, yB, zF);

然后像这样应用:

glBegin(GL_POLYGON);
one.apply();
...

您可以重载函数调用运算符,而不是使用apply()方法。我认为在这种情况下使用显式方法可以使代码更容易理解,但这是个人偏好的问题。