试图让鼠标采摘工作,但不确定我在哪里迷路了



我正在绘制一个深度为0的10x10方格网格,并尝试突出显示鼠标经过的方格。我试着遵循这里的教程:http://antongerdelan.net/opengl/raycasting.html但我不知道我做得对不对。我最后得到一个向量,但我不确定该怎么处理它。

这是一个正方形的截图(不知道它有什么帮助…)

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 %fn", 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 %dn", 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 %fn", 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, %fn", 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, %fn", mouse_world_x, mouse_world_y);
    }

更新了BDL注释中的代码。我现在得到的输出是:

规范化鼠标位置0.087500 x 0.145833世界射线:0.065083,0.081353,4.99亿世界射线:0.000130,0.000163,1.000000鼠标世界-0.006521,-0.008152

我希望"鼠标世界"行有1-10范围内的数字,而不是0.00x范围内的数字,但是,根据上面的屏幕截图显示的正方形网格,x和y的范围从0-10。

从点C(在本例中为相机位置)开始的给定射线r与z=0的x/y平面的交点可以计算如下:

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

接下来要做的就是检查这个(x,y)坐标位于哪个矩形

相关内容

  • 没有找到相关文章

最新更新