|
楼主 |
发表于 2013-6-24 11:27:36
|
显示全部楼层
本帖最后由 hunter_wwq 于 2013-6-24 11:31 编辑
刚刚试了下设置颜色数组的方式来为立方体每个面定义一种颜色,这其中颜色对顶点的修饰规则我大致总结了下,以后像我这样的菜鸟们可以参考一下,前辈们看到有总结的不对的地方还希望能指点出来啊
例举颜色修饰顶点的规则:(以绘制一个六个面均为不同颜色的立方体为例)
首先分析下立方体的构造:立方体有8个顶点、六个面,每个面有4个顶点,若以osg:: PrimitiveSet:: QUADS进行绘制,则会以4个顶点为一组进行绘制,所以 绘制几何体有两种方案:第一种,顶点数组添加4*6个顶点,依次以4个顶点为一组定义一个平面;第二种,顶点数组添加8个顶点,然后设置顶点索引,顶点索引是从顶点数组中选出顶点进行组合,也是以4个顶点为一组来定义一个平面。第一种方案的代码:
- // 设置顶点
- ref_ptr<Vec3Array> vertextArr = new Vec3Array;
- // 前
- vertextArr->push_back(Vec3(0.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 0.0f, 1.0f));
- // 左
- vertextArr->push_back(Vec3(0.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 0.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 0.0f, 1.0f));
- // 底
- vertextArr->push_back(Vec3(0.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 1.0f, 0.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 0.0f));
- // 顶
- vertextArr->push_back(Vec3(1.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 0.0f, 1.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 1.0f));
- // 右
- vertextArr->push_back(Vec3(1.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 1.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 1.0f, 0.0f));
- // 后
- vertextArr->push_back(Vec3(1.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 1.0f, 0.0f));
- geometry->setVertexArray(vertextArr.get());
复制代码 第二种方案的代码:
- // 设置顶点
- ref_ptr<Vec3Array> vertextArr = new Vec3Array;
- vertextArr->push_back(Vec3(0.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 0.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 0.0f, 1.0f));
- vertextArr->push_back(Vec3(1.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 1.0f));
- vertextArr->push_back(Vec3(0.0f, 1.0f, 0.0f));
- vertextArr->push_back(Vec3(1.0f, 1.0f, 0.0f));
- geometry->setVertexArray(vertextArr.get());
- ref_ptr<DrawElementsUInt> vertexIndex = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
- vertexIndex->push_back(0);
- vertexIndex->push_back(1);
- vertexIndex->push_back(2);
- vertexIndex->push_back(3);
- vertexIndex->push_back(0);
- vertexIndex->push_back(1);
- vertexIndex->push_back(7);
- vertexIndex->push_back(6);
- vertexIndex->push_back(0);
- vertexIndex->push_back(6);
- vertexIndex->push_back(5);
- vertexIndex->push_back(3);
- vertexIndex->push_back(4);
- vertexIndex->push_back(5);
- vertexIndex->push_back(3);
- vertexIndex->push_back(2);
- vertexIndex->push_back(4);
- vertexIndex->push_back(5);
- vertexIndex->push_back(6);
- vertexIndex->push_back(7);
- vertexIndex->push_back(4);
- vertexIndex->push_back(2);
- vertexIndex->push_back(1);
- vertexIndex->push_back(7);
- geometry->addPrimitiveSet(vertexIndex.get());
复制代码 接下来为每个面定义颜色,因为一个面均为一种填充颜色,所以一个面上的四个顶点均为同一种颜色,每个顶点都得定义一种颜色,一个立方体有6个面,则相当于要定义6*4个颜色值,而颜色值只是针对顶点数组中的顶点值去一一对应,并不会与设置的顶点索引去挂钩,所以在绘制立方体时若采用第二种方案,是没有办法为每个面第一种不同的颜色的(一个面只有一种填充色)。所以若要给立方体每个面定义一种不同的颜色时,在构造立方体就只能采用第一种方案。现在设置颜色也有两种方案,第一种是设置仅颜色数组,设置24个,对应上每个顶点;第二种是先设置颜色数组,设置6个,然后设置颜色索引,定义24个,其中每个平面上的四个点的颜色值应该一致。这里只列举第二种方案的代码:
- // 设置颜色
- ref_ptr<Vec4Array> colorArr = new Vec4Array;
- colorArr->push_back(m_mccWhite);
- colorArr->push_back(m_mccYellow);
- colorArr->push_back(m_mccBlue);
- colorArr->push_back(m_mccGreen);
- colorArr->push_back(m_mccRed);
- colorArr->push_back(m_mccOrange);
- geometry->setColorArray(colorArr.get());
- ref_ptr<TemplateIndexArray<unsigned int, Array::Vec3ArrayType, 4, 4>> colorIndex = new TemplateIndexArray<unsigned int, Array::Vec3ArrayType, 4, 4>;
- for (int i = 0; i < 6; i++)
- {
- colorIndex->push_back(i);
- colorIndex->push_back(i);
- colorIndex->push_back(i);
- colorIndex->push_back(i);
- }
- geometry->setColorIndices(colorIndex.get());
- geometry->setColorBinding(Geometry::BIND_PER_VERTEX);
复制代码 |
|