Want more features on Pastebin? Sign Up, it's FREE!
Guest

Unproject Util

By: a guest on Dec 9th, 2013  |  syntax: Java  |  size: 1.57 KB  |  views: 166  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.windpoweredgames.mocha.util;
  2.  
  3. import static org.lwjgl.opengl.GL11.glLoadIdentity;
  4. import static org.lwjgl.opengl.GL11.glMatrixMode;
  5.  
  6. import java.nio.FloatBuffer;
  7. import java.nio.IntBuffer;
  8.  
  9. import org.lwjgl.BufferUtils;
  10. import org.lwjgl.opengl.GL11;
  11. import org.lwjgl.util.glu.GLU;
  12. import org.lwjgl.util.vector.Vector3f;
  13.  
  14. import com.windpoweredgames.mocha.GameEngine;
  15. import com.windpoweredgames.mocha.display.GameDisplay;
  16.  
  17. public class UnprojectUtil {
  18.        
  19.  
  20.         public static Vector3f unproject(int mouseX, int mouseY){
  21.                 Vector3f v1 = unproject(mouseX, mouseY, 0);
  22.                 Vector3f v2 = unproject(mouseX, mouseY, 1);
  23.                 Vector3f normalized = new Vector3f(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z );
  24.                 return normalized;
  25.         }
  26.        
  27.  
  28.         public static Vector3f unproject(int mouseX, int mouseY, float depth){
  29.                 IntBuffer viewport = BufferUtils.createIntBuffer(16);
  30.                 FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
  31.                 FloatBuffer projectionView = BufferUtils.createFloatBuffer(16);
  32.                 float winX = mouseX;
  33.                 float winY = mouseY;
  34.                 FloatBuffer position = BufferUtils.createFloatBuffer(3);               
  35.                
  36.                        
  37.                         GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelView);
  38.                         GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionView);
  39.                         GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
  40.                        
  41.                        
  42.                         winY = viewport.get(3) - mouseY; // due to inverted coords
  43.                         GLU.gluUnProject(winX, winY, depth, modelView, projectionView, viewport, position);
  44.                         position.rewind();
  45.                         Vector3f result = new Vector3f(position.get(0), -position.get(1), position.get(2));
  46.  
  47.                 return result;
  48.         }
  49.        
  50.        
  51. }
clone this paste RAW Paste Data