1

我有这行代码:

boneHierarchy = BoneHierarchy(Point3D(24.5f,
                                       84.0f + HumanBoundingBox::HEIGHT/2.0f ,24.5f
                                      ),
                              CompassRadians(0.0f),
                              renderingEngine
                             );

BoneHierarchy 有一些向量作为私有成员。我获取向量的地址并将它们传递给渲染类。但是当我调用向量的 size 成员函数时,我得到了垃圾。我正在获取向量的地址,而不是其中的元素。

BoneHierarchy::BoneHierarchy(Point3D const& position,
                             CompassRadians const& heading,
                             Renderer* renderingEngine) : counter(0), renderingEngine(renderingEngine)
{    




    CoordinateSystem bodyCoordSys = CoordinateSystem(
                                                     Vector3D(0.0f,1.0f,0.0f),
                                                     Vector3D(0.0f,0.0f,1.0f));


    map<Normal3D::enumeration, TextureCoordinates> cubeTextureKind;
    Normal3D::enumeration normal;
    for (normal = Normal3D::begin(); normal != Normal3D::end(); ++normal)
    {
        cubeTextureKind[normal] = TextureAtlas::textureAtlasCoordLookup(WOOD);
    }
    Bone body = Bone(
                     Vector3D(0.0f, HumanBoundingBox::HEIGHT/2.0f,0.0f),
                     HumanBoundingBox::DIM,
                     Euler3D(0.0f,0.0f,0.0f),
                     cubeTextureKind,
                     bodyCoordSys);
    //TODO there are two positions for the human, one here and one in mesh vbo, unify these into one.
    body.setPosition(Vector3D(position.getX(),position.getY(),position.getZ()));
    body.setEuler(Euler3D((float) (Math::toDegrees(heading.getValue())),0.0f,0.0f));
    drawBone(body,UPDATE_ALL);
    root = body;


    map<string,Renderer::Buffer> vboVariableMap;
    vboVariableMap["aVertexPosition"] = Renderer::Buffer(verticesBuf);
    vboVariableMap["aTextureCoord"] = Renderer::Buffer(texCoordsBuf);
    vboVariableMap["index"] = Renderer::Buffer(indicesBuf);
    cout << "aBoneIndex " << boneIndexBuf.size() << endl;
    vboVariableMap["aBoneIndex"] = Renderer::Buffer(boneIndexBuf);
    vboVariableMap["uBoneMatrix0"] = Renderer::Buffer(transformMatrixBuf);
    GLuint textureID = 0; 
    Renderer::VBODescription vboDescription = Renderer::VBODescription("boneShader",
                                                                       vboVariableMap,
                                                                       GL_DYNAMIC_DRAW,
                                                                       indicesBuf.size(),
                                                                       textureID);
    bufferID = renderingEngine->registerBuffer(vboDescription);
    cout << "bufferID " << bufferID << endl;
    renderingEngine->printBoneIndexSize();
    cout << &(getBoneIndexBuf()) << endl;
}


Renderer::Buffer::Buffer(vector<GLfloat> const& data):
floatData(&data), ushortData(NULL), target(GL_ARRAY_BUFFER)
{
    printf("pointer: %p, size: %lu\n", floatData, floatData->size());
}

Renderer::Buffer::Buffer(vector<GLushort> const& data):
floatData(NULL), ushortData(&data), target(GL_ELEMENT_ARRAY_BUFFER)
{
}
4

3 回答 3

2

如果BoneHierarchy接受 aPoint3D和 a的构造函数CompassRadians计算它自己的向量成员的地址,那很好。他们的地址不会改变。

The code also shows an assignment statement. The addresses of the vector members of the temporary BoneHierarchy on the right will be different from those of the BoneHierarchy object stored in boneHierarchy on the left. Make sure you're not copying the addresses in your assignment operator, or else the boneHierarchy object will end up referring to the vector members of a temporary object that doesn't exist anymore. You'll also want to check your copy constructor.

于 2012-04-18T21:29:22.447 回答
0

他们的地址不会改变。此外,如果你不在初始化列表中,它们的内容应该在整个构造过程中都是有效的。

于 2012-04-18T21:29:15.863 回答
0

There is not enough code provided here. Assuming that you only wanted to create an instance, use this line of code instead:

 BoneHierarchy boneHierarchy(Point3D(24.5f, 84.0f + HumanBoundingBox::HEIGHT/2.0f ,24.5f), 
 CompassRadians(0.0f), renderingEngine);

This would create an instance in the local stack. As rob pointed out, you must be invoking the assignment operator to BoneHierarchy.

于 2012-04-18T21:33:45.990 回答