I have been working on a JOGL program for a few weeks, and I found that the init() method in my event listener is never being called. If anyone could help me out with this I would greatly appreciate it.
Main Class:
package mainLoop;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.FPSAnimator;
public class MainFrame extends JFrame {
public static MainFrame frame;
public static JPanel panel;
public static GLCapabilities glCaps;
public static GLCanvas canvas;
public static FPSAnimator animator;
public MainFrame(String s)
{
super(s);
}
public static void main(String[] args)
{
frame = new MainFrame("GL Test");
frame.setSize(1280, 720);
panel = new JPanel();
panel.setSize(1280, 720);
frame.add(panel);
frame.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
GLProfile.initSingleton();
glCaps = new GLCapabilities(GLProfile.get(GLProfile.GL3));
canvas = new GLCanvas(glCaps);
canvas.addGLEventListener(new JOGLEventListener());
animator = new FPSAnimator(60);
panel.add(canvas);
animator.add(canvas);
animator.start();
frame.setVisible(true);
}
}
Event Listener Class:
package mainLoop;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import com.jogamp.opengl.util.PMVMatrix;
import shaders.ShaderLoader;
public class JOGLEventListener implements GLEventListener{
GL3 gl;
@Override
public void display(GLAutoDrawable arg0)
{
gl = (GL3) arg0.getGL();
gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
}
@Override
public void dispose(GLAutoDrawable arg0)
{
}
@Override
public void init(GLAutoDrawable gld)
{
gl = (GL3) gld.getGL();
int[] array = new int[1];
gl.glGenVertexArrays(1, array, 0);
gl.glBindVertexArray(array[0]);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
int[] buffer = new int[1];
gl.glGenBuffers(1, buffer, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[0]);
FloatBuffer vertices = FloatBuffer.allocate(9);
vertices.put(1);
vertices.put(0);
vertices.put(-3);
vertices.put(0);
vertices.put(0);
vertices.put(-3);
vertices.put(0);
vertices.put(1);
vertices.put(-3);
vertices.rewind();
gl.glBufferData(GL.GL_ARRAY_BUFFER, 12 * Float.BYTES, vertices, GL.GL_STATIC_DRAW);
try {
gl = ShaderLoader.getShaderProgram((GL3) gl);
} catch (IOException e) {
System.exit(0);
e.printStackTrace();
}
//make matrices
PMVMatrix matrixMaker = new PMVMatrix();
matrixMaker.gluLookAt(10, 10, 10, 0, 0, 0, -1, -1, 1);
matrixMaker.gluPerspective(90, 16.0f/9.0f, 0.1f, 100);
//apply to uniform
int tempLoc = gl.glGetUniformLocation(ShaderLoader.finalShader, "uniform_Projection");
gl.glUniformMatrix4fv(tempLoc, 1, false, matrixMaker.glGetMatrixf());
//set up attributes
tempLoc = gl.glGetAttribLocation(ShaderLoader.finalShader, "attribute_Position");
gl.glVertexAttribPointer(tempLoc, 3, GL.GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(array[0]);
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4)
{
}
}
If it helps, I am using java 8 on windows 8.1