Skip to content

Instantly share code, notes, and snippets.

@zzuegg
Created January 11, 2015 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzuegg/a8469b317e5db3f0a86b to your computer and use it in GitHub Desktop.
Save zzuegg/a8469b317e5db3f0a86b to your computer and use it in GitHub Desktop.
package jrse.modules.graphics;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;
import jogamp.opengl.glu.error.Error;
import jrse.core.common.Buffers;
import javax.media.opengl.*;
import java.nio.ByteBuffer;
/**
* Created by michael on 11.01.15.
*/
public class JoglTest implements GLEventListener {
public static void main(String[] args) {
new JoglTest().start();
}
private void start() {
GLWindow window = GLWindow.create(new GLCapabilities(GLProfile.getMaxProgrammableCore(true)));
window.addGLEventListener(this);
window.setSize(800, 600);
window.setVisible(true);
FPSAnimator fpsAnimator = new FPSAnimator(window, 60);
fpsAnimator.start();
}
@Override
public void init(GLAutoDrawable drawable) {
GL4 gl4 = drawable.getGL().getGL4();
int[] vao = new int[1];
gl4.glGenVertexArrays(1, vao, 0);
gl4.glBindVertexArray(vao[0]);
int[] vbo = new int[2];
gl4.glGenBuffers(2, vbo, 0);
gl4.glBindBuffer(GL4.GL_ARRAY_BUFFER, vbo[0]);
ByteBuffer positionData = Buffers.createBuffer(new float[]{-0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, 0.5f, 0f});
gl4.glBufferData(GL4.GL_ARRAY_BUFFER, positionData.capacity(), positionData, GL4.GL_STATIC_READ);
gl4.glEnableVertexAttribArray(0);
gl4.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, 0);
gl4.glBindBuffer(GL4.GL_ARRAY_BUFFER, vbo[1]);
ByteBuffer colorData = Buffers.createBuffer(new float[]{
1, 0, 0, 0,
1, 0, 0, 0,
0, 1, 0, 0,
});
gl4.glBufferData(GL4.GL_ARRAY_BUFFER, colorData.capacity(), colorData, GL4.GL_STATIC_READ);
gl4.glEnableVertexAttribArray(1);
gl4.glVertexAttribPointer(1, 4, GL.GL_FLOAT, false, 0, 0);
String[] vShader = {
"#version 430 core\n",
"uniform vec3 offset;\n",
"out gl_PerVertex{\n",
"vec4 gl_Position;\n",
"};\n",
"layout(location=0)in vec3 position;\n",
"layout(location=1)in vec4 color;\n",
"layout(location=0)out vec4 outColor;",
"void main () {gl_Position = vec4(position,1f);outColor=color;}\n",
"\n"};
int vertexShader = gl4.glCreateShaderProgramv(GL4.GL_VERTEX_SHADER, vShader.length, vShader);
String[] fShader = {
"#version 430 core\n",
"layout(location=0)in vec4 color;\n",
"void main () {gl_FragColor=color;}\n",
"\n"
};
int fragmentShader = gl4.glCreateShaderProgramv(GL4.GL_FRAGMENT_SHADER, fShader.length, fShader);
int pipeline[] = new int[1];
gl4.glGenProgramPipelines(1, pipeline, 0);
gl4.glBindProgramPipeline(pipeline[0]);
gl4.glUseProgramStages(pipeline[0], GL4.GL_VERTEX_SHADER_BIT, vertexShader);
gl4.glUseProgramStages(pipeline[0], GL4.GL_FRAGMENT_SHADER_BIT, fragmentShader);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
drawable.getContext().getGL().getGL4().glDrawArrays(GL.GL_TRIANGLES, 0, 3);
System.out.println(Error.gluErrorString(drawable.getGL().glGetError()));
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment