Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AlphaComposite;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.Shape;
- import java.awt.font.GlyphVector;
- import java.awt.font.FontRenderContext;
- import java.awt.font.TextLayout;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.SwingUtilities;
- import com.jogamp.opengl.util.texture.Texture;
- import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLDrawable;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.GLProfile;
- import javax.media.opengl.awt.GLCanvas;
- import static javax.media.opengl.GL2.*;
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.media.opengl.*;
- import javax.media.opengl.awt.GLCanvas;
- import com.jogamp.opengl.util.*;
- public class test implements GLEventListener {
- private double theta = 0;
- private double s = 0;
- private double c = 0;
- public static void main(String[] args) {
- GLProfile glp = GLProfile.getDefault();
- GLCapabilities caps = new GLCapabilities(glp);
- GLCanvas canvas = new GLCanvas(caps);
- Frame frame = new Frame("AWT Window Test");
- frame.setSize(300, 300);
- frame.add(canvas);
- frame.setVisible(true);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- canvas.addGLEventListener(new test());
- FPSAnimator animator = new FPSAnimator(canvas, 60);
- animator.start();
- }
- public static long oldTime;
- public static long renderTime;
- private final int screenW = 1000 , screenH = 1000;
- /**
- * Serial version ID
- */
- static final long serialVersionUID = 20090529;
- public static final String[] fragShader = new String[] {
- "#version 330 core\n" +
- "in vec4 fragColor;\n" +
- "out vec4 color;\n" +
- "void main(void)\n" +
- "{\n" +
- " color = fragColor;\n" +
- "}"};
- public static final String[] vertexShader = new String[] {
- "#version 330 core\n" +
- "layout (location = 0) in vec2 p0;\n" +
- "layout (location = 1) in vec2 p1;\n" +
- "layout (location = 2) in vec2 p2;\n" +
- "layout (location = 3) in vec4 color;\n" +
- "out vec4 fragColor;\n" +
- "void main(void)\n" +
- "{\n" +
- " if(gl_VertexID == 0)\n" +
- " gl_Position.xy = p0;\n" +
- " else if(gl_VertexID == 1)\n" +
- " gl_Position.xy = p1;\n" +
- " else if(gl_VertexID == 2)\n" +
- " gl_Position.xy = p2;\n" +
- " gl_Position.z = 0;\n" +
- " gl_Position.w = 1;\n" +
- " fragColor = color;\n" +
- "}"};
- private int shader;
- public static final Font font = new Font("Arial" , Font.PLAIN , 40);
- private double fps;
- private Texture text;
- private long cycle;
- public void display(GLAutoDrawable drawable)
- {
- System.out.println("Fps: " + fps + " " + cycle);
- cycle++;
- renderTime = System.nanoTime();
- if(oldTime != 0 && renderTime != oldTime)
- fps = fps*0.9 + 100000000/(renderTime - oldTime);
- oldTime = renderTime;
- GL2 gl = drawable.getGL().getGL2();
- gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- gl.glDisable(GL_TEXTURE_2D);
- float[] color = new float[]{0, 0.5f + cycle%100/200f ,0.5f + cycle%100/200f,1};
- gl.glClearBufferfv(GL_COLOR , 0 , color , 0);
- gl.glUseProgram(shader);
- gl.glVertexAttrib2fv(0, new float[]{0 , 0} , 0);
- gl.glVertexAttrib2fv(1, new float[]{0.25f + cycle%2/4f, 0} , 0);
- gl.glVertexAttrib2fv(2, new float[]{0 , 0.25f + (cycle+1)%2/4f} , 0);
- gl.glVertexAttrib4fv(3, new float[]{0.5f + cycle%100/200f , 0.5f + cycle%100/200f , 0 , 1} , 0);
- // Draw one triangle
- gl.glDrawArrays(GL_TRIANGLES, 0, 3);
- gl.glScalef(2.0f,-0.5f,1.0f);
- }
- //Invoked when canvas is destroyed i.e. system exit. Nothing needing done.
- public void dispose(GLAutoDrawable drawable)
- {
- drawable.getGL().getGL2().glDeleteProgram(shader);
- }
- public void init(GLAutoDrawable drawable)
- {
- //Force v-sync.
- drawable.getGL().setSwapInterval(1);
- //disable 3D
- drawable.getGL().glDisable(GL_DEPTH_TEST);
- GL2 gl = drawable.getGL().getGL2();
- System.out.println(gl.glGetString(GL_SHADING_LANGUAGE_VERSION));
- //enable graphics space
- gl.glEnable(GL_BLEND);
- gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- //set screen size
- gl.glViewport(0 , 0 , screenW , screenH);
- gl.glMatrixMode(GL_PROJECTION);
- gl.glLoadIdentity();
- //set ortho view
- gl.glOrtho(0 , screenW , 0 , screenH , -1 , 1);
- gl.glMatrixMode(GL_MODELVIEW);
- gl.glLoadIdentity();
- //set screen default color
- //gl.glClearColor(0.25f,0.25f,0.25f,1);
- int v = gl.glCreateShader(GL_VERTEX_SHADER);
- gl.glShaderSource(v , vertexShader.length , vertexShader , new int[] { vertexShader[0].length() } , 0);
- gl.glCompileShader(v);
- {
- int[] compiled = new int[1];
- gl.glGetShaderiv(v , GL_COMPILE_STATUS , compiled , 0);
- if(compiled[0]!=0)
- System.out.println("Horray! vertex shader compiled");
- else
- {
- int[] logLength = new int[1];
- gl.glGetShaderiv(v , GL_INFO_LOG_LENGTH, logLength , 0);
- byte[] log = new byte[logLength[0]];
- gl.glGetShaderInfoLog(v , logLength[0] , (int[])null , 0 , log , 0);
- System.err.println("Error compiling the vertex shader: " + new String(log));
- System.exit(1);
- }
- }
- int f = gl.glCreateShader(GL_FRAGMENT_SHADER);
- gl.glShaderSource(f , fragShader.length , fragShader , new int[] { fragShader[0].length() } , 0);
- gl.glCompileShader(f);
- {
- int[] compiled = new int[1];
- gl.glGetShaderiv(f , GL_COMPILE_STATUS , compiled , 0);
- if(compiled[0]!=0)
- System.out.println("Horray! frag shader compiled");
- else
- {
- int[] logLength = new int[1];
- gl.glGetShaderiv(f , GL_INFO_LOG_LENGTH, logLength , 0);
- byte[] log = new byte[logLength[0]];
- gl.glGetShaderInfoLog(f , logLength[0] , (int[])null , 0 , log , 0);
- System.err.println("Error compiling the vertex shader: " + new String(log));
- System.exit(1);
- }
- }
- shader = gl.glCreateProgram();
- gl.glAttachShader(shader , v);
- gl.glAttachShader(shader , f);
- gl.glLinkProgram(shader);
- gl.glValidateProgram(shader);
- gl.glDeleteShader(v);
- gl.glDeleteShader(f);
- }
- // called when user resizes the window. Never happen.
- public void reshape(GLAutoDrawable drawable , int x , int y ,
- int width , int height) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement