Link Click
Link Click
CSC276
Lab. Note
//execute initialization procedure //send graphics to display window // display and wait
1. Install Open GL library 2. Write and Execute the code 3. Using different functions draw : Window, Points, lines, Triangle, Square, Hexagon 4. Change window size and position 5. Change the background color 6. Change the Primitives color
Student Note:
void init (void) { glClearColor (1.0,1.0,1.0,0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,200.0,0.0,150.0); } void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0,0.0,0.0); lineDDA (20,30,170,130); glFlush(); } void main(int argc, char** argv) { glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (50,100); glutInitWindowSize (400,300); glutCreateWindow("DDA line");
1. 2. 3. 4. 5. 6.
Write and Execute the code Change the background color Change the Primitives color Calculate the slope of the line Draw the line with the following slopes: 30 - 60 80 Modify the code to draw two lines in the same time
Student Note:
if (p<0) p+= twoDy; else { y++; p += twoDyMinusDx; } setpixel (x, y); } } void init (void) { glClearColor (1.0,1.0,1.0,0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,200.0,0.0,150.0); } void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0,0.0,0.0); lineBres (20,15,120,140); glFlush(); } void main(int argc, char** argv) { glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (50,100); glutInitWindowSize (400,300); glutCreateWindow("Bresenham's Line"); init(); glutDisplayFunc (lineSegment); glutMainLoop(); }
1. 2. 3. 4. 5. 6.
Write and Execute the code Change the background color Change the Primitives color Calculate the slope of the line Draw the line with the following slopes: 10 - 20 30 Modify the code to draw two lines in the same time
Student Note:
class scrpt { public: GLint x,y; }; scrpt circCtr; void setpixel (GLint x, GLint y) { glBegin (GL_POINTS); glVertex2i(x, y); glEnd(); } void circlePlotPoints (scrpt circCtr, scrpt circpt) { setpixel (circCtr.x + circpt.x, circCtr.y + circpt.y); setpixel (circCtr.x - circpt.x, circCtr.y + circpt.y); setpixel (circCtr.x + circpt.x, circCtr.y - circpt.y); setpixel (circCtr.x - circpt.x, circCtr.y - circpt.y); setpixel (circCtr.x + circpt.y, circCtr.y + circpt.x); setpixel (circCtr.x - circpt.y, circCtr.y + circpt.x); setpixel (circCtr.x + circpt.y, circCtr.y - circpt.x); setpixel (circCtr.x - circpt.y, circCtr.y - circpt.x); } void circleMyBres (scrpt circCtr, GLint radius)
{ scrpt circpt; GLint p = 3-2*radius; circpt.x=0; circpt.y=radius; void circlePlotPoints (scrpt, scrpt); circlePlotPoints (circCtr, circpt); while (circpt.x < circpt.y) { circpt.x++; if(p<0) p +=4* circpt.x +6; else { circpt.y--; p += 4* (circpt.x - circpt.y) +10; } circlePlotPoints (circCtr, circpt); } }
glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,200.0,0.0,150.0); } void circle (void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0,1.0,1.0); circCtr.x=100; circCtr.y=75;
GLint radius=60; circleMyBres (circCtr, radius) ; glFlush(); } void main(int argc, char** argv) { glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (50,100); glutInitWindowSize (400,300); glutCreateWindow("A circle"); init(); glutDisplayFunc (circle); glutMainLoop(); }
1. 2. 3. 4.
Write and Execute the code Use different radius Change window size and position Change the circle & background colors
Student Note:
scrpt circpt; GLint p = 3-2*radius; circpt.x=1; circpt.y=radius; void circlePlotPoints (scrpt, scrpt); circlePlotPoints (circCtr, circpt); while (circpt.x < circpt.y) { circpt.x++; if(p<0) p +=4* circpt.x +6; else { circpt.y--; p += 4* (circpt.x - circpt.y) +10; } circlePlotPoints (circCtr, circpt); } }
void init (void) { glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,250.0,0.0,250.0); } void circle (void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0,0.0,0.0); circCtr.x=75; circCtr.y=100; GLint radius=50; circleMidPoint (circCtr, radius) ;
glColor3f (0.0,1.0,0.0); circCtr.x=125; circCtr.y=150; circleMidPoint (circCtr, radius); glColor3f (0.0,0.0,1.0); circCtr.x=175; circCtr.y=100; circleMidPoint (circCtr, radius); glFlush(); } void main(int argc, char** argv) { glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100,50); glutInitWindowSize (400,400); glutCreateWindow("circle"); init(); glutDisplayFunc (circle); glutMainLoop(); }
1. 2. 3. 4.
Write and Execute the code Use different radius Change window size and position Change the primitives & background colors
Student Note:
inline int round(const float a) {return int(a+0.5);} void setPixel(int x,int y) { glBegin (GL_POINTS); glVertex2i(x, y); glEnd(); } void init (void) { glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,300.0,0.0,250.0); } void ellipsePlotPoints(int xCenter, int yCenter, int x, int y) { setPixel(xCenter + x, yCenter + y); setPixel(xCenter - x, yCenter + y);
void ellipseMidpoint(int xCenter, int yCenter, int Rx, int Ry) { int Rx2=Rx*Rx; int Ry2=Ry*Ry; int twoRx2=2*Rx2; int twoRy2=2*Ry2; int p; int x = 0; int y = Ry; int px = 0; int py=twoRx2*y;
ellipsePlotPoints(xCenter, yCenter, x, y); /*Region1*/ p=round(Ry2-(Rx2 * Ry) + (0.25 * Rx2)); while(px < py) { x++; px += twoRy2; if(p < 0) p += Ry2 + px; else{ y--; py -= twoRx2; p += Ry2 + px - py; }
ellipsePlotPoints(xCenter, yCenter, x, y); } /*Region2*/ p=round(Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1) * (y-1) - Rx2 * Ry2); while(y > 0) { y--; py -= twoRx2; if(p > 0) p += Rx2 - py; else { x++; px += twoRy2; p += Rx2 - py + px; } ellipsePlotPoints(xCenter, yCenter, x, y); } } void ellipse (void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (0.0,1.0,1.0); xCenter = 150; yCenter = 150; x = 100; y = 50; ellipseMidpoint (xCenter, yCenter, x, y) ; glFlush(); }
void main(int argc, char** argv) { glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100,50); glutInitWindowSize (400,400); glutCreateWindow("Ellipse"); init(); glutDisplayFunc (ellipse); glutMainLoop(); }
1. 2. 3. 4.
Write and Execute the code Change Ellipse size and color Change window size and position Draw Multi ellipse
Student Note:
{ glBegin (GL_POINTS); glVertex2i (x,y); glEnd ( ); } void mousePtPlot (GLint button, GLint action, GLint xMouse, GLint yMouse) { if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) setpixel (xMouse, winHeight - yMouse); glFlush ( ); } void main (int argc, char** argv) { glutInit (& argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100, 100); glutInitWindowSize (winWidth, winHeight); glutCreateWindow("Mouse Plot Points"); init(); glutDisplayFunc (displayFcn); glutReshapeFunc (winReshapeFcn); glutMouseFunc (mousePtPlot); glutMainLoop(); }
1. 2. 3. 4.
Write and Execute the code Use Right and Left mouse buttons Change point size and color Change the background color
Student Note:
gluOrtho2D (0.0, double (newWidth), 0.0, double (newHeight)); winWidth = newWidth; winHeight = newHeight; } void drawLineSegment (scrPt endPt1, scrPt endPt2) { glBegin (GL_LINES); glVertex2i (endPt1.x, endPt1.y); glVertex2i (endPt2.x, endPt2.y); glEnd (); }
void polyline (int button, int action, int xMouse, int yMouse) { static scrPt endPt1, endPt2; if (ptCtr == 0) { if (button == GLUT_LEFT_BUTTON && action ==GLUT_DOWN) { endPt1.x = xMouse; endPt1.y = winHeight - yMouse; ptCtr = 1; } else if (button == GLUT_RIGHT_BUTTON) exit (0); } else if(button == GLUT_LEFT_BUTTON && action ==GLUT_DOWN) { endPt2.x = xMouse; endPt2.y = winHeight - yMouse; drawLineSegment (endPt1, endPt2); endPt1 = endPt2;
} else if (button == GLUT_RIGHT_BUTTON) exit (0); glFlush (); } void main (int argc, char** argv) { glutInit (& argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100, 100); glutInitWindowSize (winWidth, winHeight); glutCreateWindow("Draw Interactive Polyline "); init(); glutDisplayFunc (displayFcn); glutReshapeFunc (winReshapeFcn); glutMouseFunc (polyline); glutMainLoop(); }
1. 2. 3. 4.
Write and Execute the code Try Right and Left mouse buttons Change line color Change the background color
Student Note:
glVertex2d(A*t, C*(myFunc(t)+1)); glEnd(); glFlush(); } void Display(void) { glClear(GL_COLOR_BUFFER_BIT); // clear the screen plotFunc(); } void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480); // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow("my function"); // open the screen window glutDisplayFunc(Display); // register redraw function Init(); glutMainLoop(); // go into a perpetual loop } 1. Write and Execute the code 2. Plot different Math functions 3. Change line color Student Note:
GLint size=2; glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); glRecti(x,y,x+size,y+size); glFlush(); } } void myMovedMouse(GLint x, GLint y) { GLint y1=SH-y; GLint size=5; // a dot is 4 by 4 pixels glRecti(x,y1,x+size,y1+size); glFlush(); } void Display(void) { glClear(GL_COLOR_BUFFER_BIT); } void myReshape(GLsizei W, GLsizei H) { if (R< W/H) glViewport(0,0,W,W/R); else glViewport(0,0,H*R,H); } void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480); // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow("Active Board"); // open the screen window glutDisplayFunc(Display); // register redraw function glutMotionFunc(myMovedMouse); glutMouseFunc(myMouse); glutReshapeFunc(myReshape); Init(); glutMainLoop(); // go into a perpetual loop } 1. 2. 3. 4. Write and Execute the code Try Right and Left mouse buttons Change line size and color Change the background color
Student Note:
void mouse(int btn, int state, int x, int y) { if (state == GLUT_DOWN) { if (btn == GLUT_LEFT_BUTTON) angle = angle + 1.0f; else if (btn == GLUT_RIGHT_BUTTON) angle = angle - 1.0f; else angle = 0.0f; } } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("Glut rotate"); glutMouseFunc(mouse); glutDisplayFunc(drawlines); glutIdleFunc(drawlines); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0); glRotatef(30.0, 1.0, 0.0, 0.0); glMatrixMode(GL_MODELVIEW); glClearColor(1.0, 1.0, 1.0, 0.0); glutMainLoop(); return(0); }
1. 2. 3. 4. 5.
Write and Execute the code Try different vectors Try different angles Change mouse buttons Change rotation angle
Student Note:
void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,460); // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow("CheckBoard"); // open the screen window glutDisplayFunc(Display); // register redraw function Init(); glutMainLoop(); // go into a perpetual loop }
6. 7. 8. 9.
Write and Execute the code Change the cells number Change the cells color Change window size and position
Student Note:
glVertex2i(85,125); glVertex2i(65,125); glEnd(); glFlush(); // process all OpenGL routines as quickly as possible } int main(int argc, char** argv) { glutInit(&argc,argv); // initialize GLUT glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB); //set display mode glutInitWindowSize(400,400); // set display window width and height glutInitWindowPosition(50,50); // set display window position glutCreateWindow("lines"); // create display window Init(); //execute initialization procedure glutDisplayFunc(display); //send graphics to display window glutMainLoop(); // display and wait return 0; } 1. 2. 3. 4. 5. Write and Execute the code Use different functions: Points, lines Change window size and position Change the background color Change the Primitives color
Student Note: