当前位置: 首页 > news >正文

7、基于osg引擎实现读取vtk数据通过着色器实现简单体渲染(1)

1、顶点着色器代码

#version 110
/* GLSL 1.10需要显式声明精度 (OpenGL ES要求) */
#ifdef GL_ES
precision highp  float;
#endif
// 体数据采样步长
uniform float xStepSize,yStepSize,zStepSize;
// 体数据纹理和颜色纹理
uniform sampler3D baseTexture;
uniform sampler1D tfTexture;
// 体数据包围盒边界
uniform vec3 minBound,maxBound;
varying vec3 vDirection;//模型空间下的光线方向
varying vec3 vCameraModelPosition;//模型空间下的相机位置
void main(void)
{
    // 计算裁剪空间的位置
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    vec4 cameraPos = (gl_ModelViewMatrixInverse * vec4(0, 0, 0, 1));
    vCameraModelPosition = cameraPos.xyz/cameraPos.w;
    vDirection = gl_Vertex.xyz/gl_Vertex.w- vCameraModelPosition;
    
}

2、片元着色器代码

#version 110
/* GLSL 1.10需要显式声明精度 (OpenGL ES要求) */
#ifdef GL_ES
precision highp  float;
precision highp sampler3D;
precision highp sampler1D;
#endif
#define EPSILON 1e-3

// 体数据纹理和颜色纹理
uniform sampler3D baseTexture;
uniform sampler1D tfTexture;

uniform int steps;//采样步长,值越大,采样次数越多,效果越小,但性能越差
uniform float densityFactor;//值越大,颜色变化剧烈,细节边界变得锐利,可能出现不平滑的锯齿状过渡;值越小,颜色变化缓慢,叠加的颜色较多,导致整体视觉上更加柔和、模糊,增强了雾气感
// 体数据包围盒边界
uniform vec3 minBound,maxBound;

varying vec3 vDirection;//模型空间下的光线方向
varying vec3 vCameraModelPosition;//模型空间下的相机位置

const int maxSamples = 256;
vec2 hitBox(vec3 orig, vec3 dir) {
	vec3 inv_dir = 1.0 / dir;// 光线方向倒数(处理正负方向)
	vec3 tmin_tmp = (minBound - orig) * inv_dir;// 沿光线方向到达包围盒各轴最小边界的距离
	vec3 tmax_tmp = (maxBound - orig) * inv_dir; // 沿光线方向到达包围盒各轴最大边界的距离
	vec3 tmin = min(tmin_tmp, tmax_tmp);// 各轴向的最近交点
	vec3 tmax = max(tmin_tmp, tmax_tmp);// 各轴向的最远交点
	float near = max(max(tmin.x, max(tmin.y, tmin.z)),0.0);// 光线最终进入包围盒的距离
	float far = min(tmax.x, min( tmax.y, tmax.z)); // 光线最终离开包围盒的距离
	return vec2( near, far );
}

void main(void)
{
    vec3 rayDir = normalize(vDirection);

    vec2 bounds = hitBox( vCameraModelPosition, rayDir );
    bounds.x -= 0.000001;
	if ( bounds.x >= bounds.y) discard;//光线与包围盒无交点。

	vec3 sampleStart = vCameraModelPosition + bounds.x * rayDir;
        
    // 初始化颜色累积
    vec4 finalColor = vec4(0.0);
    const float opacityThreshold = 0.99;   // 不透明度阈值

    float T = 1.0;
    // 光线步进采样
    float delta = sqrt(3.0) / float(steps);             // 均匀步长
    vec3 voxelSizeInv = 1.0 / (maxBound - minBound); 
    for (float t = bounds.x; t < bounds.y; t += delta) {
        sampleStart = vCameraModelPosition + t * rayDir;
        vec3 texCoord = (sampleStart - minBound) * voxelSizeInv;
        
        float density = texture3D(baseTexture, texCoord).r;
        vec4 color = texture1D(tfTexture, density);
        color.rbg = pow(color.rbg, vec3(2.2));//从gamma空间转换到线性空间
        color *= densityFactor * delta;
        finalColor += T*color;
        T*=1.0-color.a;
        
        if (T<0.01) break;
    }

    // 丢弃完全透明的片元
    if (finalColor.a < EPSILON) discard;
    finalColor.rgb = pow(finalColor.rgb, vec3(1.0/2.2));
    gl_FragColor = finalColor;

}

体渲染结果

http://www.dtcms.com/a/71271.html

相关文章:

  • 服务性能防腐体系:基于自动化压测的熔断机制
  • NPM 常用操作指令大全
  • 网页制作12-html,css,javascript初认识のJavascipt脚本基础
  • 备赛蓝桥杯-Python-Day1-基础语法回顾
  • SWPU 2021 新生赛
  • 设计模式之备忘录设计模式
  • 解密乐天音乐如何通过抗指纹浏览器刷变现
  • 保持docker内容器一直运行
  • 蓝桥杯2024年第十五届省赛真题-砍柴
  • Scala语言的数据库编程
  • HarmonyOS NEXT 声明式UI语法学习笔记-创建自定义组件
  • 3.3 Spring Boot多数据源动态切换:AbstractRoutingDataSource实战
  • 工作记录 2017-01-13
  • odbus TCP转Modbus RTU网关快速配置案例
  • uniapp-x 子组件样式覆盖
  • [笔记.AI]数据集——大模型的“教科书” | 数据集的细分、作用和意义
  • 高德地图猎鹰服务调用指南(Java后端)
  • postman通过json获取接口返回token,设置为全局变量
  • Unity插件-适用于画面传输的FMETP STREAM使用方法(一)FMETP STREAM介绍
  • 安全相关Python脚本
  • 【leetcode hot 100 108】将有序数组转换为二叉搜索树
  • 工厂方法模式 (Factory Method Pattern)
  • 人工智能之数学基础:保持几何结构不变的线性变换——正交变换
  • 查找Python环境中Matplotlib配置文件
  • 计算机的结构形式
  • 《Flutter:开源的跨平台移动应用开发框架》:此文为AI自动生成
  • 激活函数和批归一化(BatchNorm)
  • 数位小游戏
  • Vue生命周期_Vue生命周期钩子
  • 使用SetupTools 管理你的项目打包工作