Active rendering with JOGL

NB: this post is due to a question in JOGL board, as it is a shared code, I thought it should be good to post it here too

This explain a simple way to make active rendering with JOGL without GLEventListener

Render class:


import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

public final class RenderJOGL 
{	
	private GLContext context;
	private GLCanvas canvas;
	private GL gl;
	private GLU glu;

	public Canvas getCanvas()
	{
		return this.canvas;
	}
	
	public RenderJOGL() throws Throwable
	{
		GLCapabilities glCaps = new GLCapabilities();	
		glCaps.setRedBits(8);
		glCaps.setBlueBits(8);
		glCaps.setGreenBits(8);
		glCaps.setAlphaBits(8);
		glCaps.setDoubleBuffered(true);
		glCaps.setHardwareAccelerated(true);

		this.canvas = new GLCanvas(glCaps);
		this.canvas.setAutoSwapBufferMode(false);
		this.glu=new GLU();

		this.context=this.canvas.getContext();
		this.gl = this.context.getGL();
	}
	
	public void setSize(int viewPixelWidth,int viewPixelHeight)//,int maxAntialias)
	{
		this.canvas.setSize(viewPixelWidth,viewPixelHeight);
	}		
	
	public void render()
	{	
		this.makeContentCurrent();

		this.gl.glDisable(GL.GL_DEPTH_TEST);
		gl.glClearColor(0.0f,0.0f,1.0f,0.0f);	//Background blue
        		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        		gl.glLoadIdentity();	
        		gl.glColor3f(1.0f, 0.0f, 0.0f);	//Color red
        		gl.glBegin(GL.GL_TRIANGLES);
        		gl.glVertex3f(0,0,0.0f);
        		gl.glVertex3f(1,0,0.0f);
        		gl.glVertex3f(0,1,0.0f);        
        		gl.glEnd();	

        		this.context.release();
		this.canvas.swapBuffers();
	}
	
	private void makeContentCurrent()
	{	
		try 
		{
			while (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) 
			{
				System.out.println("Context not yet current...");
				Thread.sleep(100);
			}
		}
		catch (InterruptedException e)
		{
			 e.printStackTrace(); 
		}
	} 			
}

AppletTest class:

 
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.awt.event.*;

public class AppletTest extends Applet implements Runnable
{
	private int nbloop;
	private RenderJOGL render;
    
	private long startTime;
	private Thread process;
	private int nbStart;
	

	public void init() 
	{
		this.nbStart=0;
	}
	
	public void start() 
	{
		//Ensure that start is only called once
		if(this.nbStart!=0)
			return;
		this.nbStart++;
		
		this.render=new RenderJOGL();		
		
		//Add Render canvas to this applet
		this.setLayout(null);
		Canvas canvas=this.render.getCanvas();
		this.add(canvas);
		this.render.setSize(this.getWidth(),this.getHeight());
		
		//Start rendering Thread
		this.process=new Thread(this);
		this.process.start();
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				this.render.render(); 
				Thread.sleep(10);
			}
		}
		catch(InterruptedException ie)
		{
		}
	}
		
}

EDIT: Maybe you will have some few modifications to make it works properly, as it is a Class comming from 3DzzD API, simplified for a better understanding, so I havn’t tested it…

let me know if it helps!

Do you really think active rendering can improve the performance? Instead of sharing 3DzzD source code by small pieces, why don’t you open the whole source code???

[quote]Do you really think active rendering can improve the performance?
[/quote]
No, it don’t think it will hugely improve performance but I found that give a better control over the rendering process.

[quote]Instead of sharing 3DzzD source code by small pieces, why don’t you open the whole source code???
[/quote]
hey! :slight_smile: that’s exacly what I was thinking about this week !?, I dont really know the steps to do this, I mean steps to make a project opensourced in a good way ?

what I am affraid about is multiplicity of version, bugs add, and such !?

anyway, you are right I must share the whole and that what I am going to do, just need to found a good way to do that, It will be a lot better experience for user and will give a better control.

any idea to how opensourcing the whole in a good way are welcome, this is not really a world I know ?

Look at my source code, I only put the license (the complete text) in the ZIP and I put a reference to this in each file. I chose the GPL license version 2 to be sure that anyone can use my source code if he accepts to share his own source code too.

Thanks, I will look on it, It may take up to two weeks for me too opensource the whole 3DzzD project as I would like to give public a clean project and because I dont work a lot on it for now as I am quite busy :(.