2021-1-19 activiz立方体(各面不同颜色)并在面上贴图

建立polydata

创建cube和上一个博文中创建虚线的方法差不多。把cell从2换成4
vtkLookupTable的SetTableValue定义颜色表,分别是映射数值和颜色RGB值。

double[][] x = new double[][]{new double[]{0,0,0},new double[]{5,0,0},new double[]{5,5,0},new double[]{0,5,0},new double[]{0,0,5},new double[]{5,0,5},new double[]{5,5,5},new double[]{0,5,5}};
int[][] pts = new int[][]{ new int[] { 0, 1, 2, 3 }, new int[] { 4, 5, 6, 7 }, new int[] { 0, 1, 5, 4 },
new int[] { 1, 2, 6, 5 }, new int[]{ 2, 3, 7, 6 }, new int[]{ 3, 0, 4, 7 } };

vtkPolyData cube = new vtkPolyData();
vtkCellArray polys = new vtkCellArray();
vtkFloatArray scalars = new vtkFloatArray();
vtkPoints points = new vtkPoints();

for (int i = 0; i < 8; i++)
{
	points.InsertPoint(i, x[i][0], x[i][1], x[i][2]);
}

for (int i = 0; i < 6; i++)
{
	polys.InsertNextCell(4);
	for (int j = 0; j < 4; j++)
    {
    	polys.InsertCellPoint(pts[i][j]);
    }
}
   //定义6个面颜色
scalars.InsertTuple1(0, 0);
scalars.InsertTuple1(1, 0);
scalars.InsertTuple1(2, 0);
scalars.InsertTuple1(3, 5);
scalars.InsertTuple1(4, 5);
scalars.InsertTuple1(5, 5);
            
cube.SetPoints(points);
cube.SetPolys(polys);
cube.GetCellData().SetScalars(scalars);

vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New();
cubeMapper.SetInput(cube);

//上面是颜色手动定义6种
vtkLookupTable pColorTable = vtkLookupTable.New();
pColorTable.SetNumberOfColors(6);
pColorTable.SetTableValue(0, 1.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(1, 0.0, 1.0, 1.0, 1.0);
pColorTable.SetTableValue(2, 1.0, 1.0, 1.0, 1.0);
pColorTable.SetTableValue(3, 1.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(4, 0.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(5, 1.0, 1.0, 0.0, 1.0);

pColorTable.Build();
cubeMapper.SetScalarRange(0, 6);

vtkActor cubeActor = new vtkActor();
cubeActor.SetMapper(cubeMapper);

效果如下:设置了三个面红色三个面蓝色
在这里插入图片描述

在立方体一个面上贴图:

新建了一个平面,然后利用vtkTextureMapToPlane将图片映射上去,通过平移旋转缩放使其在一个面上。文字类图像还需要考虑正反面的问题,因此在这里进行了翻转。

vtkActor planeActor = vtkActor.New();
vtkPlaneSource planeSource = vtkPlaneSource.New();//根据点法式创建
planeSource.SetCenter(0, 0, 6);
planeSource.SetPoint1(5, 0, 6);
planeSource.SetPoint2(0, 5, 6);
planeSource.SetXResolution(1);
planeSource.SetYResolution(1);
planeSource.SetNormal(0, 0, 1);
planeSource.Update();

vtkTextureMapToPlane texturePlane = vtkTextureMapToPlane.New();
texturePlane.SetInputConnection(planeSource.GetOutputPort());
vtkTexture texture = vtkTexture.New();
texture.InterpolateOn();
vtkPolyDataMapper polydatamapper = vtkPolyDataMapper.New();
polydatamapper.SetInputConnection(texturePlane.GetOutputPort());
vtkPNGReader read = vtkPNGReader.New();
read.SetFileName("C:\\Users\\Administrator\\Desktop\\2020.12PMWS项目\\12.9\\3D-Real-Time-Pilling-Monitoring-System\\PMWS\\PMWS\\PMWS\\bin\\Debug\\打桩船1.png");
texture.SetInput(read.GetOutput());
planeActor.SetMapper(polydatamapper);
planeActor.SetTexture(texture);

//这个是进行了平面的旋转平移 这个写法主要是为了方便整体移动
renderer.AddActor(planeActor);
double[] a0 = new double[] { 0, 5 };
double[] a1 = new double[] { 0, 0 };
double[] a2 = new double[] { 5, 0 };
double[] a3 = new double[] { 5, 5 };
double[] center = new double[] { (a0[0]+ a1[0]+ a2[0]+ a3[0])/4,
                (a0[1] + a1[1] + a2[1] + a3[1]) / 4 };
//找到旋转轴
planeActor.SetOrigin(center[0], center[1], 6);
planeActor.RotateWXYZ(180, 0, 5, 0);
planeActor.SetScale(0.8, 0.8, 1);

效果如图所示:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要画出六面不同颜色立方体,可以使用OpenGL库中的函数来绘制。下面是一个简单的C程序,使用OpenGL库来绘制六面不同颜色立方体: ```c #include <GL/glut.h> void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // 绘制立方体的六个面 glBegin(GL_QUADS); // 前面 glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 后面 glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glVertex3f(1.0f, 1.0f, -1.0f); glVertex3f(1.0f, -1.0f, -1.0f); // 上面 glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); // 下面 glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(1.0f, -1.0f, -1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 左面 glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 右面 glColor3f(0.0f, 1.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f); glVertex3f(1.0f, 1.0f, -1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("Cube"); glutDisplayFunc(display); glutMainLoop(); return 0; } ``` 上述程序中,首先用`glClear`函数清除颜色和深度缓冲区。然后开启深度测试,以便正确渲染立方体的六个面。接下来使用`glBegin`和`glEnd`定义立方体的每个面,并使用`glColor`函数来指定每个面的颜色。最后使用`glFlush`函数将图像渲染到屏幕上。 运行上述程序,应该能够看到一个六面不同颜色立方体

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值