PDA

View Full Version : Picking



JJcoling
06-01-2011, 10:55 AM
Hi,
i just started with webGL and now i want to do picking. So i tried this:



// mouseX and mouseY is the point where is clicked
var pixel = quat4.create([mouseX,mouseY,0,1]);

var view = webGL.getParameter(webGL.VIEWPORT); // view = 0, 0, 800, 500

// mvMatrix =
// 1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 2, 0, -10, 1
// -------------
// pMatrix =
// 1.5088834762573242, 0, 0, 0,
// 0, 2.4142136573791504, 0, 0,
// 0, 0, -1.0020020008087158, -1,
// 0, 0, -0.20020020008087158, 0

var allMultiplied = mat4.multiplyVec4(mat4.multiply(mvMatrix, pMatrix), view);
// allMultiplied =
// -1600, 0, 7098.29833984375, -800,
// undefined, undefined, undefined, undefined,
// undefined, undefined, undefined, undefined,
// undefined, undefined, undefined, undefined

var allInversed = mat4.inverse(allMultiplied);
// allInversed =
// -1600, 0, 7098.298490047455, -800,
// undefined, ...
// if i divide the first three with the last (-800) i get the middlepoint of the nearest plane

var multiplyWithPixel = mat4.multiply(pixel,allInversed);
// now i get this:
// 1359971.25, -3992091.5, 410298.3125, 853600


I think the coordinates can't be the right one.
I use the glMatrix-0.9.5.
As a object i have a cube at [2,0,-10], width=2, height=2 and depth=2.
Does anybody see what's wrong in my script?
I hope i did not forget important facts.

MfG
JJcoling