2

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;
   }
}

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;
}
4

1 回答 1

5

鼠标功能为您提供的xandy位于屏幕像素坐标中,而不是您的场景使用的坐标中。为此,您必须将坐标居中(通过减去屏幕尺寸的一半)并将它们缩放到大约您的屏幕尺寸(通过将它们除以屏幕尺寸的顺序)。

因此,您可能想做类似的事情

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

并玩弄 3.0 直到它给你一些合理的东西。

于 2012-11-08T23:17:01.647 回答