1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| import com.sun.opengl.util.*; import com.sun.opengl.util.texture.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import java.awt.*; import java.awt.event.*; import java.io.*;
public class BasicGLSL implements GLEventListener {
private String textureImageFile = "C:/data/nhs/images/flower.jpg";
public int vertexShaderId, fragmentShaderId, shaderProgramId;
private Texture texture; private float red = 1.f, green = 0.f, blue = 0.5f;
public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL();
try { texture = TextureIO.newTexture(new File(textureImageFile), false); } catch (Exception e) { System.err.println("texture creation failed."); }
String vertexShaderSource = "void main(){ gl_Position = ftransform(); }"; vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB); gl.glShaderSourceARB(vertexShaderId, 1, new String[] { vertexShaderSource }, new int[] { vertexShaderSource.length() }, 0); gl.glCompileShaderARB(vertexShaderId);
String fragmentShaderSource = "uniform vec4 colour;\n"; fragmentShaderSource +="uniform sampler2D tex;\n"; fragmentShaderSource +="void main (void){gl_FragColor = colour*texture2D(tex, gl_TexCoord[0].st);}"; fragmentShaderId = gl .glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB); gl.glShaderSourceARB(fragmentShaderId, 1, new String[] { fragmentShaderSource }, new int[] { fragmentShaderSource .length() }, 0); gl.glCompileShaderARB(fragmentShaderId); shaderProgramId = gl.glCreateProgramObjectARB();
gl.glAttachObjectARB(shaderProgramId, vertexShaderId); gl.glAttachObjectARB(shaderProgramId, fragmentShaderId); gl.glLinkProgramARB(shaderProgramId);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClearDepth(1.0f); gl.setSwapInterval(1); }
public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
texture.bind(); texture.enable();
gl.glUseProgramObjectARB(shaderProgramId);
int colourVarNr = gl.glGetUniformLocationARB(shaderProgramId, "colour"); gl.glUniform4fARB(colourVarNr, red, green, blue, 1);
int texSampler = gl.glGetUniformLocationARB(shaderProgramId, "tex"); gl.glUniform1i(texSampler, 0);
gl.glActiveTexture(GL.GL_TEXTURE0); gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBegin(GL.GL_TRIANGLE_STRIP); gl.glTexCoord2f(0, 1); gl.glVertex3f(-1, 1, 0); gl.glTexCoord2f(0, 0); gl.glVertex3f(-1, -1, 0); gl.glTexCoord2f(1, 1); gl.glVertex3f(1, 1, 0); gl.glTexCoord2f(1, 0); gl.glVertex3f(1, -1, 0); gl.glEnd();
gl.glUseProgramObjectARB(0);
texture.disable(); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { final float h = (float) width / (float) height; GL gl = drawable.getGL(); GLU glu = new GLU(); gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(80, h, 0.1f, 20.f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0); }
public static void main(String[] args) { GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new BasicGLSL()); final Animator animator = new Animator(canvas); Frame frame = new Frame("BasicGLSL"); frame.setSize(400, 400); frame.add(canvas); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { runExit(animator); } }); frame.setVisible(true); canvas.requestFocus(); animator.start(); }
protected static void runExit(final Animator animator) { new Thread(new Runnable() { public void run() { animator.stop(); System.exit(0); } }).start(); }
} |