Multi-threaded JOGL Problem

Posted by moeabdol on Game Development See other posts from Game Development or by moeabdol
Published on 2012-10-08T11:40:04Z Indexed on 2012/10/08 15:54 UTC
Read the original article Hit count: 209

Filed under:
|

I'm writing a simple OpenGL application in Java that implements the Monte Carlo method for estimating the value of PI. The method is pretty easy. Simply, you draw a circle inside a unit square and then plot random points over the scene. Now, for each point that is inside the circle you increment the counter for in points. After determining for all the random points wither they are inside the circle or not you divide the number of in points over the total number of points you have plotted all multiplied by 4 to get an estimation of PI. It goes something like this PI = (inPoints / totalPoints) * 4. This is because mathematically the ratio of a circle's area to a square's area is PI/4, so when we multiply it by 4 we get PI.

My problem doesn't lie in the algorithm itself; however, I'm having problems trying to plot the points as they are being generated instead of just plotting everything at once when the program finishes executing. I want to give the application a sense of real-time display where the user would see the points as they are being plotted. I'm a beginner at OpenGL and I'm pretty sure there is a multi-threading feature built into it. Non the less, I tried to manually create my own thread. Each worker thread plots one point at a time. Following is the psudo-code:

/* this part of the code exists in display() method in MyCanvas.java
which extends GLCanvas and implements GLEventListener */

// main loop
for(int i = 0; i < number_of_points; i++){
    RandomGenerator random = new RandomGenerator();
    float x = random.nextFloat();
    float y = random.nextFloat();
    Thread pointThread = new Thread(new PointThread(x, y));
}

gl.glFlush();

/* this part of the code exists in run() method in 
PointThread.java which implements Runnable */
void run(){
    try{
        gl.glPushMatrix();
        gl.glBegin(GL2.GL_POINTS);
            if(pointIsIn)
                gl.glColor3f(1.0f, 0.0f, 0.0f);   // red point
            else
                gl.glColor3f(0.0f, 0.0f, 1.0f);   // blue point
            gl.glVertex3f(x, y, 0.0f);   // coordinates
        gl.glEnd();
        gl.glPopMatrix();
    }catch(Exception e){
    }
}

I'm not sure if my approach to solving this issue is correct. I hope you guys can help me out. Thanks.

© Game Development or respective owner

Related posts about multithreading

Related posts about jogl