Raycasting Voxels and OpenGL
I'm开发者_StackOverflow中文版 currently looking into raycasting and voxels, which is a nice combination. A Voxelrenderer by Sebastian Scholz implements this pretty nicely, but also uses OpenGL. I'm wondering how his formula is working; how can you use OpenGL with raycasting and voxels? Isn't the idea for raycasting that a ray is casted for every pixel (or line ie in Doom) and then to draw the result?
The mentioned raycaster is a Voxelrenderer, i.e. a method do visualize volumetric data, like opacities stored in a 3D texture. Doom's raycasting algorithm has another intention: For every pixel on the screen find the first planar surface of the map and draw the color of that there. The rasterizing capabilited of modern GPUs obsoleted this use of raycasters.
Realtime visualizing volumetric data still is a task done by special hardware, typically found in medical and geodesic imaging systems. Basically those are huge bulks of RAM (several dozens of GB) holding volumetric RGBA data. Then for every on screen pixel a ray is cast through the volume and the RGBA data integrated over that ray. A GPU Voxelrenderer does the same thing by a fragment shader; pseudocode:
vec4f prev_color;
for(i=0; i<STEPS; i++) {
p = ray_direction * i*STEP_DELTA;
voxel = texture3D(volumedata, p);
prev_color = combine(voxel, prev_color);
}
final_color = finalize(prev_color);
finalize
and combine
depend on the kind of data and what you want to visualize. For example if you want to integrate the density (like in an X ray image), combine
would be a summing operation and finalize a normalization. If you were to visualize a cloud, you'd alpha blend between voxels.
Raycasting in a voxel space wouldn't use pixels, it would be inefficient.
You already have an array to say what spaces are empty and which ones have a voxel cube.
So a fast version is tracing a line which checks the emptimess of every voxel in the direction of the line, until it reaches a full voxel.
That would take a few hundred read ops from the memory and 2-3 multiplications of the ray vector for every read op.
to read a billion memory positions of voxels takes about 1 second, so a few hundred would be very fast and always within a frame.
Raycasting often uses optmizations to detect fractional places in space where a maths formula stars, where a mesh vertex is based on it's bounding box and then it's mesh, and in voxels it's just checks of a line in an integer array progressively until you find a non void.
精彩评论