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 165 166 167 168 169 170 171 172 173 174 175
|
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dbourgais
*/
public class VBOTestJOGL {
public static void main(String[]args){
GLCanvas canvas = new GLCanvas();
VBOTest demo = new VBOTest();
canvas.addGLEventListener(demo);
final Animator animator = new Animator(canvas);
animator.start();
JFrame frame = new JFrame("VBO");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
canvas.setSize(400, 400);
frame.add(canvas, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
canvas.requestFocus();
}
}
class VBOTest implements GLEventListener, MouseWheelListener {
private float zoom = -5;
/**Extract from a tuto*/
final int positionSize = 6 * 3 * BufferUtil.SIZEOF_FLOAT;
final float[]PositionData = {
-1.0f, -1.0f, 0,
1.0f, -1.0f, 0,
1.0f, 1.0f, 0,
1.0f, 1.0f, 0,
-1.0f, 1.0f, 0,
-1.0f, -1.0f, 0
};
FloatBuffer positionData;
final int colorSize = 6 * 3 * BufferUtil.SIZEOF_BYTE;
final byte[]ColorData = {
(byte)255, (byte)0, (byte)0,
(byte)255, (byte)255, (byte)0,
(byte)0, (byte)255, (byte)0,
(byte)0, (byte)255, (byte)0,
(byte)0, (byte)0, (byte)255,
(byte)255, (byte)0, (byte)0
};
ByteBuffer colorData;
final int bufferSize = 2;
int BufferName[];
IntBuffer bufferName;
final int vertexCount = 6;
private boolean vboCompatible(final GL gl) {
String versionStr = gl.glGetString(GL.GL_VERSION);
versionStr = versionStr.substring(0, 3);
// Check if extension is available.
boolean extensionOK = gl.isExtensionAvailable("GL_ARB_vertex_buffer_object");
// Check for VBO functions.
boolean functionsOK =
gl.isFunctionAvailable("glGenBuffersARB") &&
gl.isFunctionAvailable("glBindBufferARB") &&
gl.isFunctionAvailable("glBufferDataARB") &&
gl.isFunctionAvailable("glDeleteBuffersARB");
return (extensionOK || functionsOK);
}
public void init(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
gl.glShadeModel(gl.GL_SMOOTH);
gl.glEnable(gl.GL_DEPTH_TEST);
bufferName = BufferUtil.newIntBuffer(bufferSize);
gl.glGenBuffers(bufferSize, bufferName);
colorData = BufferUtil.newByteBuffer(this.colorSize);
colorData.put(ColorData);
colorData.rewind();
positionData = BufferUtil.newFloatBuffer(this.positionSize);
positionData.put(PositionData);
positionData.rewind();
drawable.addMouseWheelListener(this);
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0, 0, 0, 1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, zoom);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
gl.glBufferData(gl.GL_ARRAY_BUFFER, colorSize, colorData, gl.GL_STREAM_DRAW);
gl.glColorPointer(3, gl.GL_UNSIGNED_BYTE, 0, 0);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER, this.positionSize, positionData, gl.GL_STREAM_DRAW);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glEnableClientState(gl.GL_COLOR_ARRAY);
gl.glDrawArrays(gl.GL_TRIANGLES, 0, vertexCount);
gl.glDisableClientState(gl.GL_COLOR_ARRAY);
gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final int maxZ = 1000;
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
/*For the camera*/
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
//Change this by glOrtho
new GLU().gluPerspective(45., (float) width / height, 1., maxZ);
//new GLU().gluOrtho2D(-10, 10, -10, 10);
/*Model & view*/
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() > 0) {
zoom += 0.5;
} else {
zoom -= 0.5;
}
System.out.println(zoom);
}
} |