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!