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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
package demo;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import com.bioxpr.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.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
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 static GLCanvas canvas;
private float x, y, z;
private boolean keyPressed;
private boolean mouseOnNode;
private int key;
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);
}
private void displayTrueColor(final GL gl){
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
gl.glMultiDrawArrays(gl.GL_QUADS, starts, counts, 1);
}
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);
System.out.println(mouseOnNode);
if(keyPressed){
moveNodeKeyboard(gl);
}
else {
displayTrueColor(gl);
if(mousePosition!=null){
if(!mouseOnNode){
mouseOnNode = checkTrueColor(gl);
}
else {
moveNodeMouse(gl);
}
}
}
displayTrueColor(gl);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
gl.glPointSize(2f);
gl.glColor3f(0, 0, 0);
gl.glBegin(gl.GL_POINT);
gl.glVertex3f(0, 1, 0);
gl.glEnd();
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;
}
}
private float[]toArray(FloatBuffer fb){
float[]array = new float[fb.limit()];
for(int i=0; i<array.length; i++){
array[i] = fb.get(i);
}
return array;
}
private boolean onNode(FloatBuffer fb){
final float[]red = {1, 0, 0};
return Arrays.equals(red, toArray(fb));
}
private double[] convertIntoGL(final GL gl, double x, double y) {
GLU glu = new GLU();
int viewport[] = new int[4];
double mvmatrix[] = new double[16];
double projmatrix[] = new double[16];
double wcoord[] = new double[4];// wx, wy, wz;// returned xyz coords
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0);
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0);
double realy = viewport[3] - (int) y - 1;
glu.gluUnProject(x, realy, 0, mvmatrix, 0, projmatrix, 0, viewport, 0, wcoord, 0);
return wcoord;
}
/**
* Calculates the translation vector in 2D for two given points
* @param xA X coordinate of the first point
* @param yA Y coordinate of the first point
* @param xB X coordinate of the second point
* @param yB Y coordinate of the second point
* @return an array of two float which is the translation vector
*/
private float[] getTranslationVector(float xA, float yA, float xB, float yB) {
float[] vector = new float[2];
vector[0] = xB - xA;
vector[1] = yB - yA;
return vector;
}
private void calculateNewCoordinates(final GL gl){
double[]coord = convertIntoGL(gl, mousePosition.x, mousePosition.y);
//float[]vector = getTranslationVector(x, y, (float) coord[0], (float) coord[1]);
x = (float)coord[0];
y = (float)coord[1];
}
private void moveNodeMouse(final GL gl) {
x = this.square.getX();
y = this.square.getY();
calculateNewCoordinates(gl);
this.square.changePosition(x, y, gl);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
FloatBuffer newPositions = square.getBufferVertices();
ByteBuffer bb = gl.glMapBuffer(gl.GL_ARRAY_BUFFER, gl.GL_WRITE_ONLY);
/*memcpy*/
FloatBuffer fb = bb.asFloatBuffer();
fb.rewind();
fb.put(newPositions);
/*End memcpy*/
gl.glUnmapBuffer(gl.GL_ARRAY_BUFFER);
mousePosition = null;
}
private void moveNodeKeyboard(final GL gl){
final float length = 0.05f;
x = this.square.getX();
y = this.square.getY();
switch(this.key){
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;
}
this.square.changePosition(x, y, gl);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
FloatBuffer newPositions = square.getBufferVertices();
ByteBuffer bb = gl.glMapBuffer(gl.GL_ARRAY_BUFFER, gl.GL_WRITE_ONLY);
/*memcpy*/
FloatBuffer fb = bb.asFloatBuffer();
fb.rewind();
fb.put(newPositions);
/*End memcpy*/
gl.glUnmapBuffer(gl.GL_ARRAY_BUFFER);
keyPressed = false;
}
private boolean checkTrueColor(final GL gl){
int[]viewport = new int[4];
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glReadBuffer(GL.GL_FRONT);
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);
return onNode(fb);
}
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 mouseDragged(MouseEvent e) {
this.mousePosition = e.getPoint();
}
public void mouseMoved(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
mousePosition = null;
if(mouseOnNode){
mouseOnNode = false;
}
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
this.key = e.getKeyCode();
keyPressed = true;
}
public void keyReleased(KeyEvent e) {
keyPressed = false;
}
} |
Partager