SHOW:
|
|
- or go back to the newest paste.
| 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 {
|
| 36 | + | public class test implements GLEventListener |
| 37 | {
| |
| 38 | - | private double theta = 0; |
| 38 | + | |
| 39 | - | private double s = 0; |
| 39 | + | |
| 40 | - | private double c = 0; |
| 40 | + | |
| 41 | GLCanvas canvas = new GLCanvas(caps); | |
| 42 | ||
| 43 | Frame frame = new Frame("AWT Window Test");
| |
| 44 | frame.setSize(300, 300); | |
| 45 | frame.add(canvas); | |
| 46 | frame.setVisible(true); | |
| 47 | ||
| 48 | frame.addWindowListener(new WindowAdapter() {
| |
| 49 | public void windowClosing(WindowEvent e) {
| |
| 50 | System.exit(0); | |
| 51 | } | |
| 52 | }); | |
| 53 | ||
| 54 | canvas.addGLEventListener(new test()); | |
| 55 | ||
| 56 | Animator animator = new Animator(canvas); | |
| 57 | animator.start(); | |
| 58 | } | |
| 59 | ||
| 60 | - | FPSAnimator animator = new FPSAnimator(canvas, 60); |
| 60 | + | |
| 61 | ||
| 62 | public static long renderTime; | |
| 63 | ||
| 64 | private final int screenW = 1000 , screenH = 1000; | |
| 65 | ||
| 66 | /** | |
| 67 | * Serial version ID | |
| 68 | */ | |
| 69 | static final long serialVersionUID = 20090529; | |
| 70 | ||
| 71 | public static final String[] fragShader = new String[] {
| |
| 72 | "#version 330 core\n" + | |
| 73 | "in vec4 fragColor;\n" + | |
| 74 | "out vec4 color;\n" + | |
| 75 | "void main(void)\n" + | |
| 76 | "{\n" +
| |
| 77 | " color = fragColor;\n" + | |
| 78 | "}"}; | |
| 79 | public static final String[] vertexShader = new String[] {
| |
| 80 | "#version 330 core\n" + | |
| 81 | "layout (location = 0) in vec2 p0;\n" + | |
| 82 | "layout (location = 1) in vec2 p1;\n" + | |
| 83 | "layout (location = 2) in vec2 p2;\n" + | |
| 84 | "layout (location = 3) in vec4 color;\n" + | |
| 85 | "out vec4 fragColor;\n" + | |
| 86 | "void main(void)\n" + | |
| 87 | "{\n" +
| |
| 88 | " if(gl_VertexID == 0)\n" + | |
| 89 | " gl_Position.xy = p0;\n" + | |
| 90 | " else if(gl_VertexID == 1)\n" + | |
| 91 | " gl_Position.xy = p1;\n" + | |
| 92 | " else if(gl_VertexID == 2)\n" + | |
| 93 | " gl_Position.xy = p2;\n" + | |
| 94 | " gl_Position.z = 0;\n" + | |
| 95 | " gl_Position.w = 1;\n" + | |
| 96 | " fragColor = color;\n" + | |
| 97 | "}"}; | |
| 98 | ||
| 99 | private int shader; | |
| 100 | ||
| 101 | public static final Font font = new Font("Arial" , Font.PLAIN , 40);
| |
| 102 | private double fps; | |
| 103 | private Texture text; | |
| 104 | ||
| 105 | public void display(GLAutoDrawable drawable) | |
| 106 | {
| |
| 107 | System.out.println("Fps: " + fps);
| |
| 108 | - | private long cycle; |
| 108 | + | renderTime = System.nanoTime(); |
| 109 | if(oldTime != 0 && renderTime != oldTime) | |
| 110 | fps = fps*0.9 + 100_000_000/(renderTime - oldTime); | |
| 111 | oldTime = renderTime; | |
| 112 | - | System.out.println("Fps: " + fps + " " + cycle);
|
| 112 | + | renderTime /= 100_000_000; //10 ticks per second |
| 113 | - | cycle++; |
| 113 | + | |
| 114 | - | renderTime = System.nanoTime(); |
| 114 | + | |
| 115 | - | if(oldTime != 0 && renderTime != oldTime) |
| 115 | + | |
| 116 | - | fps = fps*0.9 + 100000000/(renderTime - oldTime); |
| 116 | + | |
| 117 | - | oldTime = renderTime; |
| 117 | + | |
| 118 | gl.glDisable(GL_TEXTURE_2D); | |
| 119 | float[] color = new float[]{0, 0.5f + renderTime%100/200f ,0.5f + renderTime%100/200f,1};
| |
| 120 | gl.glClearBufferfv(GL_COLOR , 0 , color , 0); | |
| 121 | ||
| 122 | gl.glUseProgram(shader); | |
| 123 | ||
| 124 | - | float[] color = new float[]{0, 0.5f + cycle%100/200f ,0.5f + cycle%100/200f,1};
|
| 124 | + | |
| 125 | gl.glVertexAttrib2fv(1, new float[]{0.25f + (renderTime/10)%2/4f, 0} , 0);
| |
| 126 | gl.glVertexAttrib2fv(2, new float[]{0 , 0.25f + (renderTime/10+1)%2/4f} , 0);
| |
| 127 | gl.glVertexAttrib4fv(3, new float[]{0.5f + renderTime%100/200f , 0.5f + renderTime%100/200f , 0 , 1} , 0);
| |
| 128 | ||
| 129 | ||
| 130 | - | gl.glVertexAttrib2fv(1, new float[]{0.25f + cycle%2/4f, 0} , 0);
|
| 130 | + | |
| 131 | - | gl.glVertexAttrib2fv(2, new float[]{0 , 0.25f + (cycle+1)%2/4f} , 0);
|
| 131 | + | |
| 132 | - | gl.glVertexAttrib4fv(3, new float[]{0.5f + cycle%100/200f , 0.5f + cycle%100/200f , 0 , 1} , 0);
|
| 132 | + | |
| 133 | } | |
| 134 | ||
| 135 | //Invoked when canvas is destroyed i.e. system exit. Nothing needing done. | |
| 136 | public void dispose(GLAutoDrawable drawable) | |
| 137 | {
| |
| 138 | drawable.getGL().getGL2().glDeleteProgram(shader); | |
| 139 | } | |
| 140 | ||
| 141 | public void init(GLAutoDrawable drawable) | |
| 142 | {
| |
| 143 | //Force v-sync. | |
| 144 | //drawable.getGL().setSwapInterval(1); | |
| 145 | ||
| 146 | //disable 3D | |
| 147 | drawable.getGL().glDisable(GL_DEPTH_TEST); | |
| 148 | ||
| 149 | - | drawable.getGL().setSwapInterval(1); |
| 149 | + | |
| 150 | ||
| 151 | System.out.println(gl.glGetString(GL_SHADING_LANGUAGE_VERSION)); | |
| 152 | ||
| 153 | //enable graphics space | |
| 154 | gl.glEnable(GL_BLEND); | |
| 155 | gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 156 | ||
| 157 | //set screen size | |
| 158 | gl.glViewport(0 , 0 , screenW , screenH); | |
| 159 | gl.glMatrixMode(GL_PROJECTION); | |
| 160 | gl.glLoadIdentity(); | |
| 161 | ||
| 162 | //set ortho view | |
| 163 | gl.glOrtho(0 , screenW , 0 , screenH , -1 , 1); | |
| 164 | gl.glMatrixMode(GL_MODELVIEW); | |
| 165 | gl.glLoadIdentity(); | |
| 166 | ||
| 167 | //set screen default color | |
| 168 | //gl.glClearColor(0.25f,0.25f,0.25f,1); | |
| 169 | ||
| 170 | int v = gl.glCreateShader(GL_VERTEX_SHADER); | |
| 171 | gl.glShaderSource(v , vertexShader.length , vertexShader , new int[] { vertexShader[0].length() } , 0);
| |
| 172 | gl.glCompileShader(v); | |
| 173 | ||
| 174 | {
| |
| 175 | int[] compiled = new int[1]; | |
| 176 | gl.glGetShaderiv(v , GL_COMPILE_STATUS , compiled , 0); | |
| 177 | if(compiled[0]!=0) | |
| 178 | System.out.println("Horray! vertex shader compiled");
| |
| 179 | else | |
| 180 | {
| |
| 181 | int[] logLength = new int[1]; | |
| 182 | gl.glGetShaderiv(v , GL_INFO_LOG_LENGTH, logLength , 0); | |
| 183 | ||
| 184 | byte[] log = new byte[logLength[0]]; | |
| 185 | gl.glGetShaderInfoLog(v , logLength[0] , (int[])null , 0 , log , 0); | |
| 186 | ||
| 187 | System.err.println("Error compiling the vertex shader: " + new String(log));
| |
| 188 | System.exit(1); | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | int f = gl.glCreateShader(GL_FRAGMENT_SHADER); | |
| 193 | gl.glShaderSource(f , fragShader.length , fragShader , new int[] { fragShader[0].length() } , 0);
| |
| 194 | gl.glCompileShader(f); | |
| 195 | ||
| 196 | {
| |
| 197 | int[] compiled = new int[1]; | |
| 198 | gl.glGetShaderiv(f , GL_COMPILE_STATUS , compiled , 0); | |
| 199 | if(compiled[0]!=0) | |
| 200 | System.out.println("Horray! frag shader compiled");
| |
| 201 | else | |
| 202 | {
| |
| 203 | int[] logLength = new int[1]; | |
| 204 | gl.glGetShaderiv(f , GL_INFO_LOG_LENGTH, logLength , 0); | |
| 205 | ||
| 206 | byte[] log = new byte[logLength[0]]; | |
| 207 | gl.glGetShaderInfoLog(f , logLength[0] , (int[])null , 0 , log , 0); | |
| 208 | ||
| 209 | System.err.println("Error compiling the vertex shader: " + new String(log));
| |
| 210 | System.exit(1); | |
| 211 | } | |
| 212 | } | |
| 213 | ||
| 214 | ||
| 215 | shader = gl.glCreateProgram(); | |
| 216 | gl.glAttachShader(shader , v); | |
| 217 | gl.glAttachShader(shader , f); | |
| 218 | ||
| 219 | gl.glLinkProgram(shader); | |
| 220 | gl.glValidateProgram(shader); | |
| 221 | ||
| 222 | gl.glDeleteShader(v); | |
| 223 | gl.glDeleteShader(f); | |
| 224 | } | |
| 225 | ||
| 226 | // called when user resizes the window. Never happen. | |
| 227 | public void reshape(GLAutoDrawable drawable , int x , int y , | |
| 228 | int width , int height) {}
| |
| 229 | } |