Posted by
jmaasing on
Jul 16, 2014; 2:21pm
URL: https://forum.jogamp.org/Need-Help-Solving-OpenGL-GLSL-4-4-Issues-tp4032557p4032580.html
Try this sample, I chucked it together from code I had (so it isn't well documented and you need to fix package name et c) but it works.
Edit: oh and you probably need to fix the version line in the shader, I'm on Mac which only supports OpenGL 4.1
---------------------
package nu.zoom.corridors.client;
import com.jogamp.common.nio.Buffers;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderProgram;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.media.nativewindow.WindowClosingProtocol;
import javax.media.opengl.DebugGL4;
import javax.media.opengl.GL;
import javax.media.opengl.GL4;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLProfile;
import javax.media.opengl.TraceGL4;
/**
*
* @author Johan Maasing <johan@zoom.nu>
*/
public class TriangleSample {
//
http://www.opengl.org/wiki/Vertex_Specification final int[] vas = new int[1];
final int[] vbos = new int[1];
private int shaderPosition;
private int flatColorShaderProgram = 0;
private Animator animator;
private GLWindow glWindow;
private boolean windowDestroyed = false;
private static final float[] triangle = new float[]{
0.0f, -1.0f, 0.7f,
1.0f, 1.0f, 0.7f,
-1.0f, 1.0f, 0.7f
};
private final String vertShader
= "#version 410 core\n"
+ "in vec3 position;\n"
+ "out vec2 uv ;\n"
+ "void main(void)\n"
+ "{"
+ " uv = (position.xy + 1.0f)/2.0f;"
+ " gl_Position = vec4(position, 1.0f) ; \n"
+ "}";
private final String flatColorfragShader
= "#version 410 core\n"
+ "in vec2 uv;\n"
+ "layout(location = 0) out vec4 fragColor ;\n"
+ "void main(void)\n"
+ "{"
+ " fragColor = vec4(uv.y, uv.y, uv.y, uv.x) ; \n"
+ "}";
public static void main(String[] args) throws Exception {
TriangleSample app = new TriangleSample();
app.run();
}
private void stop(String msg) {
if (this.animator != null) {
this.animator.stop();
}
if (this.glWindow != null) {
this.windowDestroyed = true;
this.glWindow.destroy();
}
System.out.println(msg);
}
private void run() throws Exception {
final GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL4));
caps.setBackgroundOpaque(true);
caps.setDoubleBuffered(true);
caps.setDepthBits(8);
caps.setSampleBuffers(false);
this.glWindow = GLWindow.create(caps);
this.glWindow.setTitle("FBO Test");
this.glWindow.setSize(640, 480);
this.glWindow.setUndecorated(false);
this.glWindow.setPointerVisible(true);
this.glWindow.setVisible(true);
this.glWindow.setFullscreen(false);
this.glWindow.setDefaultCloseOperation(WindowClosingProtocol.WindowClosingMode.DISPOSE_ON_CLOSE);
this.glWindow.addWindowListener(new WindowAdapter() {
@Override
public void windowDestroyNotify(WindowEvent we) {
if (!windowDestroyed) {
stop("Window destroyed");
}
}
});
this.glWindow.addGLEventListener(new GLEventListener() {
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glViewport(x, y, width, height);
}
@Override
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
if (gl.isGL4core()) {
drawable.setGL(new TraceGL4(new DebugGL4(gl.getGL4()), System.out));
GL4 gL4 = drawable.getGL().getGL4();
try {
TriangleSample.this.init(gL4);
} catch (IOException ex) {
stop(ex.getLocalizedMessage());
}
} else {
stop("Not a GL4 core context");
}
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
GL4 gL4 = drawable.getGL().getGL4();
TriangleSample.this.display(gL4);
}
});
this.animator = new Animator(glWindow);
this.animator.start();
}
private void init(GL4 gl) throws IOException {
gl.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL4.GL_CULL_FACE);
gl.glCullFace(GL4.GL_BACK);
gl.glFrontFace(GL4.GL_CCW);
// Compile and link the shader then query the shader for the location of the vertex input variable
final ShaderCode vertexShader = compileShader(gl, vertShader, GL4.GL_VERTEX_SHADER);
final ShaderCode flatColorFragmentShader = compileShader(gl, flatColorfragShader, GL4.GL_FRAGMENT_SHADER);
this.flatColorShaderProgram = linkShader(gl, vertexShader, flatColorFragmentShader);
this.shaderPosition = gl.glGetAttribLocation(this.flatColorShaderProgram, "position");
// Create the mesh
createBuffer(gl, shaderPosition, triangle, 3);
}
public ShaderCode compileShader(final GL4 gl4, final String source, final int shaderType) throws IOException {
final String[][] sources = new String[1][1];
sources[0] = new String[]{source};
ShaderCode shaderCode = new ShaderCode(shaderType, sources.length, sources);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final boolean compiled = shaderCode.compile(gl4, System.err);
if (!compiled) {
System.err.println("Unable to compile " + source);
System.exit(1);
}
return shaderCode;
}
private int linkShader(GL4 gl, final ShaderCode vertexShader, final ShaderCode textureFragmentShader) throws GLException {
ShaderProgram program = new ShaderProgram();
program.init(gl);
program.add(vertexShader);
program.add(textureFragmentShader);
program.link(gl, System.out);
final boolean validateProgram = program.validateProgram(gl, System.out);
if (!validateProgram) {
System.err.println("Unable to link shader");
System.exit(1);
}
return program.program();
}
private void display(GL4 gl) {
// Clear the default framebuffer
gl.glBindFramebuffer(GL4.GL_DRAW_FRAMEBUFFER, 0);
gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);
gl.glUseProgram(this.flatColorShaderProgram);
gl.glBindVertexArray(this.vas[0]);
gl.glEnableVertexAttribArray(this.shaderPosition);
gl.glDrawArrays(GL4.GL_TRIANGLES, 0, 3);
}
public void createBuffer(final GL4 gl, final int shaderAttribute, float[] values, final int valuesPerVertex) {
gl.glGenVertexArrays(this.vas.length, this.vas, 0);
gl.glBindVertexArray(this.vas[0]);
gl.glGenBuffers(this.vbos.length, this.vbos, 0);
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, this.vbos[0]);
FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(values);
final int bufferSizeInBytes = values.length * Buffers.SIZEOF_FLOAT;
gl.glBufferData(GL4.GL_ARRAY_BUFFER, bufferSizeInBytes, fbVertices, GL4.GL_STATIC_DRAW);
gl.glVertexAttribPointer(shaderAttribute, valuesPerVertex, GL4.GL_FLOAT, false, 0, 0);
}
}