Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (784)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (858)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to MultiThread in JOGL  (Read 5641 times)
0 Members and 1 Guest are viewing this topic.
Offline moeabdol

Innocent Bystander

« Posted 2012-11-02 02:33:18 »

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;
  2 import;
  3 import;
  5 public class GLWork implements GLRunnable{
  6         private float x;
  7         private float y;
  8         private boolean in;
 10         GLWork(){
 11                 x = 0.0f;
 12                 y = 0.0f;
 13                 in = false;
 14         }
 16         GLWork(float x, float y, boolean in){
 17                 this.x = x;
 18                 this.y = y;
 19        = in;
 20         }
 22         @Override
 23         public boolean run(GLAutoDrawable drawable){
 24                 GL2 gl = drawable.getGL().getGL2();
 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();
 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;
  2 import;
  4 public class PointWork implements Runnable{
  5         private GLAutoDrawable drawable;
  6         private GLRunnable runnable;
  8         PointWork(){
  9         }
 11         PointWork(float x, float y, boolean in, GLAutoDrawable drawable){
 12                 this.drawable = drawable;
 13                 this. runnable = new GLWork(x, y, in);
 14         }
 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 Smiley thank you
Offline Danny02
« Reply #1 - Posted 2012-11-02 09:29:12 »

I think what you are doing is quite right, I think you can loose the synchronize  because it doesn't have any use.
Also remember how the overall thread structure looks like.
In OpenGL you can only render with 1 thread only, so when you have a multithreading environment you have to build render tasks and submit them to the one real render thread.
These render taks are GLRunable like your GLWork when you call invoke on a GLAutoDrawable you enque the task to be rendered(read the javadoc for a more precise description). Now the final imported thing is the boolean flag on the invoke method. If you set it to true(like you did) your invoking thread will wait until the task has been processed by the OpenGL main thread, calling it with false makes the call asynchron.

In my opinion you probably shouldn't use this functionally for doing your main rendering of objects, but for small initialization stuff like resource loading.
For example I like to load all my game resources in another thread(so I can render a loading bar for example). When I load a texture in my resource thread I first read it from a file and then want to upload it to the GPU and this uploading is then easily achieved with such a render task.
Offline gouessej
« Reply #2 - Posted 2012-11-04 10:37:24 »

I already answered his question here:

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  

hadezbladez (1643 views)
2018-11-16 13:46:03

hadezbladez (659 views)
2018-11-16 13:41:33

hadezbladez (1638 views)
2018-11-16 13:35:35

hadezbladez (346 views)
2018-11-16 13:32:03

EgonOlsen (2695 views)
2018-06-10 19:43:48

EgonOlsen (2962 views)
2018-06-10 19:43:44

EgonOlsen (1653 views)
2018-06-10 19:43:20

DesertCoockie (2364 views)
2018-05-13 18:23:11

nelsongames (2273 views)
2018-04-24 18:15:36

nelsongames (2971 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!