title: Volume Rendering: How can I turn a .raw file into an opengl-friendly isosurface?
body: <p>I want to create a 3D visualizer of .raw volume medical datasets using marching tetrahedra. </p>
  
  <p>I found <a href="http://www.siafoo.net/snippet/100" rel="nofollow noreferrer">this implementation on GPL license</a> that looks nice and is based on the info by Paul Bourke, but I don't know how to make work with a .raw file, which I've found <a href="http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=299805" rel="nofollow noreferrer">people load as a 3d texture</a>. </p>
  
  <pre><code>//assuming that the data at hand is a 256x256x256 unsigned byte data
  int XDIM=256, YDIM=256, ZDIM=256;
  const int size = XDIM*YDIM*ZDIM;
  bool LoadVolumeFromFile(const char* fileName) {
  
      FILE *pFile = fopen(fileName,"rb");
     if(NULL == pFile) {
      return false;
     }
  
     GLubyte* pVolume=new GLubyte[size]; //&lt;- here pVolume is a 1D byte array 
     fread(pVolume,sizeof(GLubyte),size,pFile);
     fclose(pFile);
  
     // now pVolume is passed to a 3d texture 
  
      //load data into a 3D texture
  
  
  glGenTextures(1, &amp;textureID);
     glBindTexture(GL_TEXTURE_3D, textureID);
  
     // set the texture parameters
     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
     glTexImage3D(GL_TEXTURE_3D,0,GL_INTENSITY,XDIM,YDIM,ZDIM,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,pVolume);
     delete [] pVolume;
     return true;
  
  
  
  
  }
  </code></pre>
  
  <p>I don't know if I should either:
  a) access the values directly from pValue
  b) access them after loading them to a 3D texture</p>
  
  <p>I have no idea of how I'm supposed to index pVolume to get the isosurface intensity at each x,y,z. if pVolume is a 1D array. I know x,y coordinates are mapped from a 1D array using a divide and a module, but how would I map x,y,z? </p>
  
  <p>On the other hand, If I load the .raw file as a 3d texture would </p>
  
  <blockquote>
    <p>glTexCoord3f</p>
  </blockquote>
  
  <p>give me the isovalue at a given x,y,z?   </p>
  
  <p><strong>Clarification: The question isn't "can OpenGL draw an isosurface directly from this?" the question is how to index the isolevels on the .raw geometry, which is needed to evaluate each tetrahedron properly. On the image one can see that each tetrahedron case is marked based on whether the isosurface is above or below it. The isosurface is indexed on it's x,y,z coordinates on the .raw file, how can I access it for each x,y,z on the dataset? Will glTexCoord3f give me its intensity on a given x,y,z? Is it better to do a x,y,z conversion from the pVolume array directly?</strong>   </p>
  
  <p><img src="https://i.stack.imgur.com/LWOPd.gif" alt="enter image description here"></p>