Login  Register

Re: need help understanding glVertexAttribPointer and GL_INVALID_OPERATION

Posted by jmaasing on Jan 09, 2013; 5:53pm
URL: https://forum.jogamp.org/need-help-understanding-glVertexAttribPointer-and-GL-INVALID-OPERATION-tp4027730p4027760.html

So, after reading a bit more tutorials I sort of kind of get how it hangs together. These helped me: stackoverflow.com "is-vertexattribpointer-needed-after-each-bindbuffer" and this one from swiftless.com (be sure to read the comments, they correct some mistakes in the example)

Here is my working sample, it is really stupid and may not follow best practice but it worked
public class GL3TestAttrib implements GLEventListener {
	private ShaderProgram program;
	int[] vertexArrays = new int[1];
	private int[] vertexBuffers = new int[1];
	int positionAttribute;

	public static void main(String[] args) {
		GL3TestAttrib app = new GL3TestAttrib();
		app.run();
	}

	public void run() {
		final GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL3));
		caps.setBackgroundOpaque(true);
		final GLWindow glWindow = GLWindow.create(caps);
		glWindow.setTitle("Sample GL3");
		glWindow.setSize(640, 480);
		glWindow.setUndecorated(false);
		glWindow.setPointerVisible(true);
		glWindow.addGLEventListener(this);
		final Animator animator = new Animator(glWindow);
		glWindow.addWindowListener(new WindowAdapter() {

			@Override
			public void windowDestroyed(WindowEvent event) {
				animator.stop();
				glWindow.setVisible(false);
				System.exit(0);
			}
		});
		glWindow.setVisible(true);
		animator.start();
	}

	@Override
	public void init(GLAutoDrawable drawable) {
		final float[] vertices = { 0.0f, 0.7f, 0.0f, 0.0f, 0.1f, 0.0f, 0.9f, 0.1f, 0.0f };

		if (drawable.getGL().isGL3()) {
			final GL3 gl = drawable.getGL().getGL3();
			drawable.setGL(new DebugGL3(gl));
			gl.glGenVertexArrays(1, vertexArrays, 0);
			gl.glBindVertexArray(this.vertexArrays[0]);
			gl.glGenBuffers(1, vertexBuffers, 0);
			gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, this.vertexBuffers[0]);
			FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(vertices);
			gl.glBufferData(GL3.GL_ARRAY_BUFFER, vertices.length * Float.SIZE / 8, fbVertices, GL3.GL_STATIC_DRAW);

			try {
				positionAttribute = loadShaderProgram(gl);
			} catch (IOException e) {
				e.printStackTrace();
				throw new IllegalStateException(e);
			}
			gl.glVertexAttribPointer(positionAttribute, 3, GL3.GL_FLOAT, false, 0, 0);

			gl.glBindVertexArray(0);
			gl.glDisableVertexAttribArray(positionAttribute);
		} else {
			System.err.println("Not a GL3 drawable");
			System.exit(1);
		}
	}

	@Override
	public void dispose(GLAutoDrawable drawable) {
	}

	@Override
	public void display(GLAutoDrawable drawable) {

		GL3 gl = drawable.getGL().getGL3();
		gl.glClearColor(1, 0, 1, 0.5f); // Purple
		gl.glClear(GL3.GL_STENCIL_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);

		gl.glUseProgram(program.program());
		gl.glBindVertexArray(this.vertexArrays[0]);
		gl.glEnableVertexAttribArray(positionAttribute);

		gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);

		gl.glDisableVertexAttribArray(positionAttribute);
		gl.glBindVertexArray(0);
		gl.glUseProgram(0);
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
	}

	private int loadShaderProgram(final GL3 gl) throws IOException {
		final String nameBase = "sample";
		final String filePrefix = "/shaders/" + nameBase;

		/*
		 * #version 150 core
		 * 
		 * in vec3 attribute_Position;
		 * 
		 * void main(void) { gl_Position = vec4(attribute_Position, 1.0); }
		 */
		ShaderCode vertexShader = ShaderCode.create(gl, GL3.GL_VERTEX_SHADER, getClass(), filePrefix, "/tmp", nameBase,
				false);
		final boolean vertexCompile = vertexShader.compile(gl);
		if (!vertexCompile) {
			throw new RuntimeException("Unable to compile vertex shader: " + nameBase);
		}

		/*
		 * #version 150 core
		 * 
		 * out vec4 fragColor ;
		 * 
		 * void main (void) { fragColor = vec4(0.0f, 1.0f, 1.0f, 1.0f); }
		 */
		ShaderCode fragmentShader = ShaderCode.create(gl, GL3.GL_FRAGMENT_SHADER, getClass(), filePrefix, "/tmp",
				nameBase, false);
		final boolean fragmentCompile = fragmentShader.compile(gl);
		if (!fragmentCompile) {
			throw new RuntimeException("Unable to compile fragment shader: " + nameBase);
		}
		program = new ShaderProgram();
		program.init(gl);

		program.add(vertexShader);
		program.add(fragmentShader);

		program.link(gl, System.out);

		final boolean validateProgram = program.validateProgram(gl, System.out);
		if (!validateProgram) {
			throw new RuntimeException("Shader program did not validate: " + nameBase);
		}

		int attribPosition = gl.glGetAttribLocation(program.program(), "attribute_Position");
		return attribPosition;
	}
}