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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
package demo;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import demo.geom.Square;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
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 GLDavid
*/
public class MovingSquare
implements GLEventListener, MouseWheelListener, MouseMotionListener,
MouseListener, KeyListener {
private float zoom = -20;
private Square square;
private IntBuffer bufferName;
private FloatBuffer positionData;
private FloatBuffer colorData;
private FloatBuffer falseColorData;
private IntBuffer starts;
private IntBuffer counts;
private final int LIMIT = 3;
private Point mousePosition;
private boolean mustCheck;
private static GLCanvas canvas;
private float x, y, z;
private boolean keyPressed;
public MovingSquare() {
this.square = new Square(x, y, z);
this.bufferName = BufferUtil.newIntBuffer(LIMIT);
}
private void initDataBuffers(final GL gl){
int i = 0;
int j = 4;
this.positionData.put(square.getBufferVertices());
this.colorData.put(square.getBufferColor());
this.falseColorData.position(square.sizeofBufferFalseColor());
starts.put(i);
counts.put(j);
i = i + square.getBufferIndicesSize();
j = square.getBufferIndicesSize();
this.positionData.rewind();
this.colorData.rewind();
this.falseColorData.rewind();
}
private void pushNodesDataBuffers(final GL gl) {
initDataBuffers(gl);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER,
this.square.sizeofBufferVertices() *
BufferUtil.SIZEOF_FLOAT,
positionData,
gl.GL_STREAM_DRAW);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
gl.glBufferData(gl.GL_ARRAY_BUFFER,
this.square.sizeofBufferColor() *
BufferUtil.SIZEOF_FLOAT,
colorData,
gl.GL_STATIC_DRAW);
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(2));
gl.glBufferData(gl.GL_ARRAY_BUFFER,
this.square.sizeofBufferFalseColor() *
BufferUtil.SIZEOF_FLOAT,
falseColorData,
gl.GL_STATIC_DRAW);
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
this.starts.rewind();
this.counts.rewind();
}
public void init(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
drawable.addMouseWheelListener(this);
drawable.addMouseMotionListener(this);
drawable.addMouseListener(this);
drawable.addKeyListener(this);
gl.glShadeModel(gl.GL_SMOOTH);
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glGenBuffers(LIMIT, bufferName);
int constant = BufferUtil.SIZEOF_FLOAT;
this.positionData = BufferUtil.newFloatBuffer(square.sizeofBufferVertices() * constant);
this.colorData = BufferUtil.newFloatBuffer(square.sizeofBufferColor() * constant);
this.falseColorData = BufferUtil.newFloatBuffer(square.sizeofBufferFalseColor() * constant);
this.starts = BufferUtil.newIntBuffer(1);
this.counts = BufferUtil.newIntBuffer(1);
pushNodesDataBuffers(gl);
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(1, 1, 1, 0);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, zoom);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
if(keyPressed){
moveNodeKeyboard(gl);
}
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
if(!mustCheck){
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
}
else{
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(2));
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
}
gl.glMultiDrawArrays(gl.GL_QUADS, starts, counts, 1);
if(mustCheck){
checkFalseColor(gl);
}
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);
/*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;
}
}
public void mouseDragged(MouseEvent e) {
this.mousePosition = e.getPoint();
mustCheck = true;
}
private boolean onNode(FloatBuffer fb){
for(int i=0; i<3; i++){
if(fb.get(i)!=0){
return false;
}
}
return true;
}
private void moveNode(final GL gl) {
final int constant = BufferUtil.SIZEOF_FLOAT;
final int size = square.sizeofBufferVertices() * constant;
this.square.changePosition(mousePosition.x, mousePosition.y, true, gl);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER,
size,
positionData,
gl.GL_STREAM_DRAW);
FloatBuffer newPositions = square.getBufferVertices();
gl.glBufferSubData(gl.GL_ARRAY_BUFFER, 0, size, newPositions);
}
private void moveNodeKeyboard(final GL gl){
final int constant = BufferUtil.SIZEOF_FLOAT;
final int size = square.sizeofBufferVertices() * constant;
this.square.changePosition(x, y, false, gl);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER,
size,
positionData,
gl.GL_STREAM_DRAW);
FloatBuffer newPositions = square.getBufferVertices();
gl.glBufferSubData(gl.GL_ARRAY_BUFFER, 0, size, newPositions);
}
private void checkFalseColor(final GL gl){
int[]viewport = new int[4];
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glReadBuffer(GL.GL_BACK);
FloatBuffer fb = BufferUtil.newFloatBuffer(3);
gl.glPixelStorei (gl.GL_UNPACK_ALIGNMENT, 1);
gl.glReadPixels(mousePosition.x, mousePosition.y, 1, 1, gl.GL_RGB,
gl.GL_FLOAT, fb);
if(onNode(fb)){
moveNode(gl);
}
mustCheck = false;
canvas.display();
}
public static void main(String[] args) {
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(true);
canvas = new GLCanvas(glc);
MovingSquare demo = new MovingSquare();
canvas.addGLEventListener(demo);
canvas.setAutoSwapBufferMode(true);
final Animator animator = new Animator(canvas);
animator.start();
JFrame frame = new JFrame("Moving VBO square");
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();
}
public void mouseMoved(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
canvas.display();
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
final float length = 0.05f;
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:{
y-=length;
}break;
case KeyEvent.VK_UP:{
y+=length;
}break;
case KeyEvent.VK_LEFT:{
x-=length;
}break;
case KeyEvent.VK_RIGHT:{
x+=length;
}break;
}
keyPressed = true;
canvas.display();
}
public void keyReleased(KeyEvent e) {
keyPressed = false;
}
} |
Partager