OpenGL鼠标控制绘制矩形

#include <windows.h>    // Windows的头文件

#include <gl\gl.h> // OpenGL32库的头文件
#include <gl\glu.h> // GLu32库的头文件
#include <gl\glaux.h> // GLaux库的头文件
#include <gl\glut.h> // Glut库头文件

#pragma comment( lib, "opengl32.lib") // OpenGL32连接库
#pragma comment( lib, "glu32.lib") // GLu32连接库
#pragma comment( lib, "glaux.lib") // GLaux连接库
#pragma comment( lib, "glut.lib") // Glut链接库

struct GLintPoint
{
GLint x,y;

};

GLintPoint corner[2];
bool selected = false;
int screenWidth=640;
int screenHeight=480;

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f,1.0f,1.0f);
if(selected)
{
glBegin(GL_QUADS);
glVertex2i(corner[0].x,corner[0].y);
glVertex2i(corner[0].x,corner[1].y);
glVertex2i(corner[1].x,corner[1].y);
glVertex2i(corner[1].x,corner[0].y);
glEnd();
}
glutSwapBuffers();

}

void myMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
corner[0].x=x;
corner[0].y=screenHeight-y;
selected=true;
}
glutPostRedisplay();

}

void myPassiveMotion(int x,int y)
{
corner[1].x=x;
corner[1].y=screenHeight-y;
glutPostRedisplay();

}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitWindowSize(screenWidth,screenHeight);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Rubber Rect Demo");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,screenWidth,0,screenHeight);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glViewport(0,0,screenWidth,screenHeight);

glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
glutPassiveMotionFunc(myPassiveMotion);

glutMainLoop();
return 0;

}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/01/22/2328794.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值