JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
GLRenderer.java
Go to the documentation of this file.
1 package jarmos.app.visual;
2 
5 
6 import javax.microedition.khronos.egl.EGLConfig;
7 import javax.microedition.khronos.opengles.GL10;
8 
9 import android.opengl.GLSurfaceView.Renderer;
10 
21 public class GLRenderer extends OpenGLBase implements Renderer {
22 
23  public GLRenderer(VisualizationData vData) {
24  super(vData);
25  }
26 
27  public GLRenderer(VisualizationData vData, int width, int height) {
28  super(vData, width, height);
29  }
30 
34  @Override
35  public void onDrawFrame(GL10 gl) {
36  // clear the screen to black (0,0,0) color
37  // gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
38  gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
39  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
40 
41  if (!is2D()) // enable depth rb.test for 3D rendering
42  gl.glEnable(GL10.GL_DEPTH_TEST);
43 
44  if (isFrontFace || is2D()) {
45  // enable blending (for rendering wireframe)
46  if (!is2D())
47  gl.glDisable(GL10.GL_CULL_FACE);
48  gl.glFrontFace(GL10.GL_CCW);
49  } else {
50  gl.glEnable(GL10.GL_CULL_FACE);
51  gl.glFrontFace(GL10.GL_CW);
52  }
53 
54  // reset transformation matrix
55  gl.glLoadIdentity();
56 
57  // setup Light
58  if (!is2D()) {
59  gl.glEnable(GL10.GL_LIGHTING); // Enable light
60  gl.glEnable(GL10.GL_LIGHT0); // turn on the light
61  gl.glEnable(GL10.GL_COLOR_MATERIAL); // turn on color lighting
62 
63  // material shininess
64  gl.glMaterialx(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 128);
65  // ambient light
66  float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
67  gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
68  // diffuse light
69  float lightDiffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
70  gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
71  // specular light
72  float[] lightSpecular = { 0.7f, 0.7f, 0.7f, 1.0f };
73  gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecular, 0);
74  // light position
75  float[] lightPosition = { -getBoxSize(), -getBoxSize(), 0.0f, 0.0f };
76  gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
77  // light direction
78  float[] lightDirection = { getBoxSize(), getBoxSize(), getBoxSize(), 0.0f };
79  gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, lightDirection, 0);
80  // 90 degree FOV
81  gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_CUTOFF, 45.0f);
82 
83  // using our normal data
84  floatBuf.position(getCurrentNormalsOffset());
85  gl.glNormalPointer(GL10.GL_FLOAT, 0, floatBuf);
86  }
87 
88  // zoom in/out the model
90 
91  /*
92  * touchscreen control Rotation, zoom etc
93  */
94  if (is2D()) {
95  gl.glTranslatef(getXTranslation(), getYTranslation(), 0);
96  } else {
97  gl.glMultMatrixf(getRotationMatrix(), 0);
98  }
99 
100  /*
101  * Set pointer to vertex data Always uses currentFrame, which is zero in
102  * case of no animation.
103  */
104  floatBuf.position(getCurrentVertexOffset());
105  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, floatBuf);
106 
107  /*
108  * specify the color data for the current frame Four values each: R, G,
109  * B, Alpha
110  */
111  floatBuf.position(getCurrentColorOffset());
112  gl.glColorPointer(4, GL10.GL_FLOAT, 0, floatBuf);
113 
114  /*
115  * Draw the elements using the above declared nodes and color data
116  */
117  shortBuf.position(getFaceOffset());
118  gl.glDrawElements(GL10.GL_TRIANGLES, getNumFaces() * 3, GL10.GL_UNSIGNED_SHORT, shortBuf);
119 
120  // Draw the wireframe for a n field object
121  if (!is2D() && !isFrontFace) {
122  // Draw the wireframe mesh
123  gl.glColor4f(0.1f, 0.1f, 0.1f, 0.5f);
124  shortBuf.position(getCurrentWireframeOffset());
125  gl.glDrawElements(GL10.GL_LINES, getNumFaces() * 6, GL10.GL_UNSIGNED_SHORT, shortBuf);
126  }
127 
128  frameRendered();
129  }
130 
134  @Override
135  public void onSurfaceChanged(GL10 gl, int w, int h) {
136  setSize(w, h);
137  gl.glViewport(0, 0, w, h);
138  }
139 
144  @Override
145  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
146  /*
147  * Calls internal rendering preparations involving data from the
148  * GLObject
149  */
150  initRendering();
151 
152  // define the color we want to be displayed as the "clipping wall"
153  gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
154  gl.glMatrixMode(GL10.GL_PROJECTION);
155 
156  // orthographic view
157  float[] o = getOrtographicProj();
158  gl.glOrthof(o[0], o[1], o[2], o[3], o[4], o[5]);
159 
160  gl.glViewport(0, 0, getWidth(), getHeight());
161  gl.glMatrixMode(GL10.GL_MODELVIEW);
162 
163  // define the color we want to be displayed as the "clipping wall"
164  // gl.glClearColor(0f, 0f, 0f, 1.0f);
165  gl.glClearColor(1f, 1f, 1f, 1.0f);
166 
167  // enable the differentiation of which side may be visible
168  gl.glEnable(GL10.GL_CULL_FACE);
169  // which is the front? the one which is drawn counter clockwise
170  gl.glFrontFace(GL10.GL_CCW);
171  // which one should NOT be drawn
172  gl.glCullFace(GL10.GL_BACK);
173 
174  // Switch on client states in order to make GL10 use the vertex and
175  // color data
176  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
177  gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
178 
179  // Enable normal for 3D object
180  if (!is2D())
181  gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
182  }
183 
184  // @Test
185  // public void testGL() {
186  // FileModelManager f = new FileModelManager("models");
187  // try {
188  // f.useModel("demo1");
189  // } catch (ModelManagerException e) {
190  // e.printStackTrace();
191  // fail(e.getMessage());
192  // }
193  //
194  // RBContainer rb = new RBContainer();
195  // assertTrue(rb.loadModel(f));
196  //
197  // // Perform the solve
198  // RBSystem s=rb.mRbSystem;
199  // double[] par = s.getParams().getRandomParam();
200  // // double[] par = new double[]{.5, .5};
201  // s.getParams().setCurrent(par);
202  // s.solveRB(s.getNBF()/2);
203  //
204  // SimulationResult res = s.getSimulationResults();
205  // GeometryData g = rb.mRbSystem.getGeometry();
206  // VisualizationData v = new VisualizationData(g);
207  // v.useResult(res);
208  //
209  // v.computeVisualFeatures(new ColorGenerator());
210  //
211  // GLRenderer gl = new GLRenderer(v);
212  // gl.initRendering();
213  // }
214 }
void initRendering()
Initializes the rendering process (Vertex, face, color and normal openGL buffers) ...
void onSurfaceChanged(GL10 gl, int w, int h)
void setSize(int width, int height)
boolean isFrontFace
Flag that indicates whether the front face should be rendered or not.
Definition: OpenGLBase.java:76
GLRenderer(VisualizationData vData)
Definition: GLRenderer.java:23
OpenGL renderer implementation using the khronos OpenGL ES android implementation.
Definition: GLRenderer.java:21
void onSurfaceCreated(GL10 gl, EGLConfig config)
A container class for all model visual data.
GLRenderer(VisualizationData vData, int width, int height)
Definition: GLRenderer.java:27
Base class for OpenGL visualisation.
Definition: OpenGLBase.java:18