package org.maoni.jogl.test; import java.io.File; import java.io.IOException; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.media.opengl.GL; import javax.media.opengl.GL3; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLException; import org.jocl.Sizeof; import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.glsl.ShaderCode; import com.jogamp.opengl.util.glsl.ShaderProgram; import com.jogamp.opengl.util.glsl.ShaderUtil; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO; public class SimpleScene implements GLEventListener { private final float[] vertexBufferData = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f }; private final int[] element_buffer_data = { 0, 1, 2, 3 }; private long startTime; private final GlResources gr = new GlResources(); @Override public void display(GLAutoDrawable drawable) { update(); render(drawable); } private void render(GLAutoDrawable drawable) { GL3 gl = drawable.getGL().getGL3(); gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); gl.glClear(GL3.GL_COLOR_BUFFER_BIT); gl.glUseProgram(gr.program); gl.glUniform1f(gr.uniforms.fade_factor, gr.fade_factor); gl.glActiveTexture(GL3.GL_TEXTURE0); gl.glBindTexture(GL3.GL_TEXTURE_2D, gr.textures[0]); gl.glUniform1i(gr.uniforms.textures[0], 0); gl.glActiveTexture(GL3.GL_TEXTURE1); gl.glBindTexture(GL3.GL_TEXTURE_2D, gr.textures[1]); gl.glUniform1i(gr.uniforms.textures[1], 1); gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, gr.vertex_buffer); gl.glVertexAttribPointer( gr.attributes.position, 2, GL3.GL_FLOAT, false, Sizeof.cl_float * 2, 0); gl.glEnableVertexAttribArray(gr.attributes.position); gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, gr.element_buffer); gl.glDrawElements(GL3.GL_TRIANGLE_STRIP, 4, GL3.GL_UNSIGNED_SHORT, 0); gl.glDisableVertexAttribArray(gr.attributes.position); drawable.swapBuffers(); } private void update() { gr.fade_factor = ((float) Math.sin((float) System.nanoTime() - this.startTime * 0.000001f)) * 0.5f + 0.5f; System.out.println(gr.fade_factor); } @Override public void dispose(GLAutoDrawable arg0) { // cleanup code } @Override public void init(GLAutoDrawable arg0) { this.startTime = System.nanoTime(); System.out.println("Running GL Init code."); GL3 gl = arg0.getGL().getGL3(); try { make_resources(gl); } catch (IOException e) { e.printStackTrace(); } } @Override public void reshape(GLAutoDrawable gl, int x, int y, int width, int height) { } public int make_buffer(GL3 gl, int target, float[] bufferData, int buffer_size) { FloatBuffer fb = FloatBuffer.wrap(bufferData); fb.rewind(); int buffer[] = new int[1]; gl.glGenBuffers(1, buffer, 0); gl.glBindBuffer(target, buffer[0]); gl.glBufferData(target, buffer_size, fb, GL3.GL_STATIC_DRAW); return buffer[0]; } public int make_buffer(GL3 gl, int target, int[] bufferData, int buffer_size) { IntBuffer ib = IntBuffer.wrap(bufferData); ib.rewind(); int buffer[] = new int[1]; gl.glGenBuffers(1, buffer, 0); gl.glBindBuffer(target, buffer[0]); gl.glBufferData(target, buffer_size, ib, GL3.GL_STATIC_DRAW); return buffer[0]; } public int make_texture(final String filename) throws GLException, IOException { Texture t = TextureIO.newTexture(new File(filename), false); t.bind(); return t.getTextureObject(); } public ShaderCode make_shader(final GL3 gl, int type, final String filename) throws IOException { ShaderCode sc = ShaderCode.create(gl, type, 1, getClass(), new String[] { filename }); sc.compile(gl); if (!sc.isValid()) { throw new GLException("Shader " + filename + " is invalid"); } return sc; } public int make_program(GL3 gl, final ShaderCode vertex_shader, final ShaderCode fragment_shader) { ShaderProgram sp = new ShaderProgram(); sp.add(vertex_shader); sp.add(fragment_shader); sp.link(gl, null); if(!ShaderUtil.isProgramValid(gl, sp.program())) { throw new GLException("Shader program invalid, " + ShaderUtil.getProgramInfoLog(gl, sp.program())); } return sp.id(); } public int make_resources(GL3 gl) throws IOException { // Setup the buffers. gr.vertex_buffer = make_buffer(gl, GL3.GL_ARRAY_BUFFER, vertexBufferData, vertexBufferData.length * Sizeof.cl_float); gr.element_buffer = make_buffer(gl, GL3.GL_ELEMENT_ARRAY_BUFFER, element_buffer_data, element_buffer_data.length * Sizeof.cl_int); // Setup the textures. gr.textures[0] = make_texture("resources/textures/hello1.tga"); gr.textures[1] = make_texture("resources/textures/hello2.tga"); // Make the shaders. gr.vertex_shader = make_shader(gl, GL3.GL_VERTEX_SHADER, "resources/shaders/hello-gl.v.glsl"); gr.fragment_shader = make_shader(gl, GL3.GL_FRAGMENT_SHADER, "resources/shaders/hello-gl.f.glsl"); gr.program = make_program(gl, gr.vertex_shader, gr.fragment_shader); // Setup the uniforms and attributes. gr.uniforms.fade_factor = gl.glGetUniformLocation(gr.program, "fade_factor"); gr.uniforms.textures[0] = gl.glGetUniformLocation(gr.program, "textures[0]"); gr.uniforms.textures[1] = gl.glGetUniformLocation(gr.program, "textures[1]"); gr.attributes.position = gl.glGetAttribLocation(gr.program, "position"); return 1; } }