Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Help with GLM based mouse picking

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jan 2017
    Posts
    107

    Help with GLM based mouse picking

    I'm using the following code to try to do mouse picking with GLM but it's only picking one object on the screen and that even isn't correct.
    Any ideas?

    Code :
    glm::f32 tmin = 1000.0f;
    	for (int i = 0; i < frame.objects.size(); i++)
    	{
    		VObject *object = frame.objects[i];
     
    		glm::mat4 model = object->model;
    		model *= object->scale;
     
    		glm::mat4 mpvInverse = glm::inverse(model *  projection * this->camera->GetViewMatrix());
     
    		glm::vec4 origin = mpvInverse * glm::vec4(
    			(x - glm::f32(screenWidth / 2.0f)) / glm::f32(screenWidth / 2.0f), (glm::f32(screenHeight / 2.0f) - y) / glm::f32(screenHeight / 2.0f), -1.0f, 1.0f);
    		glm::vec4 dir = mpvInverse * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
    		glm::vec3 O = glm::vec3(origin.x, origin.y, origin.z);
    		glm::vec3 D = glm::normalize(glm::vec3(dir.x, dir.y, dir.z));
     
     
    		for (int j = 0; j<object->numVertices; j += 3)
    		{
     
    			glm::vec3 K = glm::vec3(object->vertices[j]);
     
    			glm::vec3 L = glm::vec3(object->vertices[j + 1]);
     
    			glm::vec3 M = glm::vec3(object->vertices[j + 2]);
    			// Compute vectors E, F, and G
    			glm::vec3 E = K - M;
    			glm::vec3 F = L - M;
    			glm::vec3 G = O - M;
    			// Compute the vector containing t and the coordinates k and l
    			glm::vec3 tkl = 1.0f / glm::dot(glm::cross(D, F), E) *
    				glm::vec3(glm::dot(glm::cross(G, E), F),
    					glm::dot(glm::cross(D, F), G),
    					glm::dot(glm::cross(G, E), D));
    			// Check if t and the intersection point (k, l) are acceptable
    			if (tkl.x < tmin && tkl.y > 0.0f && tkl.z > 0.0f && (tkl.y + tkl.z) < 1.0f) 
    			{
    				tmin = tkl.x;
    			}
    		}
     
     
    	}
    	if (tmin < 1000.f)
    	{
    		MessageBox(NULL, "hit", "", 0);
    		return;
    	}

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    467
    another way to do picking is to render each item in the scene with a unique identifier (int) into a texture
    when finished with rendering, you can read a certain part of the texture and re-interpret the identifiers

    http://www.opengl-tutorial.org/misce...ng-on-objects/
    http://www.opengl-tutorial.org/misce...n-opengl-hack/

    if you use MRT (multi-render-target) framebuffer, you can do it without an extra render pass
    using a 32 bit integer texture as a color attachment makes it easy to re-interpret the identifiers

    https://sites.google.com/site/john87...dertargets-mrt

    example (Renderer_OpenGL.cpp):
    https://sites.google.com/site/john87...ject-selection


    looping over each vertex in a model isnt a good idea, sometimes models have 100k+ vertices / faces
    Last edited by john_connor; 01-31-2017 at 01:59 AM.

  3. #3
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Quote Originally Posted by paul_g_griffiths View Post
    I'm using the following code to try to do mouse picking with GLM but it's only picking one object on the screen and that even isn't correct.
    Any ideas?
    Do you expect it to be able to pick several objects ?

    This can be useful to you.

    What john_connor gave can be better than your code, since it does not need to project/unproject and do ray/triangle intersection on all the polygons of all the objects involved in your scene. Even if this involves read/write to a framebuffer. However, if you need some mathematical information about your picking, then don't go to this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •