This is my first post on JGO so I apologize if it seems weirdly worded.
So for the past week or so I have been working on my first JOGL program. Lots of information on the internet is conflicting, so I am not entirely sure I am doing this right. I am trying to render a single triangle and all I get is just a blank frame. If anybody could take a look at my code and point me in the right direction it would be greatly appreciated. This is using java 8 if it makes any difference.
Main Class: (MainFrame)
[spoiler]
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;
public class MainFrame extends JFrame {
public static MainFrame frame;
public static JPanel panel;
public static GLCapabilities glCaps;
public static GLCanvas canvas;
public static Animator 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);
frame.setVisible(true);
GLProfile.initSingleton();
glCaps = new GLCapabilities(GLProfile.get(GLProfile.GL3));
canvas = new GLCanvas(glCaps);
canvas.addGLEventListener(new JOGLEventListener());
animator = new Animator(canvas);
panel.add(canvas);
animator.start();
}
}
[/spoiler]
Event Listener Class: (JOGLEventListener)
[spoiler]
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)
{
}
}
[/spoiler]
Shader Handling Class: (ShaderLoader)
[spoiler]
package shaders;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.media.opengl.GL3;
public class ShaderLoader {
public static String[] vertexShader;
public static String[] fragmentShader;
public static int vertShader;
public static int fragShader;
public static int finalShader;
public static GL3 getShaderProgram(GL3 gl) throws IOException
{
vertShader = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
fragShader = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
BufferedReader questionReader = new BufferedReader(new InputStreamReader(ShaderLoader.class.getResourceAsStream("VTest.glsl")));
String buffLine = questionReader.readLine();
while( buffLine != null )
{
vertexShader[0] += (buffLine + System.lineSeparator());
buffLine = questionReader.readLine();
}
questionReader = new BufferedReader(new InputStreamReader(ShaderLoader.class.getResourceAsStream("FTest.glsl")));
buffLine = questionReader.readLine();
while( buffLine != null )
{
fragmentShader[0] += (buffLine + System.lineSeparator());
buffLine = questionReader.readLine();
}
gl.glShaderSource(vertShader, 1, vertexShader, null, 0);
gl.glCompileShader(vertShader);
gl.glShaderSource(fragShader, 1, vertexShader, null, 0);
gl.glCompileShader(fragShader);
finalShader = gl.glCreateProgram();
gl.glAttachShader(finalShader, vertShader);
gl.glAttachShader(finalShader, fragShader);
gl.glLinkProgram(finalShader);
gl.glUseProgram(finalShader);
return gl;
}
}
[/spoiler]
Shaders:
Vertex Shader: (VTest.glsl)
#version 400
uniform mat4 uniform_Projection;
attribute vec4 attribute_Position;
void main()
{
gl_Position = uniform_Projection * attribute_Position;
}
Fragment Shader: (FTest.glsl)
#version 400
void main()
{
gl_FragColor = vec4(1, 1, 0.5, 1);
}
Thanks in advance to anyone who is knowledgeable on this subject.