package com.peterhi.ui.gl2;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL3;
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.awt.GLCanvas;
import com.jogamp.opengl.util.GLBuffers;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
/**
*
* @author gbarbieri
*/
public class Test implements GLEventListener {
private int imageWidth = 800;
private int imageHeight = 600;
private GLCanvas canvas;
private GLSLProgramObject programObject;
private int[] vertexBufferObject = new int[1];
private int[] vertexArrayObject = new int[1];
private float[] vertexData = new float[]{
0.25f, 0.25f, 0.75f, 1.0f,
0.25f, -0.25f, 0.75f, 1.0f,
-0.25f, -0.25f, 0.75f, 1.0f,
-0.25f, 0.25f, 0.75f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f};
private String shadersFilepath = "/shaders/";
private Texture texture;
private int textureUnLoc;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Test test = new Test();
Frame frame = new Frame("Test");
frame.add(test.getCanvas());
frame.setSize(test.getCanvas().getWidth(), test.getCanvas().getHeight());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
frame.setVisible(true);
}
public Test() {
initGL();
}
private void initGL() {
GLProfile profile = GLProfile.getDefault();
GLCapabilities capabilities = new GLCapabilities(profile);
canvas = new GLCanvas(capabilities);
canvas.setSize(imageWidth, imageHeight);
canvas.addGLEventListener(this);
}
@Override
public void init(GLAutoDrawable glad) {
System.out.println("init");
canvas.setAutoSwapBufferMode(false);
GL3 gl3 = glad.getGL().getGL3();
buildShaders(gl3);
initializeVertexBuffer(gl3);
texture = initializeTexture(gl3);
gl3.glGenVertexArrays(1, IntBuffer.wrap(vertexArrayObject));
gl3.glBindVertexArray(vertexArrayObject[0]);
gl3.glEnable(GL3.GL_CULL_FACE);
gl3.glCullFace(GL3.GL_BACK);
gl3.glFrontFace(GL3.GL_CW);
}
@Override
public void dispose(GLAutoDrawable glad) {
System.out.println("dispose");
}
@Override
public void display(GLAutoDrawable glad) {
System.out.println("display");
GL3 gl3 = glad.getGL().getGL3();
gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl3.glClear(GL3.GL_COLOR_BUFFER_BIT);
programObject.bind(gl3);
{
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
gl3.glEnableVertexAttribArray(0);
gl3.glEnableVertexAttribArray(1);
{
gl3.glActiveTexture(GL3.GL_TEXTURE0);
texture.enable(gl3);
texture.bind(gl3);
gl3.glUniform1i(textureUnLoc, 0);
gl3.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl3.glVertexAttribPointer(1, 2, GL3.GL_FLOAT, false, 0, 4 * 4 * 4);
gl3.glDrawArrays(GL3.GL_QUADS, 0, 4);
texture.disable(gl3);
}
gl3.glDisableVertexAttribArray(0);
gl3.glDisableVertexAttribArray(1);
}
programObject.unbind(gl3);
glad.swapBuffers();
}
@Override
public void reshape(GLAutoDrawable glad, int x, int y, int w, int h) {
System.out.println("reshape() x: " + x + " y: " + y + " width: " + w + " height: " + h);
GL3 gl3 = glad.getGL().getGL3();
gl3.glViewport(x, y, w, h);
}
private void buildShaders(GL3 gl3) {
System.out.print("Building shaders...");
programObject = new GLSLProgramObject(gl3);
programObject.attachVertexShader(gl3, shadersFilepath + "orthoWithOffsetVS.txt");
programObject.attachFragmentShader(gl3, shadersFilepath + "standardColorFS.txt");
programObject.initializeProgram(gl3, true);
textureUnLoc = gl3.glGetUniformLocation(programObject.getProgramId(), "myTexture");
System.out.println("ok");
}
private void initializeVertexBuffer(GL3 gl3) {
gl3.glGenBuffers(1, IntBuffer.wrap(vertexBufferObject));
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
{
FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertexData);
gl3.glBufferData(GL3.GL_ARRAY_BUFFER, vertexData.length * 4, buffer, GL3.GL_STATIC_DRAW);
}
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
}
private Texture initializeTexture(GL3 gl3) {
Texture t = null;
try {
t = TextureIO.newTexture(this.getClass().getResource("/textures/texture.jpg"), false, ".jpg");
t.setTexParameteri(gl3, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
t.setTexParameteri(gl3, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
t.setTexParameteri(gl3, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
t.setTexParameteri(gl3, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
} catch (IOException ex) {
ex.printStackTrace();
} catch (GLException ex) {
ex.printStackTrace();
}
return t;
}
public GLCanvas getCanvas() {
return canvas;
}
public void setCanvas(GLCanvas canvas) {
this.canvas = canvas;
}
}