gpt4 book ai didi

c++ - gluTessCallback 指向回调函数的指针。编译错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:35:35 25 4
gpt4 key购买 nike

我有一个名为 Polygon 的类,我在其中尝试渲染一个镶嵌多边形。

我在这个类中有一个方法,称为 render,还有几个方法来绘制我的多边形。

其中包括:

public class Polygon : public Entity
{
protected:
...
public:
bool render();
GLdouble* NewVector(GLdouble x, GLdouble y);
void CALLBACK BeginCallback(GLenum type);
void CALLBACK EndCallback();
void CALLBACK VertexCallback(GLvoid *vertex);
void CALLBACK CombineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat weight[4], GLdouble **dataOut);
void CALLBACK ErrorCallback(GLenum errorCode);
};

在我的方法 render 中,我正在尝试为 BeginCallbackEndCallbackgluTessCallback 注册回调等等……

gluTessCallback(tess, GLU_TESS_BEGIN, (void(*)())&this->BeginCallback);

但这给我一个编译错误,

error C2276: '&' : illegal operation on bound member function expression

我需要成为这个方法(来自当前实例),因为我需要在其中创建和释放一些实例值。

谁能帮帮我?

最佳答案

With GLU_TESS_*_DATA, the user-specified data may be used for "per-polygon" data.

使用私有(private)静态成员函数作为回调并通过user_data 传入您的实例指针.

在每个回调中你可以static_cast<> user_data指向您的 Polygon 的指针根据需要键入和操作结果实例。

像这样:

class Poly
{
public:
Poly( vector< dvec3 >& poly )
{
GLUtesselator* tess = gluNewTess();
gluTessCallback( tess, GLU_TESS_BEGIN, (GLvoid (CALLBACK *)()) Poly::begin );
gluTessCallback( tess, GLU_TESS_EDGE_FLAG, (GLvoid (CALLBACK *)()) Poly::edgeFlag );
gluTessCallback( tess, GLU_TESS_END, (GLvoid (CALLBACK *)()) Poly::end );
gluTessCallback( tess, GLU_TESS_VERTEX_DATA, (GLvoid (CALLBACK *)()) Poly::vertexData );
gluTessCallback( tess, GLU_TESS_COMBINE_DATA, (GLvoid (CALLBACK *)()) Poly::combineData );
gluTessNormal( tess, 0.0, 0.0, 1.0 );

gluTessBeginPolygon( tess, (void*)this );
gluTessBeginContour( tess );

for( size_t i = 0; i < poly.size(); ++i )
{
gluTessVertex( tess, &poly[i][0], &poly[i] );
}

gluTessEndContour( tess );
gluTessEndPolygon( tess );

gluDeleteTess(tess);

for( size_t i = 0; i < mCombined.size(); ++i )
{
delete mCombined[i];
}
mCombined.clear();
}

void Render()
{
glBegin( GL_TRIANGLES );
for( size_t i = 0; i < mTriangles.size(); ++i )
{
glVertex3dv( &mTriangles[i][0] );
}
glEnd();
}

private:
static void begin( GLenum type ) {}
static void edgeFlag( GLboolean flag ) {}
static void end() {}

static void vertexData( void *vertex_data, void *polygon_data )
{
Poly& p = *static_cast< Poly* >( polygon_data );
dvec3& pt = *static_cast< dvec3* >( vertex_data );
p.mTriangles.push_back( pt );
}

static void combineData( GLdouble coords[3], void *vertex_data[4], GLfloat weight[4], void **outData, void *polygon_data )
{
Poly& p = *static_cast< Poly* >( polygon_data );

dvec3* newVert = new dvec3();
(*newVert)[0] = coords[0];
(*newVert)[1] = coords[1];
(*newVert)[2] = coords[2];

p.mCombined.push_back( newVert );
*outData = newVert;
}

vector< dvec3 > mTriangles;
vector< dvec3* > mCombined;
};

关于c++ - gluTessCallback 指向回调函数的指针。编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23614356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com