1

我在 cocos3d 中使用 CC3MeshNode 绘制了一个复杂的 3d 形状,但是这个形状不受我在世界上设置的光源(灯)的影响(阴影​​和明亮的地方取决于位置),如果我使用其中一种 populateAs 方法绘制类似球体的东西会受到光源的影响。手动绘制 CC3MeshNode 时应该怎么做,这样它会受到光线的影响。

下面是在 cocos3d 中手动绘制矩形的示例代码

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

    CC3Mesh* 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, -3,0) at: 3];
        [theArrayMesh setVertexLocation: cc3v(-3, -3,0 ) at: 2];
        [theArrayMesh setVertexLocation: cc3v(-3, 3, 0) at: 1];
        [theArrayMesh setVertexLocation: cc3v(3, 3, 0) at: 0];

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

1 回答 1

1

I think the problem is you need to supply vertex normals for your mesh. Normals are required for the lighting calculations to operate correctly. The routines to automatically populate a sphere, etc. are generating and storing vertex normals in the mesh. Desktop OpenGL will generate normals for a mesh without them when you use glEnable(GL_AUTO_NORMAL), but this functionality doesn't exist in mobile, at least not in OpenGL ES 2.0.

于 2013-06-20T21:46:47.683 回答