//PointAndLine.c
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#include "GL/glut.h"
#include"iostream"
//鼠标事件
void myMouse(int _button, int _state, int _x, int _y) {
//_button与_state是独立的, 根据需要进行组合
switch (_button) {
case GLUT_LEFT_BUTTON:
std::cout<<"LEFT <= mouse button is pressed, "; break;
case GLUT_MIDDLE_BUTTON:
std::cout<<"MIDD || mouse button is pressed, "; break;
case GLUT_RIGHT_BUTTON:
std::cout<<"RIGH => mouse button is pressed, "; break;
}
if (_state)
std::cout<<"UP at ["<<_x<<","<<_y<<"]\n";
else
std::cout<<"DOWN at ["<<_x<<","<<_y<<"]\n";
}
//初始化窗口
void SetupRC(void)
{
// 设置窗口背景颜色为黑色
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}
void ChangeSize(int w, int h)
{
if(h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-100.0f, 100.0f, -100.0f*h/w, 100.0f*h/w, -100.0f, 100.0f);
else
glOrtho (-100.0f*w/h, 100.0f*w/h, -100.0f, 100.0f, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 在窗口中绘制图形
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//设置点的大小以及线宽
glPointSize(5);
glLineWidth(5);
//马
glBegin(GL_LINE_STRIP);
glColor3f(1.0,1.0,0.0);
glVertex2f(-40.5f,20.0f);
glVertex2f(-20.0f,20.0f);
glVertex2f(-20.0f,8.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0,1.0,0.0);
glVertex2f(-30.0f,16.0f);
glVertex2f(-30.0f,10.0f);
glVertex2f(-16.0f,10.0f);
glVertex2f(-20.0f,-15.0f);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0,1.0,0.0);
glVertex2f(-35.0f,0.0f);
glVertex2f(-20.0f,0.0f);
glEnd();
//乐
glBegin(GL_LINE_STRIP);
glColor3f(1.0,1.0,0.0);
glVertex2f(34.0f,22.0f);
glVertex2f(20.0f,20.0f);
glVertex2f(20.0f,9.0f);
glVertex2f(34.0f,9.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0,1.0,0.0);
glVertex2f(29.0f,19.0f);
glVertex2f(29.0f,-15.0f);
glVertex2f(26.0f,-13.0f);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0,1.0,0.0);
glVertex2f(20.0f,0.0f);
glVertex2f(10.0f,-5.0f);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0,1.0,0.0);
glVertex2f(30.0f,0.0f);
glVertex2f(37.0f,-5.0f);
glEnd();
glPopMatrix();
glutSwapBuffers(); // 刷新命令缓冲区
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
glutCreateWindow("名字");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
SetupRC();
//注册鼠标事件处理函数 <==========NEW!!!
glutMouseFunc(myMouse);
glutMainLoop();
return 0;
}