#include <stdio.h> #include <glut.h> int numLines; typedef enum state{waitingforclick,clickedonce}; typedef struct point { int x; int y; }point; point line[256][2]; int gState=waitingforclick; bool lineisvalid=false; int gHeight; float gColor[3]={0,1,0}; void drawlines() { glColor3fv(gColor); glBegin(GL_QUADS); for(int i = 0;i<=numLines; i++) { glVertex2i(line[i][0].x,gHeight -line[i][0].y); glVertex2i(line[i][1].x,gHeight-line[i][1].y); } glEnd(); } void display() { glClear(GL_COLOR_BUFFER_BIT); drawlines(); glutSwapBuffers(); } //for menu.you should ignore it void menufunc(int val) { switch(val) { case 0: gColor[0] = 1; gColor[1] = 0; gColor[2] = 0; break; case 1: gColor[0] = 0; gColor[1] = 1; gColor[2] = 0; break; case 2: gColor[0] = 0; gColor[1] = 0; gColor[2] = 1; break; } } //for menu.you should ignore it void createMenu () { int menu = glutCreateMenu(menufunc); glutAddMenuEntry("Red", 0); glutAddMenuEntry("green", 1); glutAddMenuEntry("Blue", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); } void init() { glClearColor(0,0,0,1); glMatrixMode(GL_PROJECTION); //I can change glOrtho by gluPerspective ? glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0); numLines=- 1; glMatrixMode(GL_MODELVIEW); createMenu(); } void reshape(int width, int height) { gHeight = height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); //I can change gluOrtho2D by gluLookAt ? gluOrtho2D(0, width, 0, height); glMatrixMode(GL_MODELVIEW); } void mouseclick(int button, int state,int x, int y) { //when mouse click if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { switch (gState) { case waitingforclick: printf("1 clicked"); ++numLines; //line position line[numLines][0].x = x; line[numLines][0].y = y; line[numLines][1].x = x; line[numLines][1].y = y; line[numLines][2].x = x; line[numLines][3].y = y; gState++; break; case clickedonce: printf("2 clicked"); line[numLines][1].x = x; line[numLines][1].y = y; gState = waitingforclick; break; } } glutPostRedisplay (); } void mousedrag(int x, int y) { if(gState == clickedonce) { line[numLines][1].x = x; line[numLines][1].y = y; } glutPostRedisplay(); } int main(int argc, char ** argv) { glutInit(& argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(100,100); glutInitWindowSize(500,500); glutCreateWindow("App"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouseclick); glutPassiveMotionFunc(mousedrag); init(); glutMainLoop(); return 0; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)