Skip to content

Instantly share code, notes, and snippets.

@schmidsi
Created September 23, 2017 12:43
Show Gist options
  • Save schmidsi/03fe9794551d0f16bc5da469ac96777b to your computer and use it in GitHub Desktop.
Save schmidsi/03fe9794551d0f16bc5da469ac96777b to your computer and use it in GitHub Desktop.
import ch.fhnw.util.math.Mat4;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Lissajous implements WindowListener, GLEventListener{
// --------- globale Daten ---------------------------
String windowTitle = "Lissajous";
int windowWidth = 500;
int windowHeight = 500;
String vShader = MyShaders.vShader0; // Vertex-Shader
String fShader = MyShaders.fShader0; // Fragment-Shader
int maxVerts = 2048; // max. Anzahl Vertices im Vertex-Array
GLCanvas canvas; // OpenGL Window
MyGLBase1 mygl; // eigene OpenGL-Basisfunktionen
// ---------- Settings -------------
final float amplitudeX = 0.5f;
final float amplitudeY = 0.5f;
final float frequencyX = 1;
final float frequencyY = 1.0f;
final float shiftDelta = 0.1f;
final int points = 400;
// changing params
int t = 0;
public Lissajous() // Konstruktor
{ createFrame();
System.out.println(Math.sin(Math.toRadians(270)));
}
public void drawLissajous(GL3 gl, int t) {
mygl.rewindBuffer(gl);
for (int i = 0; i < points; i++) {
float shift = t * shiftDelta;
double xDegree = Math.toRadians(frequencyX * (t + i));
double yDegree = Math.toRadians(frequencyY * (t + i) - shift);
double x = amplitudeX * Math.cos(xDegree);
double y = amplitudeY * Math.sin(yDegree);
float xf = (float) x; // - 0.5f;
float yf = (float) y; // - 0.5f;
mygl.putVertex(xf, yf, 0);
}
mygl.copyBuffer(gl);
mygl.drawArrays(gl,GL3.GL_LINE_STRIP);
}
void createFrame() // Fenster erzeugen
{ Frame f = new Frame(windowTitle);
f.setSize(windowWidth, windowHeight);
f.addWindowListener(this);
GLProfile glp = GLProfile.get(GLProfile.GL3);
GLCapabilities glCaps = new GLCapabilities(glp);
canvas = new GLCanvas(glCaps);
canvas.addGLEventListener(this);
f.add(canvas);
f.setVisible(true);
};
// ---------- OpenGL-Events ---------------------------
@Override
public void init(GLAutoDrawable drawable) // Initialisierung
{ GL3 gl = drawable.getGL().getGL3();
System.out.println("OpenGl Version: " + gl.glGetString(gl.GL_VERSION));
System.out.println("Shading Language: " + gl.glGetString(gl.GL_SHADING_LANGUAGE_VERSION));
System.out.println();
gl.glClearColor(0,0,0,1); // Hintergrundfarbe
int programId = MyShaders.initShaders(gl,vShader,fShader); // Compile/Link Shader-Programme
mygl = new MyGLBase1(gl, programId, maxVerts); // OpenGL Basis-Funktionen
FPSAnimator anim = new FPSAnimator(canvas, 1000, true);
anim.start();
}
@Override
public void display(GLAutoDrawable drawable)
{ GL3 gl = drawable.getGL().getGL3();
gl.glClear(GL3.GL_COLOR_BUFFER_BIT); // Bildschirm loeschen
mygl.setColor(1,0,0); // Farbe der Vertices
drawLissajous(gl, t++);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y,
int width, int height)
{ GL3 gl = drawable.getGL().getGL3();
// Set the viewport to be the entire window
gl.glViewport(0, 0, width, height);
}
@Override
public void dispose(GLAutoDrawable drawable) { } // not needed
// ----------- main-Methode ---------------------------
public static void main(String[] args)
{ new Lissajous();
}
// --------- Window-Events --------------------
public void windowClosing(WindowEvent e)
{ System.out.println("closing window");
System.exit(0);
}
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment