Opengl鼠标旋转问题

Posted

技术标签:

【中文标题】Opengl鼠标旋转问题【英文标题】:Opengl mouse rotation issue 【发布时间】:2020-07-06 06:56:35 【问题描述】:

当我单击鼠标时,我的图形能够旋转,但是当我将鼠标图形返回初始位置时,我不知道为什么这是我的事件

这里是关于我的问题的插图

我认为这和在图片框上移动完全不同

Mouse_up 事件

 private void simpleOpenGlControl1_MouseUp(object sender, MouseEventArgs e)
        
            Dragging2 = false;
         

 private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e)
        
            xrot -= 0.5;
            simpleOpenGlControl1.Refresh();

         

Mouse_down 事件

        
            
            if (e.Button == MouseButtons.Left)
            
                    Dragging2 = true;
            
           
         
            double olcek3d_gen1 = obj.genislikDeger1 / pictureBox1.Width;
            double olcek3d_gen2 = obj.genislikDeger2 / pictureBox1.Width;
            double olcek3d_gen3 = obj.genislikDeger3 / pictureBox1.Width;
            double olcek3d_gen4 = obj.genislikDeger4 / pictureBox1.Width;

            double olcek3d_yuk1 = obj.yukselikDeger1 / pictureBox1.Width;
            double olcek3d_yuk2 = obj.yukselikDeger2 / pictureBox1.Width;
            double olcek3d_yuk3 = obj.yukselikDeger3 / pictureBox1.Width;
            double olcek3d_yuk4 = obj.yukselikDeger4 / pictureBox1.Width;

            double olcek_derinlik = obj.L / pictureBox1.Width;





            //Gl.glColorMask(1,1,1,1);




            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT | Gl.GL_ACCUM_BUFFER_BIT);

            Gl.glEnable(Gl.GL_DEPTH_TEST); //clear buffers to preset values
            Gl.glClearColor(0, 0, 0, 0);

           /* Gl.glEnable(Gl.GL_COLOR_MATERIAL);

            Gl.glEnable(Gl.GL_LIGHTING);

            Gl.glEnable(Gl.GL_LIGHT0);*/




            Gl.glBlendFunc(Gl.GL_ONE, Gl.GL_ONE_MINUS_SRC_ALPHA);

            Gl.glMatrixMode(Gl.GL_MODELVIEW);

            Gl.glLoadIdentity();                 // load the identity matrix
                                                 // load the identity 
            Gl.glTranslated(left_right, up_down, sabit3D);



            //Gl.glEnable(Gl.GL_TEXTURE_2D);
            if (Dragging2)
            
                Gl.glRotated(xrot, xRotate, yRotate, zRotate);
                listBox1.Items.Add("angle  " + xrot.ToString() +"  xrot  "+ xRotate+"  yrot  "+ yRotate+"  zrot  "+zRotate+"  left_right  "+left_right+"  up_Down  "+ up_down);
            
           







            //rotate on y
            //Gl.glRotated(zrot += 0.5, 0, 0, 1); //rotate on z



            //face 1.0

            //face 2.0 görünen açıdan sağdaki
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(90, 90, 90);
            Gl.glVertex3d(olcek3d_gen1, 0, 0);
            Gl.glVertex3d(olcek3d_gen1, 0, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, 0);
            Gl.glEnd();

            //face 3.0 ön yüzey
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(100, 100, 100);
            Gl.glVertex3d(0, 0, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen1, 0, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, olcek_derinlik);
            Gl.glVertex3d(0, olcek3d_yuk1, olcek_derinlik);
            Gl.glEnd();

            //face 4.0 arka yüzey

            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(110, 110, 110);
            Gl.glVertex3d(0, 0, 0);
            Gl.glVertex3d(0, 0, olcek_derinlik);
            Gl.glVertex3d(0, olcek3d_yuk1, olcek_derinlik);
            Gl.glVertex3d(0, olcek3d_yuk1, 0);
            Gl.glEnd();

            //face 5.0 üst yüzey



            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(100, 100, 100);
            Gl.glVertex3d(0, olcek3d_yuk1, 0);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, 0);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, olcek_derinlik);
            Gl.glVertex3d(0, olcek3d_yuk1, olcek_derinlik);
            Gl.glEnd();


            //face 6.0 // sorun olan bölge başlangıc en zıt arkası
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(100, 100, 100);
            Gl.glVertex3d(0, 0, 0);
            Gl.glVertex3d(olcek3d_gen1, 0, 0);
            Gl.glVertex3d(olcek3d_gen1, olcek3d_yuk1, 0);
            Gl.glVertex3d(0, olcek3d_yuk1, 0);
            Gl.glEnd();

            //face 1.1

            Gl.glBegin(Gl.GL_QUADS);    //start drawing GL_QUADSis the connection mode
            Gl.glColor3ub(90, 90, 90);

            Gl.glVertex3d(0, -1 * olcek3d_yuk2, 0);
            Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, 0);
            Gl.glVertex3d(olcek3d_gen2, 0, 0);
            Gl.glVertex3d(0, 0, 0);
            Gl.glEnd();

            //face 2.1
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(70, 70, 70);
            Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, 0);
            Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen2, 0, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen2, 0, 0);
            Gl.glEnd();

            //face 3.1
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(80, 80, 80);
            Gl.glVertex3d(0, -1 * olcek3d_yuk2, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen2, 0, olcek_derinlik);
            Gl.glVertex3d(0, 0, olcek_derinlik);
            Gl.glEnd();

            //face 4.1
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(90, 90, 90);
            Gl.glVertex3d(0, -1 * olcek3d_yuk2, 0);
            Gl.glVertex3d(0, -1 * olcek3d_yuk2, olcek_derinlik);
            Gl.glVertex3d(0, 0, olcek_derinlik);
            Gl.glVertex3d(0, 0, 0);
            Gl.glEnd();

            //face cover.1 çizgi level
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3ub(80, 80, 80);
            Gl.glVertex3d(olcek3d_gen1, 0, 0);
            Gl.glVertex3d(olcek3d_gen2, 0, 0);
            Gl.glVertex3d(olcek3d_gen2, 0, olcek_derinlik);
            Gl.glVertex3d(olcek3d_gen1, 0, olcek_derinlik);
            Gl.glEnd();


            if (sev.Value == 2)
            
                //face 5.1
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(85, 85, 85);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, 0);
                Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, 0);
                Gl.glVertex3d(olcek3d_gen2, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glEnd();
            


            if (sev.Value >= 3)
            
                //face 1.2

                Gl.glBegin(Gl.GL_QUADS);    //start drawing GL_QUADSis the connection mode
                Gl.glColor3ub(85, 85, 85);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glVertex3d(olcek3d_gen3, -1 * olcek3d_yuk2, 0);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, 0);
                Gl.glEnd();

                //face 2.2
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(70, 70, 70);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen3, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen3, -1 * olcek3d_yuk2, 0);
                Gl.glEnd();

                //face 3.2
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(90, 90, 90);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen3, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glEnd();

                //face 4.2
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(85, 85, 85);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, olcek_derinlik);
                Gl.glVertex3d(0, -1 * olcek3d_yuk2, 0);
                Gl.glEnd();

                //face kapak.2
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(80, 80, 80);

                Gl.glVertex3d(olcek3d_gen2, -1 * (olcek3d_yuk1), 0);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk1), 0);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk1), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen2, -1 * (olcek3d_yuk1), olcek_derinlik);
                Gl.glEnd();



                if (sev.Value == 3)
                
                    //face 5.2
                    Gl.glBegin(Gl.GL_QUADS);
                    Gl.glColor3ub(85, 85, 85);
                    Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                    Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                    Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                    Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                    Gl.glEnd();

                


            
            if (sev.Value == 4)
            
                //face 1.3

                Gl.glBegin(Gl.GL_QUADS);    //start drawing GL_QUADSis the connection mode
                Gl.glColor3ub(85, 85, 85);

                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glEnd();

                //face 2.3
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(70, 70, 70);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glEnd();

                //face 3.3
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(90, 90, 90);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glEnd();

                //face 4.3
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(85, 85, 85);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), olcek_derinlik);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3), 0);
                Gl.glEnd();
                //face 5.3
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(85, 85, 85);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glVertex3d(0, -1 * (olcek3d_yuk2 + olcek3d_yuk3 + olcek3d_yuk4), olcek_derinlik);
                Gl.glEnd();

                //face kapak.2
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3ub(80, 80, 80);

                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk1 + olcek3d_yuk2), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk1 + olcek3d_yuk2), 0);
                Gl.glVertex3d(olcek3d_gen4, -1 * (olcek3d_yuk1 + olcek3d_yuk2), olcek_derinlik);
                Gl.glVertex3d(olcek3d_gen3, -1 * (olcek3d_yuk1 + olcek3d_yuk2), olcek_derinlik);
                Gl.glEnd();
            

【问题讨论】:

【参考方案1】:

因为您仅在Dragging2 处于活动状态时才应用xrot 旋转...您应该在每帧执行此操作或在鼠标向上时将最终旋转应用于一些用于在开始时初始化模型视图的初始矩阵框架而不是身份

所以最简单的就是改变:

if (Dragging2)
    
    Gl.glRotated(xrot, xRotate, yRotate, zRotate);
    listBox1.Items.Add("angle  " + xrot.ToString() +"  xrot  "+ xRotate+"  yrot  "+ yRotate+"  zrot  "+zRotate+"  left_right  "+left_right+"  up_Down  "+ up_down);
    

进入:

Gl.glRotated(xrot, xRotate, yRotate, zRotate);
if (Dragging2)
    
    listBox1.Items.Add("angle  " + xrot.ToString() +"  xrot  "+ xRotate+"  yrot  "+ yRotate+"  zrot  "+zRotate+"  left_right  "+left_right+"  up_Down  "+ up_down);
    

不确定列表框的用途(或者是否没有内存泄漏...)。但是,如果您的 xRotate, yRotate, zRotate 发生变化,那么您需要实现初始累积矩阵 ...

【讨论】:

@sarac 我强烈建议您查看我的Understanding 4x4 homogenous transform matrices 和子链接(尤其是答案末尾的那些)...了解如何使用矩阵的一些想法...

以上是关于Opengl鼠标旋转问题的主要内容,如果未能解决你的问题,请参考以下文章

基于mfc的opengl怎么实现鼠标操控

OpenGL - 旋转 180 度后对象反转

在 OpenGL 上单击鼠标时无法旋转和更改对象的颜色

当涉及旋转时,将鼠标坐标转换为 OpenGL 中的模型坐标

OpenGL鼠标拾取策略

OpenGl读取导入3D模型并且添加鼠标移动旋转显示