JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JOGLRenderer.java
Go to the documentation of this file.
1 
2 package jarmos.pc.visual;
3 
6 
7 import javax.media.opengl.GL2;
8 import javax.media.opengl.GLAutoDrawable;
9 import javax.media.opengl.GLEventListener;
10 
20 public class JOGLRenderer extends OpenGLBase implements GLEventListener {
21 
22  public JOGLRenderer(VisualizationData vData, int width, int height) {
23  super(vData, width, height);
24  }
25 
29  @Override
30  public void display(GLAutoDrawable drawable) {
31  // clear the screen to black (0,0,0) color
32  GL2 gl = drawable.getGL().getGL2();
33  gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
34  gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
35 
36  if (!is2D()) // enable depth rb.test for 3D rendering
37  gl.glEnable(GL2.GL_DEPTH_TEST);
38 
39  if ((isFrontFace) || (is2D())) {
40  // enable blending (for rendering wireframe)
41  if (!is2D())
42  gl.glDisable(GL2.GL_CULL_FACE);
43  gl.glFrontFace(GL2.GL_CCW);
44  } else {
45  gl.glEnable(GL2.GL_CULL_FACE);
46  gl.glFrontFace(GL2.GL_CW);
47  }
48 
49  // reset transformation matrix
50  gl.glLoadIdentity();
51 
52  // setup Light
53  if (!is2D()) {
54  gl.glEnable(GL2.GL_LIGHTING); // Enable light
55  gl.glEnable(GL2.GL_LIGHT0); // turn on the light
56  gl.glEnable(GL2.GL_COLOR_MATERIAL); // turn on color lighting
57 
58  // material shininess
59  gl.glMaterialf(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 128);
60 
61  // ambient light
62  float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
63  gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, lightAmbient, 0);
64  // diffuse light
65  float lightDiffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
66  gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightDiffuse, 0);
67  // specular light
68  float[] lightSpecular = { 0.7f, 0.7f, 0.7f, 1.0f };
69  gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, lightSpecular, 0);
70  // light position
71  float[] lightPosition = { -getBoxSize(), -getBoxSize(), 0.0f, 0.0f };
72  gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPosition, 0);
73  // light direction
74  float[] lightDirection = { getBoxSize(), getBoxSize(), getBoxSize(), 0.0f };
75  gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPOT_DIRECTION, lightDirection, 0);
76  // 90 degree FOV
77  gl.glLightf(GL2.GL_LIGHT0, GL2.GL_SPOT_CUTOFF, 45.0f);
78 
79  // using our normal data
80  floatBuf.position(getCurrentNormalsOffset());
81  gl.glNormalPointer(GL2.GL_FLOAT, 0, floatBuf);
82  }
83 
84  // zoom in/out the model
86 
87  /*
88  * Control view 2D: we just move the object around 3D: we rotate the
89  * object
90  */
91  if (is2D()) {
92  gl.glTranslatef(getXTranslation(), getYTranslation(), 0);
93  } else {
94  gl.glMultMatrixf(getRotationMatrix(), 0);
95  }
96 
97  /*
98  * Set pointer to vertex data Always uses currentFrame, which is zero in
99  * case of no animation.
100  */
101  floatBuf.position(getCurrentVertexOffset());
102  gl.glVertexPointer(3, GL2.GL_FLOAT, 0, floatBuf);
103 
104  /*
105  * specify the color data for the current frame Four values each: R, G,
106  * B, Alpha
107  */
108  floatBuf.position(getCurrentColorOffset());
109  gl.glColorPointer(4, GL2.GL_FLOAT, 0, floatBuf);
110 
111  /*
112  * Draw the elements using the above declared nodes and color data
113  */
114  shortBuf.position(getFaceOffset());
115  gl.glDrawElements(GL2.GL_TRIANGLES, getNumFaces() * 3, GL2.GL_UNSIGNED_SHORT, shortBuf);
116 
117  // Draw the wireframe for a n field object
118  // vData.isConstantFeature(currentColorField))
119  if (!is2D()) {
120  // Draw the wireframe mesh
121  gl.glColor4f(0.1f, 0.1f, 0.1f, 0.5f);
122  shortBuf.position(getCurrentWireframeOffset());
123  gl.glDrawElements(GL2.GL_LINES, getNumFaces() * 6, GL2.GL_UNSIGNED_SHORT, shortBuf);
124  }
125 
126  frameRendered();
127  }
128 
129  /*
130  * (non-Javadoc)
131  *
132  * @see
133  * javax.media.opengl.GLEventListener#dispose(javax.media.opengl.GLAutoDrawable
134  * )
135  */
136  @Override
137  public void dispose(GLAutoDrawable drawable) {
138  // TODO Auto-generated method stub
139 
140  }
141 
142  /*
143  * (non-Javadoc)
144  *
145  * @see
146  * javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable
147  * )
148  */
149  @Override
150  public void init(GLAutoDrawable drawable) {
151  initRendering();
152 
153  // define the color we want to be displayed as the "clipping wall"
154  GL2 gl = drawable.getGL().getGL2();
155  gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
156  gl.glMatrixMode(GL2.GL_PROJECTION);
157 
158  // orthographic view
159  float[] o = getOrtographicProj();
160  gl.glOrthof(o[0], o[1], o[2], o[3], o[4], o[5]);
161 
162  gl.glViewport(0, 0, getWidth(), getHeight());
163  gl.glMatrixMode(GL2.GL_MODELVIEW);
164 
165  // define the color we want to be displayed as the "clipping wall"
166  // gl.glClearColor(0f, 0f, 0f, 1.0f);
167  gl.glClearColor(1f, 1f, 1f, 1.0f);
168 
169  // enable the differentiation of which side may be visible
170  gl.glEnable(GL2.GL_CULL_FACE);
171  // which is the front? the one which is drawn counter clockwise
172  gl.glFrontFace(GL2.GL_CCW);
173  // which one should NOT be drawn
174  gl.glCullFace(GL2.GL_BACK);
175 
176  // Switch on client states in order to make GL10 use the vertex and
177  // color data
178  gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
179  gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
180 
181  // Enable normal for 3D object
182  if (!is2D())
183  gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
184  }
185 
190  @Override
191  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
192  setSize(w, h);
193  GL2 gl = drawable.getGL().getGL2();
194  gl.glViewport(0, 0, w, h);
195  }
196 
197 }
void initRendering()
Initializes the rendering process (Vertex, face, color and normal openGL buffers) ...
void display(GLAutoDrawable drawable)
void init(GLAutoDrawable drawable)
void setSize(int width, int height)
void dispose(GLAutoDrawable drawable)
boolean isFrontFace
Flag that indicates whether the front face should be rendered or not.
Definition: OpenGLBase.java:76
A container class for all model visual data.
Provides the OpenGL rendering routines for the JOGL package.
JOGLRenderer(VisualizationData vData, int width, int height)
void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
Base class for OpenGL visualisation.
Definition: OpenGLBase.java:18