Hello,
I’ve the following code with shaders in opengl ES. I would like to add a texture to the square we display… How could I do simply?
Thanks in advance
Xavier
the current java code :
protected ShaderState shaderState = new ShaderState();;
protected ShaderProgram shaderProgram = new ShaderProgram();
protected GL2ES2 gl;
public static void main(String[] args)
{
GLCapabilities capabilities = new GLCapabilities(GLProfile.getGL2ES2());
GLCanvas canvas = new GLCanvas(capabilities);
Frame frame = new Frame("Simple JOGL Application with shaders");
canvas.addGLEventListener(new SimpleShader());
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
// Run this on another thread than the AWT event queue
// so that Animator.stop() completes before exiting
new Thread(new Runnable()
{
public void run()
{
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable)
{
gl = drawable.getGL().getGL2ES2();
// Create & Compile the shader objects
ShaderCode vertexShaderCode = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, SimpleShader.class,
"shader", "shader/bin", "simpleshader");
ShaderCode fragmentShaderCode = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, SimpleShader.class,
"shader", "shader/bin", "simpleshader");
shaderProgram.add(vertexShaderCode);
shaderProgram.add(fragmentShaderCode);
if(!shaderProgram.link(gl, System.err)) {
throw new GLException("Couldn't link program: "+shaderProgram);
}
shaderState.attachShaderProgram(gl, shaderProgram);
shaderState.glUseProgram(gl, true);
GLArrayDataClient vertices = GLArrayDataClient.createGLSL(gl, "mgl_Vertex", 3, GL2ES2.GL_FLOAT, false, 4);
{
// Fill them up
FloatBuffer verticeb = (FloatBuffer)vertices.getBuffer();
verticeb.put(-2); verticeb.put( 2); verticeb.put( 0);
verticeb.put( 2); verticeb.put( 2); verticeb.put( 0);
verticeb.put(-2); verticeb.put( -2); verticeb.put( 0);
verticeb.put( 2); verticeb.put( -2); verticeb.put( 0);
}
vertices.seal(gl, true);
gl.glClearColor(0, 0, 0, 1);
shaderState.glUseProgram(gl, false);
}
public void display(GLAutoDrawable drawable)
{
shaderState.glUseProgram(gl, true);
gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT );
// Draw a square
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
shaderState.glUseProgram(gl, false);
}
The current fragment shader code :
void main() {
gl_FragColor = vec4(1.0,1.0,0.0,1.0);
}
The current vertex shader code :
attribute vec4 mgl_Vertex;
void main() {
gl_Position = mgl_Vertex;
}