Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (208)
games submitted by our members
Games in WIP (781)
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 3762 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:

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Galdo (420 views)
2017-01-12 13:44:09

Archive (575 views)
2017-01-02 05:31:41

0AndrewShepherd0 (1048 views)
2016-12-16 03:58:39

0AndrewShepherd0 (972 views)
2016-12-15 21:50:57

Lunch (1104 views)
2016-12-06 16:01:40

ral0r2 (1339 views)
2016-11-23 16:08:26

ClaasJG (1433 views)
2016-11-10 17:36:32

CoffeeChemist (1420 views)
2016-11-05 00:46:53

jay4842 (1503 views)
2016-11-01 19:04:52

theagentd (1252 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!