cocos3d以编程方式创建网格

时间:2013-01-31 06:22:11

标签: mesh dynamically-generated cocos3d

我正在努力以编程方式创建网格对象。 这非常有效:

CC3MeshNode *pMeshBox = [[CC3MeshNode alloc] init];
[pMeshBox populateAsCenteredRectangleWithSize:CGSizeMake(3, 3) andTessellation:ccg(5, 0)];    // AsSphereWithRadius:1 andTessellation:ccg(5, 0)];     // edges, rounded-corners
[self addChild:pMeshBox];
[pMeshBox release];

但这并没有显示任何东西(我希望有一个高度为0的扁平方块,并在x / z方向产生)。


float fPolygonVertices[] = {
    -3, 0,  3,
     3, 0,  3,
     3, 0, -3,
    -3, 0, -3
};

CC3VertexLocations* pvlPolygon = [CC3VertexLocations vertexArrayWithName: @"PolygonVL"];
pvlPolygon.vertexCount = 4;
pvlPolygon.vertices = fPolygonVertices;

CC3VertexArrayMesh* pvamPolygon = [CC3VertexArrayMesh meshWithName:@"PolygonM"];
pvamPolygon.vertexLocations = pvlPolygon;

CC3MeshNode *pMeshNode = [[CC3MeshNode alloc] init];
pMeshNode.mesh = pvamPolygon;

ccColor3B color = { 50, 0, 200 };
pMeshNode.color = color;

[self addChild:pMeshNode];
[pMeshNode release];

我假设相机设置和其他一切都正确,因为场景显示了populateAsCenteredRectangleWithSize创建的对象......

我尝试了各种颜色和材质设置,但没有运气。

1 个答案:

答案 0 :(得分:1)

试试这个:

GLuint totalVertexCount = 4;
GLuint triangleCount = 2;

CC3MeshNode *pMeshNode = [[CC3MeshNode alloc] init];
//[pMeshNode setPureColor:color];
//[pMeshNode setIsTouchEnabled:YES];

CC3VertexArrayMesh* theArrayMesh = [pMeshNode prepareParametricMesh];
// Prepare the vertex content and allocate space for vertices and indices.
[theArrayMesh ensureVertexContent];
theArrayMesh.allocatedVertexCapacity = totalVertexCount;
theArrayMesh.allocatedVertexIndexCapacity = (triangleCount * 3);
GLushort* indices = theArrayMesh.vertexIndices.vertices;

/*         
 *  1-------0
 *  |      /|   -z 
 *  |     / |    ⥣
 *  |    /  |     =>+x
 *  |   /   |
 *  |  /    |
 *  | /     |
 *  |/      |
 *  2-------3
 */
{
    [theArrayMesh setVertexLocation: cc3v(3, 0, -3) at: 0];
    [theArrayMesh setVertexLocation: cc3v(-3, 0, -3) at: 1];
    [theArrayMesh setVertexLocation: cc3v(-3, 0, 3) at: 2];
    [theArrayMesh setVertexLocation: cc3v(3, 0, 3) at: 3];

}
GLubyte indxIndx = 0;
GLubyte vtxIndx = 0;
for (int side = 0; side < 1; side++) {
    // First trangle of side - CCW from bottom left
    indices[indxIndx++] = vtxIndx++;        // vertex 0
    indices[indxIndx++] = vtxIndx++;        // vertex 1
    indices[indxIndx++] = vtxIndx;          // vertex 2

    // Second triangle of side - CCW from bottom left
    indices[indxIndx++] = vtxIndx++;        // vertex 2
    indices[indxIndx++] = vtxIndx++;        // vertex 3
    indices[indxIndx++] = (vtxIndx - 4);    // vertex 0
}

[self addChild:pMeshNode];