I have started learning modern opengl (version 3.3). I use JOGL2 RC2 Signed Released (jogl-2.0-b23-20110303-windows-i586). I have a problem with vertex shader - uniform vec2 offset doesn’t work. The triangle doesn’t move … My initShader function works properly. I don’t know what is wrong.
Tutorial: http://arcsynthesis.org/gltut/Positioning/Tut03%20A%20Better%20Way.html
Code:
public class Scene3D implements GLEventListener
{
private Frame frame;
private FPSAnimator animator;
private int vertexBufferObject;
private int shaderProgramID;
private FloatBuffer vertexDataBuffer;
private float fXOffset = 0.01f;
private float fYOffset = 0.01f;
private int offsetLocation;
private float vertices[];
private static String vertexShader =
"#version 330" + "\n" +
"layout (location = 0) in vec4 position;" + "\n" +
"uniform vec2 offset;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);" + "\n" +
"gl_Position = position + totalOffset;" + "\n" +
"}";
private static String fragmentShader =
"#version 330" + "\n" +
"out vec4 outputColor;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);" + "\n" +
"}";
public Scene3D(GLCapabilities capabilities)
{
GLCanvas glComponent = new GLCanvas(capabilities);
glComponent.setFocusable(true);
glComponent.addGLEventListener(this);
frame = new JFrame("JOGL");
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
runExit();
}
});
frame.setLayout(new BorderLayout());
glComponent.setPreferredSize(new Dimension(800, 600));
frame.add(glComponent, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
glComponent.requestFocus();
animator = new FPSAnimator(glComponent, 60, true);
animator.start();
}
public static void main(String[] args)
{
GLProfile profile = GLProfile.get(GLProfile.GL3);
final GLCapabilities capabilities = new GLCapabilities(profile);
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Scene3D(capabilities);
}
});
}
@Override
public void init(GLAutoDrawable glad)
{
GL3 gl = glad.getGL().getGL3();
gl.setSwapInterval(1);
gl.glEnable(GL3.GL_DEPTH_TEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
vertices = new float[]{0.0f, 0.5f, 0.0f, 1.0f,
0.5f, -0.366f, 0.0f, 1.0f,
-0.5f, -0.366f, 0.0f, 1.0f};
vertexDataBuffer = Buffers.newDirectFloatBuffer(vertices.length);
vertexDataBuffer.put(vertices);
vertexDataBuffer.flip();
initShaders(gl);
initVBO(gl);
offsetLocation = gl.glGetUniformLocation(shaderProgramID, "offset");
}
@Override
public void dispose(GLAutoDrawable glad)
{
}
@Override
public void display(GLAutoDrawable glad)
{
GL3 gl = glad.getGL().getGL3();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glUseProgram(shaderProgramID);
gl.glUniform2f(offsetLocation, fXOffset, fYOffset);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);
}
@Override
public void reshape(GLAutoDrawable glad, int x, int y, int width, int height)
{
}
private void initVBO(GL3 gl)
{
int buffer[] = new int[1];
gl.glGenBuffers(1, IntBuffer.wrap(buffer));
vertexBufferObject = buffer[0];
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject);
gl.glBufferData(GL3.GL_ARRAY_BUFFER, Buffers.SIZEOF_FLOAT * vertexDataBuffer.capacity(), vertexDataBuffer, GL3.GL_STREAM_DRAW);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
}
private void initShaders(GL3 gl)
{
shaderProgramID = gl.glCreateProgram();
int vertexShaderID = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
gl.glShaderSource(vertexShaderID, 1,
new String[]{vertexShader}, null);
gl.glCompileShader(vertexShaderID);
gl.glAttachShader(shaderProgramID, vertexShaderID);
gl.glDeleteShader(vertexShaderID);
int fragmentShaderID = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
gl.glShaderSource(fragmentShaderID, 1,
new String[]{fragmentShader}, null);
gl.glCompileShader(fragmentShaderID);
gl.glAttachShader(shaderProgramID, fragmentShaderID);
gl.glDeleteShader(fragmentShaderID);
gl.glLinkProgram(shaderProgramID);
gl.glValidateProgram(shaderProgramID);
}
private void runExit()
{
new Thread(new Runnable()
{
@Override
public void run()
{
animator.stop();
System.exit(0);
}
}).start();
}
}