Hey,
I’m trying to learn how to use JOGL and it’s been going pretty-well so-far. I’ve modified most of the core code of a particle system to use JOGL to do all of the rendering.
The only problem I have now is getting something to actually render to the screen. The first piece of code below is the Driver class that I’ve hacked together. From all of the example I’ve seen, it should work. Although it’s entirely possible that I’ve messed something up.
The second piece of code is the render method used on all particles. The render() method call in the display() method of the Driver class below ends up calling the render methods of all of the particles.
The only changes made to the program were to the Driver class, the render() method of the Particle class, and to the few render() methods that just pass along variables until the render() methods of the particles are called.
package core;
import com.jogamp.opengl.util.Animator;
import effect.Effect;
import effect.RainbowSnow;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import java.awt.*;
public class Driver implements GLEventListener {
// Testing Stuff:
private static Effect[] effect = new Effect[1];
// End Testing Stuff.
public static void main(String[] args) {
GLProfile.initSingleton();
GLProfile glp = GLProfile.get(GLProfile.GL2);
GLCapabilities caps = new GLCapabilities(glp);
// Create the frame.
Frame frame = new Frame();
frame.setTitle("Particle Test");
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.requestFocus();
/*
// Create the screen.
Screen screen = new Screen();
screen.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
screen.setFocusable(true);
screen.setVisible(true);
screen.setBackground(Color.black);
frame.add(screen);
frame.setVisible(true);
screen.start();
*/
GLCanvas screen = new GLCanvas(caps);
screen.addGLEventListener(new Driver());
screen.setVisible(true);
frame.add(screen);
frame.setVisible(true);
// Testing Stuff:
effect[0] = new RainbowSnow(0.0f, 0.0f, (short)1920);
// effect[0] = new SplitWave(0.0, 0.0, 1920);
// effect[0] = new Snow(0.0, 0.0, 1920);
// effect[0] = new Fire(512.0, 512.0);
// End Testing Stuff.
Animator animator = new Animator(screen);
animator.start();
}
@Override
public void init(GLAutoDrawable drawable) {
// put your OpenGL initialization code here
drawable.getGL().setSwapInterval(1); // Ennable V-Sync
}
@Override
public void dispose(GLAutoDrawable drawable) {
// put your cleanup code here
}
@Override
public void display(GLAutoDrawable drawable) {
update();
render(drawable);
}
// When called this updates all of the game's logic.
private void update() {
for(Effect e : effect) {
e.update();
}
/*
if(KEY.isKeyPressed(KeyEvent.VK_ESCAPE) || KEY.isKeyPressed(KeyEvent.VK_ALT) && KEY.isKeyPressed(KeyEvent.VK_F4)) {
isProgramRunning = false;
System.exit(0);
}
*/
}
// When called this updates the screen.
private void render(final GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
for(Effect e : effect) {
e.render(gl);
}
gl.glFlush();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// called when user resizes the window
}
}
/**
* Renders the particle to the screen.
* @param gl The graphics object to render with.
* @param isOval Whether or not to render the particle as an oval.
*/
public void render(final GL2 gl, final boolean isOval) {
if(isOval) {
// TODO Figure out the math for this.
} else {
gl.glBegin(GL2.GL_POLYGON);
gl.glColor4f(color.getRed() / 255, color.getGreen() / 255, color.getBlue() / 255, color.getAlpha() / 100);
gl.glVertex2f(xCurrent, yCurrent); // Top Left
gl.glVertex2f(xCurrent + SIZE, yCurrent); // Top Right
gl.glVertex2f(xCurrent + SIZE, yCurrent + SIZE); // Bottom Right
gl.glVertex2f(xCurrent, yCurrent + SIZE); // Bottom Left
gl.glEnd();
}
}
If anyone can spot an error or can tell me what I may have done wrong, thanks.