// Polygon Drawing with mouse and moving with arrow keys
//Use arrow keys on numpad(4 for laft and 6 for right).
#include "stdafx.h"
#include "math.h"
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include "canvas.h"
Canvas cvs(640,480,"Moving Polygon,Mouse and Keyboard control");
int screenHeight=480;
int p=0,m=0,chk=0,gva=0; //global variables
int gvb=0;
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void draw(int x,int y) //to draw polygon
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(x,screenHeight-y);
glVertex2i(x+40,screenHeight-y);
glVertex2i(x,screenHeight-(y-40));
glVertex2i(x+40,screenHeight-(y-40));
glEnd();
}
void myMouse( int button, int state,int x,int y)
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
draw(x,y); //draw polygon where left button clicked
gva=x; //passing polygon starting coordinates to global
gvb=y; //variables gva and gvb
}
}
void myKeyboard(unsigned char theKey,int x,int y)
{
GLint a=gva; //passing global values (from mouse func) to keyboard
GLint b=gvb; //function variables
switch(theKey)
{
case '+':
p=p+20; // add 20 pixels
break;
case '-':
m=m-20; // subtract 20 pixels
break;
case '4':
glClear(GL_COLOR_BUFFER_BIT);
a=a-10; //to move on left
chk=p+m;
if (chk>0){chk=-1*chk;} //to keep direction left ,if p+m becomes greater than zero
a=a+chk;
draw(a,b);
break;
case '6':
glClear(GL_COLOR_BUFFER_BIT);
a=a+10;
chk=p+m;
if (chk<0){chk=-1*chk;}
a=a+chk;
draw(a,b);
break;
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glutMouseFunc(myMouse);
glutKeyboardFunc(myKeyboard);
glFlush();
}
void main(int argc, char** argv)
{
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
Q2.rar_assignment
版权申诉
128 浏览量
2022-09-14
17:23:11
上传
评论
收藏 1KB RAR 举报
APei
- 粉丝: 63
- 资源: 1万+
最新资源
- python-leetcode面试题解之第198题打家劫舍-题解.zip
- python-leetcode面试题解之第191题位1的个数-题解.zip
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
- python-leetcode面试题解之第170题两数之和III数据结构设计-题解.zip
- python-leetcode面试题解之第168题Excel表列名称-题解.zip
- python-leetcode面试题解之第167题两数之和II输入有序数组-题解.zip
- python-leetcode面试题解之第166题分数到小数-题解.zip
- python-leetcode面试题解之第165比较版本号-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈