VTK属性:环境光照效果

60 篇文章 ¥59.90 ¥99.00
本文介绍了如何在VTK中使用环境光照技术,通过设置vtkProperty对象的AmbientColor属性来实现整体光照效果。提供了一个源代码示例,展示如何创建一个带有灰色环境光的球体,并说明如何调整颜色以实现不同光照效果。利用VTK的环境光属性,可以增强可视化场景的真实感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境光照是一种在计算机图形学中常用的技术,用于模拟场景中的整体光照效果。在Visualization Toolkit(VTK)中,我们可以通过设置环境光属性来实现这种效果。本文将详细介绍如何在VTK中使用环境光照,并提供相应的源代码示例。

在VTK中,环境光照是通过设置vtkProperty对象的AmbientColor属性来实现的。AmbientColor属性定义了场景中的环境光颜色。环境光照会均匀地照亮场景中的所有对象,为它们提供一种整体的光照效果。

下面是一个使用VTK实现环境光照效果的示例代码:

import vtk

# 创建一个球体
sphereSource = vtk.vtkSphereSource()
sphereSource.Update()

# 创建一个球体的Mapper
mapper <
/*========================================================================= Program: Visualization Toolkit Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include <vtkActor.h> #include <vtkCamera.h> #include <vtkInteractorStyleTrackballCamera.h> #include <vtkJPEGReader.h> #include <vtkLight.h> #include <vtkNew.h> #include <vtkOpenGLPolyDataMapper.h> #include <vtkPolyDataNormals.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkSkybox.h> #include <vtkTexture.h> #include <vtkVersion.h> #include <vtkXMLPolyDataReader.h> #if VTK_VERSION_NUMBER >= 89000000000ULL #define USE_SHADER_PROPERTIES 1 #include <vtkShaderProperty.h> #endif //---------------------------------------------------------------------------- int main(int argc, char* argv[]) { if (argc < 3) { std::cout << "Usage: " << argv[0] << " file.vtp wintersun.jpg" << std::endl; return EXIT_SUCCESS; } vtkNew<vtkRenderer> renderer; renderer->SetBackground(0.0, 0.0, 0.0); vtkNew<vtkRenderWindow> renderWindow; renderWindow->SetSize(640, 480); renderWindow->AddRenderer(renderer); vtkNew<vtkRenderWindowInteractor> interactor; interactor->SetRenderWindow(renderWindow); vtkNew<vtkLight> light; light->SetLightTypeToSceneLight(); light->SetPosition(1.0, 7.0, 1.0); renderer->AddLight(light); vtkNew<vtkXMLPolyDataReader> reader; reader->SetFileName(argv[1]); vtkNew<vtkPolyDataNormals> norms; norms->SetInputConnection(reader->GetOutputPort()); vtkNew<vtkTexture> texture; texture->InterpolateOn(); // mipmapping works on many systems but is not // core 3.2 for cube maps. VTK will silently // ignore it if it is not supported. We commented it // out here to make valid images easier // texture->MipmapOn(); vtkNew<vtkJPEGReader> imgReader; imgReader->SetFileName(argv[2]); texture->SetInputConnection(imgReader->GetOutputPort()); vtkNew<vtkOpenGLPolyDataMapper> mapper; mapper->SetInputConnection(norms->GetOutputPort()); vtkNew<vtkActor> actor; actor->SetPosition(0, 0, 0); actor->SetScale(6.0, 6.0, 6.0); actor->GetProperty()->SetSpecular(0.8); actor->GetProperty()->SetSpecularPower(20); actor->GetProperty()->SetDiffuse(0.1); actor->GetProperty()->SetAmbient(0.1); actor->GetProperty()->SetDiffuseColor(1.0, 0.0, 0.4); actor->GetProperty()->SetAmbientColor(0.4, 0.0, 1.0); renderer->AddActor(actor); actor->SetTexture(texture); actor->SetMapper(mapper); #if USE_SHADER_PROPERTIES vtkShaderProperty* sp = actor->GetShaderProperty(); sp->AddVertexShaderReplacement( "//VTK::PositionVC::Dec", // replace true, // before the standard replacements "//VTK::PositionVC::Dec\n" // we still want the default "out vec3 TexCoords;\n", false // only do it once ); #else mapper->AddShaderReplacement( vtkShader::Vertex, "//VTK::PositionVC::Dec", // replace true, // before the standard replacements "//VTK::PositionVC::Dec\n" // we still want the default "out vec3 TexCoords;\n", false // only do it once ); #endif #if USE_SHADER_PROPERTIES sp->AddVertexShaderReplacement( "//VTK::PositionVC::Impl", // replace true, // before the standard replacements "//VTK::PositionVC::Impl\n" // we still want the default "vec3 camPos = -MCVCMatrix[3].xyz * mat3(MCVCMatrix);\n" "TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n", false // only do it once ); #else mapper->AddShaderReplacement( vtkShader::Vertex, "//VTK::PositionVC::Impl", // replace true, // before the standard replacements "//VTK::PositionVC::Impl\n" // we still want the default "vec3 camPos = -MCVCMatrix[3].xyz * mat3(MCVCMatrix);\n" "TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n", false // only do it once ); #endif #if USE_SHADER_PROPERTIES sp->AddFragmentShaderReplacement( "//VTK::Light::Dec", // replace true, // before the standard replacements "//VTK::Light::Dec\n" // we still want the default "in vec3 TexCoords;\n", false // only do it once ); #else mapper->AddShaderReplacement( vtkShader::Fragment, "//VTK::Light::Dec", // replace true, // before the standard replacements "//VTK::Light::Dec\n" // we still want the default "in vec3 TexCoords;\n", false // only do it once ); #endif #if USE_SHADER_PROPERTIES sp->AddFragmentShaderReplacement( "//VTK::Light::Impl", // replace true, // before the standard replacements "//VTK::Light::Impl\n" " float phix = length(vec2(TexCoords.x, TexCoords.z));\n" " vec3 skyColor = texture(actortexture, vec2(0.5*atan(TexCoords.z, " "TexCoords.x)/3.1415927 + 0.5, atan(TexCoords.y,phix)/3.1415927 + " "0.5)).xyz;\n" " gl_FragData[0] = vec4(ambientColor + diffuse + specular + " "specularColor*skyColor, opacity);\n", // we still want the default false // only do it once ); #else mapper->AddShaderReplacement( vtkShader::Fragment, "//VTK::Light::Impl", // replace true, // before the standard replacements "//VTK::Light::Impl\n" " float phix = length(vec2(TexCoords.x, TexCoords.z));\n" " vec3 skyColor = texture(actortexture, vec2(0.5*atan(TexCoords.z, " "TexCoords.x)/3.1415927 + 0.5, atan(TexCoords.y,phix)/3.1415927 + " "0.5)).xyz;\n" " gl_FragData[0] = vec4(ambientColor + diffuse + specular + " "specularColor*skyColor, opacity);\n", // we still want the default false // only do it once ); #endif vtkNew<vtkSkybox> world; world->SetProjectionToSphere(); world->SetTexture(texture); renderer->AddActor(world); renderer->GetActiveCamera()->SetPosition(0.0, 0.55, 2.0); renderer->GetActiveCamera()->SetFocalPoint(0.0, 0.55, 0.0); renderer->GetActiveCamera()->SetViewAngle(60.0); renderer->ResetCamera(); renderer->GetActiveCamera()->Zoom(1.1); renderer->GetActiveCamera()->Azimuth(0); renderer->GetActiveCamera()->Elevation(5); renderer->ResetCameraClippingRange(); renderWindow->Render(); renderWindow->SetWindowName("SphereMap"); renderWindow->Render(); vtkNew<vtkInteractorStyleTrackballCamera> style; renderWindow->GetInteractor()->SetInteractorStyle(style); interactor->Start(); return EXIT_SUCCESS; } 详细解析下这个代码
最新发布
07-05
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值