📄 first.cpp
字号:
#include<glut.h>
static int i = 0;
int k = 0;
int kother = 0;
int delFirstPoint = -1;
int delSecondPoint = -1;
struct GLintPoint{
GLint x,y;
};
GLintPoint corner[120];
bool selected = false;
bool begin = false;
bool move = false;
bool refresh = false;
bool forK = false;
bool forKother= false ;
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0,0.0,1.0);
if(selected){
int j = 0;
for(;j<i-1;j = j+2)
{
glBegin(GL_LINES);
glVertex2i(corner[j].x,corner[j].y);
glVertex2i(corner[j+1].x,corner[j+1].y);
glEnd();
}
}
if (move)
{
glBegin(GL_LINES);
if ( k%2 ==0 )
glVertex2i(corner[k+1].x,corner[k+1].y);
else
glVertex2i(corner[k-1].x,corner[k-1].y);
glVertex2i(corner[k].x,corner[k].y);
glEnd();
if(forKother)
{
glBegin(GL_LINES);
if ( kother%2 ==0 )
glVertex2i(corner[kother+1].x,corner[kother+1].y);
else
glVertex2i(corner[kother-1].x,corner[kother-1].y);
glVertex2i(corner[kother].x,corner[kother].y);
glEnd();
}
forK = false;
forKother= false;
}
if(refresh )
{
glLoadIdentity();
glClearColor(1.0,1.0,1.0,0.0);
int j = 0;
for(;j<i-1;j = j+2)
{
glBegin(GL_LINES);
glVertex2i(corner[j].x,corner[j].y);
glVertex2i(corner[j+1].x,corner[j+1].y);
glEnd();
}
}
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(begin)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && i< 120)
{
corner[i].x = x;
corner[i].y = 500 - y;
i++;
selected = true;
}
glutPostRedisplay();
}
if (move)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP&& forK)
{
corner[k].x = x;
corner[k].y = 500 - y;
if(forKother)
{
corner[kother].x = x;
corner[kother].y = 500 - y;
}
forK = false;
return ;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN&& forK == false)
{
for(int t = 0;t < i-1;t++)
if( corner[t].x - x <= 4 && corner[t].y -(500 - y)<=4&&corner[t].x - x>=-4&&corner[t].y -(500 - y)>=-4)
{
if(!forK)
{
k = t;
forK = true;
}
else
{
kother = t;
forKother = true;
return ;
}
}
}
}
}
void myPassiveMotion(int x,int y)
{
if(move && forK)
{
corner[k].x = x;
corner[k].y =500 - y;
}
if (selected)
{
corner[i-1].x = x;
corner[i-1].y =500 - y;
}
glutPostRedisplay();
}
void mykey(unsigned char key, int mouseX, int mouseY)
{
GLint x = mouseX,y = mouseY;
if (key == 'q')
exit(0);
if (key == 'b')
{
begin = true;
selected = true;
move = false;
refresh = false;
forK = false;
}
if (key == 'm')
{
begin = false;
move = true;
refresh = false;
}
if (key == 'r')
{
begin = false;
move = false;
refresh = true;
selected = false;
}
}
void myInit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0,1.0,1.0,0.0);
glViewport(0,0,500,500);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,150);
glutCreateWindow("Draw house");
myInit();
glutMouseFunc(mouse);
glutDisplayFunc(myDisplay);
glutPassiveMotionFunc(myPassiveMotion);
glutKeyboardFunc(mykey);
glutMainLoop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -