游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3568|回复: 3

opengl 中对选中的物体移动不正确,希望高人帮帮我!小

[复制链接]

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2008-12-5 17:36:00 | 显示全部楼层 |阅读模式
可以对物体正确的选取,但是对物体移动时却有问题,是不是要乘以当前的矩阵啊?
把代码贴一下哈:
void SView::Selection()、、选取函数
{
        POINT mousePos;
        GetCursorPos(&mousePos);
        int mouse_x=mousePos.x;
        int mouse_y=mousePos.y;
        GLuint        buffer[512];                                                                                // Set Up A Selection Buffer
        GLint        hits;                                                                                                // The Number Of Objects That We Selected       
        PlaySound("data/shot.wav",NULL,SND_ASYNC);                                        // Play Gun Shot Sound

        // The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is <length>, [3] Is <width>
        GLint        viewport[4];

        // This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window
        glGetIntegerv(GL_VIEWPORT, viewport);
        glSelectBuffer(512, buffer);                                                                // Tell OpenGL To Use Our Array For Selection

        // Puts OpenGL In Selection Mode. Nothing Will Be Drawn.  Object ID's and Extents Are Stored In The Buffer.
        (void) glRenderMode(GL_SELECT);

        glInitNames();                                                                                                // Initializes The Name Stack
        glPushName(0);                                                                                                // Push 0 (At Least One Entry) Onto The Stack

        glMatrixMode(GL_PROJECTION);                                                                // Selects The Projection Matrix
        glPushMatrix();                                                                                                // Push The Projection Matrix
        glLoadIdentity();                                                                                        // Resets The Matrix

        // This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is.
        gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport);

        // Apply The Perspective Matrix
        gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.5f, 12000.0f);

        glMatrixMode(GL_MODELVIEW);                // Select The Modelview Matrix
         如果在这里加上m_Camera.setLook()它是设置摄像机的;就不能进行选取和移动了,为什么啊?
        RenderScene();                        // Render The Targets To The Selection Buffer
        glMatrixMode(GL_PROJECTION);                                                                // Select The Projection Matrix
        glPopMatrix();                                                                                                // Pop The Projection Matrix
        glMatrixMode(GL_MODELVIEW);                                                                        // Select The Modelview Matrix
        hits=glRenderMode(GL_RENDER);                                                                // Switch To Render Mode, Find Out How Many
                                                                                                                                // Objects Were Drawn Where The Mouse Was
        if (hits > 0)                                                                                                // If There Were More Than 0 Hits
        {
                int        choose = buffer[3];                                                                        // Make Our Selection The First Object
                int depth = buffer[1];                                                                        // Store How Far Away It Is
                for (int loop = 1; loop < hits; loop++)                                        // Loop Through All The Detected Hits
                {
                        // If This Object Is Closer To Us Than The One We Have Selected
                        if (buffer[loop*4+1] < GLuint(depth))
                        {
                                choose = buffer[loop*4+3];                                                // Select The Closer Object
                                depth = buffer[loop*4+1];                                                // Store How Far Away It Is
                        }      
                }
                g_3DModel[choose].hit=true;

    }


3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2008-12-5 17:38:00 | 显示全部楼层

Re: opengl 中对选中的物体移动不正确,希望高人帮帮我!

Vector3 CSView::GetSelectionRay(int mouse_x, int mouse_y)、、这是实现移动的函数,得到世界中的坐标
{
        // 获取 Model-View、Projection 矩阵 & 获取Viewport视区
    GLdouble    modelview[16];
    GLdouble    projection[16];
    GLint       viewport[4];
        glPushMatrix();
    glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev (GL_PROJECTION_MATRIX, projection);
    glGetIntegerv (GL_VIEWPORT, viewport);
    GLdouble world_x, world_y, world_z,win_z;  
    // 获取近裁剪面上的交点
    gluUnProject( (GLdouble) mouse_x, (GLdouble) mouse_y, 0.0,
                    modelview, projection, viewport,
                    &world_x, &world_y, &world_z);
    Vector3 near_point(world_x, world_y, world_z);
        glReadPixels(   (GLdouble) mouse_x,  viewport[3]-(GLdouble)mouse_y,   1,   1,   GL_DEPTH_COMPONENT,   GL_FLOAT,   &win_z  );
    gluUnProject( (GLdouble) mouse_x, viewport[3]-(GLdouble)mouse_y, 1.0,
                modelview, projection, viewport,
        &world_x, &world_y, &world_z);
    Vector3 far_point(world_x, world_y, world_z);

                int model_x=(near_point.x*far_point.z-far_point.x*near_point.z)/far_point.z-near_point.z;
                int model_z=(near_point.y*far_point.z-far_point.y*near_point.z)/far_point.z-near_point.z;
                return Vector3(model_x,4.5,model_z);
}
高人帮忙啊,别让帖子沉下去 啊,哈哈 [em2] [em2] [em2] [em2]

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2008-12-7 11:14:00 | 显示全部楼层

Re: opengl 中对选中的物体移动不正确,希望高人帮帮我!

怎么没有人回答啊,高手帮忙啊!小弟在这先谢谢了! [em10] [em10]

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2008-12-7 14:50:00 | 显示全部楼层

Re:opengl 中对选中的物体移动不正确,希望高人帮帮我!小

哈哈,自己解决了!坐标计算错误
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 18:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表