动态生成 - 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];

我假设相机设置和其他一切都是正确的,因为场景显示填充为中心矩形与大小创建的对象......

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

试试这个:

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];

相关内容

  • 没有找到相关文章

最新更新