SHOW:
|
|
- or go back to the newest paste.
| 1 | import javax.swing.JFrame; | |
| 2 | import javax.swing.SwingUtilities; | |
| 3 | ||
| 4 | import javax.media.opengl.GL2; | |
| 5 | import javax.media.opengl.GLAutoDrawable; | |
| 6 | import javax.media.opengl.GLCapabilities; | |
| 7 | import javax.media.opengl.GLDrawable; | |
| 8 | import javax.media.opengl.GLEventListener; | |
| 9 | import javax.media.opengl.GLProfile; | |
| 10 | import javax.media.opengl.awt.GLCanvas; | |
| 11 | import static javax.media.opengl.GL2.*; | |
| 12 | ||
| 13 | public class test extends JFrame implements Runnable | |
| 14 | {
| |
| 15 | private GLCanvas canvas; | |
| 16 | ||
| 17 | public static long oldTime; | |
| 18 | ||
| 19 | public static long renderTime; | |
| 20 | ||
| 21 | private final int screenW = 800 , screenH = 600; | |
| 22 | ||
| 23 | /** | |
| 24 | * Serial version ID | |
| 25 | */ | |
| 26 | static final long serialVersionUID = 20090529; | |
| 27 | ||
| 28 | public static final String[] fragShader = new String[] {
| |
| 29 | "#version 330 core\n" + | |
| 30 | "in vec4 fragColor;\n" + | |
| 31 | "out vec4 color;\n" + | |
| 32 | "void main(void)\n" + | |
| 33 | "{\n" +
| |
| 34 | " color = fragColor;\n" + | |
| 35 | "}"}; | |
| 36 | public static final String[] vertexShader = new String[] {
| |
| 37 | "#version 330 core\n" + | |
| 38 | "layout (location = 0) in vec2 p0;\n" + | |
| 39 | "layout (location = 1) in vec2 p1;\n" + | |
| 40 | "layout (location = 2) in vec2 p2;\n" + | |
| 41 | "layout (location = 3) in vec4 color;\n" + | |
| 42 | "out vec4 fragColor;\n" + | |
| 43 | "void main(void)\n" + | |
| 44 | "{\n" +
| |
| 45 | " if(gl_VertexID == 0)\n" + | |
| 46 | " gl_Position.xy = p0;\n" + | |
| 47 | " else if(gl_VertexID == 1)\n" + | |
| 48 | " gl_Position.xy = p1;\n" + | |
| 49 | " else if(gl_VertexID == 2)\n" + | |
| 50 | " gl_Position.xy = p2;\n" + | |
| 51 | " gl_Position.z = 0;\n" + | |
| 52 | " gl_Position.w = 1;\n" + | |
| 53 | " fragColor = color;\n" + | |
| 54 | "}"}; | |
| 55 | ||
| 56 | private int shader; | |
| 57 | ||
| 58 | public static void main(String[] args) | |
| 59 | - | JFrame frame = new test(); |
| 59 | + | |
| 60 | SwingUtilities.invokeLater(new test()); | |
| 61 | } | |
| 62 | ||
| 63 | public test() | |
| 64 | {
| |
| 65 | //init jogl | |
| 66 | this.canvas = new GLCanvas(new GLCapabilities(GLProfile.getDefault())); | |
| 67 | canvas.setIgnoreRepaint(true); | |
| 68 | canvas.addGLEventListener(new GLHandler()); | |
| 69 | ||
| 70 | //system settings | |
| 71 | setIgnoreRepaint(true); | |
| 72 | setResizable(false); | |
| 73 | setSize(screenW , screenH); | |
| 74 | setUndecorated(true); | |
| 75 | setVisible(true); | |
| 76 | ||
| 77 | //customized initalization process and stuff | |
| 78 | add(canvas); | |
| 79 | validate(); | |
| 80 | this.run(); | |
| 81 | } | |
| 82 | ||
| 83 | public void run() | |
| 84 | {
| |
| 85 | while(true) | |
| 86 | {
| |
| 87 | canvas.display(); | |
| 88 | try //Sleep a bit, let other threads do their stuff | |
| 89 | {
| |
| 90 | Thread.sleep(1); | |
| 91 | } | |
| 92 | catch(InterruptedException ex) | |
| 93 | {
| |
| 94 | ex.printStackTrace(); | |
| 95 | System.exit(0); | |
| 96 | } | |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 100 | private double fps; | |
| 101 | private long cycle; | |
| 102 | private class GLHandler implements GLEventListener | |
| 103 | {
| |
| 104 | public void display(GLAutoDrawable drawable) | |
| 105 | {
| |
| 106 | System.out.println("Fps: " + fps + " " + cycle);
| |
| 107 | cycle++; | |
| 108 | renderTime = System.nanoTime(); | |
| 109 | if(oldTime != 0 && renderTime != oldTime) | |
| 110 | fps = fps*0.9 + 100000000/(renderTime - oldTime); | |
| 111 | oldTime = renderTime; | |
| 112 | ||
| 113 | GL2 gl = drawable.getGL().getGL2(); | |
| 114 | ||
| 115 | gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| 116 | ||
| 117 | gl.glDisable(GL_TEXTURE_2D); | |
| 118 | //test OpenGL responsiveness | |
| 119 | float[] color = new float[]{0.5f,0.25f +cycle%100/400f,0.25f+cycle%100/400f,1};
| |
| 120 | gl.glClearBufferfv(GL_COLOR , 0 , color , 0); | |
| 121 | ||
| 122 | gl.glUseProgram(shader); | |
| 123 | ||
| 124 | gl.glVertexAttrib2fv(0, new float[]{0 , 0} , 0);
| |
| 125 | gl.glVertexAttrib2fv(1, new float[]{0.25f + (cycle%2)*0.25f, 0} , 0);
| |
| 126 | gl.glVertexAttrib2fv(2, new float[]{0 , 0.5f} , 0);
| |
| 127 | gl.glVertexAttrib4fv(3, new float[]{0 , 1.0f*(cycle%128)/128f , 1.0f*(cycle%256)/256f , 1} , 0);
| |
| 128 | ||
| 129 | // Draw one triangle | |
| 130 | gl.glDrawArrays(GL_TRIANGLES, 0, 3); | |
| 131 | } | |
| 132 | ||
| 133 | //Invoked when canvas is destroyed i.e. system exit. Nothing needing done. | |
| 134 | public void dispose(GLAutoDrawable drawable) | |
| 135 | {
| |
| 136 | drawable.getGL().getGL2().glDeleteProgram(shader); | |
| 137 | } | |
| 138 | ||
| 139 | public void init(GLAutoDrawable drawable) | |
| 140 | {
| |
| 141 | //Force v-sync. | |
| 142 | drawable.getGL().setSwapInterval(1); | |
| 143 | ||
| 144 | //disable 3D | |
| 145 | drawable.getGL().glDisable(GL_DEPTH_TEST); | |
| 146 | ||
| 147 | GL2 gl = drawable.getGL().getGL2(); | |
| 148 | ||
| 149 | System.out.println(gl.glGetString(GL_SHADING_LANGUAGE_VERSION)); | |
| 150 | ||
| 151 | //enable graphics space | |
| 152 | gl.glEnable(GL_BLEND); | |
| 153 | gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 154 | ||
| 155 | //set screen size | |
| 156 | gl.glViewport(0 , 0 , screenW , screenH); | |
| 157 | gl.glMatrixMode(GL_PROJECTION); | |
| 158 | gl.glLoadIdentity(); | |
| 159 | ||
| 160 | //set ortho view | |
| 161 | gl.glOrtho(0 , screenW , 0 , screenH , -1 , 1); | |
| 162 | gl.glMatrixMode(GL_MODELVIEW); | |
| 163 | gl.glLoadIdentity(); | |
| 164 | ||
| 165 | //set screen default color | |
| 166 | //gl.glClearColor(0.25f,0.25f,0.25f,1); | |
| 167 | ||
| 168 | int v = gl.glCreateShader(GL_VERTEX_SHADER); | |
| 169 | gl.glShaderSource(v , vertexShader.length , vertexShader , new int[] { vertexShader[0].length() } , 0);
| |
| 170 | gl.glCompileShader(v); | |
| 171 | ||
| 172 | {
| |
| 173 | int[] compiled = new int[1]; | |
| 174 | gl.glGetShaderiv(v , GL_COMPILE_STATUS , compiled , 0); | |
| 175 | if(compiled[0]!=0) | |
| 176 | System.out.println("Horray! vertex shader compiled");
| |
| 177 | else | |
| 178 | {
| |
| 179 | int[] logLength = new int[1]; | |
| 180 | gl.glGetShaderiv(v , GL_INFO_LOG_LENGTH, logLength , 0); | |
| 181 | ||
| 182 | byte[] log = new byte[logLength[0]]; | |
| 183 | gl.glGetShaderInfoLog(v , logLength[0] , (int[])null , 0 , log , 0); | |
| 184 | ||
| 185 | System.err.println("Error compiling the vertex shader: " + new String(log));
| |
| 186 | System.exit(1); | |
| 187 | } | |
| 188 | } | |
| 189 | ||
| 190 | int f = gl.glCreateShader(GL_FRAGMENT_SHADER); | |
| 191 | gl.glShaderSource(f , fragShader.length , fragShader , new int[] { fragShader[0].length() } , 0);
| |
| 192 | gl.glCompileShader(f); | |
| 193 | ||
| 194 | {
| |
| 195 | int[] compiled = new int[1]; | |
| 196 | gl.glGetShaderiv(f , GL_COMPILE_STATUS , compiled , 0); | |
| 197 | if(compiled[0]!=0) | |
| 198 | System.out.println("Horray! frag shader compiled");
| |
| 199 | else | |
| 200 | {
| |
| 201 | int[] logLength = new int[1]; | |
| 202 | gl.glGetShaderiv(f , GL_INFO_LOG_LENGTH, logLength , 0); | |
| 203 | ||
| 204 | byte[] log = new byte[logLength[0]]; | |
| 205 | gl.glGetShaderInfoLog(f , logLength[0] , (int[])null , 0 , log , 0); | |
| 206 | ||
| 207 | System.err.println("Error compiling the vertex shader: " + new String(log));
| |
| 208 | System.exit(1); | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | ||
| 213 | shader = gl.glCreateProgram(); | |
| 214 | gl.glAttachShader(shader , v); | |
| 215 | gl.glAttachShader(shader , f); | |
| 216 | ||
| 217 | gl.glLinkProgram(shader); | |
| 218 | gl.glValidateProgram(shader); | |
| 219 | ||
| 220 | gl.glDeleteShader(v); | |
| 221 | gl.glDeleteShader(f); | |
| 222 | } | |
| 223 | ||
| 224 | // called when user resizes the window. Never happen. | |
| 225 | public void reshape(GLAutoDrawable drawable , int x , int y , | |
| 226 | int width , int height) {}
| |
| 227 | } | |
| 228 | } |