Guest User

Untitled

a guest
Mar 22nd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import java.awt.AlphaComposite;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.Shape;
  9. import java.awt.font.GlyphVector;
  10. import java.awt.font.FontRenderContext;
  11. import java.awt.font.TextLayout;
  12. import java.awt.image.BufferedImage;
  13. import java.io.File;
  14. import javax.imageio.ImageIO;
  15. import javax.swing.JFrame;
  16. import javax.swing.SwingUtilities;
  17.  
  18. import com.jogamp.opengl.util.texture.Texture;
  19. import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
  20. import javax.media.opengl.GL2;
  21. import javax.media.opengl.GLAutoDrawable;
  22. import javax.media.opengl.GLCapabilities;
  23. import javax.media.opengl.GLDrawable;
  24. import javax.media.opengl.GLEventListener;
  25. import javax.media.opengl.GLProfile;
  26. import javax.media.opengl.awt.GLCanvas;
  27. import static javax.media.opengl.GL2.*;
  28.  
  29. import java.awt.Frame;
  30. import java.awt.event.WindowAdapter;
  31. import java.awt.event.WindowEvent;
  32. import javax.media.opengl.*;
  33. import javax.media.opengl.awt.GLCanvas;
  34. import com.jogamp.opengl.util.*;
  35.  
  36. public class test implements GLEventListener {
  37.  
  38. private double theta = 0;
  39. private double s = 0;
  40. private double c = 0;
  41.  
  42. public static void main(String[] args) {
  43. GLProfile glp = GLProfile.getDefault();
  44. GLCapabilities caps = new GLCapabilities(glp);
  45. GLCanvas canvas = new GLCanvas(caps);
  46.  
  47. Frame frame = new Frame("AWT Window Test");
  48. frame.setSize(300, 300);
  49. frame.add(canvas);
  50. frame.setVisible(true);
  51.  
  52. frame.addWindowListener(new WindowAdapter() {
  53. public void windowClosing(WindowEvent e) {
  54. System.exit(0);
  55. }
  56. });
  57.  
  58. canvas.addGLEventListener(new test());
  59.  
  60. FPSAnimator animator = new FPSAnimator(canvas, 60);
  61. animator.start();
  62. }
  63.  
  64. public static long oldTime;
  65.  
  66. public static long renderTime;
  67.  
  68. private final int screenW = 800 , screenH = 600;
  69.  
  70. /**
  71. * Serial version ID
  72. */
  73. static final long serialVersionUID = 20090529;
  74.  
  75. public static final String[] fragShader = new String[] {
  76. "#version 330 core\n" +
  77. "in vec4 fragColor;\n" +
  78. "out vec4 color;\n" +
  79. "void main(void)\n" +
  80. "{\n" +
  81. " color = fragColor;\n" +
  82. "}"};
  83. public static final String[] vertexShader = new String[] {
  84. "#version 330 core\n" +
  85. "layout (location = 0) in vec2 p0;\n" +
  86. "layout (location = 1) in vec2 p1;\n" +
  87. "layout (location = 2) in vec2 p2;\n" +
  88. "layout (location = 3) in vec4 color;\n" +
  89. "out vec4 fragColor;\n" +
  90. "void main(void)\n" +
  91. "{\n" +
  92. " if(gl_VertexID == 0)\n" +
  93. " gl_Position.xy = p0;\n" +
  94. " else if(gl_VertexID == 1)\n" +
  95. " gl_Position.xy = p1;\n" +
  96. " else if(gl_VertexID == 2)\n" +
  97. " gl_Position.xy = p2;\n" +
  98. " gl_Position.z = 0;\n" +
  99. " gl_Position.w = 1;\n" +
  100. " fragColor = color;\n" +
  101. "}"};
  102.  
  103. private int shader;
  104.  
  105. public static final Font font = new Font("Arial" , Font.PLAIN , 40);
  106. private double fps;
  107. private Texture text;
  108. private long cycle;
  109.  
  110. public void display(GLAutoDrawable drawable)
  111. {
  112. System.out.println("Fps: " + fps + " " + cycle);
  113. cycle++;
  114. renderTime = System.nanoTime();
  115. if(oldTime != 0 && renderTime != oldTime)
  116. fps = fps*0.9 + 100000000/(renderTime - oldTime);
  117. oldTime = renderTime;
  118.  
  119. GL2 gl = drawable.getGL().getGL2();
  120.  
  121. gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  122.  
  123. gl.glDisable(GL_TEXTURE_2D);
  124. float[] color = new float[]{0, 0.5f + cycle%100/200f ,0.5f + cycle%100/200f,1};
  125. gl.glClearBufferfv(GL_COLOR , 0 , color , 0);
  126.  
  127. gl.glUseProgram(shader);
  128.  
  129. gl.glVertexAttrib2fv(0, new float[]{0 , 0} , 0);
  130. gl.glVertexAttrib2fv(1, new float[]{0.25f + (cycle%2)*0.25f, 0} , 0);
  131. gl.glVertexAttrib2fv(2, new float[]{0 , 0.5f} , 0);
  132. gl.glVertexAttrib4fv(3, new float[]{0 , 1.0f*(cycle%256)/256f , 1.0f*(cycle%256)/256f , 1} , 0);
  133.  
  134.  
  135. // Draw one triangle
  136. gl.glDrawArrays(GL_TRIANGLES, 0, 3);
  137. gl.glScalef(2.0f,-0.5f,1.0f);
  138. }
  139.  
  140. //Invoked when canvas is destroyed i.e. system exit. Nothing needing done.
  141. public void dispose(GLAutoDrawable drawable)
  142. {
  143. drawable.getGL().getGL2().glDeleteProgram(shader);
  144. }
  145.  
  146. public void init(GLAutoDrawable drawable)
  147. {
  148. //Force v-sync.
  149. drawable.getGL().setSwapInterval(1);
  150.  
  151. //disable 3D
  152. drawable.getGL().glDisable(GL_DEPTH_TEST);
  153.  
  154. GL2 gl = drawable.getGL().getGL2();
  155.  
  156. System.out.println(gl.glGetString(GL_SHADING_LANGUAGE_VERSION));
  157.  
  158. //enable graphics space
  159. gl.glEnable(GL_BLEND);
  160. gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  161.  
  162. //set screen size
  163. gl.glViewport(0 , 0 , screenW , screenH);
  164. gl.glMatrixMode(GL_PROJECTION);
  165. gl.glLoadIdentity();
  166.  
  167. //set ortho view
  168. gl.glOrtho(0 , screenW , 0 , screenH , -1 , 1);
  169. gl.glMatrixMode(GL_MODELVIEW);
  170. gl.glLoadIdentity();
  171.  
  172. //set screen default color
  173. //gl.glClearColor(0.25f,0.25f,0.25f,1);
  174.  
  175. int v = gl.glCreateShader(GL_VERTEX_SHADER);
  176. gl.glShaderSource(v , vertexShader.length , vertexShader , new int[] { vertexShader[0].length() } , 0);
  177. gl.glCompileShader(v);
  178.  
  179. {
  180. int[] compiled = new int[1];
  181. gl.glGetShaderiv(v , GL_COMPILE_STATUS , compiled , 0);
  182. if(compiled[0]!=0)
  183. System.out.println("Horray! vertex shader compiled");
  184. else
  185. {
  186. int[] logLength = new int[1];
  187. gl.glGetShaderiv(v , GL_INFO_LOG_LENGTH, logLength , 0);
  188.  
  189. byte[] log = new byte[logLength[0]];
  190. gl.glGetShaderInfoLog(v , logLength[0] , (int[])null , 0 , log , 0);
  191.  
  192. System.err.println("Error compiling the vertex shader: " + new String(log));
  193. System.exit(1);
  194. }
  195. }
  196.  
  197. int f = gl.glCreateShader(GL_FRAGMENT_SHADER);
  198. gl.glShaderSource(f , fragShader.length , fragShader , new int[] { fragShader[0].length() } , 0);
  199. gl.glCompileShader(f);
  200.  
  201. {
  202. int[] compiled = new int[1];
  203. gl.glGetShaderiv(f , GL_COMPILE_STATUS , compiled , 0);
  204. if(compiled[0]!=0)
  205. System.out.println("Horray! frag shader compiled");
  206. else
  207. {
  208. int[] logLength = new int[1];
  209. gl.glGetShaderiv(f , GL_INFO_LOG_LENGTH, logLength , 0);
  210.  
  211. byte[] log = new byte[logLength[0]];
  212. gl.glGetShaderInfoLog(f , logLength[0] , (int[])null , 0 , log , 0);
  213.  
  214. System.err.println("Error compiling the vertex shader: " + new String(log));
  215. System.exit(1);
  216. }
  217. }
  218.  
  219.  
  220. shader = gl.glCreateProgram();
  221. gl.glAttachShader(shader , v);
  222. gl.glAttachShader(shader , f);
  223.  
  224. gl.glLinkProgram(shader);
  225. gl.glValidateProgram(shader);
  226.  
  227. gl.glDeleteShader(v);
  228. gl.glDeleteShader(f);
  229. }
  230.  
  231. // called when user resizes the window. Never happen.
  232. public void reshape(GLAutoDrawable drawable , int x , int y ,
  233. int width , int height) {}
  234. }
Advertisement
Add Comment
Please, Sign In to add comment