Mesh.getvertices()] based on mouse position

Hi, I am in the middle of my project and I am kinda stuck. I am trying to mesh.getvertices()[i].x based on mouse position but I am struggling to work this out. Does anyone have any ideas on how to do this. this what I have so far but its leggy and inefficient. I am hoping for a O(1) solution to my problem.

for (int i = 0; i < mesh.getNumVertices(); i++) {

		if ((mouse.x > mesh.getVertices()[i].x && mouse.x < mesh.getVertices()[i].x + size) && (mouse.y > mesh.getVertices()[i].y && mouse.y < mesh.getVertices()[i].y + size)) {


			mesh.setColor(i, ofFloatColor(0, 0, 0, 0));

			cout << "match" << endl;

			break;
		}

	}

Thanks please help

1 Like

Your approach is not going to work. What if you click on the middle of a triangle composing your mesh?

If you are trying to select an object. There are basically two ways of selecting the mesh under the cursor.

  1. One it is to cast a ray from the camera position to the direction of the mouse, and see if the ray hits the mesh. If it hits the mesh, you return can return the 3 position of the three vertices that compose the triangle. This is well explained here, http://antongerdelan.net/opengl/raycasting.html, and if you do not want to implement everything by sketch, I have done this addon a while ago that might come useful to you https://github.com/edap/ofxRaycaster/blob/master/example-3d/src/ofApp.cpp#L166
  2. the other solution, it does not allow you to find the triangle of the mesh hit, but it allows you to simply detect if a mesh is selected or not. It is faster but less precise. Basically, you draw your scene using just two colors on an FBO, let’s say black for the background and white for the mesh. Then you check if the mouse coordinates are over the black area or over the white area.
1 Like

Hey Davide,

Thanks for your help I try using your ray cast add on but I am still facing the same problem. I just want to get back the mesh.getVertices()[i] of the mesh. I just want to change the alpha of the vertice For example when my mouse is over index 0 I can change the alpha to 0. and so on. What I want to accomplish is as the user hover there mouse over the a index on the mesh, the mesh alpha changes. I don’t know I miss read your post did you say the first example I can use to get the exact mesh index when I hover my mouse over it ? I just want to get the index when mouse over it so I can change the mesh to mesh.setColor(i, ofFloatColor(0, 0, 0, 0));

Thanks

There’s an example just about this in examples/3d/pointPickerExample

1 Like

Hey Man, this is exactly what I wanted. It seem like I have to use the loop no matter what to get the index. I think its not practical to loop through all the index until you find what you looking for. Thanks

one thing you could do to speed up the algorithm, is to build a bounding box of your object, and start to iterate through all the vertices only in the case that the bounding box was hit. See https://www.scratchapixel.com/lessons/advanced-rendering/introduction-acceleration-structure/bounding-volume-hierarchy-BVH-part1 as reference.

1 Like

I think that this addon might help to speed it up.

1 Like

I will take a look at this when i get back to the office