iPhone opengl es: Touch detection - iphone

I have been messing around with opengl es on the iphone and right now I have some cubes on the screen. Currently I am trying to detect touches on these cubes. After a lot of searching on google this is what I have so far
Use gluUnProject to find the x,y cordinates on the near plane in the world cordinate system
Use gluUnProject to find the x,y cordinates on the far plane in the world cordinate system
Subtract the vector obtained in 2 from the vector obtained in 1 to obtain the direction vector
Normalize the direction vector to obtain the unit vector
Iterate through all the trianlges and use the ray-triangle intersection to check if the ray intersects this triangle
I think my mistake is in step 5. I have a feeling I am supposed to transform my triangles by the modelview matrix? Is my assumption correct? If yes any clues how to transform a triangle (an array of 3 floats) by the modelview matrix (an array of 16 floats)

Related

3d vectors from a Unity Plane to a flat 2D point as a pixel on a texture

If I had a sharp sword and I were to perfectly slice an object in half, I would like to sample the colours at various points along this flat, freshly cut face, and place these colours on a texture.
Imagine the face is a Unity Plane defined by its Vector3 normal that goes through a location Vector3 p.
Let the texture be a 100 x 100 sized image.
Lets say the samples I want to take are three 3D points all on this plane, and defined as Vector3 A, B and C.
How do I go about converting the 3D points (x,y,z) from the defined plane into a 2D pixel (x,y) of this texture?
I have read many similar questions but honestly could not understand the answers. I don't know in my scenario if I'm dealing with Orthographic vs Projection perspective, whether I need to create a "conversion matrix", whether I need be concerned about rotations, or if there is just a simpler solution.
I appreciate any tips or suggestions. Thanks

How can i find a two points of triangle from a point and a center of gravity in a 3D

Using Matlab, how can I calculate a two points in Equilateral triangle if there are known one point and the Center of Gravity in a 3D ?
I know there is a infinite solutions but i need just a random one.
Thank you.
Take the vector pointing from the center of gravity to the point.
Create an orthogonal vector (this can be done in a few ways, I usually take the first vector, add 1.0 to each component until it is not parallel, then take the cross product with the original vector).
Rotate your vector 120 degrees about the orthogonal vector. (look up the rotation matrix about an arbitrary vector)
Create your second point by adding that vector to your center of gravity.
Create your third point by rotating it again or in the opposite direction.

Understanding depth values in 3D point cloud

I have problems understanding the depth (Z) value in 3D point cloud resulted from 3d sparse reconstruction like this example in MATLAB: http://www.mathworks.com/help/vision/ug/sparse-3-d-reconstruction-from-multiple-views.html
I have attached a picture showing the reconstructed 3D point cloud in the above example. I have put some datatips on the figure so we know the (x,y,z) coordinates of the points. here are my questions:
1- what does the Z value in point cloud represent? is it the distance in millimeters from the camera? if that's the case then it does not make sense based on the picture I attached since I am sure the distance of the sphere and checkerboard from the camera must be greater than 200 mm.
Or maybe it is from some reference point in space? then what is this reference point? and how can I make a 3D point cloud that the Z values indicate the distance from the camera?
2- why is there negative values for Z? what does that mean in terms of distance to the camera?
I appreciate if someone can explain.
In this example the world coordinates are defined by the checkerboard. The checkerboard defines the X-Y plane, and the Z-axis points into the checkerboard, as explained in the documentation:
Since your 3D points are above the checkerboard, they have negative Z-coordinates.
Your (x,y,z) coordinates are in world units, which are completely disconnected from metric values (unless you build a scale between world and metric, there are various methods to do it). So the z value tells you about the depth of each point in world coordinates.
If you have the pose of each camera, and you multiply each point by the camera projection matrix, you will get the (x',y',z') points in camera coordinates. At that point, if z' is negative, it means it's behind the camera.

Contour triangulation

I write my study and is stuck when i try triangulate the contour of surface. When it is in 2D its ok. When it in 3D a have trouble with triangle angle detection, i tried with:
Triange have 3 Vertices v1,v2,v3
I create 2 vectors(vec21, vec23) from v2v1 and v2v3
then vec21 x vec23 and obtain a det of matrix
on the stand which I define Span angle
I also check if edges do not crossing and if any point isnt in area of triangle.
But when it in 3D i choose point around polygon then this metod didn't work
Points of contour i want triangulate to flat polygon: https://docs.google.com/open?id=0Bw5-VXnqutXBckRJMGNJMW9JaXc
Bad resoult: https://docs.google.com/open?id=0Bw5-VXnqutXBMzV5elIxX1FaeDQ
In 2d:
Points on 2D :https://docs.google.com/open?id=0Bw5-VXnqutXBWVE4bWJsZ09mOVk
Good resoults:https://docs.google.com/open?id=0Bw5-VXnqutXBdGFKM2Z4UnFRdXc
Where i made mistake? Can u explain me this?
Greetings!
PS. Im interested in algoithm at 2 last case:http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html
Typically one would use a Delaunay Triangulation for the 2D case. For the 3D case you can project the points to 2D, triangulate and project the triangles back to 3D. This will of course only work if the patch to be triangulated can be projected to 2D (without selfintersections).

Extract arbitrarily rotated plane of data from 3D array as 2D array

I have a 3D matrix of data in matlab, but I want to extract an arbitrarily rotated slice of data from that matrix and store it as a 2D matrix, which I can access. Similar to how the slice() function displays data sliced at any angle, except I would also like to be able to view and modify the data as if it were an array.
I have the coordinates of the pivot-point of the plane as well as the angles of rotation (in x, y and z axis), I have also calculated the equation of the plane in the form:
Ax + By + Cz = D
and can extract a 3D matrix containing only the data that fall on that plane, but I don't know how to then convert that into a simple 2D array.
Another way of doing it would be to somehow rotate the source matrix in the opposite direction of the angle of the plane, so as to line up the plane of data with the XY axis, and simply extract that portion of the matrix, but I do not know if rotating a matrix like that is possible.
I hope this hasn't been answered elsewhere, I've been googling it all day, but none of the problems seem to exactly match mine.
Thanks
You can take a look at the code here. I think the function is similar to what you are trying to solve.
The function extracts an arbitrary plane from a volume given the size of the plane, the center point of the plane, and the plane normal, i.e. [A,B,C]. It also outputs the volumetric index and coordinate of each pixel on the plane.
Aha! May have just solved it myself.
To produce the plane equation I rotate a normal vector of (0,0,1) using rotation matrices and then find D. If I also rotate the following vectors:
(1,0,0) //step in the x direction of our 2D array
and
(0,1,0) //step in the y direction of our 2D array
I'll have the gradients that denote how much my coordinates in x,y,z have to change before I step to the next column in my array, or to the next row.
I'll mock this up ASAP and mark it as the answer if it works
EDIT: Ok slight alteration, when I'm rotating my vectors I should also rotate the point in 3D space that represents the xyz coordinates of x=0,y=0,z=0 (although I'm rotating around the centre of the structure, so it's actually -sizex/2,-sizey/2,-sizez/2, where size is the size of the data, and then I simply add size/2 to each coordinate after the rotations to translate it back to where it should be).
Now that I have the gradient change in 3D as I increase the x coordinate of my 2D array and the gradient change as I increase the y coordinate, I can simply loop through all possible x and y coordinates (the resulting array will be 50x50 for a 50x50x50 array, I'm not sure what it will be for irregular sizes, which I'll need to work out eventually) in my 2D array and calculate the resulting 3D coordinates on my plane in the data. My rotated corner value serves as the starting point. Hooray!
Just got to work out a good test for this encompassing all angles and then I'll approve this as an answer