Content 1. Introduction 2. Contributors 3. Foreword 4. Introduction to OpenGL 5. Syllabus 6. Problems and Solutions fsmk.org 1 About Free Software Movement Karnataka (FSMK) Free Software Movement Karnataka (FSMK) is a nonprofit organization formed in March 2009 to spread the ideals of free software. We try to increase the understanding of the philosophy behind free software and encourage its use in educational institutions, our very own community center and other organisations. The phrase "free and open source software" can be used to collectively describe a set of operating systems and standalone applications that are free from the clutches of large corporate organisations which produce software only for commercial purposes. Free software is often freelyavailable and is created by a thriving community of programmers, designers and writers. The source code(i.e, the computer program) that underlies an application or an operating system is also open to the perusal of the common individual, which allows every curious tinkerer, student orotherwise, to play around with the source code.Most free software organisations welcome the general public to be part of their community and to contribute in any of the three spheres mentioned above. In the event of you not being a programmer, designer or writer, you can also become a member of the community by actively using free software applications, thereby reporting any issues that you notice, and also by spreading awareness about free software among your friends and family.At FSMK, we believe that it is unfortunate that schools, colleges and other small organisations that can use Linux and related free software for no cost, instead invest in using proprietary and commercial software, thereby spending large amounts annually on licensing and other fees, which can instead be used for the betterment of education or related services, and thereby, the betterment of society. I want to be involved Website: http://fsmk.org/ Mailing list: http://www.fsmk.org/?q=mailinglistsubscribe About Spoken Tutorial Project: The Spoken Tutorial project is an initiative of National Mission on Education through ICT, Government of India, to promote IT literacy through Free and Open Source Software. The project is being developed and coordinated by IIT-Bombay and led by Dr. Kannan M. Moudgalya. The project aims at building a repository of self learning courses through video tutorials of various open source softwares. These courses are then used to organize 2 hour workshops in government organizations, NGOs, SMEs and School and Colleges in India completely free of cost for the participants. These tutorials are not only available in English but also in various regional languages for the learner to be able to learn in the language he/she is comfortable in. Currently, Spoken Tutorials are available for free software tools like Blender, GIMP, Latex, Scilab, LibreOffice Suite, Ubuntu Linux, Mozilla Firefox, Thunderbird, MySQL and also programming languages and scripts like C, C++, Python, Ruby, Perl, PHP, Java. All the spoken tutorials which are released under Creative Commons License are available for download free of cost at their website http://spoken-tutorial.org. About Jnana Vikas Institute of Technology, Bidadi fsmk.org 2 Jnana Vikas Institute of Technology was established in the year 2001 by JNANA VIKAS VIDYA SANGHA with a mission to not just provide a solid educational foundation to students but to build their careers, to make them eminent personalities in the society and to make the industry doors open to them. It is approved by AICTE, New Delhi and affiliated to VTU, Belgaum. It has a residential campus with nearly 73 faculties, 28 technical and non-technical supporting staff, 27 administrative and supporting staff and 590 students and is a self-contained campus located in a beautiful green land of about 25 acres. The institute has four academic departments in various disciplines of engineering and three departments in general science with nearly 19 laboratories all together, organized in a unique pattern. There is a separate department for management discipline. The campus is located at Bidadi, in southern part of city of Bengaluru. More information about the college is provided at their website, http://www.jvitedu.in/ fsmk.org 3 Student. Bangalore Rahul Kondi. Government Engineering College.S Institute of Technology. Bangalore South Campus Kuna Sharathchandra. College of Engineering. Student. B.E. Student. Student.M.Dr.E. Vivekananda College of Engineering and Technology.E. Sir M. Hassan Samyama H. Puttur Nandan Hegde. Mandya Abdul Nooran..S..A.A. Student.S. P. Student. A. Bangalore South Campus Nawaf Abdul. Student.Student. Bangalore South Campus Suhas. Sir M. Faculty. Institute of Technology. College of Engineering. Mandya Chaitra Kulkarani. Jnana Vikas Institute of Technology. A. Mandya Arjun M. P. Hassan John Paul S.E. Bangalore Bhavya D. Student. Bangalore Shashank M C. Core member of FSMK Jeeva J. Student. Jain.S. College of Engineering. Institute of Technology..Y. Hassan Nagaraj. Bangalore South Campus Vikram S. Mangalore Vamsikrishna G. S. Student. A.. Mandya Bindu Madavi K P. P.. Core member of FSMK HariPrasad. The Oxford College of Engineering.T. Vivekananda College of Engineering and Technology. Faculty. Core member of FSMK fsmk..T. P. P. Vivekananda College of Engineering and Technology. Ambedkar Institute of Technology. Bangalore Soheb Mohammed. Government Engineering College. Student. Bangalore Rajesh N. College of Engineering. P.S. Bangalore Padmavathi K.M.S Institute of Technology.E. Student. Student.E. Sir M. Faculty. Faculty. College of Engineering.K..E. Student.. Government Engineering College. Student. Ambedkar Institute of Technology. Student. Bangalore Manu H. Bangalore South Campus Ajith K. Puttur Abhiram R.E.. B. Mandya Manas J. Student.Contributors Following is the list of all the volunteers who contributed to the making of the Lab Manual. The Oxford College of Engineering. Bidadi Aruna S. S. Institute of Technology.E. Institute of Technology.B. College of Engineering. The Oxford College of Engineering. P.T. College of Engineering.E.M. Student.Dr. Bangalore Nitin R. Core member of FSMK Jayakumar . P.S Institute of Technology.S. Visvesvaraya Institute of Technology. Ambedkar Institute of Technology. Student. Mysore Shwetha M K. Government Engineering College. Dr. B. Faculty. P. Institute of Technology. Faculty.S Institute of Technology..M. Visvesvaraya Institute of Technology. Faculty. Student.E.E College of Engineering. The Oxford College of Engineering. Student.S. P. Bangalore South Campus Indra Priyadarshini. Bangalore Samruda. Banglore Shashidhar S. Core member of FSMK Jickson. Mandya Meghana S.S. P.S Institute of Technology. Bangalore Akshay Gudi. Student.J. Visvesvaraya Institute of Technology. Bangalore South Campus Vivek Basavraj. College of Engineering.M. P. Jnana Vikas Institute of Technology.org 4 . Core member of FSMK Isham. Bidadi Karan Jain. Mysore Arun Chavan L. Bangalore Byregowda B K.E. Bangalore Poojitha Koneti. Bangalore Rohit G.E.S Institute of Technology. Student.E.S. The Oxford College of Engineering.S Institute of Technology. Banglore Deepika. Student. Student. Banglore Neeta Ann Jacob.... Student. College of Engineering.. Mangalore Nitesh A. P. Faculty. Student.J.M..S.B. Mandya Kiran B. Mysore Manjunatha H C. Student. Student. Puttur Narmada B.E College of Engineering. Faculty. Student. Student.M. P. Student.S. Hassan Santosh Kumar. P.E College of Engineering. Faculty. P. Faculty. Ananda Kumar H N.S. Faculty.S. Core member of FSMK Vijay Kulkarni.org 5 . Core member of FSMK Yajnesh. Core member of FSMK Prabodh C P. Core member of FSMK Shijil TV. Core member of FSMK RaghuRam N. Core member of FSMK Rameez Thonnakkal. Core member of FSMK fsmk. Core member of FSMK Sarath M S.Karthik Bhat. Core member of FSMK Vignesh Prabhu. Based on this ideology. Close to 40 participants attended it. Bidadi at their campus from 23 January. we decided to conduct a documentation workshop for college faculty members where they all could come together and help us produce this lab manual. we conducted a 3 day residential documentation workshop jointly organized with Jnana Vikas Institute of Technology. The videos themselves are released under Creative Commons license. We were very clear on the idea that this lab manual should be produced also from the students and faculty members of the colleges as they knew the right way to explain the problems to a large audience with varying level knowledge in the subject. It was attended by 16 faculty members of different colleges and 40 volunteers from FSMK. However our efforts were limited due to number of volunteers who could visit different colleges physically and explain what is Free Software and why colleges and students should use Free Software.A. But with more than 150engineering colleges in Karnataka. Hence we were looking for ways to reach out to colleges and students in a much larger way. Finally. We are also interested to extend the lab manual project to cover MCA departments and ECE departments and are looking for volunteers who can put the effort in this direction. Spoken Tutorials are very good learning material to learn about various Free Software tools and hence the videos are excellent companion to this Lab Manual. help faculty members to get access to good documentation on how to conduct various labs in Free Software and also help students by providing good and clear explanations of various lab programs specified by the university. mainly amongst engineering college faculty and students.org 6 . Head of Department. 2014. FSMK took their suggestions and decided to create a lab manual which will help colleges to migrate their labs to Free Software. Ganesh Aithal. distribute it to their students and there by helping us in spreading free software. the lab manual will go through constant improvement and we would like the faculty members and students to send us regular feedback on how we can improve the quality of the lab manual. Engineering College and Dr. Please contact us if you are interested to support us. Bangalore on 12 Jan. we are glad that efforts and support by close to 60 people for around 3 months has lead to creation of this Lab Manual. CSE. Principal. P.. we conducted two major Free Software camps which were attended by close to 160 students from 25 different colleges. As this was a first attempt for even FSMK. we still wanted to find more avenues to reach out to studentsand take the idea of free software in a much larger way to them. Based on this experience. an initiative by Government of India to promote IT literacy through Open Source software.print. fsmk. future is ours" is the motto with which Free Software Movement Karnataka has been spreading Free Software to all parts of society.Foreword "Free Software is the future. 2014.mainly our students from various colleges and we tried documenting various labs specified by VTU. so students can easily download them and share it with others. Jnana Vikas Institute of Technology during our various interactions with them individually. In the year 2013. FSMK promotes freedom of knowledge in all respects and hence we were also very clear that the development and release of this lab manual should under Creative Commons License so that colleges can adopt the manual and share. Swarnajyothi L. The documentation workshop was sponsored by Spoken Tutorial Project. However like any Free Software project.we decided to conduct a mock documentation workshop for one day at Indian Institute of Science. Lab Manual running on Free Software idea was initially suggested by Dr. We would highly recommend our students to go through the Spoken Tutorials while using this Lab Manual and web links to the respective spoken tutorials are shared within the lab manual also. x fsmk./output.org 7 .cpp -o output.x Execution: $.x For a C++ program Compilation: $g++ -lglut -lGL -lGLU filename./output.x Execution: $.c -o output. How to install OpenGL? Using terminal: To install OpenGL.OpenGL User Guide 1. install following packages: freeglut3 freeglut3-dev 2. Compilation and Execution For a C program Compilation: $gcc -lglut -lGL -lGLU filename. run the following command from a terminal prompt: $sudo apt-get install freeglut3 freeglut3-dev Using Ubuntu software Center: Using Ubuntu Software Center. 5. 7. Program to draw a color cube and spin it using OpenGL transformation matrices. Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. 8.org 8 . (Use appropriate data structures.Lab Programs list for Computer Graphics and Visualization Lab as specified by VTU for 6th Semester students: 1. Define suitably the position and properties of the light source along with the properties of the properties of the surfaces of the solid object used in the scene. Program to create a house like figure and rotate it about a given fixed point using OpenGL functions. Program to display a set of values {fij} as a rectangular mesh. window for clipping and view port for displaying the clipped image. 9. using OpenGL functions.) 10. 6. Project: 11. Program to implement Liang-Barsky line clipping algorithm. The number of recursive steps is to be specified by the user. Allow the user to specify the circle and the quadrilateral. Program to implement the Cohen-Sutherland line-clipping algorithm. 3. Program to fill any given polygon using scan-line area filling algorithm. Program to recursively subdivide a tetrahedron to from 3D Sierpinski gasket. Use OpenGL functions. Program. 2. 4. Develop a suitable Graphics package to implement the skills learnt in the theory and the exercises indicated in Part A. to draw a simple shaded scene consisting of a tea pot on a table. Program to create a cylinder and a parallelepiped by extruding a circle and quadrilateral respectively. Use the OpenGL. fsmk. Make provision to specify the input line. glVertex3fv(c).point b.1.0}. glVertex3fv(b).0.0.0.0.-0. Theory A geometric method of creating the gasket is to start with a triangle and cut out the middle piece as shown in the generator below. int n. {-0. Algorithm: 1.point c) { glBegin(GL_POLYGON). } void divide_triangle(point a.333333}.org 9 . point v[] = { {0.-0.471405.Aim: Program to recursively subdivide a tetrahedron to from 3D Sierpinski gasket.int m) { point v1. Start with a single triangle. The number of recursive steps is to be specified by the user.0}.942809. indefinitely. again with the corners in the centers of the sides of the triangles that point upwards 4. The nine resulting smaller triangles are cut in the same way. static GLfloat theta[] = {0.0. repeat Code: sierpanski.471405. In each of these 9.c #include<stdio. fsmk. draw again smaller upside down triangles. glVertex3fv(a).816497. glEnd().v3. Any triangular portion is an exact replica of the whole gasket The construction of the 3 dimensional version of the gasket follows similar rules for the 2D case except that the building blocks are square based pyramids instead of triangles.816497.v2.0.0.333333}.h> typedef float point[3]. and so on. draw 3 smaller triangles in each of the 3 triangles that are pointing upwards.0. 5. The gasket is perfectly self similar.{0. {0. This results in three smaller triangles to which the process is continued.333333} }. 2.-0. In the 27 triangles pointing upwards. Now. Now there are 9 triangles pointing upwards.point b. It's corners should be exactly in the centers of the sides of the large triangle 3. draw a smaller upside down triangle.h> #include<GL/glut. again draw 27 triangles pointing downwards. Inside this triangle.0.-0.-0. glNormal3fv(a). an attribute of many fractal images.point c. 6. void triangle(point a. 0. } } // tetrahedron has 4 faces.v[1]. void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT). glFlush().0).v1.0).0.0).m-1). tetrahedron(n).0.0.0. //repeat 'm' no of times as specified by user if(m>0) { for(j=0.j<3. void myReshape(int w. divide_triangle(v[0]. Here it draw a tetrahedron.0. each face is traingle.j<3.0.w.j++) // get midpoint of second edge v2[j]=(a[j]+c[j])/2. glLoadIdentity().c)). divide_triangle(v[0]. glMatrixMode(GL_PROJECTION).0.m).1.0.0.0.v2. if(w<=h) glOrtho(-2. divide_triangle(v[0].-2.j++) // get midpoint of first edge v1[j]=(a[j]+b[j])/2. for(j=0.v[2].v1.-10. glColor3f(0.b. } //this is called everytime the display is refreshed. divide_triangle(v[3].0.0.10.v[2].v[2].0. get exact aspect ratio fsmk.m). divide_triangle(b. divide_triangle(c.1.h).m).0*(GLfloat)h/(GLfloat)w.v3.j++) // get midpoint of third edge v3[j]=(b[j]+c[j])/2.j<3.org //to 10 .0*(GLfloat)h/(GLfloat)w.v[1].m-1).0. glColor3f(0.m). glColor3f(0.2. glLoadIdentity(). // consider midpoints as vertex and divide bigger triangle // to 3 parts recursively divide_triangle(a.v2.m-1).0.v[3]. } // This function is executed when the wiindow size is changed.v[1].0).int h) { glViewport(0.2.v3.0). we send each // face of tetrahedron and divide each faces/triangles into 3 // triangles recursively 'm' times void tetrahedron(int m) { glColor3f(1. for(j=0.0.v[3].int j. } //draw the sub divided traingles else { (triangle(a.0. -2.-10. glutMainLoop(). } int main(int argc. glMatrixMode(GL_MODELVIEW). compile as gcc -lGLU -lGL -lglut sierpanski. //window with a title glutReshapeFunc(myReshape).org 11 .0). glutDisplayFunc(display).10.c -o sierpanski If no errors.1. glEnable(GL_DEPTH_TEST). glutPostRedisplay(). glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH).1. of divisions \n").2.1. glutCreateWindow("3DGasket").2. run as .0./sierpanski fsmk. glClearColor(1.0.argv).500).&n).0. scanf("%d".0*(GLfloat)w/(GLfloat)h. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals.0.else glOrtho(-2. glutInitWindowSize(500. glutInit(&argc.0.char **argv) { printf("no.0.0*(GLfloat)w/(GLfloat)h.0). //viewport boundary #define true 1 #define false 0 int cliptest(double p. if(p<0.h> #include<GL/glut. if(t>*t2) return false.yvmin=200.org 12 . //line portion is outside } else if(p>0. any line or part thereof which is outside of the viewing area is removed.Aim: Program to implement Liang-Barsky line clipping algorithm.xvmax=300. With these intersections it knows which portion of the line should be drawn. Theory In computer graphics.double *t1.h> double xmin=50.yvmax=300.0) fsmk.double *t2) { double t=q/p. Code: liangBasky.0) { if(t>*t1) *t1=t.xmax=100. Typically.double q.ymin=50. The Liang-Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clipping window.c #include<stdio. //window boundary double xvmin=200.ymax=100. 'line clipping' is the process of removing lines or portions of lines outside of an area of interest. glColor3f(1. glVertex2f(xvmin.yvmax). double vx1=xvmin+(x1-xmin)*sx.0). glBegin(GL_LINES).&tc.0) { x0=x0+tc*dx.0.yvmin). y0=y0+tc*dy.double x1. if(t<*t1) return false.0. //line portion is outside } void liang(double x0. glBegin(GL_LINE_LOOP). } //sx and sy is used to scale the line. //draw a box to show clipped line. glVertex2d(vx1. double vy0=yvmin+(y0-ymin)*sy. double sy=(yvmax-yvmin)/(ymax-ymin). glVertex2d(x1. glBegin(GL_LINES). } } void display() { double x0=60.0) { x1=x0+t1*dx.0).&tc. y1=y0+t1*dy.y1=120.{ if(t<*t2) *t2=t.double y1) { double dx=x1-x0.vy0).dy=y1-y0.org 13 .y1).x1=80.0.0.t1=1.&t1)) //inside test wrt Bottom edge if(cliptest(dy.0. fsmk. return true. it zooms the the clipping window double sx=(xvmax-xvmin)/(xmax-xmin). //draw a line that needs to be clipped glVertex2d(x0.&tc.&tc. } else if(p==0.0) if(q<0. glColor3f(0.0.0.0. glVertex2f(xvmax.double y0.&t1)) //inside test wrt Top edge { if(t1<1.yvmin). double vx0=xvmin+(x0-xmin)*sx.0.1.vy1). if(cliptest(-dx.0) return false.ymax-y0. glVertex2f(xvmin.&t1)) //inside test wrt left edge if(cliptest(dx. double vy1=yvmin+(y1-ymin)*sy.&t1)) //inside test wrt Right edge if(cliptest(-dy.xmax-x0. glEnd().0.yvmax). //line coordinates glClear(GL_COLOR_BUFFER_BIT).y0).0. glVertex2f(xvmax. //draw line after clipping glVertex2d(vx0. } if(tc>0.x0-xmin.y0-ymin.0.0). //draw red coloured viewport glColor3f(1. glEnd().tc=0. glEnd().0.y0=20. glMatrixMode(GL_PROJECTION).499.c -o liang If no errors.0.char **argv) { glutInit(&argc. glVertex2f(xmax.ymax).0.0.500).499. glVertex2f(xmin.ymin). glEnd(). return 0.0.0. liang(x0.1. glutInitWindowPosition(0. glLoadIdentity(). glColor3f(1. glPointSize(1.0. glutDisplayFunc(display).1.y1). glutCreateWindow("Liang Barksy Line Clipping algorithm"). run as .y0.0.0. myinit().1.0).0. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals.0. //draw a box //function liang() is called by passing line endpoints } //initialses point sizes and colors void myinit() { glClearColor(1.0).0. glVertex2f(xmax. glutInitWindowSize(500. argv). compile as gcc -lGLU -lGL -lglut liangBasky. glutMainLoop().0.0.ymax).glColor3f(0.0).org 14 . } int main(int argc. gluOrtho2D(0.x1.0. glFlush(). glVertex2f(xmin.0).0).ymin). glBegin(GL_LINE_LOOP)./liang Screenshots:- fsmk.1. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB).0). org 15 .fsmk. {0. glNormal3fv(normals[a]).-1.0.0}.0. {-1. {1.0. glNormal3fv(normals[b]).0.int b.0}.0}. Code: spinCube. polygon(1.0. {1.1.-1.1.0. glNormal3fv(normals[c]).-1.0. glVertex3fv(vertices[b]).0}.0.0.-1.6.5).1.0. {1. polygon(0.0.0}.0. glVertex3fv(vertices[a]).0.1.0.0.0}.0.0.0}.1.0}.1.1.1. glColor3fv(colors[a]).4.0}.0}. polygon(4.0.0.0.0.0}}.org 16 .1). 3. glNormal3fv(normals[d]). 2.0.c #include<stdlib. {1. {-1.h> #include<GL/glut. {0.1.0.0.0.1. {1.0}. Choose eight 3 dimensional coordinate points such that will make a cube Select the axis to rotate.4).0.0.0}.0}.0}.0}. { -1.0}. void polygon(int a.5.3).1.6.-1.1. GLfloat colors [][3] = {{0.0.6). Algorithm 1.0. {1.0. glVertex3fv(vertices[d]).-1.1.1. } static GLfloat theta[]={0.0}.0.0. {0.0.-1.0. {-1.0. {1.1.0.0.-1.0.0. 4.5.-1.0.-1.1.1. glEnd().int d) { glBegin(GL_POLYGON). {1.-1.0.int c. static GLint axis=2.0}}.0. {1. polygon(2.1.0.1.1. Rotate the cube by a small angle every small interval of time.0.3.-1. GLfloat normals [][3] = {{-1.0.0. void display(void) fsmk.-1.0}}.1.-1.-1.0}.0.0}.0. polygon(0.0.0.7.1.0.-1.0. glColor3fv(colors[d]).7).-1.0}.7.2. {-1. glColor3fv(colors[c]).0.0.0. {1.1.1. glVertex3fv(vertices[c]). {1.3.-1.0.2.0}. glColor3fv(colors[b]).0. 1.h> GLfloat vertices [][3] = {{-1. Repeat 3rd step as long as axis is not changed. {1.0.Aim: Program to draw a color cube and spin it using OpenGL transformation matrices.0}. } void colorcube(void) { polygon(0. {1. glMatrixMode(GL_MODELVIEW). spincube().0.0.0*(GLfloat) w/(GLfloat) h. glRotatef(theta[1]. glutMainLoop(). glLoadIdentity(). 2.-2. } void spincube() { theta[axis]+=2. run as .0.0. glutIdleFunc(spincube).0). if(theta[axis]>360.int state. glutReshapeFunc(myReshape).0.2. } void myReshape(int w.org 17 .int x.0. -2. glEnable(GL_DEPTH_TEST)./spincube Screenshots:fsmk. 2. glutMouseFunc(mouse).0.0) theta[axis]-=360.0*(GLfloat) w/(GLfloat) h.int h) { glViewport(0.char **argv) { glutInit(&argc. glutDisplayFunc(display).1.0. if(w<=h) glOrtho(-2. } void mouse(int btn.0. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals.0*(GLfloat) h/(GLfloat) w.0. if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) axis=2.0. if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=1. -10.0. compile as gcc -lGLU -lGL -lglut spinCube.10. glLoadIdentity().0.0.0). } int main(int argc.0. glutInitWindowSize(500.int y) { if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) axis=0.-10.0.1.2.0.{ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT). glRotatef(theta[2]. glutPostRedisplay().0) . glFlush().0. colorcube(). glRotatef(theta[0].10.w. glutCreateWindow("Color Cube and Spin it! "). else glOrtho(-2. glMatrixMode(GL_PROJECTION).argv).0*(GLfloat) h/(GLfloat)w.0). glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH).0.500).h).0.0).c -o spincube If no errors. glutSwapBuffers().1. org 18 .fsmk. 200. Multiply rotation matrix with coordinate points of house. rot_mat[2][1]=0.0.100.n.1. 3.r. rot_mat[2][2]=1. rot_mat[1][2]=n. rot_mat[1][0]=sin(theta).0. GLfloat rot_mat[3][3]={{0}.1. void multiply() { int i.j<9.i++) for(j=0.1.0. 2. Draw a house by choosing appropriate coordinate points. rot_mat[2][0]=0.0.i<3.0.150.0. GLfloat result[3][9]={{0}.1.{0}}.100. {100.0}}.org 19 .200.1.0}. } } void rotate() { GLfloat m.l.l<3.0.1.150. rot_mat[0][2]=m.0.0.0.0.400.j.{0}}. GLfloat theta.0}. m=-h*(cos(theta)-1)+k*(sin(theta)).{0}.0.0.l++) result[i][j]=result[i][j]+rot_mat[i][l]*house[l][j]. n=-k*(cos(theta)-1)-h*(sin(theta)).300. GLfloat k=100.0.t rotation angle. Calculate the rotation matrix. for(l=0.100.175. which is calculate w. 4.0.j++) { result[i][j]=0. rot_mat[1][1]=cos(theta).0.0. 0.c #include<stdio.250.250.0. rot_mat[0][0]=cos(theta).0.0.0. for(i=0.h> GLfloat house[3][9]={{100. This gives us coordiante points of rotated house Code: rotateHouse.150.0.0.Aim Program to create a house like figure and rotate it about a given fixed point using OpenGL functions.100. GLfloat h=100.300.150. rot_mat[0][1]=-sin(theta).0.h> #include<GL/glut. } fsmk.1. {1. Algorithm 1.0.1.h> #include<math. multiply().{0}. 1. glLoadIdentity(). glEnd(). glVertex2f(result[0][3].0. glVertex2f(house[0][7].0. glBegin(GL_LINE_LOOP).result[1][7]). drawhouse().0. glVertex2f(house[0][5].0. glVertex2f(result[0][3]. glutInitWindowPosition(0. glBegin(GL_LINE_LOOP).0. glColor3f(1. glBegin(GL_LINE_LOOP).0.result[1][5]).result[1][4]). myinit(). glutInit(&argc. glBegin(GL_LINE_LOOP).house[1][7]).0. glEnd().1.0.499.0).house[1][6]).house[1][1]).0).1.0. glVertex2f(result[0][7].house[1][0]).0. glVertex2f(result[0][1].0.argv). glVertex2f(house[0][2]. fsmk.0).0. glEnd(). glVertex2f(house[0][6]. } void drawrotatedhouse() { glColor3f(0.1. glColor3f(1.house[1][2]).0.char **argv) { printf("Enter the rotation angle\n"). drawrotatedhouse(). glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB). glVertex2f(house[0][0].result[1][3]). glutDisplayFunc(display). glFlush().1.500).0.0).result[1][3]).0).//Draw Iniatial house void drawhouse() { glColor3f(0. glVertex2f(result[0][2]. glBegin(GL_LINE_LOOP). glVertex2f(result[0][1]. glVertex2f(result[0][4]. glEnd(). glVertex2f(result[0][5]. glVertex2f(house[0][1].org 20 .0). glColor3f(0.house[1][3]). glVertex2f(result[0][6]. } int main(int argc.0.0.0). glColor3f(1.house[1][3]).result[1][0]).result[1][1]).0.0). glColor3f(0.1. rotate(). glPointSize(10.0.0). scanf("%f".0. glVertex2f(result[0][0].1.0. glutInitWindowSize(500. glEnd().0). glVertex2f(house[0][3]. glMatrixMode(GL_PROJECTION).0). glVertex2f(house[0] [3].house[1][5]). } void myinit() { glClearColor(1.0.0.0.0. glVertex2f(house[0][8]. glEnd().0.0. glVertex2f(house[0][1]. } void display() { glClear(GL_COLOR_BUFFER_BIT).result[1][1]).house[1][4]). glutCreateWindow("house rotation").&theta).0.499. gluOrtho2D(0.result[1][6]).house[1][1]).house[1][8]). glVertex2f(house[0][4]. glVertex2f(result[0][8].0.result[1][8]).0. glBegin(GL_LINE_LOOP).result[1][2]).1.0. c -o rthouse If no errors. run as . compile as gcc -lGLU -lGL -lglut rotateHouse.glutMainLoop()./rthouse Screenshots:- fsmk. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals.org 21 . Iteratively clipped by testing trivial-acceptance or trivial-rejected.x2. the entire line lies inside the window. // viewport boundary double xvmin=200. 3. The algorithm quickly detects and dispenses with two common and trivial cases. Typically. and divided into two segments until completely inside or trivial-rejected.left.xvmax=300. Each bit in the outcode is set to either 1 (true) or 0 (false). to the left of left edge X < Xmin Theory In computer graphics. fsmk. On the other hand.y1. The Cohen�Sutherland algorithm is a computer graphics algorithm used for line clipping. Make provision to specify the input line.yvmax=300. End-points pairs are check for trivial acceptance or trivial rejected using the outcode. window for clipping and view port for displaying the clipped image. // bitcode for the right. 2. any line or part thereof which is outside of the viewing area is removed. to the right of right edge X > Xmax Bit 4 : outside halfplane of left edge.ymin=50. To perform trivial accept and reject tests. we extend the edges of the clip rectangle to divide the plane of the clip rectangle into nine regions.h> #include<GL/glut.ymax=100. It is trivially rejected and needs to be neither clipped nor displayed. If both endpoints of a line lie inside the window. above top edge Y > Ymax Bit 2 : outside halfplane of bottom edge. below bottom edge Y < Ymin Bit 3 : outside halfplane of right edge. we need to consider only its endpoints. // 1 is 0001 int x1.c #include<stdio. The algorithm divides a two-dimensional space into 9 regions (or a three-dimensional space into 27 regions). // 8 is 1000 const int LEFT=2. Algorithm: 1. 'line clipping' is the process of removing lines or portions of lines outside of an area of interest. It is trivially accepted and needs no clipping. Code: cohenSutherland. To clip a line. if both endpoints of a line lie entirely to one side of the window. If not trivial-accepance or trivial-rejected. 4.yvmin=200. and then efficiently determines the lines and portions of lines that are visible in the center region of interest (the viewport). the line must lie entirely outside of the window.Aim: Program to implement the Cohen-Sutherland line-clipping algorithm. the 4 bits in the code correspond to the following conditions: Bit 1 : outside halfplane of top edge. // 2 is 0010 const int TOP=4. Each region is assigned a 4-bit code deteermined by where the region lies with respect to the outside halfplanes of the clip-rectangle edges. // 4 is 0100 const int BOTTOM=1.xmax=100. divided into two segments at a clip edge.y2.h> // outcode is same as int #define outcode int // window boundary double xmin=50.top & bottom const int RIGHT=8.org 22 . pick it. y=ymin.double y1) { outcode outcode0.y.y1). //Atleast one endpoint is outside the clip rectangle. done=true.y0). } else { // failed both tests.double y0. } // point is below the clip rectangle else if(outcodeOut & BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0). outcodeOut=outcode0?outcode0:outcode1.double y). //Now find the intersection point //use formula y=y0+slope*(x-x0). x=xmax.done=false.y) to P1=(x1.Trivially reject & exit else if(outcode0 & outcode1) { done=true.ymax) void CohenSutherlandLineClipAndDraw(double x0. bool accept=false.// used to compute bitcode of a point outcode ComputeOutcode(double x.so calculate the line segment to clip // from an outside point to an intersection with clip edge double x. // Cohen-Sutherland clipping algo clips a line from // P0=(x0. } // point lies right of clipping rectangle else if(outcodeOut & RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0). y=ymax.ymin) to (xmax. } // point lies leftside of clipping rectangle else fsmk.outcode1.double x1. do { // logical or is 0 Trivially accept & exit if(!(outcode0|outcode1)) { accept=true. outcode1=ComputeOutcode(x1.y1) against a rectangle with // diagonal from (xmin. x=x0+(1/slope)*(y-y0) //point is above the rectangular clip if(outcodeOut & TOP) { x=x0+(x1-x0)*(ymax-y0)/(y1-y0).outcodeOut. // compute outcodes outcode0=ComputeOutcode(x0.org 23 . } // logical and is not 0. 0). if(accept) { // Window to viewport mappings // scaling parameters double sx=(xvmax-xvmin)/(xmax-xmin). if(y>ymax) { code|=TOP.y) using the clip rectangle // bounded diagonally by (xmin. glVertex2f(xvmax. glBegin(GL_LINE_LOOP).ymax) outcode ComputeOutcode(double x.yvmin). y0=y.0. glVertex2d(vx1.y0).0.0).org 24 . glVertex2d(vx0. vy1=yvmin+(y1-ymin)*sy. } else { x1=x. } } } while(!done). } fsmk. glVertex2f(xvmin.yvmax). double sy=(yvmax-yvmin)/(ymax-ymin).yvmax).0. double double double double vx0=xvmin+(x0-xmin)*sx.{ y=y0+(y1-y0)*(xmin-x0)/(x1-x0). // draw a red coloured viewport glColor3f(1.double y) { outcode code=0. } } // compute the bitcode for a point(x. vx1=xvmin+(x1-xmin)*sx.vy1). } // now we move outside point to intersection point to // clip and get ready for next pass if(outcodeOut==outcode0) { x0=x. glEnd(). glVertex2f(xvmax. outcode1=ComputeOutcode(x1.vy0).0. vy0=yvmin+(y0-ymin)*sy. x=xmin. glBegin(GL_LINES). // draws blue colour viewport glColor3f(0. outcode0=ComputeOutcode(x0.yvmin). glVertex2f(xvmin.y1). glEnd().1.ymin) and (xmax.0.0.0. y1=y. glVertex2d(x1.&x1.0).0. } return code. glPointSize(1.ymin).0). glEnd(). <!--ToDo Why are these two lines commented?--> //glVertex2d(x0.org 25 .ymax). glFlush(). glVertex2f(xmin.500).1.y1)").y2). glutInitWindowSize(500. fsmk. } void main(int argc. glutInit(&argc. glVertex2f(xmax.1.0.500. } else if(x<xmin) { code|=LEFT.y1=120.char **argv) { printf("Enter End Points:(x0.else if(y<ymin) { code|=BOTTOM. glLoadIdentity().1. glClear(GL_COLOR_BUFFER_BIT).y1).0. glVertex2f(xmin.x1=80.y0=20.0. CohenSutherlandLineClipAndDraw(x1. glMatrixMode(GL_PROJECTION). } void display() { // double x0=60.0.0). scanf("%d%d%d%d".0.&y2).500. } void myinit() { glClearColor(1.0.0. glColor3f(1. glBegin(GL_LINES).0.0.0.y1).0).0.0.0. //glVertex2d(x1.y2).0).ymin).0. // draw blue coloured window glColor3f(0.y0).y1.x1. glVertex2d(x2.&y1.1. glBegin(GL_LINE_LOOP). // draw the line with red colour glColor3f(1.ymax).y0. glEnd(). } if(x>xmax) { code|=RIGHT.0).0. glVertex2f(xmax.&x2.x2. gluOrtho2D(0.argv).0. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB).0. /cohen Screenshots:- fsmk. glutCreateWindow("Cohen Sutherland Line Clipping Algorithm").0). run as .c -o cohen If no errors.glutInitWindowPosition(0. myinit(). glutDisplayFunc(display). } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals. compile as gcc -lGLU -lGL -lglut cohenSutherland. glutMainLoop().org 26 . } ++x.cy). while(y > x) { plotpixels(h.0.-x+k).x+k). glEnd(). Algorithm: 1. draw_pixel(y+h.y+k).k.y).org 27 .x+k). y=r. } void Circle_draw(GLint h. GLint y) { draw_pixel(x+h. Code: //Cylinder and Parallelepiped by extruding Circle and Quadrilateral #include <GL/glut.h> void draw_pixel(GLint cx. Under Cylinder_Draw function. r=50. glBegin(GL_POINTS).0.h> #include <stdio. draw_pixel(-y+h.y). we call the Circle_draw function inside a for loop. else { d+=2*(x-y)+5. draw_pixel(-x+h. } void Cylinder_draw() { GLint xc=100. GLint r) // Midpoint Circle Drawing Algorithm { GLint d = 1-r. GLint x. } plotpixels(h. glVertex2i(cx. --y.-x+k).y+k).0. The parellopiped_draw module calls the parellopiped module inside a for loop which draws the parellelopiped onto the screen.x. draw_pixel(-y+h.0). 2. GLint k. x=0.x. The Circle_Draw function is a midpoint circle drawing algorithm which draws a circle by calling plotpixels method.Aim: Program to create a cylinder and a parallelepiped by extruding a circle and quadrilateral respectively. yc=100. fsmk.0. } void plotpixels(GLint h. The plotpixels method draws the cylinder by plotting multiple pixels. draw_pixel(x+h. draw_pixel(-x+h.-y+k). GLint cy) { glColor3f(1. GLint k.-y+k). 3. draw_pixel(y+h.k.h> #include <math. if(d < 0) d+=2*x+3. y4+i).// call parallelepiped glFlush().org 28 . int y2.x2+i. return 0. // Create Display Window init().GLint i.0. // Process all OpenGL routines as quickly as possible } int main(int argc. glEnd().i+=3) { Circle_draw(xc. glBegin(GL_LINE_LOOP). // Set circle color to red (R G B) glPointSize(2.r). // Clear Display Window glColor3f(1. glutDisplayFunc(display). // Set Display window width and height glutCreateWindow("Cylinder and parallelePiped Display by Extruding Circle and Quadrilaterl ").0.y4=175.1. glPointSize(2.0.i+=2) { parallelepiped(x1+i. int y4) { glColor3f(0.y4). } void parallelepiped_draw() { int x1=200.0.i<n.yc+i.y1=100.y3=100.0). int y3. Cylinder_draw().int y1.y1+i. for(i=0. // Set top left window position glutInitWindowSize(400./parallelopiped fsmk. glVertex2i(x2. // Initialize GLUT glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB).n=50.0).0.0). glVertex2i(x1. // Call cylinder parallelepiped_draw().x2=300.0. 0. run as . } } void parallelepiped(int x1.0. glVertex2i(x2.y1). char **argv) { glutInit(&argc.y2). 1.0.0.0.50).c -o parallelopiped If no errors.0.300. GLint i.y2=175.300).0). } void display(void) { glClear(GL_COLOR_BUFFER_BIT).0).int x2. // Set Display mode glutInitWindowPosition(50.0.400. for(i=0.y3). glVertex2i(x1.i<n.argv). // Send the graphics to Display Window glutMainLoop(). } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals. // Set display window color to white glMatrixMode(GL_PROJECTION).0).y3+i.n=40. // Set Projection parameters gluOrtho2D(0. } } void init(void) { glClearColor(1. compile as gcc -lGLU -lGL -lglut parallelopiped.0.y2+i.1.0. Screenshots:- fsmk.org 29 . legLen).0.0.0. } void table(double topWid. double dist=0. glTranslated(dist.dist).0.double len) { glPushMatrix(). glTranslated(0.topThick. glTranslated(0.5.0. } void displaySolid(void) fsmk.0. glPushMatrix(). glutSolidCube(1. tableLeg(legThick.org 30 . Algorithm: 1.0). tableLeg(legThick.0-legThick/2. glutSolidCube(1.double legLen) { glPushMatrix(). glScaled(1.len/2.2*dist).0).0.legLen). Use function glutSolidCube() to draw wall and table 2.legLen.1. glTranslated(-2*dist. using OpenGL functions.5). glutSolidCube(1. glTranslated(0.thick).0. glutSolidTeapot() is used to draw teapot Code: //teapot. glScaled(topWid.0. tableLeg(legThick. } void tableLeg(double thick. glPopMatrix().5*thickness. glTranslated(0.thickness.h> void wall(double thickness) { glPushMatrix().c #include<stdio. glPopMatrix().0).-2*dist).0).95*topWid/2.len. tableLeg(legThick.0). glPopMatrix().h> #include<GL/glut. glTranslated(0. glPopMatrix().topWid). to draw a simple shaded scene consisting of a tea pot on a table. Use the same function to draw 4 cubes and then scale it in downward direction to make it look like tablelegs 3. glScaled(thick.double topThick.-2*dist).legLen).legLen).0.0).Aim: Program. Define suitably the position and properties of the light source along with the properties of the properties of the surfaces of the solid object used in the scene.double legThick. glTranslated(0. glMaterialfv(GL_FRONT.0f.0. glOrtho(-winHt*64/48.0.0.0.1).0f.mat_ambient).char **argv) { glutInit(&argc.0.0.0.winHt*64/48.7f.3.GL_DIFFUSE.1. mat_shininess[]={50.0. glMaterialfv(GL_FRONT. table(0. glPushMatrix(). glMatrixMode(GL_MODELVIEW).02).0.0.6.08. wall(0.0.7f.0.0.0.argv).4).GL_SPECULAR. glFlush().1. GLfloat light_position[]={2. glPushMatrix().0.0f}.0).mat_shininess).1.0). glRotated(30.GL_DIFFUSE.0f}.6.0.7f.0).42. glLightfv(GL_LIGHT0.38. glRotated(-90. glPopMatrix(). glPopMatrix().mat_specular).0f}.0.0.GL_POSITION. GLfloat lightIntensity[]={0.5f.0f}.0).GL_AMBIENT.1.4. glMatrixMode(GL_PROJECTION).0f}. glTranslated(0.0.1.02.1.0.light_position). glPopMatrix().3).0f.0.02.mat_diffuse).0. //The glLightfv function returns light source parameter values.0. glLoadIdentity().5f.winHt.1. glLightfv(GL_LIGHT0.0.0.{ GLfloat GLfloat GLfloat GLfloat mat_ambient[]={0. glTranslated(0.7f.25.08). glScaled(0. double winHt=1.5f.5).08).0.0.org 31 .02).6). //draw second wall after rotating x axis by 90degre //draw floor } int main(int argc. glRotated(90.2.0f.4. glMaterialfv(GL_FRONT. wall(0. glPushMatrix().0f. mat_diffuse[]={0.6. mat_specular[]={1.4.0. glTranslated(0.0.-winHt.1. glMaterialfv(GL_FRONT.08.0.0.0.0.0. glPushMatrix().25.7f.0).0.0.0.02). fsmk.0.0f.GL_SHININESS.0. //The glMaterialfv function specifies material parameters for the lighting model.1. glutSolidTeapot(0.0.3. glPopMatrix(). glPopMatrix().7f}. glPopMatrix(). glPushMatrix(). glRotated(45.35). gluLookAt(2.0.0.0. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH).0.lightIntensity).100.0. wall(0.1.3.0.1. glLoadIdentity().0.0. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT).7f. glPushMatrix(). Acce pted values are GL_FLAT and GL_SMOOTH. glViewport(0. glShadeModel(GL_SMOOTH). glutCreateWindow("Simple shaded scene consisting of a teapot"). glutMainLoop(). glEnable(GL_NORMALIZE).480). glEnable(GL_LIGHTING). } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals. compile as gcc -lGLU -lGL -lglut teapot.c -o teapot If no errors.org 32 . glutDisplayFunc(displaySolid).glutInitWindowSize(640. run as . glEnable(GL_DEPTH_TEST).0).//Specifies a symbolic value representing a shading technique.1. glEnable(GL_LIGHT0).640.1.0.1. glutInitWindowPosition(100. glClearColor(0.480)./teapot Screenshots:- fsmk.100).0.0.0. static GLdouble viewer[]={0.0.0.1.1.0.0.0. {1.4. {1.0.0.0.-1.0}.1. {1. polygon(4.-1.0}.0. void polygon(int a. glNormal3fv(normals[d]).0.Aim: Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing.0.0.1.7.0. {-1.int d) { glBegin(GL_POLYGON).0. glEnd(). } void colorcube() { polygon(0.1.0.0.1).0}. GLfloat normals[][3]={{-1.0}. {1. {1.0.1.1. {1. glVertex3fv(vertices[b]).0.0}.0}.1.1.-1.1.0}.int c. glNormal3fv(normals[b]). {1.1.0.-1. {1.0.0.0 }. polygon(1. {0.0. glColor3fv(colors[c]).0. {1.0}. polygon(2.3).0. glNormal3fv(normals[c]).5. {0.0}}.-1.0.0.h> #include<GL/glut.0}}. {-1. glVertex3fv(vertices[a]).h> GLfloat vertices[][3] = {{-1.0.0. {-1.6.1.-1.0.0. glColor3fv(colors[b]).-1. static GLint axis=2. glColor3fv(colors[d]).5.0}. {-1.0.0.0.0}.0.0. {1. {1.0.-1.0.0. polygon(0. polygon(0.0.org 33 .0}.0.0. } static GLfloat theta[]={0.2.0.0.0.-1.-1.1.1.1. Choose appropriate coordinates to make a cube 2.5.0.0.0.1.-1. glColor3fv(colors[a]).-1.0}.0.7.4). fsmk.0.0.2.0}.-1.1.c #include<stdlib.1.7). glNormal3fv(normals[a]). glVertex3fv(vertices[d]). glVertex3fv(vertices[c]). Repeat Code moveCube.-1.0.0. {1.1.1.0}.0}.0.0.0}. Algorithm 1.0.0}}.0}.0}.0}.0.0}.0}.int b.0. {0.0 .-1.1.1.0.1.0.1. Rotate or move the cube by a small variation on user input 3.0.-1.5). GLfloat colors[][3] = {{0.0.0}.0. Use OpenGL functions.3. {1.1.3.6).6. {-1. 0.-2. if(key=='z')viewer[2]-=1. if(key=='Y')viewer[1]+=1.0.0. glLoadIdentity().0.0) .argv).0. 2.10.viewer[2]. glRotatef(theta[1]. glutDisplayFunc(display).500).0. glutSwapBuffers().int x.void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT).0*((GLfloat)h/(GLfloat)w). glutMouseFunc(mouse).0). glutReshapeFunc(myReshape). else glOrtho(-2.2. glLoadIdentity(). 2. glRotatef(theta[2].0.0. if(key=='Z')viewer[2]+=1.0*((GLfloat)w)/((GLfloat)h). gluLookAt(viewer[0].char **argv) { glutInit(&argc.0.viewer[1].1.0*((GLfloat)w/(GLfloat)h).10.0.0 ).0.0.0. colorcube().0.0.0. } void mouse(int btn. glMatrixMode(GL_PROJECTION). if(theta[axis]>360.0.0.-10.0.0*((GLfloat)h/(GLfloat)w).0) theta[axis]=-360.1. display().0).0.0. -2.0.0. if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) axis=2.0.0. } void keys(unsigned char key.0. fsmk.0. glMatrixMode(GL_MODELVIEW). glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH). if(key=='y')viewer[1]-=1. glutInitWindowSize(500.0.0.0.0).0.int y) { if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) axis=0. theta[axis]+=2.0.int state.0.1.1.2.-10. if(w<=h) glOrtho(-2. glRotatef(theta[0].int y) { if(key=='x')viewer[0]-=1.0.org 34 . glutCreateWindow("colorcube viewer").int x.int h) { glViewport(0.0. glFlush(). glutPostRedisplay(). if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=1. } void myReshape(int w.0.h).0. if(key=='X')viewer[0]+=1.w.0). } int main(int argc. c -o movecube If no errors. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals. glEnable(GL_DEPTH_TEST).org 35 . glutMainLoop().glutKeyboardFunc(keys)./movecube Screenshots:- fsmk. run as . compile as gcc -lGLU -lGL -lglut moveCube. float y1.float y2.x. glBegin(GL_POINTS).y1. int i.h> #include<stdio. } if((y2-y1)!=0) mx=(x2-x1)/(y2-y1).y4.i++) { if(x<(float)le[i]) le[i]=(int)x.float x2.org 36 . temp=x1.c #define BLACK 0 #include<stdlib.x4.float x3.x1=x2.h> float x1.float y3.y2. glVertex2i(x.0.x2=temp.0. int k=0.h> #include<GL/glut. x=x1. Code : //scanFill. else mx=x2-x1.temp. void edgedetect(float x1.Aim: Program to fill any given polygon using scan-line area filling algorithm.float y4) { fsmk. x+=mx.y1=y2.x3.float x4. Find the intersections of the scan line with all edges of the polygon.0.float y2. Algorithm: For each scan line 1.float y1. if((y2-y1)<0) { temp=y1.x2. glEnd().int value) { glColor3f(1.y3.float x2.int *re) { float mx. for(i=y1. } void scanfill(float x1.y2=temp. Fill in all pixels between pairs of intersections. 3. Sort the intersections by increasing x-coordinate.i<=y2.y).int y. } } void draw_pixel(int x.0).0. 2.int *le. if(x>(float)re[i]) re[i]=(int)x. 0.0.x3. edgedetect(x3. glVertex2f(x3.x3.0).y3.1.y1). fsmk. //le[i]=20.1.y4). scanfill(x1.y1=200.int le[500].y4=300.0).x2.0. glVertex2f(x4. int i.re).le.i<(int)re[j].y2=300.y2.x1. glVertex2f(x2.i<500.0.0.re).y4). } edgedetect(x1.le. glPointSize(1.org 37 .y3). gluOrtho2D(0.y2. myinit().0.y2.0).0. glutCreateWindow("Filling a polygon using scan_fill algorithm").j=j+1) { if(le[j]<=re[j]) for(i=(int)le[j]. int y) { if(key=='k') k++.y3. glutKeyboardFunc(mykey).0.j. glMatrixMode(GL_PROJECTION).x4=300. for(j=0. glutInitWindowSize(500. glutDisplayFunc(display).0.499.0). glColor3f(0.le.x3=200.0.re[500]. } void myinit() { glClearColor(1.0).i=i+1) draw_pixel(i. glVertex2f(x1.0. } void mykey(unsigned char key.char **argv) { glutInit(&argc.0.x4.y1.0.y4. glutInitWindowPosition(0.y3.500).499. re[i]=0. glBegin(GL_LINE_LOOP). } } void display() { x1=200. } int main(int argc.y1.1. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB).0.0. edgedetect(x2.x2. glFlush().y4.x4. int x.argv).x2=100. for(i=0. glClear(GL_COLOR_BUFFER_BIT).le. glEnd().0.0.0.0. glColor3f(1. edgedetect(x4.0.i++) { le[i]=500.y1.0.j<500.BLACK).j.0.re).0).1.y3=400.re).y2). glLoadIdentity(). c -o scanfill If no errors.glutMainLoop().org 38 . } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals. run as ./scanfill. Screenshots:- fsmk. compile as gcc -lGLU -lGL -lglut scanfill. 0.0. In the display function . GLfloat x0=50.y[maxy]={0. glPointSize(5.0. GLint i.y[j+1]).0. 5. Code: rectMesh.0. void init() { glClearColor(1. glBegin(GL_LINE_LOOP).i<maxx.1. for(j=0.0. glColor3f(1.0. for(i=0.0.i++) x[i]=x0+i*dx. for(i=0.org 39 . glVertex2f(x[i+1].0.1.0.1.y[j+1]).0. } void display(void) { glClear(GL_COLOR_BUFFER_BIT). glutPostRedisplay().0.h> #include<stdlib. we call glLoadIdentity() to start over from the origin.0.y[j]).j++) { glColor3f(0. glMatrixMode(GL_PROJECTION).0. Define two variables maxx and maxy for the sides of the rectangular mesh.j.Aim: Program to display a set of values {fij} as a rectangular mesh. fsmk. 4.0).y[j]).0.499.j<maxy-1.j++) y[j]=y0+j*dy.0. 3.y0=50.1. glVertex2f(x[i]. glVertex2f(x[i+1]. Display the rectangular mesh by passing GL_LINE_LOOP to glBegin as a parameter. glVertex2f(x[i]. gluOrtho2D(0.499. glEnd(). //glColor3f(1.0).0).h> #define maxx 20 #define maxy 25 #define dx 10 #define dy 15 GLfloat x[maxx]={0.i<maxx-1.0}.j<maxy.h> #include<GL/glut.0.0).1. Under init() . Define dx. // glColor3f(0.0).y[j]) positions.0.0.c #include<stdio.0).dy and also define two arrays x and y.0. glLoadIdentity(). 2.0. Algorithm : 1. you run for loops that update the arrays with (x[i].0).0}.i++) for(j=0. 400). glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)./mesh Screenshots:- fsmk. init(). } glFlush().0). glutDisplayFunc(display).glFlush(). glutInitWindowSize(500.org 40 . } int main(int argc. glutCreateWindow("Rectangular Mesh"). compile as gcc -lGLU -lGL -lglut rectMesh.c -o mesh If no errors. glutMainLoop(). run as .argv). glutInitWindowPosition(0. } Output: Commands for execution:Open a terminal and Change directory to the file location in both the terminals.char **argv) { glutInit(&argc.