Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.88 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package volvis;
  6.  
  7. import com.jogamp.opengl.GL;
  8. import com.jogamp.opengl.GL2;
  9. import com.jogamp.opengl.util.texture.Texture;
  10. import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
  11. import gui.RaycastRendererPanel;
  12. import gui.TransferFunction2DEditor;
  13. import gui.TransferFunctionEditor;
  14. import java.awt.image.BufferedImage;
  15. import util.TFChangeListener;
  16. import util.VectorMath;
  17. import volume.GradientVolume;
  18. import volume.Volume;
  19. import volume.VoxelGradient;
  20.  
  21. /**
  22. *
  23. * @author michel
  24. */
  25. public class RaycastRenderer extends Renderer implements TFChangeListener {
  26.  
  27. private Volume volume = null;
  28. private GradientVolume gradients = null;
  29. public static int renderingMethod = 0;
  30. RaycastRendererPanel panel;
  31. TransferFunction tFunc;
  32. TransferFunctionEditor tfEditor;
  33. TransferFunction2DEditor tfEditor2D;
  34.  
  35. public RaycastRenderer() {
  36. panel = new RaycastRendererPanel(this);
  37. panel.setSpeedLabel("0");
  38. }
  39.  
  40. public void setVolume(Volume vol) {
  41. System.out.println("Assigning volume");
  42. volume = vol;
  43.  
  44. System.out.println("Computing gradients");
  45. gradients = new GradientVolume(vol);
  46.  
  47. // set up image for storing the resulting rendering
  48. // the image width and height are equal to the length of the volume diagonal
  49. int imageSize = (int) Math.floor(Math.sqrt(vol.getDimX() * vol.getDimX() + vol.getDimY() * vol.getDimY()
  50. + vol.getDimZ() * vol.getDimZ()));
  51. if (imageSize % 2 != 0) {
  52. imageSize = imageSize + 1;
  53. }
  54. image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
  55. // create a standard TF where lowest intensity maps to black, the highest to white, and opacity increases
  56. // linearly from 0.0 to 1.0 over the intensity range
  57. tFunc = new TransferFunction(volume.getMinimum(), volume.getMaximum());
  58.  
  59. // uncomment this to initialize the TF with good starting values for the orange dataset
  60. tFunc.setTestFunc();
  61.  
  62.  
  63. tFunc.addTFChangeListener(this);
  64. tfEditor = new TransferFunctionEditor(tFunc, volume.getHistogram());
  65.  
  66. tfEditor2D = new TransferFunction2DEditor(volume, gradients);
  67. tfEditor2D.addTFChangeListener(this);
  68.  
  69. System.out.println("Finished initialization of RaycastRenderer");
  70. }
  71.  
  72. public RaycastRendererPanel getPanel() {
  73. return panel;
  74. }
  75.  
  76. public TransferFunction2DEditor getTF2DPanel() {
  77. return tfEditor2D;
  78. }
  79.  
  80. public TransferFunctionEditor getTFPanel() {
  81. return tfEditor;
  82. }
  83.  
  84. /*
  85. Get voxel using floor
  86. */
  87. short getVoxel(double[] coord) {
  88.  
  89. if (coord[0] <= 0 || coord[0] > volume.getDimX() || coord[1] <= 0 || coord[1] >= volume.getDimY()
  90. || coord[2] <= 0 || coord[2] > volume.getDimZ()) {
  91. return 0;
  92. }
  93.  
  94. int x = (int) Math.floor(coord[0]);
  95. int y = (int) Math.floor(coord[1]);
  96. int z = (int) Math.floor(coord[2]);
  97.  
  98. return volume.getVoxel(x, y, z);
  99. }
  100.  
  101. /*
  102. Get voxel using linear interpolation
  103. */
  104. double getVoxelInterpolatedGivenVolume(double[] coord, Volume volume) {
  105.  
  106. if (coord[0] <= 0 || coord[0] >= volume.getDimX() || coord[1] <= 0 || coord[1] >= volume.getDimY()
  107. || coord[2] <= 0 || coord[2] >= volume.getDimZ()) {
  108. return 0;
  109. }
  110.  
  111. int x_floor = Math.max((int) Math.floor(coord[0]), 0);
  112. int x_ceil = Math.min((int) Math.ceil(coord[0]), volume.getDimX()-1);
  113. int y_floor = Math.max((int) Math.floor(coord[1]), 0);
  114. int y_ceil = Math.min((int) Math.ceil(coord[1]), volume.getDimY()-1);
  115. int z_floor = Math.max((int) Math.floor(coord[2]), 0);
  116. int z_ceil = Math.min((int) Math.ceil(coord[2]), volume.getDimZ()-1);
  117.  
  118. short s0 = volume.getVoxel(x_floor, y_floor, z_floor);
  119. short s1 = volume.getVoxel(x_ceil, y_floor, z_floor);
  120. short s2 = volume.getVoxel(x_floor, y_ceil, z_floor);
  121. short s3 = volume.getVoxel(x_ceil, y_ceil, z_floor);
  122. short s4 = volume.getVoxel(x_floor, y_floor, z_ceil);
  123. short s5 = volume.getVoxel(x_ceil, y_floor, z_ceil);
  124. short s6 = volume.getVoxel(x_floor, y_ceil, z_ceil);
  125. short s7 = volume.getVoxel(x_ceil, y_ceil, z_ceil);
  126.  
  127. double alpha = (coord[0] - x_floor)/(x_ceil - x_floor);
  128. double beta = (coord[1] - y_floor)/(y_ceil - y_floor);
  129. double gamma = (coord[2] - z_floor)/(z_ceil - z_floor);
  130.  
  131. return (1 - alpha)*(1 - beta)*(1-gamma)*s0
  132. + alpha*(1 - beta)*(1 - gamma)*s1
  133. + (1 - alpha)*beta*(1 - gamma)*s2
  134. + alpha*beta*(1 - gamma) * s3
  135. + (1 - alpha)*(1 - beta)*gamma*s4
  136. + alpha*(1 - beta) * gamma * s5
  137. + (1 - alpha)*beta*gamma*s6
  138. + alpha*beta*gamma*s7;
  139. }
  140.  
  141.  
  142. void slicer(double[] viewMatrix) {
  143.  
  144. // clear image
  145. for (int j = 0; j < image.getHeight(); j++) {
  146. for (int i = 0; i < image.getWidth(); i++) {
  147. image.setRGB(i, j, 0);
  148. }
  149. }
  150.  
  151. // vector uVec and vVec define a plane through the origin,
  152. // perpendicular to the view vector viewVec
  153. double[] viewVec = new double[3];
  154. double[] uVec = new double[3];
  155. double[] vVec = new double[3];
  156. VectorMath.setVector(viewVec, viewMatrix[2], viewMatrix[6], viewMatrix[10]);
  157. VectorMath.setVector(uVec, viewMatrix[0], viewMatrix[4], viewMatrix[8]);
  158. VectorMath.setVector(vVec, viewMatrix[1], viewMatrix[5], viewMatrix[9]);
  159.  
  160. // image is square
  161. int imageCenter = image.getWidth() / 2;
  162.  
  163. double[] pixelCoord = new double[3];
  164. double[] volumeCenter = new double[3];
  165. VectorMath.setVector(volumeCenter, volume.getDimX() / 2, volume.getDimY() / 2, volume.getDimZ() / 2);
  166.  
  167. // sample on a plane through the origin of the volume data
  168. double max = volume.getMaximum();
  169. TFColor voxelColor = new TFColor();
  170.  
  171.  
  172. for (int j = 0; j < image.getHeight(); j++) {
  173. for (int i = 0; i < image.getWidth(); i++) {
  174. pixelCoord[0] = uVec[0] * (i - imageCenter) + vVec[0] * (j - imageCenter)
  175. + volumeCenter[0];
  176. pixelCoord[1] = uVec[1] * (i - imageCenter) + vVec[1] * (j - imageCenter)
  177. + volumeCenter[1];
  178. pixelCoord[2] = uVec[2] * (i - imageCenter) + vVec[2] * (j - imageCenter)
  179. + volumeCenter[2];
  180.  
  181. int val = getVoxel(pixelCoord);
  182.  
  183. // Map the intensity to a grey value by linear scaling
  184. voxelColor.r = val/max;
  185. voxelColor.g = voxelColor.r;
  186. voxelColor.b = voxelColor.r;
  187. voxelColor.a = val > 0 ? 1.0 : 0.0; // this makes intensity 0 completely transparent and the rest opaque
  188. // Alternatively, apply the transfer function to obtain a color
  189. // voxelColor = tFunc.getColor(val);
  190.  
  191.  
  192. // BufferedImage expects a pixel color packed as ARGB in an int
  193. int c_alpha = voxelColor.a <= 1.0 ? (int) Math.floor(voxelColor.a * 255) : 255;
  194. int c_red = voxelColor.r <= 1.0 ? (int) Math.floor(voxelColor.r * 255) : 255;
  195. int c_green = voxelColor.g <= 1.0 ? (int) Math.floor(voxelColor.g * 255) : 255;
  196. int c_blue = voxelColor.b <= 1.0 ? (int) Math.floor(voxelColor.b * 255) : 255;
  197. int pixelColor = (c_alpha << 24) | (c_red << 16) | (c_green << 8) | c_blue;
  198. image.setRGB(i, j, pixelColor);
  199. }
  200. }
  201.  
  202. }
  203.  
  204. void mip(double[] viewMatrix) {
  205.  
  206. // clear image
  207. for (int j = 0; j < image.getHeight(); j++) {
  208. for (int i = 0; i < image.getWidth(); i++) {
  209. image.setRGB(i, j, 0);
  210. }
  211. }
  212.  
  213. if (!interactiveMode){
  214. // vector uVec and vVec define a plane through the origin,
  215. // perpendicular to the view vector viewVec
  216. double[] viewVec = new double[3];
  217. double[] uVec = new double[3];
  218. double[] vVec = new double[3];
  219. VectorMath.setVector(viewVec, viewMatrix[2], viewMatrix[6], viewMatrix[10]);
  220. VectorMath.setVector(uVec, viewMatrix[0], viewMatrix[4], viewMatrix[8]);
  221. VectorMath.setVector(vVec, viewMatrix[1], viewMatrix[5], viewMatrix[9]);
  222.  
  223. // image is square
  224. int imageCenter = image.getWidth() / 2;
  225.  
  226. double[] pixelCoord = new double[3];
  227. double[] volumeCenter = new double[3];
  228. VectorMath.setVector(volumeCenter, volume.getDimX() / 2, volume.getDimY() / 2, volume.getDimZ() / 2);
  229.  
  230. // sample on a plane through the origin of the volume data
  231. double max = volume.getMaximum();
  232. TFColor voxelColor = new TFColor();
  233.  
  234. for (int j = 0; j < image.getHeight(); j++) {
  235. for (int i = 0; i < image.getWidth(); i++) {
  236. int val = 0;
  237. int maxVal = 0;
  238. for (int d = -100; d
  239. < 100; d = d + 2) {
  240. pixelCoord[0] = d * viewVec[0] + uVec[0] * (i - imageCenter) + vVec[0] * (j - imageCenter)
  241. + volumeCenter[0];
  242. pixelCoord[1] = d * viewVec[1] + uVec[1] * (i - imageCenter) + vVec[1] * (j - imageCenter)
  243. + volumeCenter[1];
  244. pixelCoord[2] = d * viewVec[2] + uVec[2] * (i - imageCenter) + vVec[2] * (j - imageCenter)
  245. + volumeCenter[2];
  246. maxVal = Math.max((int) Math.ceil(getVoxelInterpolatedGivenVolume(pixelCoord, volume)), maxVal);
  247. }
  248.  
  249. val = maxVal;
  250.  
  251. // Map the intensity to a grey value by linear scaling
  252. voxelColor.r = val / max;
  253. voxelColor.g = voxelColor.r;
  254. voxelColor.b = voxelColor.r;
  255. voxelColor.a = val > 0 ? 1.0 : 0.0; // this makes intensity 0 completely transparent and the rest opaque
  256. // Alternatively, apply the transfer function to obtain a color
  257. // voxelColor = tFunc.getColor(val);
  258.  
  259.  
  260. // BufferedImage expects a pixel color packed as ARGB in an int
  261. int c_alpha = voxelColor.a <= 1.0 ? (int) Math.floor(voxelColor.a * 255) : 255;
  262. int c_red = voxelColor.r <= 1.0 ? (int) Math.floor(voxelColor.r * 255) : 255;
  263. int c_green = voxelColor.g <= 1.0 ? (int) Math.floor(voxelColor.g * 255) : 255;
  264. int c_blue = voxelColor.b <= 1.0 ? (int) Math.floor(voxelColor.b * 255) : 255;
  265. int pixelColor = (c_alpha << 24) | (c_red << 16) | (c_green << 8) | c_blue;
  266. image.setRGB(i, j, pixelColor);
  267. }
  268. }
  269. }
  270.  
  271.  
  272. }
  273.  
  274.  
  275. void composite(double[] viewMatrix) {
  276.  
  277. // clear image
  278. for (int j = 0; j < image.getHeight(); j++) {
  279. for (int i = 0; i < image.getWidth(); i++) {
  280. image.setRGB(i, j, 0);
  281. }
  282. }
  283.  
  284. if (!interactiveMode){
  285. // vector uVec and vVec define a plane through the origin,
  286. // perpendicular to the view vector viewVec
  287. double[] viewVec = new double[3];
  288. double[] uVec = new double[3];
  289. double[] vVec = new double[3];
  290. VectorMath.setVector(viewVec, viewMatrix[2], viewMatrix[6], viewMatrix[10]);
  291. VectorMath.setVector(uVec, viewMatrix[0], viewMatrix[4], viewMatrix[8]);
  292. VectorMath.setVector(vVec, viewMatrix[1], viewMatrix[5], viewMatrix[9]);
  293.  
  294. // image is square
  295. int imageCenter = image.getWidth() / 2;
  296.  
  297. double[] pixelCoord = new double[3];
  298. double[] volumeCenter = new double[3];
  299. VectorMath.setVector(volumeCenter, volume.getDimX() / 2, volume.getDimY() / 2, volume.getDimZ() / 2);
  300.  
  301. TFColor voxelColor = new TFColor();
  302.  
  303. for (int j = 0; j < image.getHeight(); j++) {
  304. for (int i = 0; i < image.getWidth(); i++) {
  305. double Cr = 0;
  306. double Cg = 0;
  307. double Cb = 0;
  308. for (int d = -100; d
  309. < 100; d = d + 2) {
  310. pixelCoord[0] = d * viewVec[0] + uVec[0] * (i - imageCenter) + vVec[0] * (j - imageCenter)
  311. + volumeCenter[0];
  312. pixelCoord[1] = d * viewVec[1] + uVec[1] * (i - imageCenter) + vVec[1] * (j - imageCenter)
  313. + volumeCenter[1];
  314. pixelCoord[2] = d * viewVec[2] + uVec[2] * (i - imageCenter) + vVec[2] * (j - imageCenter)
  315. + volumeCenter[2];
  316. int thisVal = (int) Math.ceil(getVoxelInterpolatedGivenVolume(pixelCoord, volume));
  317. TFColor ci = tFunc.getColor(thisVal);
  318.  
  319. Cr = ci.a * ci.r + (1 - ci.a) * Cr;
  320. Cg = ci.a * ci.g + (1 - ci.a) * Cg;
  321. Cb = ci.a * ci.b + (1 - ci.a) * Cb;
  322. }
  323.  
  324. voxelColor.r = Cr;
  325. voxelColor.g = Cg;
  326. voxelColor.b = Cb;
  327. voxelColor.a = 100;
  328.  
  329. // BufferedImage expects a pixel color packed as ARGB in an int
  330. int c_alpha = voxelColor.a <= 1.0 ? (int) Math.floor(voxelColor.a * 255) : 255;
  331. int c_red = voxelColor.r <= 1.0 ? (int) Math.floor(voxelColor.r * 255) : 255;
  332. int c_green = voxelColor.g <= 1.0 ? (int) Math.floor(voxelColor.g * 255) : 255;
  333. int c_blue = voxelColor.b <= 1.0 ? (int) Math.floor(voxelColor.b * 255) : 255;
  334. int pixelColor = (c_alpha << 24) | (c_red << 16) | (c_green << 8) | c_blue;
  335. image.setRGB(i, j, pixelColor);
  336. }
  337. }
  338. }
  339.  
  340. }
  341.  
  342. void twoDTransfer(double[] viewMatrix) {
  343.  
  344. // clear image
  345. for (int j = 0; j < image.getHeight(); j++) {
  346. for (int i = 0; i < image.getWidth(); i++) {
  347. image.setRGB(i, j, 0);
  348. }
  349. }
  350.  
  351. if (!interactiveMode){
  352. // vector uVec and vVec define a plane through the origin,
  353. // perpendicular to the view vector viewVec
  354. double[] viewVec = new double[3];
  355. double[] uVec = new double[3];
  356. double[] vVec = new double[3];
  357. VectorMath.setVector(viewVec, viewMatrix[2], viewMatrix[6], viewMatrix[10]);
  358. VectorMath.setVector(uVec, viewMatrix[0], viewMatrix[4], viewMatrix[8]);
  359. VectorMath.setVector(vVec, viewMatrix[1], viewMatrix[5], viewMatrix[9]);
  360.  
  361. // image is square
  362. int imageCenter = image.getWidth() / 2;
  363.  
  364. double[] pixelCoord = new double[3];
  365. double[] volumeCenter = new double[3];
  366. VectorMath.setVector(volumeCenter, volume.getDimX() / 2, volume.getDimY() / 2, volume.getDimZ() / 2);
  367.  
  368. TFColor colorSetting = tfEditor2D.triangleWidget.color;
  369. double radius = tfEditor2D.triangleWidget.radius;
  370. double baseIntensity = tfEditor2D.triangleWidget.baseIntensity;
  371.  
  372. System.out.println(radius);
  373. System.out.println(baseIntensity);
  374. System.out.println(colorSetting.a);
  375.  
  376.  
  377. TFColor voxelColor = new TFColor(0,0,0,255);
  378. for (int j = 0; j < image.getHeight(); j++) {
  379. for (int i = 0; i < image.getWidth(); i++) {
  380. voxelColor.a = 0;
  381. voxelColor.r = 0;
  382. voxelColor.g = 0;
  383. voxelColor.b = 0;
  384. for (int d = -100; d
  385. < 100; d = d + 1) {
  386. pixelCoord[0] = d * viewVec[0] + uVec[0] * (i - imageCenter) + vVec[0] * (j - imageCenter)
  387. + volumeCenter[0];
  388. pixelCoord[1] = d * viewVec[1] + uVec[1] * (i - imageCenter) + vVec[1] * (j - imageCenter)
  389. + volumeCenter[1];
  390. pixelCoord[2] = d * viewVec[2] + uVec[2] * (i - imageCenter) + vVec[2] * (j - imageCenter)
  391. + volumeCenter[2];
  392.  
  393. if (pixelCoord[0] <= 0 || pixelCoord[0] >= volume.getDimX() || pixelCoord[1] <= 0 || pixelCoord[1] >= volume.getDimY()
  394. || pixelCoord[2] <= 0 || pixelCoord[2] >= volume.getDimZ()) {
  395. } else {
  396. VoxelGradient gradient;
  397.  
  398. // Get the x,y and z coordinates for this sample
  399. int x = (int) pixelCoord[0];
  400. int y = (int) pixelCoord[1];
  401. int z = (int) pixelCoord[2];
  402.  
  403. // Get the gradient for this voxel
  404. gradient = gradients.getGradient(x, y, z);
  405.  
  406. // Get the intensity for this voxel
  407. double intensity = getVoxel(pixelCoord);
  408.  
  409. // The factor that we apply to the gradient
  410. double alpha;
  411.  
  412. // Maximum opacity
  413. if (gradient.mag == 0f && intensity == baseIntensity) {
  414. alpha = colorSetting.a;
  415. } else if (gradient.mag > 0 && (intensity - (radius * gradient.mag) <= baseIntensity) ||
  416. intensity + (radius * gradient.mag) >= baseIntensity) {
  417. alpha = colorSetting.a * (1d - (1d / radius) * Math.abs((baseIntensity - intensity) / (gradient.mag)));
  418. } else {
  419. alpha = 0;
  420. }
  421.  
  422. voxelColor.r = voxelColor.r * (1 - alpha) + colorSetting.r * alpha;
  423. voxelColor.g = voxelColor.g * (1 - alpha) + colorSetting.g * alpha;
  424. voxelColor.b = voxelColor.b * (1 - alpha) + colorSetting.b * alpha;
  425. voxelColor.a = voxelColor.a * (1 - alpha) + alpha;
  426. }
  427. }
  428.  
  429. // BufferedImage expects a pixel color packed as ARGB in an int
  430. int c_alpha = voxelColor.a <= 1.0 ? (int) Math.floor(voxelColor.a * 255) : 255;
  431. int c_red = voxelColor.r <= 1.0 ? (int) Math.floor(voxelColor.r * 255) : 255;
  432. int c_green = voxelColor.g <= 1.0 ? (int) Math.floor(voxelColor.g * 255) : 255;
  433. int c_blue = voxelColor.b <= 1.0 ? (int) Math.floor(voxelColor.b * 255) : 255;
  434. int pixelColor = (c_alpha << 24) | (c_red << 16) | (c_green << 8) | c_blue;
  435. image.setRGB(i, j, pixelColor);
  436. }
  437. }
  438. }
  439.  
  440. }
  441.  
  442. private void drawBoundingBox(GL2 gl) {
  443. gl.glPushAttrib(GL2.GL_CURRENT_BIT);
  444. gl.glDisable(GL2.GL_LIGHTING);
  445. gl.glColor4d(1.0, 1.0, 1.0, 1.0);
  446. gl.glLineWidth(1.5f);
  447. gl.glEnable(GL.GL_LINE_SMOOTH);
  448. gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST);
  449. gl.glEnable(GL.GL_BLEND);
  450. gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
  451.  
  452. gl.glBegin(GL.GL_LINE_LOOP);
  453. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  454. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  455. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  456. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  457. gl.glEnd();
  458.  
  459. gl.glBegin(GL.GL_LINE_LOOP);
  460. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  461. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  462. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  463. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  464. gl.glEnd();
  465.  
  466. gl.glBegin(GL.GL_LINE_LOOP);
  467. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  468. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  469. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  470. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  471. gl.glEnd();
  472.  
  473. gl.glBegin(GL.GL_LINE_LOOP);
  474. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  475. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  476. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  477. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  478. gl.glEnd();
  479.  
  480. gl.glBegin(GL.GL_LINE_LOOP);
  481. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  482. gl.glVertex3d(-volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  483. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  484. gl.glVertex3d(volume.getDimX() / 2.0, volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  485. gl.glEnd();
  486.  
  487. gl.glBegin(GL.GL_LINE_LOOP);
  488. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  489. gl.glVertex3d(-volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  490. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, volume.getDimZ() / 2.0);
  491. gl.glVertex3d(volume.getDimX() / 2.0, -volume.getDimY() / 2.0, -volume.getDimZ() / 2.0);
  492. gl.glEnd();
  493.  
  494. gl.glDisable(GL.GL_LINE_SMOOTH);
  495. gl.glDisable(GL.GL_BLEND);
  496. gl.glEnable(GL2.GL_LIGHTING);
  497. gl.glPopAttrib();
  498.  
  499. }
  500.  
  501. @Override
  502. public void visualize(GL2 gl) {
  503.  
  504. if (volume == null) {
  505. return;
  506. }
  507.  
  508. drawBoundingBox(gl);
  509.  
  510. gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, viewMatrix, 0);
  511.  
  512. long startTime = System.currentTimeMillis();
  513.  
  514. switch(renderingMethod){
  515. case 0:
  516. slicer(viewMatrix);
  517. break;
  518. case 1:
  519. mip(viewMatrix);
  520. break;
  521. case 2:
  522. composite(viewMatrix);
  523. break;
  524. case 3:
  525. twoDTransfer(viewMatrix);
  526. break;
  527. default:
  528. slicer(viewMatrix);
  529. break;
  530. }
  531.  
  532. long endTime = System.currentTimeMillis();
  533. double runningTime = (endTime - startTime);
  534. panel.setSpeedLabel(Double.toString(runningTime));
  535.  
  536. Texture texture = AWTTextureIO.newTexture(gl.getGLProfile(), image, false);
  537.  
  538. gl.glPushAttrib(GL2.GL_LIGHTING_BIT);
  539. gl.glDisable(GL2.GL_LIGHTING);
  540. gl.glEnable(GL.GL_BLEND);
  541. gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
  542.  
  543. // draw rendered image as a billboard texture
  544. texture.enable(gl);
  545. texture.bind(gl);
  546. double halfWidth = image.getWidth() / 2.0;
  547. gl.glPushMatrix();
  548. gl.glLoadIdentity();
  549. gl.glBegin(GL2.GL_QUADS);
  550. gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  551. gl.glTexCoord2d(0.0, 0.0);
  552. gl.glVertex3d(-halfWidth, -halfWidth, 0.0);
  553. gl.glTexCoord2d(0.0, 1.0);
  554. gl.glVertex3d(-halfWidth, halfWidth, 0.0);
  555. gl.glTexCoord2d(1.0, 1.0);
  556. gl.glVertex3d(halfWidth, halfWidth, 0.0);
  557. gl.glTexCoord2d(1.0, 0.0);
  558. gl.glVertex3d(halfWidth, -halfWidth, 0.0);
  559. gl.glEnd();
  560. texture.disable(gl);
  561. texture.destroy(gl);
  562. gl.glPopMatrix();
  563.  
  564. gl.glPopAttrib();
  565.  
  566.  
  567. if (gl.glGetError() > 0) {
  568. System.out.println("some OpenGL error: " + gl.glGetError());
  569. }
  570.  
  571. }
  572. private BufferedImage image;
  573. private double[] viewMatrix = new double[4 * 4];
  574.  
  575. @Override
  576. public void changed() {
  577. for (int i=0; i < listeners.size(); i++) {
  578. listeners.get(i).changed();
  579. }
  580. }
  581. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement