1

I have some conceptual questions about 3D textures and texture mapping in OpenGL that I am trying to wrap my head around. The questions pertain to the implementation of the code especially for 3D texturing in this tutorial: http://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering?fid=1807805&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Normal&spc=Relaxed&select=4729498&fr=1#xx4738025xx

The dimensions of the dataset used in the tutorial is 256 x 256 x 109 (109 2D slices)

1.

The code provides a way to map 2D images to a 3D texture by loading all the 2D slices into a single data array. Then, it proceeds to map the data to the 3D texture by incrementing fIndx from -1.0f to +1.0f in steps of 0.003f. Why is the increment in steps of 0.003f? There are ~666.667 increments which is roughly 6.11 times the total number of 2D slices (109 slices).

for ( float fIndx = -1.0f; fIndx <= 1.0f; fIndx+=0.003f )
{
    glBegin(GL_QUADS);
        MAP_3DTEXT( fIndx );
    glEnd();
}

2.

I am trying to figure out how to move through the rendered volume using 3D textures. What I am trying to do is similar to the video in this website: http://cvlab.epfl.ch/research/medical/em/synapses

The data in that website has to be, for sure, 2D images which are mapped to 2D textures. So, just making each layer in the volume vanish is apparently easy (but, I don't know how to do that!). My data looks like this though: http://ctrlv.in/292069

EDIT

There are 256 slices in my dataset, and I want to go through each slice one by one. How can I move through the slices one by one just as in the video?

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
Eagle
  • 1,187
  • 5
  • 22
  • 40

1 Answers1

3

1.

Why indeed... floating-point loop logic is evil. 0.003f cannot be represented in floating-point. After say 256 iterations of this, the loop counter is now ~= -0.231997 instead of -0.232. It would be better to do this loop using an integer control expression and then divide the end-result by 1000.0f inside the body of the loop. I would not trust the author of this tutorial to write any financial software. They will lose your money by doing things like this ;)

0.003f is actually ~ 0.003000000026077032089233398438... (it is a repeating number in floating-point because it cannot be expressed as division by a power-of-two). After enough rounding errors accumulate, the difference in value can be enough to throw off your loop counting logic. So much so, that even if that number incremented in steps large enough to address only 190 slices, the loop would run 1 less time and you would be missing a slice.

If you had 256 slices, and incremented 1/256 (which is expressed using a power-of-two denominator) per-iteration, floating-point loop control would work. Alas, 3/1000 has no such property. This is a bad floating-point value to use for looping. Since finding out whether a series of numbers can be represented precisely using floating-point is a nuisance, it is usually better to avoid floating-point loops altogether.

2.

If you want 256 unique values in the range [-1.0, 1.0] (NDC coordinate space), you should start at -1.0 and increment by exactly 1/128 (0.0078125) each iteration.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • Perfect! Thank you for explaining that! This makes sense now. However, I would not want to use floating point values in my loop and I would prefer to use just integers to achieve the texture mapping. Also, I think I haven't made myself clear in the 2nd question. I wanted to know how to move through the various layers of images as shown in the video http://cvlab.epfl.ch/research/medical/em/synapses – Eagle Feb 07 '14 at 00:21
  • Yeah, I see what you mean now. If you are drawing a series of quads to accomplish this, you can either start at a higher index or another option is to dynamically adjust your clipping plane. Since the 3D volume is rotated in the video you linked to, I think that simply skipping a few of the quads will be the easiest solution. – Andon M. Coleman Feb 07 '14 at 00:27
  • Well, I can't skip through the quads. The data that I am working with is corneal tissue cross-sections. So, I need to make each image vanish before displaying the next. I don't get how to make each image vanish after rendering it? I could use the glScissor function, but, I need to make sure that the other textures I render are not clipped either – Eagle Feb 07 '14 at 01:12
  • Also, I tried using a higher index and it doesn't work with the example code present in the 3D texturing tutorial webpage. – Eagle Feb 07 '14 at 01:15
  • Each quad represents a slice of the volume, correct? Making the images disappear should be as simple as clearing the color buffer, and then re-drawing the quads (without the slice you are no longer interested in). Now, to achieve the effect in that video, you actually do not want to remove the quad immediately - you should move it slightly backward each frame until it reaches the next slice (then remove it). This all assumes that your texture coordinates are related to the position. By the way, datenwolf is an expert in this, he might have more to add to the discussion. – Andon M. Coleman Feb 07 '14 at 01:29
  • Also, keep in mind that there is nothing wrong with drawing ***more*** quads to visualize your 3D image than the number of 2D cross-section images in the data set. If you are using linear texture filtering, this will produce interpolated imagery for any of the quads that lie somewhere in-between exact slices. This property is very useful for giving the illusion of a continuous 3D volume. – Andon M. Coleman Feb 07 '14 at 01:40
  • @Datenwolf Your thoughts please? – Eagle Feb 07 '14 at 20:03
  • Sure, I'll try working on your suggestions. I worked on the first one, where I render the first slice, clear the color buffer and then render the second slice after but, that doesn't seem to work. I will have to try and see if double-buffering works here. – Eagle Feb 07 '14 at 20:04
  • I tried working on an integer-controlled for loop, and even that fails. My rendering is way off somewhere in the distance. The trouble is there is no other code which is available online which explains 3D texturing well enough. 3D texturing is poorly documented :/ – Eagle Feb 07 '14 at 21:30