• 大小: 8.46MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-22
  • 语言: 其他
  • 标签: glut  opengl  鼠标控制  

资源简介

基于glutMouseFunc和glutMotionFunc的用鼠标控制物体的缩放和旋转的小例子,分享学习一下。

资源截图

代码片段和文件信息

#include“stdlib.h“
#include“iostream“
#include“gltools.h“
#include“GLBatch.h“
#include“GLShaderManager.h“
#include“math.h“
#ifdef __APPLE__
#include           // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include             // Windows FreeGlut equivalent
#endif

using namespace std;

GLShaderManager shaderManager;
GLTriangleBatch sphereBatch;

bool mouseLeftDown=false;
bool mouseRightDown=false;
float mouseXmouseY;
float cameraZ;
float rotXrotY;
const float offset = 0.01;
void ChangeSize(GLint wGLint h)
{
if(h == 0)
h = 1;
glViewport(00wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60(GLfloat)w/(GLfloat)h0.1100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//不要忘记加载模型视图矩阵!!!
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//简单地调用以下平移和旋转函数实现控制
glTranslatef(00cameraZ);
glRotatef(rotX 1.0f 0.0f 0.0f);
glRotatef(rotY 0.0f 1.0f 0.0f);
glColor3f(011);
glPolygonMode(GL_FRONT_AND_BACKGL_LINE);
sphereBatch.Draw();
glutSwapBuffers();
// glutPostRedisplay();
}
/*
//button:GLUT_LEFT_BUTTON  GLUT_MIDDLE_BUTTON  GLUT_RIGHT_BUTTON
//state:GLUT_DOWN  GLUT_UP
//x  y坐标值
*/
void mouse(int button int state int x int y)
{
mouseX = x;
mouseY = y;
if(button == GLUT_LEFT_BUTTON)
{
if( state == GLUT_DOWN)
mouseLeftDown = true;
else if(state == GLUT_UP)
mouseLeftDown = false;
}
else if(button == GLUT_RIGHT_BUTTON)
{
if( state == GLUT_DOWN)
mouseRightDown = true;
else if(state == GLUT_UP)
mouseRightDown = false;
}
glutPostRedisplay();
}

void motion(int x int y)
{
float dx = x-mouseX;
float dy = y-mouseY;
//x与y的位置相互颠倒
if(mouseLeftDown == true)
{
rotY += dx;
rotX += dy;
}
else if(mouseRightDown == true)
{
cameraZ += dy*offset;
}
mouseX = x;
mouseY = y;

glutPostRedisplay();
}
void SetupRC()
{
glClearColor(0001);
glEnable(GL_DEPTH_TEST);
gltMakeSphere(sphereBatch51530);
}
int main(int argc char* argv[])
{
gltSetWorkingDirectory(argv[0]);

glutInit(&argc argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH);
glutInitWindowSize(800 600);
glutCreateWindow(“MouseMotion“);
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);

glutMouseFunc(mouse);
glutMotionFunc(motion);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr “GLEW Error: %s\n“ glewGetErrorString(err));
return 1;
}
SetupRC();

glutMainLoop();

return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     459776  2015-04-08 08:49  mouseMove\Debug\mouseMove.exe

     文件    1475988  2015-04-08 08:49  mouseMove\Debug\mouseMove.ilk

     文件    1715200  2015-04-08 08:49  mouseMove\Debug\mouseMove.pdb

     文件        606  2015-04-08 08:49  mouseMove\mouseMove\Debug\cl.command.1.tlog

     文件      29988  2015-04-08 08:49  mouseMove\mouseMove\Debug\CL.read.1.tlog

     文件        320  2015-04-08 08:49  mouseMove\mouseMove\Debug\CL.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link-cvtres.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link-cvtres.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link-rc.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link-rc.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576-cvtres.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576-cvtres.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576-rc.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576-rc.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1576.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880-cvtres.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880-cvtres.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880-rc.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880-rc.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.1880.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948-cvtres.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948-cvtres.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948-rc.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948-rc.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.4948.write.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.6084-cvtres.read.1.tlog

     文件          2  2015-04-08 08:49  mouseMove\mouseMove\Debug\link.6084-cvtres.write.1.tlog

............此处省略50个文件信息

评论

共有 条评论