View Full Version : Creating 3D Texture - Sample Help

05-20-2011, 11:35 AM
One of the possible solutions I have been told in the forum is to load my medical images slices into a 3d Textures. How do to this, is another question in itself. I found a tutorial for 3d texturing http://gpwiki.org/index.php/OpenGL:Tutorials:3D_Textures

I was able to compile the code by making some changes. I'm going to study the code to see what I learn.

My hope was either to
1) Read the image and put in a slice of the cube. But how are the coordinates supposed to be set? (texture coords)

2) Set each pixel color to the correct part of the array and then bind the slice.

Any additional info that you guys can provide.

I also purchased Real-time volume graphics that I hope it helps. I should it have by next week.

05-20-2011, 01:23 PM
While waiting for your book to come, start reading the following courses/tutorials:


The book is actually based upon these materials.

05-20-2011, 09:19 PM
But how are the coordinates supposed to be set? (texture coords)

This depends on what method you use for rendering of the volume.
If you use object aligned 3D texture slicing, you will need to update the texture coordinates depending on the axis overlapping the viewing direction. If you use the view aligned 3D texture slicing, the texture coordinates are generated using a simple ray/box intersection.
The book gives everything you would need. In addition, I have given some details on my blog here http://mmmovania.blogspot.com/2008/06/object-aligned-slice-based-volume.html
Any more help, just ask here.

05-21-2011, 07:13 AM
do you think I could get the code your wrote? I would be very interested not only the visualization but how to read the medical images. I was thinking to use imebra to read them.


05-22-2011, 08:46 AM
Well i can help u in the process. Itsjust backtofront blending with 3d texturemapping. The code can be obtained from the realtime volume gfx book site given above. For medical image formats i used a basic loader to read in raw ct data.for our application wehad a proprietory format.
You shud giv it a try i will help u in the process.

05-22-2011, 10:16 AM
Sure, I will. I already received the book (saturday delivery) and I'm finishing chapter 3. I'm going to take this week to read the book as much as I can. I like the book, but is all little pieces that I have to put together. It seems very difficult but I think I can do it.

05-23-2011, 12:14 PM
I'm having trouble compiling the code from the book. I posted another thread for that:
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=297798#Post2977 98

05-23-2011, 12:21 PM
nevermind ... it compiles now...
However, I encounter another error.

05-23-2011, 10:00 PM
what is the error?

05-24-2011, 07:27 AM
Now it compiles fine.
The errors I get is at runtime due to CG. I have an ATI card which maybe the problem.

05-24-2011, 08:41 AM
Well, the CG compiler does a pretty good job at highlighting the details of what's wrong so i suspect u might be doing something wrong. Could u post the CG shader that u r using so that we may help u with the error.

05-24-2011, 01:31 PM
It comes from the sample of the book located at the site SimpleSlicer3DTex .the command line says featured not supported, but the demo runs. I don't know know if it runs as it should.
I think I'm going to try to get me a machine that has nvidia to test it as well.

However, I think I have to move on. I think I have to create 3d Textures sample. Here I need help.

I want to test with dummy data. So I want to create a texture that it will be 256x256x2 , with different coles, 256 times, to build a big cube. I guess is either that or I work on not rendering some of the objects that are not visible. I think I should start with the 3d Texture.

This is to simulate each 2d image that I will have which is 256x256 with a small seperation. Instead of having a small seperation, I will put all of them together,hence my original idea of having small cubes each representing a pixel.

so, I'm not sure if texturing thing will work or will I have to use something like octrees or different techniques.

as of now of course, if Y try to render
256x256x256 cubes , it will not render... it will render so far up to 64x64x64 but moving around gets slow.

Now, someone may ask? why do you want to have all the images stack. It is part of some experiment that my PhD adviser wants me to do.

with all of this said, maybe I can start with baby steps to get where I want. I was happy when I was able to render 32x32x32 but when I notice that past N=64 would get slow, I have reach a mental block.
The book is helping a bit.

thanks for help, I will await for feedback/instructions/comments


05-24-2011, 07:17 PM
What do u want to do? Are u looking to create a 3D texture? or are u trying to see cubic voxelization?

05-25-2011, 12:18 PM
if I want to have small cubes each representing the color of a given pixel, would it be better with 3d textures to just put all the image in one bigger cube, or do the latter you mention. I think the latter is what I want. I'm including a picture here of what I have. but it is too slow if I increment the N. this big cube, is build with many cubes (i'm building the cubes using triangles, instead of quads) and to each cube I assigned a color. I have NxNxN cubes.

PS: How do I add code. I try before <code> int i =0;</code> but it didn't work

The Little Body
05-25-2011, 12:47 PM
I see only here a big cube where the color is interpolated between the 8 cube's corners
(or composed of NxNxN very very smalls cubes)

=> we can have visualy the same result with only 6 quad faces (or 12 triangles) ...

What is the url for the SimpleSlicer3DTex site ?

PS : it's (UBBCode)
not <something></something> (HTML)

The Little Body
05-25-2011, 12:55 PM
I see now the "Mach bands"
=> your NxNxN cubes are not as little as this :)

You can draw horizontals and verticals lines on the texture for to represent the separation between each "voxels" and only use a big cube.

05-25-2011, 01:08 PM
Ok, So textures is the answer.

One 3d texture that has all the 256 2d slices?

05-25-2011, 01:09 PM
The web site is http://www.real-time-volume-graphics.org/
however, that cube is not based on their code.

The Little Body
05-25-2011, 01:10 PM
Yes, "One 3d texture that has all the 256 2d slices" :)

05-25-2011, 01:19 PM
great. (Which had been suggested to me.)
I found some code in the red book (checkerboard - checker.c) that gives me an idea how to do it with 2d textures. Something similar would be for 3d textures. However, what confuses me big deal is the coordinates for the textures.
Any 3D Texture sample, similar to this, anywhere? specially if it uses GLSL since my code is for opengl 3.2 (the one that I have been using , with the help of the opengl bible libraries from the opengl bible fifth edition)

The Little Body
05-25-2011, 02:16 PM
In 2D texture, only the (s,t) coordinates are used
=> glTexCoord2f(s,t)

In 3D texture, there is too a third coordinate r to use
(where the r coordinate represent the slice plane in your case)
=> glTexcoord3f(s,t,r)

You have to use the alpha component and draw slice by slice for to handle the transparency

You can draw the horizontals and verticals separation lines with something like this on a texture3D[DEPTH][HEIGHT][WIDTH]:

M = 256 / N ;

if( (r%M == 0) || (t%M == 0) || (s%M) == 0) )
texture3D[r][s][t] = separation_color;

(this can be a lot optimised if we draw directly the verticals/horizontals lines instead to test if the texel is on a separation or not)

The Little Body
05-25-2011, 03:15 PM
Ok, you don't want a separation between voxels and what do you want to make is GPU ray casting

I have never play with this :(

But it's never too late for to begin :)

I have found a GPU Raycasting Tutorial at http://cg.alexandra.dk/tag/source-code-and-tutorials
=> I test to adapt it for the linux platform and come back after
(it already use glut/glew/CG, so with a little chance I have only to delete the "#include <windows.h>" and create a makefile for to port it)

The Little Body
05-25-2011, 04:25 PM
The linux port is make :)

Only the files main.cpp, raycasting_shader.cg and Vector3.h found at http://www.daimi.au.dk/%7Etrier/raycasting_tutorial.zip are necessary

I have only modify a little the beginning of the main.cpp file and rename it raycasting.cpp :

// --------------------------------------------------------------------------

// GPU raycasting tutorial

// Made by Peter Trier jan 2007


// This file contains all the elements nessesary to implement a simple

// GPU volume raycaster.

// Notice this implementation requires a shader model 3.0 gfxcard
// YLP 26/05/2011 : adapted to the Linux plateform

// --------------------------------------------------------------------------

// #include <windows.h>

#include <iostream>

#include <fstream>

#include <sstream>

#include <vector>

#include <cmath>

#include <ctime>

#include <cassert>

#include <GL/glew.h>

//#include <GL/gl.h>

//#include <GL/glu.h>

#include <GL/glut.h>

#include <Cg/cg.h>

#include <Cg/cgGL.h>

#include "Vector3.h"

and this is the makefile

all: raycasting

raycasting: raycasting.cpp
g++ -o raycasting -L/usr/X11R6/lib -lGL -lGLU -lglut -lCg -lCgGL -lGLEW raycasting.cpp

A little 'make' and I have now a raycasting executable that work :)

But the %CPU is now near 50% when I execute it :(
(100% when I execute it with one Free TV channel on VLC in //)
=> I see if I can adapt this for to have a very lesser %CPU

The Little Body
05-25-2011, 04:41 PM
This is really really very slow : only 0.022 fps on my linux box :(
(ok, it's only a NVidia GeForce 7100 GS but I have very very more fps on others gl programs, about 190 fps on the Lesson47 of Jeff Molofee's at http://nehe.gamedev.net for example, so I don't think that it is a hardware/software driver path problem )

The Little Body
05-25-2011, 04:56 PM
I'm "happy", I have found something to optimize **a lot** :)

05-25-2011, 06:00 PM
What did you find?
thanks for the help

The Little Body
05-25-2011, 06:06 PM
I have open a new thread at http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=297985#Post2979 85 :)