package org.practice; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.media.opengl.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.*; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.newt.swt.NewtCanvasSWT; public class Trangles { private int programID; private int vertexArrayObject; private int vertexBufferObject; private final FloatBuffer data = FloatBuffer.wrap(new float[] { 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f }); private final String vertexShaderSource = "in vec4 vPosition;" + "\n" + "void main()" + "\n" + "{" + "\n" + " gl_Position = vPosition;" + "\n" + "}"; private final String fragmentShaderSource = "void main()" + "\n" + "{" + "\n" + " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);" + "\n" + "}"; public Trangles(GLCapabilities capabilities) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite composite = new Composite(shell, SWT.BORDER); GLWindow window = GLWindow.create(capabilities); NewtCanvasSWT canvas = new NewtCanvasSWT(composite, SWT.NO_BACKGROUND, window); window.addGLEventListener(new Event()); shell.setSize(300, 200); shell.open(); canvas.setVisible(true); //window.setVisible(true); while (!display.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } class Event implements GLEventListener { @Override public void init(GLAutoDrawable drawable) { //Set the debug code. GL3 gl = drawable.getGL().getGL3(); drawable.setGL(new DebugGL3(gl)); gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); initShaders(gl); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); initVBO(gl); } @Override public void display(GLAutoDrawable drawable) { GL3 gl = drawable.getGL().getGL3(); gl.glClear(GL3.GL_COLOR_BUFFER_BIT); gl.glUseProgram(programID); //Render the vertex buffer object. gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject); gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3); //gl.glFlush(); gl.glDisableVertexAttribArray(0); } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL3 gl = drawable.getGL().getGL3(); gl.glViewport(0, 0, width, height); } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } private void initShaders(GL3 gl) { //Create a vertex shader. int vertexShaderID = gl.glCreateShader(GL3.GL_VERTEX_SHADER); gl.glShaderSource(vertexShaderID, 1, new String[]{vertexShaderSource}, null); gl.glCompileShader(vertexShaderID); //Create a fragment shader. int fragmentShaderID = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER); gl.glShaderSource(fragmentShaderID, 1, new String[]{fragmentShaderSource}, null); gl.glCompileShader(fragmentShaderID); //Create a program. programID = gl.glCreateProgram(); gl.glAttachShader(programID, vertexShaderID); gl.glAttachShader(programID, fragmentShaderID); gl.glLinkProgram(programID); } private void initVBO(GL3 gl) { //Check. if(vertexArrayObject != 0) { gl.glDeleteVertexArrays(1, new int[]{vertexArrayObject}, 0); vertexArrayObject = 0; } if(vertexBufferObject != 0) { gl.glDeleteBuffers(1, new int[]{vertexBufferObject}, 0); vertexBufferObject = 0; } int[] tempArray = new int[1]; //Create the vertex array object. gl.glGenVertexArrays(1, IntBuffer.wrap(tempArray)); vertexArrayObject = tempArray[0]; gl.glBindVertexArray(vertexArrayObject); //Create the vertex buffer object. gl.glGenBuffers(1, IntBuffer.wrap(tempArray)); vertexBufferObject = tempArray[0]; //Initialize the vertex buffer object. gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject); gl.glBufferData(GL3.GL_ARRAY_BUFFER, data.capacity() * 4, data, GL3.GL_STATIC_DRAW); //Initialize the attribute location of the input shader program. int location = gl.glGetAttribLocation(programID, "vPosition"); gl.glVertexAttribPointer(location, 3, GL3.GL_FLOAT, false, 0, 0); gl.glEnableVertexAttribArray(location); } } public static void main(String[] args) { GLCapabilities capabilities = new GLCapabilities( GLProfile.get(GLProfile.GL3)); new Trangles(capabilities); } }