Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
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 2192 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 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 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:
http://forum.jogamp.org/How-to-MultiThread-in-JOGL-td4026697.html

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (116 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org 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‑gaming.org
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!