Last active
August 29, 2015 14:10
-
-
Save blewert/1e1b02d1578787cb2bab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package test; | |
import java.io.File; | |
import javax.media.opengl.*; | |
import javax.media.opengl.glu.*; | |
/** | |
* For our purposes only two of the GLEventListeners matter. Those would be | |
* init() and display(). | |
*/ | |
public class SimpleEventListener implements GLEventListener | |
{ | |
private VolumetricDataSet dataset; | |
private GLU glu; | |
private int[] dimensions = new int[2]; | |
private int[][][] volumeData; | |
/** | |
* Take care of initialization here. | |
*/ | |
public void init(GLAutoDrawable drawable) | |
{ | |
GL gl = drawable.getGL(); | |
glu = new GLU(); | |
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
// Put camera at approprite position | |
gl.glMatrixMode(GL.GL_MODELVIEW); | |
gl.glLoadIdentity(); | |
glu.gluLookAt( | |
25, 15, 0, // eye | |
25, 15, 30, // at | |
0, 1, 0 // up | |
); | |
// Set up camera for Orthographic projection: | |
gl.glMatrixMode(GL.GL_PROJECTION); | |
gl.glLoadIdentity(); | |
try | |
{ | |
VolumetricDataSet dataset = new VolumetricDataSet(new File("marschnerlobb.raw"), 41, 41, 41); | |
this.dataset = dataset; | |
this.dimensions = dataset.getDimensions(); | |
this.volumeData = dataset.getVolumeData(); | |
} | |
catch(Exception e) | |
{ | |
} | |
} | |
/** | |
* Take care of drawing here. | |
*/ | |
public void display(GLAutoDrawable drawable) | |
{ | |
GL gl = drawable.getGL(); | |
gl.glColor3f(1, 1, 1); | |
gl.glPushMatrix(); | |
gl.glTranslatef(50f, 50f, 0f); | |
gl.glScaled(-1, 1, 1); | |
gl.glScaled(2, 2, 1); | |
int zStep = 30; | |
for(int x = 0; x < dimensions[0]; x++) | |
{ | |
for(int y = 0; y < dimensions[1]; y++) | |
{ | |
int valueAtPoint = volumeData[x][y][zStep]; | |
gl.glColor3f(valueAtPoint / 255f, valueAtPoint / 255f, valueAtPoint / 255f); | |
drawPixel(gl, x, y); | |
} | |
} | |
gl.glPopMatrix(); | |
} | |
private void drawPixel(GL gl, int x, int y) | |
{ | |
gl.glBegin(gl.GL_QUADS); | |
gl.glVertex3i(x, y, 0); | |
gl.glVertex3i(x + 1, y, 0); | |
gl.glVertex3i(x + 1, y + 1, 0); | |
gl.glVertex3i(x, y + 1, 0); | |
gl.glEnd(); | |
} | |
/** | |
* Called when the GLAutoDrawable (GLCanvas or GLJPanel) has changed in size. We won't | |
* need this, but you will eventually need it -- just not yet. | |
*/ | |
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) | |
{ | |
GL gl = drawable.getGL(); | |
gl.glMatrixMode(gl.GL_PROJECTION); | |
gl.glLoadIdentity(); | |
gl.glOrtho(0, width, 0, height, 0, -1.0); | |
} | |
/** | |
* If the display depth is changed while the program is running this method is called. | |
* Nowadays this doesn't happen much, unless a programmer has his program do it. | |
*/ | |
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment