Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. import ch.fhnw.util.math.Mat4;
  2. import com.jogamp.opengl.*;
  3. import com.jogamp.opengl.awt.GLCanvas;
  4. import com.jogamp.opengl.util.FPSAnimator;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.WindowEvent;
  8. import java.awt.event.WindowListener;
  9.  
  10. public class Lissajous implements WindowListener, GLEventListener{
  11. // --------- globale Daten ---------------------------
  12.  
  13. String windowTitle = "Lissajous";
  14. int windowWidth = 500;
  15. int windowHeight = 500;
  16. String vShader = MyShaders.vShader0; // Vertex-Shader
  17. String fShader = MyShaders.fShader0; // Fragment-Shader
  18. int maxVerts = 2048; // max. Anzahl Vertices im Vertex-Array
  19. GLCanvas canvas; // OpenGL Window
  20. MyGLBase1 mygl; // eigene OpenGL-Basisfunktionen
  21.  
  22. // ---------- Settings -------------
  23. final float amplitudeX = 0.5f;
  24. final float amplitudeY = 0.5f;
  25. final float frequencyX = 1;
  26. final float frequencyY = 1.0f;
  27. final float shiftDelta = 0.1f;
  28. final int points = 400;
  29.  
  30. // changing params
  31. int t = 0;
  32.  
  33.  
  34. public Lissajous() // Konstruktor
  35. { createFrame();
  36. System.out.println(Math.sin(Math.toRadians(270)));
  37. }
  38.  
  39. public void drawLissajous(GL3 gl, int t) {
  40. mygl.rewindBuffer(gl);
  41.  
  42. for (int i = 0; i < points; i++) {
  43. float shift = t * shiftDelta;
  44. double xDegree = Math.toRadians(frequencyX * (t + i));
  45. double yDegree = Math.toRadians(frequencyY * (t + i) - shift);
  46. double x = amplitudeX * Math.cos(xDegree);
  47. double y = amplitudeY * Math.sin(yDegree);
  48. float xf = (float) x; // - 0.5f;
  49. float yf = (float) y; // - 0.5f;
  50. mygl.putVertex(xf, yf, 0);
  51. }
  52. mygl.copyBuffer(gl);
  53. mygl.drawArrays(gl,GL3.GL_LINE_STRIP);
  54. }
  55.  
  56. void createFrame() // Fenster erzeugen
  57. { Frame f = new Frame(windowTitle);
  58. f.setSize(windowWidth, windowHeight);
  59. f.addWindowListener(this);
  60. GLProfile glp = GLProfile.get(GLProfile.GL3);
  61. GLCapabilities glCaps = new GLCapabilities(glp);
  62. canvas = new GLCanvas(glCaps);
  63. canvas.addGLEventListener(this);
  64. f.add(canvas);
  65. f.setVisible(true);
  66. };
  67.  
  68. // ---------- OpenGL-Events ---------------------------
  69.  
  70. @Override
  71. public void init(GLAutoDrawable drawable) // Initialisierung
  72. { GL3 gl = drawable.getGL().getGL3();
  73. System.out.println("OpenGl Version: " + gl.glGetString(gl.GL_VERSION));
  74. System.out.println("Shading Language: " + gl.glGetString(gl.GL_SHADING_LANGUAGE_VERSION));
  75. System.out.println();
  76. gl.glClearColor(0,0,0,1); // Hintergrundfarbe
  77. int programId = MyShaders.initShaders(gl,vShader,fShader); // Compile/Link Shader-Programme
  78. mygl = new MyGLBase1(gl, programId, maxVerts); // OpenGL Basis-Funktionen
  79. FPSAnimator anim = new FPSAnimator(canvas, 1000, true);
  80. anim.start();
  81. }
  82.  
  83.  
  84. @Override
  85. public void display(GLAutoDrawable drawable)
  86. { GL3 gl = drawable.getGL().getGL3();
  87. gl.glClear(GL3.GL_COLOR_BUFFER_BIT); // Bildschirm loeschen
  88. mygl.setColor(1,0,0); // Farbe der Vertices
  89. drawLissajous(gl, t++);
  90. }
  91.  
  92.  
  93. @Override
  94. public void reshape(GLAutoDrawable drawable, int x, int y,
  95. int width, int height)
  96. { GL3 gl = drawable.getGL().getGL3();
  97. // Set the viewport to be the entire window
  98. gl.glViewport(0, 0, width, height);
  99. }
  100.  
  101. @Override
  102. public void dispose(GLAutoDrawable drawable) { } // not needed
  103.  
  104. // ----------- main-Methode ---------------------------
  105. public static void main(String[] args)
  106. { new Lissajous();
  107. }
  108.  
  109. // --------- Window-Events --------------------
  110. public void windowClosing(WindowEvent e)
  111. { System.out.println("closing window");
  112. System.exit(0);
  113. }
  114. public void windowActivated(WindowEvent e) { }
  115. public void windowClosed(WindowEvent e) { }
  116. public void windowDeactivated(WindowEvent e) { }
  117. public void windowDeiconified(WindowEvent e) { }
  118. public void windowIconified(WindowEvent e) { }
  119. public void windowOpened(WindowEvent e) { }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement