m0_37932954
steffff
2017-04-17 08:07

vs2015环境下OpenGL中怎样实现鼠标右键控制物体旋转

  • c++
  • 鼠标
  • opengl

#include "StdAfx.h"
#include "CloudPoint.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include

#include

#include

CCloudPoint::CCloudPoint(void)
{

}

CCloudPoint::~CCloudPoint(void)
{
}

CCloudPoint CCloudPoint::s_pCurrent = NULL;
char *m_cFilename = NULL;
//int CCloudPoint::s_argc = 0;//char
* CCloudPoint::s_argv = 0;

bool mouseLeftDown;
bool mouseRightDown;

float CCloudPoint::c=3.1415926535/180.0f; //弧度和角度转换参数

int CCloudPoint::du=90,CCloudPoint::oldmy=-1,CCloudPoint::oldmx=-1; //du是视点绕y轴的角度,opengl里默认y轴是上方向
float CCloudPoint::r=0.3f,CCloudPoint::h=0.0f; //r是视点绕y轴的半径,h是视点高度即在y轴上的坐标
int CCloudPoint::scale = 10;
int CCloudPoint::iWindowsWidth = 800, CCloudPoint::iWindowHeight = 800;
float CCloudPoint::x[1000000], CCloudPoint::y[1000000], CCloudPoint::z[1000000];
int CCloudPoint::iPointNumber = 0;
int mButton = -1;

void CCloudPoint::setFilename(char * m_cFilename)
{
this->m_cFilename = m_cFilename;
}

char* CCloudPoint::getFilename()
{
return m_cFilename;
}

void CCloudPoint::MouseCallback(int button, int state, int x, int y)
{
CCloudPoint::s_pCurrent->Mouse(button, state, x, y);
}

void CCloudPoint::onMouseMoveCallback( int x,int y)
{
CCloudPoint::s_pCurrent->onMouseMove( x,y);
}
void CCloudPoint::reshapeCallback(int w,int h)
{
CCloudPoint::s_pCurrent->reshape(w,h);
}
void CCloudPoint::myDisplayCallback(void)
{
CCloudPoint::s_pCurrent->myDisplay();
}

void CCloudPoint::Mouse(int button, int state, int x, int y) //处理鼠标点击

{
if (state == GLUT_DOWN)
{
oldmx = x, oldmy = y;
}
else
{
if (state == GLUT_UP && button == GLUT_WHEEL_UP)
{
if (scale>1)
scale -= 1;
else
scale = 1;
//glutPostRedisplay();

}
else if (state == GLUT_UP && button == GLUT_WHEEL_DOWN)
{
scale += 1;
//glutPostRedisplay();

}
else if (state == GLUT_DOWN && button == GLUT_RIGHT_BUTTON)
{
mouseRightDown = true;

    }
    else if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
    {
        mouseLeftDown = true;
    }
}

}

void CCloudPoint::onMouseMove(int x,int y) //处理鼠标拖动

{

    //printf("%d\n",du); 

if (mouseRightDown)
{
    du += x - oldmx; //鼠标在窗口x轴方向上的增量加到视点绕y轴的角度上,这样就左右转了  
    h += 0.03f*(y - oldmy); //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了  
    if (h > 1.0f) h = 1.0f; //视点y坐标作一些限制,不会使视点太奇怪  
    else if (h < -1.0f) h = -1.0f;
    oldmx = x, oldmy = y; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备  



}



}

void CCloudPoint::init()

{

glEnable(GL_DEPTH_TEST);

}

void CCloudPoint::reshape(int w,int h)

{

glViewport( 0, 0, (GLsizei)w, (GLsizei)h );

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

gluPerspective(90.0f, (GLfloat)w / (GLfloat)h, 1.0f, 1.0f);

glMatrixMode( GL_MODELVIEW );

}

void CCloudPoint::loadFile(char cFileName)
{
FILE
file = fopen(cFileName,"r");
if (!file)
{
printf("load file %s failed\n",cFileName);
}
else
{
/*fseek(file,0,SEEK_END);
long fileSize = ftell(file);*/
char buffer[1000];
while(fgets(buffer,300,file) != NULL)
{

        sscanf(buffer,"%f, %f, %f", &x[iPointNumber], &y[iPointNumber], &z[iPointNumber]);
        iPointNumber++;
        //printf("x=%f, y=%f, z=%f\n", x, y, z);
    }
}

}

void CCloudPoint::myDisplay(void)
{ int i;
/*glClear(GL_COLOR_BUFFER_BIT);*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//printf("At:%.2f %.2f %.2f\n",r*cos(c*du),h,r*sin(c*du)); //这就是视点的坐标
gluLookAt(r*cos(c*du), h, r*sin(c*du), 0, 0, 0, 0, 1.0f, 0); //从视点看远点,y轴方向(0,1,0)是上方向

glBegin(GL_POINTS);
/*glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.5f);*/
for (i = 0; i < iPointNumber; i++)
{
glVertex3f(x[i]/scale, y[i]/scale, z[i]/scale);
}
glEnd();
glFlush();
glutSwapBuffers();
}
void CCloudPoint::LoadAndShow()
{ int argc=1;
char* argv[]={"good"};
//loadFile(CCloudPoint::getFilename());
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 0);
glutInitWindowSize(iWindowsWidth, iWindowHeight);
glutCreateWindow("OpenGL程序");

init();  
glutReshapeFunc(CCloudPoint::reshapeCallback);
glutDisplayFunc(myDisplayCallback);


/*glutDisplayFunc(display);*/   
glutIdleFunc(CCloudPoint::myDisplayCallback);  //设置不断调用显示函数  
glutMouseFunc(CCloudPoint::MouseCallback);  
glutMotionFunc(CCloudPoint::onMouseMoveCallback);  
glutMainLoop();
return;

}
上面是我的代码,但是没有用,如果将void CCloudPoint::onMouseMove(int x,int y) 中的if语句去掉,就是任意鼠标键移动控制旋转,想问问大神

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换