Volume Rendering (rendering) Overview 4: ray casting implementation process and code (CPU-based implementation)

Source: Internet
Author: User
Tags mul

From: http://blog.csdn.net/liu_lin_xm/article/details/4850630

Excerpt from "GPU programming and Cg language primer 1rd edition"

 

 

Algorithm flow

 

 

 

Figure 47 shows the implementation process of Volume Rendering using the ray projection algorithm.

First, we need to render the forward and backward surface depth graphs. This is to calculate the maximum distance of Ray traversing and serves as the basis for loop sampling control; then, the vertex position and Ray direction are calculated in the vertex coloring program. The ray direction is determined by the line of sight and the world coordinate of the point. In fact, the ray direction can also be calculated in the fragment coloring program. Then the most important thing is cyclic texture sampling and synthesis.

Each cycle calculates the new sample texture coordinates and sampling distance, and then performs color synthesis and transparency accumulation. If the sampling distance exceeds the maximum traversing distance, or the transparency is accumulated to 1, the loop ends. Output The synthesized color value.

Figure 48 shows how to use the ray projection algorithm for Volume Rendering:

 

 

 

15.4 Implementation of ray projection algorithm

This section provides the coloring program implementation code of the ray projection algorithm. There are three parts: struct, vertex coloring program, and fragment coloring program.

Code 22: Structure of the ray projection algorithm

 

 

 

Struct vertexin

{

Float4 position: position;

Float4 texcoord: texcoord;

};

 

Struct vertexscreen

{

Float4 position: position;

Float4 worldpos: texcoord0;

Float4 projpos: texcoord1;

Float4 texcoord: texcoord2;

};

Code 23 Vertex coloring program of the ray projection algorithm

 

 

 

Vertexscreen main_v (vertexin posin,

Uniform float4x4 world,

Uniform float4x4 worldviewproj,

Uniform float4x4 texviewproj)

{

Vertexscreen posout;

Posout. Position = MUL (worldviewproj, posin. position );

Posout. worldpos = MUL (World, posin. position );

Posout. projpos = MUL (texviewproj, posout. worldpos );

Posout. texcoord = posin. texcoord;

 

Return posout;

}

 

Code 24 ray projection algorithm fragment coloring Program

 

Void main_f (vertexscreen posin,

Uniform float3 eyeposition,

Uniform sampler3d volumetex: Register (S0 ),

Uniform sampler2d frontdepthtex: Register (S1 ),

Uniform sampler2d backdepthtex: Register (S2 ),

Out float4 result: color)

{

// Calculates the direction based on the viewpoint and the current vertex world coordinates.

Float3 dir = posin. worldpos. XYZ-eyeposition;

Dir = normalize (DIR );

Float3 deltadir = float3 (0.0, 0.0, 0.0 );

// Obtain the 3D texture coordinates of the current vertex

Float3 Tex = posin. texcoord. XYZ;

Float2 uvdelta;

Uvdelta. x= 0.0; // DDX (Tex). X;

Uvdelta. Y = 0.0; // ddy (Tex). Y;

// Retrieve the depth interval value and set the sampling interval

Float2 UV = posin. projpos. XY/posin. projpos. W;

Float frontdis = tex2d (frontdepthtex, UV). X;

Float backdis = tex2d (backdepthtex, UV). X;

Float Len = backdis-frontdis;

// Initialize the color value, sample value, and transparency.

Float3 norm_dir = normalize (DIR );

Float stepsize = 0.01;

Float Delta = stepsize;

Float3 delta_dir = norm_dir * delta;

Float delta_dir_len = length (delta_dir );

Float3 VEC = posin. texcoord. XYZ;

Float4 col_acc = float4 (0, 0, 0 );

Float alpha_acc = 0;

Float length_acc = 0;

Float4 color_sample;

Float alpha_sample;

 

For (INT I = 0; I <800; I ++ ){

Color_sample = tex3d (volumetex, VEC );

Alpha_sample = color_sample.a * stepsize;

Col_acc + = (1.0-alpha_acc) * color_sample * alpha_sample * 3;

Alpha_acc + = alpha_sample;

VEC + = delta_dir;

Length_acc + = delta_dir_len;

If (length_acc> = Len | alpha_acc> 1.0) break; // sampling Cycle Control Condition

}

Result. XYZ = col_acc.xyz * 2.0 + float3 (0.2, 0.2, 0.2 );

Result. W = col_acc.w;

}

 

15.5 summary of this Chapter

Chapter 15 of this book describes the basic principles and implementation processes of the ray projection algorithm in volume rendering. In fact, on this basis, we can extend the ray projection algorithm. For example, we can combine the ray projection algorithm and the shadow rendering algorithm to render more realistic images.

In addition, some body data is empty in the middle. When sampling in the ray direction, the empty area needs to be skipped, which also requires additional algorithm processing, in English, it is called "Object-order empty space skipping ".

The best textbook on volume rendering and ray casting algorithms I have found is "Advanced illumination techniques for GPU-based volume raycasting" written by Markus hadwiger and others ". This book was published on Siggraph asia2008. It is the latest and authoritative teaching material available for the moment, with a total of 166 pages. Students with good English reading ability can take a look.

This chapter is the last chapter of this book. At last, we hope that China's computer science can truly reach the level of scientific research, rather than continuing to be devoted to the work of masons in chaos.

Volume Rendering (rendering) Overview 4: ray casting implementation process and code (CPU-based implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.