openglmatrixprojectionmouse-picking

Trying to get mouse picking to work, but not sure where I'm lost


I'm drawing a 10x10 grid of squares at a depth of 0 and trying to highlight the one the mouse is over. I've tried following the tutorial here: http://antongerdelan.net/opengl/raycasting.html but I don't know if I did it right. I end up with a vector at the end, but I'm not sure what to do with it.

Here's a screenshot of the squares (not sure how it helps..)

http://postimg.org/image/dau330qwt/2

/* Enable attribute index 1 as being used */
    glEnableVertexAttribArray(1);
        float camera_z = 50;
        float camera_x = 0;
        float camera_y = 0;

        GLuint MatrixID = glGetUniformLocation(program, "MVP");
        GLuint ColorID = glGetUniformLocation(program, "input_color");

        int mouse_x;
        int mouse_y;


        while (1) {

                int window_width;
                int window_height;
                SDL_GetWindowSize(win, &window_width, &window_height);
                glm::mat4 Projection = glm::perspective(45.0f, ((float)window_width) / window_height, 0.1f, 100.0f);



                // printf("Camera at %f %f\n", camera_x, camera_y);
                glm::mat4 View = glm::lookAt(glm::vec3(camera_x,camera_y,camera_z), // camera position
                                                                         glm::vec3(camera_x,camera_y,0), // looking at
                                                                         glm::vec3(0,1,0)); // up



                int map_width = map.width();
                int map_height = map.height();

          /* Make our background black */
           glClearColor(0.0, 0.0, 0.0, 1.0);
            glClear(GL_COLOR_BUFFER_BIT);

                // go through my 10x10 map and
                for (int i = 0; i < map_width; i++) {
                        for ( int j = 0; j < map_height; j++) {

                                glm::mat4 Model = glm::translate(glm::mat4(1.0f), glm::vec3(i, j, 0.0f));              
                                glm::mat4 MVP = Projection * View * Model;
                                glm::vec3 color = random_color();

                                glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
                                glUniform3fv(ColorID, 1, &color[0]);
                            glDrawArrays(GL_LINE_LOOP, 0, 4);

                        }
                }


        /* Swap our buffers to make our changes visible */
                SDL_GL_SwapWindow(win);
                // printf("Window dimensions %d x %d\n", window_width, window_height);

                float normalized_mouse_x =  (2.0f * mouse_x) / window_width - 1.0f;
                float normalized_mouse_y = 1.0f - (2.0f * mouse_y) / window_height;

                printf("Normalized mouse position %f x %f\n", normalized_mouse_x, normalized_mouse_y);
                glm::vec3 normalized_mouse_vector = glm::vec3(normalized_mouse_x, normalized_mouse_y, 1.0f);

                glm::vec4 ray_clip = glm::vec4 (normalized_mouse_vector.x, normalized_mouse_vector.y, -1.0, 1.0);

                glm::vec4 ray_eye = glm::inverse(Projection) * ray_clip;
                ray_eye = glm::vec4(ray_eye.xy(), -1.0, 0.0);


                glm::vec3 ray_world = (glm::inverse(View) * ray_eye).xyz();

                ray_world = glm::normalize(ray_world);

                // this prints out values like: World ray: 0.000266, 0.000382, 1.000000
                printf("World ray: %f, %f, %f\n", ray_world.x, ray_world.y, ray_world.z);

                // l = -(camera_z / ray_world.z)
                float l = -(camera_z / ray_world.z);

                float mouse_world_x = camera_x + l * ray_world.x;
                float mouse_world_y = camera_y + l * ray_world.y;

                printf("mouse world %f, %f\n", mouse_world_x, mouse_world_y);

    }

Updated with code from BDL's comment. The output I get now is:

Normalized mouse position 0.087500 x 0.145833 World ray: 0.065083, 0.081353, 499.000000 World ray: 0.000130, 0.000163, 1.000000 mouse world -0.006521, -0.008152

I'm expecting the "mouse world" line to have numbers in the 1-10 range, not in the .00x range, though, based on the screenshot above showing a grid of squares with x and y ranging from 0-10.

Thanks for looking.


Solution

  • The intersection between a given ray r, starting at point C (in this case the camera position) with a x/y plane with z=0 can be calculated as follows:

    C ... Camera position [cx,cy,cz]
    r ... ray direction [rx,ry,rz]
    
    We are searching for the point on the ray that has z=0
    
    C + l*r = [x,y,0]
    
    =>
    
    cz + l*rz = 0
    l * rz = -cz
    l = -(cz / rz)
    
    The xy-coordinates of the intersection are now:
    
    x = cx + l * rx
    y = cy + l * ry
    

    What is left to do is to check in which rectangle this (x,y) coordinates are located.