May someone help me to find out why this code doesn't work?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

May someone help me to find out why this code doesn't work?

Peter Dinklemore
I'm quite new to JOGL and though I have built some smaller applications with it I can't get my code to work when using OpenGL 4.

The two tutorials I'm following mainly are https://learnopengl.com/#!Getting-started/Hello-Triangle and http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/.

HelloOpenGL.java:
import java.awt.Dimension;

import javax.swing.JFrame;

import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLJPanel;
import com.jogamp.opengl.util.FPSAnimator;

public final class HelloOpenGL {
	public static final void main(final String[] args) {
		final JFrame frame = new JFrame("Hello, OpenGL!");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);

		final GLProfile glProfile = GLProfile.get(GLProfile.GL4);
		final GLCapabilities glCapabilities = new GLCapabilities(glProfile);
		final GLJPanel glJPanel = new GLJPanel(glCapabilities);
		glJPanel.setPreferredSize(new Dimension(960, 640));
		final EventListener eventListener = new EventListener();
		glJPanel.addGLEventListener(eventListener);
		frame.add(glJPanel);

		frame.setVisible(true);
		frame.pack();
		frame.setLocationRelativeTo(null);

		final FPSAnimator fpsAnimator = new FPSAnimator(glJPanel, 60);
		fpsAnimator.start();
	}

	private HelloOpenGL() {
	}
}

EventListener.java:
import static com.jogamp.opengl.GL.GL_ARRAY_BUFFER;
import static com.jogamp.opengl.GL.GL_FLOAT;
import static com.jogamp.opengl.GL.GL_STATIC_DRAW;
import static com.jogamp.opengl.GL.GL_TRIANGLES;
import static com.jogamp.opengl.GL2ES2.GL_FRAGMENT_SHADER;
import static com.jogamp.opengl.GL2ES2.GL_VERTEX_SHADER;

import java.nio.FloatBuffer;

import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderProgram;

public final class EventListener implements GLEventListener {
	private static final int VERTEX_ATTRIB_POSITION = 0;

	private final FloatBuffer vertices;

	private int VBO;
	private int VAO;
	private int shaderProgram;

	public EventListener() {
		this.vertices = FloatBuffer.wrap(new float[] { 0, 0, 0, 1, 0, 0, .5f, 1, 0 });
	}

	@Override
	public final void init(final GLAutoDrawable glAutoDrawable) {
		final GL4 gl4 = glAutoDrawable.getGL().getGL4();

		this.initBuffers(gl4);
		this.initVertexArrays(gl4);
		this.initShaderProgram(gl4);
	}

	private final void initBuffers(final GL4 gl4) {
		final int[] buffers = new int[1];
		gl4.glGenBuffers(buffers.length, buffers, 0);
		this.VBO = buffers[0];
	}

	private final void initVertexArrays(final GL4 gl4) {
		final int[] vertexArrays = new int[1];
		gl4.glGenVertexArrays(vertexArrays.length, vertexArrays, 0);
		this.VAO = vertexArrays[0];

		gl4.glBindVertexArray(this.VAO);
		{
			gl4.glBindBuffer(GL_ARRAY_BUFFER, this.VBO);
			gl4.glBufferData(GL_ARRAY_BUFFER, this.vertices.capacity(), this.vertices, GL_STATIC_DRAW);

			gl4.glVertexAttribPointer(EventListener.VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, false, 3 * Float.BYTES, 0);
			gl4.glEnableVertexAttribArray(EventListener.VERTEX_ATTRIB_POSITION);
		}
		gl4.glBindVertexArray(0);
	}

	private final void initShaderProgram(final GL4 gl4) {
		final ShaderProgram shaderProgram = new ShaderProgram();
		final ShaderCode vertexShader = ShaderCode.create(gl4, GL_VERTEX_SHADER, EventListener.class, "", null,
				"shader", "vert", null, true);
		shaderProgram.add(vertexShader);
		final ShaderCode fragmentShader = ShaderCode.create(gl4, GL_FRAGMENT_SHADER, EventListener.class, "", null,
				"shader", "frag", null, true);
		shaderProgram.add(fragmentShader);
		shaderProgram.init(gl4);
		shaderProgram.link(gl4, System.err);
		// vertexShader.destroy(gl4);
		// fragmentShader.destroy(gl4);
		this.shaderProgram = shaderProgram.program();
	}

	@Override
	public final void reshape(final GLAutoDrawable glAutoDrawable, final int x, final int y, final int width,
			final int height) {
		final GL4 gl4 = glAutoDrawable.getGL().getGL4();
		gl4.glViewport(0, 0, width, height);
	}

	@Override
	public final void display(final GLAutoDrawable glAutoDrawable) {
		final GL4 gl4 = glAutoDrawable.getGL().getGL4();

		gl4.glUseProgram(this.shaderProgram);

		gl4.glBindVertexArray(this.VAO);
		gl4.glDrawArrays(GL_TRIANGLES, 0, 40);
		gl4.glBindVertexArray(0);
	}

	@Override
	public final void dispose(final GLAutoDrawable glAutoDrawable) {
	}
}

shader.vert:
#version 450 core

in layout(location = 0) vec3 position;

void main() {
	gl_Position = vec4(position, 1);
}

shader.frag
#version 450 core

out vec4 color;

void main() {
	color = vec4(1, 0, 0, 1);
}

Thanks in advance
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: May someone help me to find out why this code doesn't work?

gouessej
Administrator
Hi

What do you mean by "it doesn't work"? Why not using elect's modern OpenGL examples that are fully tested and work with JOGL?

Don't use FloatBuffer.wrap(), rather create direct NIO buffers with the class Buffers in GlueGen:
http://jogamp.org/deployment/jogamp-next/javadoc/gluegen/javadoc/com/jogamp/common/nio/Buffers.html#newDirectFloatBuffer(float[])

Julien Gouesse | Personal blog | Website
Loading...