Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / JOGL Development / How to MultiThread in JOGL on: 2012-11-02 03: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
Pages: [1]
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.074 seconds with 21 queries.