#include #include using namespace std; static float g_theta = 30.0f; static float g_x1 = 0; static float g_y1 = 0; static float g_x2 = 0; static float g_y2 = 0; bool g_eraseOld = false; void Init() { glMatrixMode(GL_PROJECTION); glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -10.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } void DrawModel() { glColor3f(1, 1, 0); glPushMatrix(); glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); glPopMatrix(); } void OnDisplay() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawModel(); glutSwapBuffers(); } void OnReshape(int w, int h) { glViewport(0, 0, w, h); } void OnIdle() { //g_theta += 0.5f; //glutPostRedisplay(); } void DrawRectangle(float x1, float y1, float x2, float y2) { glPushMatrix(); glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x2, y2); glVertex2f(x1, y2); glVertex2f(x1, y2); glVertex2f(x1, y1); glEnd(); glPopMatrix(); } void OnMouse(int button, int state, int x, int y) { g_x1 = static_cast(x-250)/500.0f; g_y1 = static_cast(500-y)/500.0f; if (state == 0) { glEnable(GL_COLOR_LOGIC_OP); glLogicOp(GL_XOR); glDrawBuffer(GL_FRONT); } else { glDisable(GL_COLOR_LOGIC_OP); glLogicOp(GL_COPY); glDrawBuffer(GL_BACK); g_eraseOld = false; } } void OnMouseMove(int x, int y) { float x2 = static_cast(x-250)/500.0f; float y2 = static_cast(500 - y)/500.0f; //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //DrawModel(); glColor3f(1, 0, 0); if (g_eraseOld) { DrawRectangle(g_x1, g_y1, g_x2, g_y2); } DrawRectangle(g_x1, g_y1, x2, y2); g_x2 = x2; g_y2 = y2; g_eraseOld = true; glFlush(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("OpenGL Experiment for Remote Desktop"); const char* str = reinterpret_cast(glGetString(GL_VERSION)); cout << "GL Version: " << str << endl; Init(); glutDisplayFunc(OnDisplay); glutReshapeFunc(OnReshape); glutMotionFunc(OnMouseMove); glutMouseFunc(OnMouse); glutIdleFunc(OnIdle); glutMainLoop(); return 0; }