#ifdef WIN32 #include #endif #include #include #include #include #include #include #define MAX_NCURVES 100 #define MAX_NVERTS 500 typedef struct { double x,y; } POINT2; typedef struct { int len; POINT2 *verts; } CURVE; CURVE *curves = new CURVE[MAX_NCURVES]; int nCurves = 0; enum {IDLE, ADDING}; int mode = IDLE; int winWidth,winHeight; int imgWidth,imgHeight; int scaledImgWidth,scaledImgHeight; double lineSize=0.5; double xMin=0,xMax=1,yMin=0,yMax=1,f=2; char *curveFile=NULL,*imgFile=NULL; byte *origImg=NULL,*scaledImg=NULL; double x1Chg,y1Chg,x2Chg,y2Chg; int drawImage = 1; ////////////////////////////////////////////////////// void handleReshape(int w, int h) { winWidth = w; winHeight = h; // // Set the viewport // glViewport(0, 0, w, h); glDisable(GL_CULL_FACE); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); GLfloat ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat specular[] = { 0.0f, 0.0f, 0.0f, 1.0f }; GLfloat position[] = { 0.0f, 0.0f, -1.0f, 0.0f }; GLfloat lmodel_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; int local_view[] = { false }; /**** set lighting parameters ****/ glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModeliv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // glPixelStorei(GL_PACK_ALIGNMENT, 1); // resize the image to fit the screen. if (imgFile != NULL) { if (scaledImg != NULL) free(scaledImg); scaledImgWidth = winWidth; scaledImgHeight = winHeight; // printf("win:(%d %d) orig: (%d %d) scaled:(%d %d)\n", // winWidth,winHeight,imgWidth,imgHeight, // scaledImgWidth,scaledImgHeight); scaledImg = new byte[scaledImgWidth*scaledImgHeight*4]; gluScaleImage(GL_RGBA, imgWidth, imgHeight, GL_UNSIGNED_BYTE, origImg, scaledImgWidth, scaledImgHeight, GL_UNSIGNED_BYTE, scaledImg); } // // Now start setting the projection matrix stack // glMatrixMode(GL_PROJECTION); glLoadIdentity(); double dx = (xMax-xMin)/2.0; double dy = (yMax-yMin)/2.0; glOrtho(-dx,dx, -dy,dy, 0.1*f, f*2.0); x1Chg = 0.0; y1Chg = 0.0; x2Chg = 1.0; y2Chg = 1.0; } ////////////////////////////////////////////////////// void readImage() { if (imgFile != NULL) { FILE *fp = fopen(imgFile,"rb"); if (fp == NULL) { fprintf(stderr,"Can't read image file %s\n",imgFile); exit(1); } int w,h,found=false; int row; char line[80]; fgets(line,79,fp); line[2] = '\0'; if (strcmp(line,"P6") != 0) { fprintf(stderr,"Not a raw PPM image! Magic=%s\n",line); exit(1); } while (!found) { fgets(line,79,fp); if (line[0] != '#') found = true; } sscanf(line,"%d %d",&w,&h); imgWidth = w; imgHeight = h; winWidth = w; winHeight = h; found = false; while (!found) { fgets(line,79,fp); if (line[0] != '#') found = true; } // Ok, now we've read the header. Time to grab the bytes. if (origImg != NULL) free(origImg); origImg = new byte[4*h*w]; // // PPM images are stored top-to-bottom, but glDrawPixels // draws from bottom-to-top, so we have to store rows // of pixels in reverse order for it to look right. // To make life easier, store pixels as RGBA, not RGB. // That simplifies alignment. // byte *oneRow = new byte[3*w]; byte *src,*dst; for (row=h-1; row>=0; row--) { fread(oneRow,3,w,fp); src = oneRow; dst = origImg + row*w*4; for (int col=0; col MAX_NCURVES) { fprintf(stderr,"Can't allocate more than %d curves, sorry.\n", MAX_NCURVES); } else { int i = nCurves++; curves[i].len = 0; curves[i].verts = new POINT2[MAX_NVERTS]; } } ////////////////////////////////////////////////////// void addVertex(double x, double y) { // printf("addVertex: (x y)=(%6.3lf %6.3lf)\n",x,y); int i = nCurves-1; if (curves[i].len > MAX_NVERTS) { fprintf(stderr,"Curve too long. Max is %d vertices\n", MAX_NVERTS); } else { int j = curves[i].len++; curves[i].verts[j].x = x; curves[i].verts[j].y = y; } } ////////////////////////////////////////////////////// void delVertex() { int i = nCurves-1; if (curves[i].len > 0) { curves[i].len--; } } ////////////////////////////////////////////////////// void readCurves() { FILE *fp; if ((fp=fopen(curveFile,"r")) == NULL) { fprintf(stderr,"Can't read file %s\n",curveFile); return; } int i,j,n,len; double x,y; fscanf(fp,"%d",&n); for (i=0; i