Skip to content

Instantly share code, notes, and snippets.

@dnery
Created April 4, 2016 06:04
Show Gist options
  • Save dnery/2b09bb2ec81b23ff5a024d9276511e6c to your computer and use it in GitHub Desktop.
Save dnery/2b09bb2ec81b23ff5a024d9276511e6c to your computer and use it in GitHub Desktop.
Simple bouncing ball scene using the Java based cg2014 framework
package br.usp.icmc.vicg.gl.app;
import com.jogamp.opengl.util.AnimatorBase;
import com.jogamp.opengl.util.FPSAnimator;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;
/**
* Created by danilo on 4/10/16.
*/
public class BouncingBall {
public static void main(String args[])
{
// Profile to use
GLProfile gprof = GLProfile.get(GLProfile.GL3);
// Cautionary: set capabilities
GLCapabilities gcaps = new GLCapabilities(gprof);
gcaps.setDoubleBuffered(true);
gcaps.setHardwareAccelerated(true);
// Canvas to draw on
GLCanvas gcanvas = new GLCanvas(gcaps);
gcanvas.addGLEventListener(new BoucingBallScene());
// Animator/redrawer
AnimatorBase anim = new FPSAnimator(gcanvas, 60);
// The old frame, old methods
JFrame frame = new JFrame("Bouncing Ball");
frame.getContentPane().add(gcanvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.setVisible(true);
// Start the redrawer
anim.start();
}
}
package br.usp.icmc.vicg.gl.app;
import br.usp.icmc.vicg.gl.matrix.Matrix4;
import br.usp.icmc.vicg.gl.model.SimpleModel;
import br.usp.icmc.vicg.gl.model.Sphere;
import br.usp.icmc.vicg.gl.util.Shader;
import br.usp.icmc.vicg.gl.util.ShaderFactory;
import javax.media.opengl.GL;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
/**
* Created by danilo on 3/10/16.
*/
public class BoucingBallScene implements GLEventListener {
private Shader shader; // The colored shader
private SimpleModel sphere; // Sphere object
private SimpleModel shadow; // 'Shadow' object
private Matrix4 matrix; // Transformation matrix
private float delta; // Translation multiplier
private float delta_inc; // and the incremental factor
private float ping; // Cosine function multiplier
private float ping_deg; // and the incremental factor
private int color_handle;
public BoucingBallScene()
{
shader = ShaderFactory.getInstance(ShaderFactory.ShaderType.SIMPLE_COLOR_SHADER);
matrix = new Matrix4();
sphere = new Sphere();
shadow = new Sphere();
delta = 0;
delta_inc = 0.01f;
ping = 0;
ping_deg = 0f;
}
@Override
public void init(GLAutoDrawable glad) /* Focus on this */
{
// LibVersion
GL3 gl = glad.getGL().getGL3();
// Set the clear color used by glClear
gl.glClearColor(0.9f, 1.0f, 1.0f, 1.0f);
// Send to device
shader.init(gl);
// Bind shader here (don't rebind on display)
shader.bind();
// Start these objects shaded with this shader
sphere.init(gl, shader);
shadow.init(gl, shader);
// Start the base matrix, giving it a handle in the shader
matrix.init(gl, shader.getUniformLocation("u_modelMatrix"));
// Get the shader color handle
color_handle = shader.getUniformLocation("u_color");
}
@Override
public void display(GLAutoDrawable glad) /* Focus on this */
{
// LibVersion
GL3 gl = glad.getGL().getGL3();
// Clear the framebuffer
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// Handle the ping factor
//ping = (float) ((float) Math.abs(Math.cos(Math.toRadians(ping_deg))) *
// Math.exp((double) -0.1f * Math.toRadians(ping_deg)));
ping = (float) Math.abs(Math.cos(Math.toRadians(ping_deg)));
ping_deg += 6f;
// Handle the delta factor
if (delta > 0.5f || delta < 0.0f)
delta_inc = -delta_inc;
delta += delta_inc;
// Transform the shadow
gl.glUniform4f(color_handle, 0.5f, 0.5f, 0.5f, 0.5f);
matrix.loadIdentity();
matrix.translate((0.5f * ping) + 0.25f, -0.25f, 0.0f);
matrix.rotate(15, 1, 1, 0);
matrix.scale((0.25f * ping) + 0.25f, 0.0f, 0.25f);
matrix.rotate(100 * delta, 0, 1, 0);
matrix.bind();
shadow.bind();
shadow.draw();
// Transform the sphere
gl.glUniform4f(color_handle, 0.25f, 0.25f, 0.25f, 0.0f);
matrix.loadIdentity();
matrix.translate(0, ping, 0);
matrix.scale(0.25f, 0.25f, 0.25f);
matrix.rotate(100 * delta, 0, 1, 0);
matrix.bind();
sphere.bind();
sphere.draw();
}
@Override
public void dispose(GLAutoDrawable glad)
{
}
@Override
public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3)
{
}
}
#version 150
uniform mat4 u_modelMatrix;
in vec3 a_position;
void main(void)
{
gl_Position = u_modelMatrix * vec4(a_position, 1.0);
}
#version 150
uniform vec4 u_color;
out vec4 fragColor;
void main(void)
{
fragColor = u_color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment