OpenGL的新功能:glutMouseFunc [英] new to OpenGL: glutMouseFunc

查看:317
本文介绍了OpenGL的新功能:glutMouseFunc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击鼠标后更改球体位置,但是当使用glutMouseFunc中的x和y时,它不起作用,这是代码:

I'm trying to change the sphere position after a mouse click but it doesnt work when using the x and y from glutMouseFunc ,, here is the code :

//

   #include "stdafx.h"
    #include <stdlib.h>
    #include <GL/glut.h>


    bool Cone=false , ConeSelected=false,
    Cube=false, CubeSelected=false,
    Sphere=false, SphereSelected=false,
    Teapot=false, TeapotSelected =false,
    Torus=false, TorusSelected=false;
    static float XSphere=0, YSphere=-1.5 ,ZSphere=0;

    void init(void) 
    {
    GLfloat blankMaterial[] = {1.0, 0.0, 0.0};
    GLfloat whiteDiffuseLight[] = {30}; 
    glClearColor (0.0, 0.1, 0.2, 0.0);
    glClearDepth(1.0);
    glShadeModel (GL_FLAT);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blankMaterial);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blankMaterial);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,  whiteDiffuseLight);
     }


    void display(void)
    {
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
      glEnable(GL_DEPTH_TEST);

      //Drawing Cube
       if(Cube)
        {
        glPushMatrix();
        glColor3f(0, 1, 0);
        glTranslatef(-3,0,-3);
        glRotatef(20, 1,0,0);
        glutSolidCube(2);
        glPopMatrix() ;
        }

      //drawing cone
       if(Cone)
      {
        glPushMatrix();
        glColor3f (1.0, 0.0, 1.0);
        glTranslatef(0,2,0);
        glRotatef(50, 1,0,0);
        glutSolidCone(.5,1,10,10);
        glPopMatrix() ;
       }

      //Drawing Solid Sphere
      if(Sphere)
      { 
       glPushMatrix();
       glTranslatef(XSphere,YSphere,ZSphere);
       glutSolidSphere(.5, 20, 20);
       glPopMatrix();
       glDisable(GL_DEPTH_TEST);
   }
   glColor3f (1.0f, 1.0f, 1.0f);

   //drawing Torus
   if(Torus)
   {
        glPushMatrix();
        glColor3f (1.0, 1.0, 1.0);
        glTranslatef(2,2,0);
        glRotatef(60, 1,0,0);
        glutSolidTorus(.2,.5,40,30);
        glPopMatrix() ;
   }

   //Drawing teapot
   if(Teapot)
   {
       glPushMatrix ();
       glTranslatef (2.0, 0.0, 0.0);
       glutSolidTeapot(.5);
       glPopMatrix ();
   }


glFlush();
glutSwapBuffers();



}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard (unsigned char key, int x, int y)
{int modifiers = glutGetModifiers();

   switch (key) {
       case 'A':
               Teapot=Cone=Sphere=Cube=Torus=true;
               glutPostRedisplay();
               break;
        case 'D':
               Teapot=Cone=Sphere=Cube=Torus=false;
               glutPostRedisplay();
               break;
        case 'S':
                Teapot=Cone=Cube=Torus=false;
                Sphere=true;
                TeapotSelected=ConeSelected=CubeSelected=TorusSelected=false;
                SphereSelected=true;

               break;       
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

在鼠标部分中,我尝试在上述绘图球体中设置gltranslation,但当我检查x和y的结果较大时,这就是为什么单击鼠标后看不到球体的原因,我该如何解决这个问题??

Here in the mouse section I tried to set the gltranslation in the above drawing sphere but when I checked the results for x and y they were large and that's why I couldnt see the sphere after clicking the mouse , how can i fix this ??

void MyMouse(int button, int state, int x, int y)
{

switch (button)
{
    case GLUT_LEFT_BUTTON:

        if(state == GLUT_UP)
        {
            if (SphereSelected)
            {
                Cone=Cube=Torus=Teapot=false;
                Sphere=true;
                XSphere=x;
                YSphere=y;
                ZSphere=1;
                glutPostRedisplay();
            }

        }
    break;
    }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
   glutInitWindowSize (900, 900); 
   glutInitWindowPosition (0, 100);
   glutCreateWindow ("Scene Modeling and Interaction");
   init (); 
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMouseFunc(MyMouse);
   glutMainLoop();
   return 0;
}

推荐答案

鼠标功能为您提供的xy在屏幕像素坐标中,而不在场景使用的坐标中.为此,您必须将坐标居中(通过将屏幕尺寸减去一半),然后将其缩放到大约屏幕尺寸(通过将其除以屏幕尺寸的顺序).

The x and y that the mouse function gives you are in screen pixel coordinates, not in the coordinates that your scene uses. In order to do this, you have to center your coordinates (by subtracting by half your screen size) and scaling them to about your screen size (by dividing them by something on the order of your screen size).

因此,您可能想要做类似

Thus, you probably want to do something like

XSphere = (x - 450) * 3.0 / 900;
YSphere = (450 - y) * 3.0 / 900;

并尝试使用3.0,直到为您提供合理的东西为止.

and play around with the 3.0 until it gives you something reasonable.

这篇关于OpenGL的新功能:glutMouseFunc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆