Skip to content

Instantly share code, notes, and snippets.

@danem

danem/Main.java Secret

Created April 19, 2016 18:07
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 danem/574312cb32aac8a3f1a6f9b8279e0bba to your computer and use it in GitHub Desktop.
Save danem/574312cb32aac8a3f1a6f9b8279e0bba to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
GLFrameConfig config = new GLFrameConfig.Builder()
.setEventListener(new SimpleTest())
.setProfile(GLProfile.getMaxProgrammableCore(true))
.build();
GLFrame glFrame = new GLFrame(config);
glFrame.setVisible(true);
}
}
#version 410
in vec3 color;
out vec4 _color;
void main() {
_color = vec4(color,1);
}
#version 410
layout (location = 0) in vec3 position;
out vec3 color;
void main() {
if (gl_VertexID == 0){
gl_Position = vec4(-0.5f,-0.5f,0,1);
} else if (gl_VertexID == 1){
gl_Position = vec4(0.5f,-0.5f,0,1);
} else {
gl_Position = vec4(0.0f,0.5f,0,1);
}
color = position;
}
public class SimpleTest extends GLContext{
int vbo;
int vao;
Shader shader;
GL4 gl;
@Override
public void init(GLAutoDrawable glAutoDrawable) {
// Provide GL instance singleton to other parts of program, and optionally enable tracing
initGL(glAutoDrawable,false,false);
gl = (GL4)_gl.getGL4();
gl.glFrontFace(GL.GL_CCW);
vbo = Utils.genBuffer(gl);
vao = Utils.genVertexArray(gl);
// Used as colors in the fragment shader in order to
// log what values the shader is receiving.
float[] vertices = new float[]{
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f
};
gl.glBindVertexArray(vao);
Utils.logGLErrors(gl);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
Utils.logGLErrors(gl);
gl.glBufferData(GL.GL_ARRAY_BUFFER, 4 * 9, Utils.arrayToBuffer(vertices), GL.GL_STATIC_DRAW);
Utils.logGLErrors(gl);
gl.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 3 * 4, 0);
Utils.logGLErrors(gl);
gl.glEnableVertexAttribArray(0);
Utils.logGLErrors(gl);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
Utils.logGLErrors(gl);
gl.glBindVertexArray(0);
Utils.logGLErrors(gl);
try {
shader = new Shader("./data/shaders/simple-vs.glsl", "./data/shaders/simple-fs.glsl",gl);
Utils.logGLErrors(gl);
}catch (Exception err){
err.printStackTrace();
System.exit(1);
}
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
gl = (GL4)glAutoDrawable.getGL().getGL4();
gl.glClearColor(0.5f,0.5f,0.5f,1);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
shader.use(gl);
gl.glBindVertexArray(vao);
gl.glDrawArrays(GL.GL_TRIANGLES,0,3);
gl.glBindVertexArray(0);
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int i, int i1, int i2, int i3) {
gl.glViewport(0,0,i2,i3);
Utils.logGLErrors(gl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment