/* Displays a picture with and without aliasing using jittering to achieve smooth edges on the picture without aliasing. Written by Caroline Geiersbach and Scott D. Anderson scott.anderson@acm.org Summer 2003 */ #include #include // for exit(0) and atoi() #include //menu typedef enum { BEAR, TEAPOT, TETRAHEDRON } options; int rMenu; int menuNum = 0; void myMenu(); //window values #define GAP 35 GLuint wWidth = 400; GLuint wHeight = 400; GLuint mainWidth = wWidth*2+GAP*3; GLuint mainHeight = wHeight+GAP*2; GLuint window, rightWin, leftWin; void redisplayAll(); //values for the camera GLfloat near = 3.0; GLfloat far = 21.0; GLfloat left = -3.0; GLfloat right = 3.0; GLfloat bottom = -3.0; GLfloat top = 3.0; void reshape(int width, int height) { glViewport(0,0,width, height); } /* Set up camera. Note that near and far remain fixed; for scene antialiasing we no longer move the eye location. */ void accCamera(GLfloat pixdx, GLfloat pixdy) { GLfloat dx, dy; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); GLfloat windowWidth=viewport[2]; GLfloat windowHeight=viewport[3]; GLfloat frustumWidth=right-left; GLfloat frustumHeight=top-bottom; glMatrixMode(GL_PROJECTION); glLoadIdentity(); dx = pixdx*frustumWidth/windowWidth; dy = pixdy*frustumHeight/windowHeight; glFrustum(left+dx,right+dx,bottom+dy,top+dy,near,far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); twError(); } void myCamera() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(left,right,bottom,top,near,far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); twError(); } void lighting() { GLfloat direction[]={1,1,1,0}; twGrayLight(GL_LIGHT0,direction,0.1,0.8,1.0); glShadeModel(GL_SMOOTH); twAmbient(0.5); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE); glEnable(GL_LIGHTING); twError(); } //parent window void mainDisplay() { //set up camera glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,mainWidth,mainHeight,0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); twDisplayInit(); glDisable(GL_LIGHTING); glColor3f(0,0,0); twSetFont("helvetica", 18); twDrawString(GAP,25,"Original image"); twDrawString(GAP*2+wWidth, 25, "Image after antialiasing"); glEnable(GL_LIGHTING); glutSwapBuffers(); twError(); } twTriple teapotRed = {1,0,0}; twTriple tetraBlue = {0,0,1}; void drawObject() { glPushMatrix(); switch(menuNum) { case(BEAR): glTranslatef(0,0,-4); glScalef(6,6,6); twTeddyBear(); break; case(TEAPOT): twColor(teapotRed, 0.7, 128); glTranslatef(0,0,-5.5); glRotatef(25,1,0,0); glScalef(2.5,2.5,2.5); glutSolidTeapot(1); break; case(TETRAHEDRON): twColor(tetraBlue, 0.7, 128); glTranslatef(0,0,-6.5); // glRotatef(90,1,0,0); glScalef(3.5,3.5,3.5); glutSolidIcosahedron(); break; } glPopMatrix(); twError(); } //displays image before antialiasing void jaggedDisplay() { myCamera(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawObject(); glFlush(); glutSwapBuffers(); twError(); } // From the OpenGL Programming Guide, first edition, table 10-5 float jitterTable[][2] = { {0.5625, 0.4375}, {0.0625, 0.9375}, {0.3125, 0.6875}, {0.6875, 0.8124}, {0.8125, 0.1875}, {0.9375, 0.5625}, {0.4375, 0.0625}, {0.1875, 0.3125}}; int NumJitters; //displays image after antialiasing void smoothDisplay() { int jitter; glClear(GL_ACCUM_BUFFER_BIT); for(jitter=0; jitter