Dear All,
I have been trying so hard to wrap my head around the multithreading concept in JOGL. I looked into many documentations and it seems to me that every documentation I go through passes me to a next one without me finding a complete and simple code that implements multithreaded rendering.
The program that i’m trying ti implement is simple. Within the class that implement GLEventListener and inside the display() method I have a simple algorithm which is a for loop. Inside the loop am generating random x and y coordinates. What I want to do is draw these points as they are being determined by the random generator to give the viewer a sense of how the points are generated in real-time.
To solve this, I have read documentations on JOGL’s website that suggested using GLWindow instead of a JFrame to enable concurrency. I did that! Moreover, I have created a work class that implements a GLRunnable this class includes all the code necessary to do the work of drawing each point following is the content of this class
1 import javax.media.opengl.GLRunnable;
2 import javax.media.opengl.GLAutoDrawable;
3 import javax.media.opengl.GL2;
4
5 public class GLWork implements GLRunnable{
6 private float x;
7 private float y;
8 private boolean in;
9
10 GLWork(){
11 x = 0.0f;
12 y = 0.0f;
13 in = false;
14 }
15
16 GLWork(float x, float y, boolean in){
17 this.x = x;
18 this.y = y;
19 this.in = in;
20 }
21
22 @Override
23 public boolean run(GLAutoDrawable drawable){
24 GL2 gl = drawable.getGL().getGL2();
25
26 gl.glPointSize(10.0f);
27 gl.glPushMatrix();
28 gl.glBegin(GL2.GL_POINTS);
29 if(in)
30 gl.glColor3f(1.0f, 0.0f, 0.0f);
31 else
32 gl.glColor3f(0.0f, 0.0f, 1.0f);
33 gl.glVertex3f(x, y, 0.0f);
34 gl.glEnd();
35 gl.glPopMatrix();
36
37 return true;
38 }
39 }
Following I created another work class that implements Runnable and has a private variable of type GLWork. following is the code
1 import javax.media.opengl.GLAutoDrawable;
2 import javax.media.opengl.GLRunnable;
3
4 public class PointWork implements Runnable{
5 private GLAutoDrawable drawable;
6 private GLRunnable runnable;
7
8 PointWork(){
9 }
10
11 PointWork(float x, float y, boolean in, GLAutoDrawable drawable){
12 this.drawable = drawable;
13 this. runnable = new GLWork(x, y, in);
14 }
15
16 public void run(){
17 synchronized(drawable){
18 drawable.invoke(true, runnable);
19 }
20 }
21 }
Note that I have synchronized the GLAutoDrawable so no other thread can draw on the drawable while this thread is. I’m not really sure if this is the correct way to do it or not.
Now, for the simple algorithm that does the creation of each point and instantiating threads to draw the point. This is inside the display() method.
57 float x; // x coordinate
58 float y; // y coordinate
59 boolean in; // true if point is inside circle
60 int inCount = 0; // counter for points that are in
61 float pi; // estimated pi value
62 int totalPoints = 1000; // total number of random points
63 Random floatRandomGenerator = new Random(); // random number generator
64 for(int i = 0; i < totalPoints; i++){
65 x = floatRandomGenerator.nextFloat();
66 y = floatRandomGenerator.nextFloat();
67 if((Math.pow(x - 0.5, 2) + Math.pow(y - 0.5, 2)) <= Math.pow(RADIUS, 2)){
68 in = true;
69 inCount++;
70 }
71 else
72 in = false;
73 new Thread(new PointWork(x, y, in, drawable)).start();
74 }
The in variable is just to determine if I should draw the point in red or blue.
I would really appreciate if someone can provide me with a simple general purpose example of how to implement multi-threaded rendering.
I hope that my explanation and code is easy to follow and understand, and I hope that someone out there can help me with this issue. Once I understand how to multithread in JOGL I will be able to do many awesome stuff thank you