├── 01. opengl_intro.cpp ├── 02. Flag_design.cpp ├── 03. DDA.cpp ├── 04. modified DDA.txt ├── 05.Bresenham's(Modified).cpp ├── 06. Concentric circle using midpoint algo.cpp ├── 07. Rotating ellipse.cpp ├── 08. globe.cpp ├── 09. tranformation.cpp ├── 10. reflection about an arbritrary line.cpp ├── 11. composite transformation.txt ├── 12. bezier curve.txt ├── 13. cubic bezier curve.txt ├── 14. orthographic projection.txt ├── 15. Blending Function.txt ├── 16. Cubic Bezier_taylor's.txt ├── 17. Hermite Curve.txt ├── 18 Plane Curve.txt ├── 19. Space curve.txt ├── 20. Flag using curves.txt ├── 21. DXBall2.cpp ├── 22. Flag using Bezier Surface.txt ├── README.md ├── test.cpp └── test2.txt /01. opengl_intro.cpp: -------------------------------------------------------------------------------- 1 | //Jeff Chastine 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | void changeViewPort(int w, int h) 10 | { 11 | glViewport(0, 0, w, h); 12 | } 13 | 14 | void render() 15 | { 16 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 17 | glutSwapBuffers(); 18 | } 19 | 20 | 21 | 22 | int main(int argc, char* argv[]) { 23 | 24 | // Initialize GLUT 25 | glutInit(&argc, argv); 26 | // Set up some memory buffers for our display 27 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 28 | // Set the window size 29 | glutInitWindowSize(800, 600); 30 | // Create the window with the title "Hello,GL" 31 | glutCreateWindow("Hello, GL"); 32 | // Bind the two functions (above) to respond when necessary 33 | glutReshapeFunc(changeViewPort); 34 | glutDisplayFunc(render); 35 | 36 | // Very important! This initializes the entry points in the OpenGL driver so we can 37 | // call all the functions in the API. 38 | GLenum err = glewInit(); 39 | if (GLEW_OK != err) { 40 | fprintf(stderr, "GLEW error"); 41 | return 1; 42 | } 43 | 44 | 45 | glutMainLoop(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /02. Flag_design.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define HEIGHT 600 8 | #define WIDTH 600 9 | 10 | unsigned char matrix[HEIGHT][WIDTH][3]; // r,g,b 11 | double offset=0; 12 | 13 | void init() 14 | { 15 | int i,j; 16 | //stick 17 | for(i=50;i<500;i++) 18 | for(j=25;j<50;j++) 19 | { 20 | matrix[i][j][0] = 139; // red 21 | matrix[i][j][1] = 69; // green 22 | matrix[i][j][2] = 19; // blue 23 | } 24 | 25 | 26 | for(i=350;i<400;i++) 27 | for(j=50;j<275;j++) // 2:3 ratio flag 28 | { 29 | matrix[i][j][0] = 0; // red 30 | matrix[i][j][1] = 255; // green 31 | matrix[i][j][2] = 0; // blue 32 | } 33 | 34 | for(i=400;i<450;i++) 35 | for(j=50;j<275;j++) 36 | { 37 | matrix[i][j][0] = 255; // red 38 | matrix[i][j][1] = 255; // green 39 | matrix[i][j][2] = 255; // blue 40 | } 41 | 42 | for(i=450;i<500;i++) 43 | for(j=50;j<275;j++) 44 | { 45 | matrix[i][j][0] = 255; // red 46 | matrix[i][j][1] = 153; // green 47 | matrix[i][j][2] = 51; // blue 48 | } 49 | 50 | 51 | glClearColor(0,0,0.4,0); 52 | glOrtho(-1,1,-1,1,-1,1); 53 | } 54 | void DrawCircle(float cx, float cy, float r, int num_segments) 55 | { 56 | glBegin(GL_LINE_LOOP); 57 | glColor3d(0,0,255); 58 | for(int ii = 0; ii < num_segments; ii++) 59 | { 60 | float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle 61 | 62 | float x = r * cosf(theta);//calculate the x component 63 | float y = r * sinf(theta);//calculate the y component 64 | 65 | glVertex2f(x + cx, y + cy);//output vertex 66 | 67 | } 68 | glEnd(); 69 | 70 | } 71 | void DrawLines(float cx, float cy, float r, int num_segments) 72 | { 73 | glBegin(GL_LINE_LOOP); 74 | glColor3d(0,0,255); 75 | for(int ii = 0; ii < num_segments; ii++) 76 | { 77 | float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle 78 | 79 | float x = r * cosf(theta);//calculate the x component 80 | float y = r * sinf(theta);//calculate the y component 81 | 82 | glVertex2f(x + cx, y + cy);//output vertex 83 | 84 | glBegin(GL_LINES); 85 | glVertex2f(-0.48, 0.415); 86 | } 87 | glEnd(); 88 | 89 | } 90 | void display() 91 | { 92 | glClear(GL_COLOR_BUFFER_BIT); 93 | glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,matrix); 94 | 95 | 96 | DrawCircle(-0.48,0.415,0.07,200); 97 | DrawLines(-0.48,0.415,0.07,24); 98 | glutSwapBuffers(); 99 | 100 | 101 | } 102 | 103 | void idle() 104 | { 105 | static float count = 0.0; 106 | 107 | int i,j; 108 | count += 0.05; 109 | offset-=0.14; 110 | 111 | for(i=350;i<400;i++) 112 | for(j=50;j<275;j++) 113 | { 114 | matrix[i][j][0] = 0; // red 115 | matrix[i][j][1] = 230 + 25 * sin(count + j * 0.02); // green 116 | matrix[i][j][2] = 0; // blue 117 | } 118 | 119 | for(i=400;i<450;i++) 120 | for(j=50;j<275;j++) 121 | { 122 | matrix[i][j][0] = 230 + 25 * sin(count + j * 0.02); // red 123 | matrix[i][j][1] = 230 + 25 * sin(count + j * 0.02); // green 124 | matrix[i][j][2] = 230 + 25 * sin(count + j * 0.02); // blue 125 | } 126 | 127 | for(i=450;i<500;i++) 128 | for(j=50;j<275;j++) 129 | { 130 | matrix[i][j][0] = 230 + 25 * sin(count + j * 0.02); // red 131 | matrix[i][j][1] = 128 + 25 * sin(count + j * 0.02); // green 132 | matrix[i][j][2] = 26 + 25 * sin(count + j * 0.02); // blue 133 | } 134 | 135 | glutPostRedisplay(); 136 | } 137 | 138 | void main( int argc, char* argv[]) 139 | { 140 | glutInit(&argc,argv); 141 | glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); 142 | glutInitWindowSize(WIDTH,HEIGHT); 143 | glutInitWindowPosition(100,100); 144 | glutCreateWindow("FLAG"); 145 | glutDisplayFunc(display); 146 | 147 | glutIdleFunc(idle); 148 | init(); 149 | 150 | glutMainLoop(); 151 | } -------------------------------------------------------------------------------- /03. DDA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define ROUND(x) ((int)(x+0.5)) 8 | int xa,xb,ya,yb; 9 | void display (void) 10 | { 11 | int dx=xb-xa,dy=yb-ya,steps,k; 12 | float xIncrement,yIncrement,x=xa,y=ya; 13 | glClear (GL_COLOR_BUFFER_BIT); 14 | glColor3f (1.0, 0.0, 0.0); 15 | if(abs(dx)>abs(dy)) 16 | steps=abs(dx); 17 | else steps=abs(dy); 18 | xIncrement=dx/(float)steps; 19 | yIncrement=dy/(float)steps; 20 | glBegin(GL_POINTS); 21 | glVertex2s(ROUND(x),ROUND(y)); 22 | for(k=0;k 3 | #include 4 | #include 5 | using namespace std; 6 | double X1, Y1, X2, Y2; 7 | 8 | float round_value(float v) 9 | { 10 | return floor(v + 0.5); 11 | } 12 | void LineDDA(void) 13 | { 14 | double dx=(X2-X1); 15 | double dy=(Y2-Y1); 16 | double steps; 17 | float xInc,yInc,x=X1,y=Y1; 18 | /* Find out whether to increment x or y */ 19 | steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy)); 20 | xInc=dx/(float)steps; 21 | yInc=dy/(float)steps; 22 | 23 | 24 | glClear(GL_COLOR_BUFFER_BIT); 25 | 26 | 27 | glBegin(GL_POINTS); 28 | glColor3f(1.0,1.0,1.0); 29 | glVertex2d(x,y); 30 | int k; 31 | /*for(int i=0;i<=625;i++){ 32 | for(int j=0;j<=625;j++){ 33 | float error1=0.0; 34 | float error2=0.0; 35 | float error3=0.0; 36 | for(int m=i;m<=i+2;m++){ 37 | for(int n=j;n<=j+2;n++){ 38 | //unsigned char color[3]; 39 | /*glReadPixels(k,l,1,1, GL_RGB , GL_UNSIGNED_BYTE , color); 40 | error1+=color[0]/255.0; 41 | error2+=color[1]/255.0; 42 | error3+=color[2]/255.0;*/ 43 | /* x=X1; 44 | y=Y1; 45 | for(k=0;k 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | int x1, b, x2, y2; 10 | 11 | void myInit() 12 | { 13 | glClear(GL_COLOR_BUFFER_BIT); 14 | glClearColor(1.0, 0.0, 0.0, 0.0); 15 | glMatrixMode(GL_PROJECTION); 16 | gluOrtho2D(0, 500, 0, 500); 17 | } 18 | 19 | void draw_pixel(int x, int y) 20 | { 21 | glBegin(GL_POINTS); 22 | glColor3f (1.0, 1.0, 1.0); 23 | glVertex2i(x, y); 24 | glEnd(); 25 | } 26 | void draw_gray(int x, int y) 27 | { 28 | glBegin(GL_POINTS); 29 | glColor3f (0.5, 0.5, 0.5); 30 | glVertex2i(x, y); 31 | glEnd(); 32 | } 33 | void draw_line(int x1, int x2, int b, int y2) 34 | { 35 | int dx, dy, i, e; 36 | int incx, incy, inc1, inc2; 37 | int x,y; 38 | 39 | dx = x2-x1; 40 | dy = y2-b; 41 | 42 | if (dx < 0) dx = -dx; 43 | if (dy < 0) dy = -dy; 44 | incx = 1; 45 | if (x2 < x1) 46 | incx = -1; 47 | incy = 1; 48 | if (y2 < b) 49 | incy = -1; 50 | x = x1; 51 | y = b; 52 | if (dx > dy) 53 | { 54 | draw_pixel(x, y); 55 | e = 2 * dy-dx; 56 | inc1 = 2*(dy-dx); 57 | inc2 = 2*dy; 58 | for (i=0; i= 0) 61 | { 62 | y += incy; 63 | draw_gray(x,y); 64 | e += inc1; 65 | x += incx; 66 | } 67 | else 68 | {e += inc2;x += incx; 69 | draw_gray(x,y-incy);} 70 | draw_pixel(x, y); 71 | } 72 | } 73 | else 74 | { 75 | draw_pixel(x, y); 76 | e = 2*dx-dy; 77 | inc1 = 2*(dx-dy); 78 | inc2 = 2*dx; 79 | for (i=0; i= 0) 82 | { 83 | x += incx; 84 | draw_gray(x,y); 85 | y += incy; 86 | e += inc1; 87 | 88 | } 89 | else 90 | {e += inc2; y += incy; 91 | draw_gray(x+incx,y);} 92 | 93 | draw_pixel(x, y); 94 | } 95 | } 96 | } 97 | 98 | void myDisplay() 99 | { 100 | //draw_line(0,500,250,250); 101 | //draw_line(250,250,0,500); 102 | draw_line(x1, x2, b, y2); 103 | glFlush(); 104 | } 105 | 106 | void main(int argc, char **argv) 107 | { 108 | 109 | printf( "Enter (x1, b, x2, y2)\n"); 110 | scanf("%d %d %d %d", &x1, &b, &x2, &y2); 111 | 112 | glutInit(&argc, argv); 113 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 114 | glutInitWindowSize(500, 500); 115 | glutInitWindowPosition(0, 0); 116 | glutCreateWindow("Bresenham's Line Drawing"); 117 | myInit(); 118 | glutDisplayFunc(myDisplay); 119 | glutMainLoop(); 120 | } -------------------------------------------------------------------------------- /06. Concentric circle using midpoint algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int pntX1, pntY1, r1,r2,r3; 7 | int pntX2, pntY2; 8 | int pntX3, pntY3; 9 | int flag =1; 10 | 11 | void plot(int x, int y) 12 | { 13 | glBegin(GL_POINTS); 14 | glVertex2i(x+pntX1, y+pntY1); 15 | glEnd(); 16 | } 17 | 18 | void plot2(int x, int y) 19 | { 20 | glBegin(GL_POINTS); 21 | glVertex2i(x+pntX1, y+pntY1); 22 | glEnd(); 23 | } 24 | 25 | void plot3(int x, int y) 26 | { 27 | glBegin(GL_POINTS); 28 | glVertex2i(x+pntX1, y+pntY1); 29 | glEnd(); 30 | } 31 | 32 | void plot_off(int x, int y) 33 | { 34 | glBegin(GL_POINTS); 35 | glClear (GL_COLOR_BUFFER_BIT); 36 | glColor3f (1.0, 1.0, 1.0); 37 | glVertex2i(x+pntX1, y+pntY1); 38 | glEnd(); 39 | } 40 | 41 | 42 | void myInit (void) 43 | { 44 | glClearColor(1.0, 1.0, 1.0, 0.0); 45 | glColor3f(0.0f, 0.0f, 0.0f); 46 | glPointSize(4.0); 47 | glMatrixMode(GL_PROJECTION); 48 | glLoadIdentity(); 49 | gluOrtho2D(0.0, 640.0, 0.0, 480.0); 50 | } 51 | 52 | 53 | void midPointCircleAlgo() 54 | { 55 | int x = 0; 56 | int y = r1; 57 | float p = 5/4 - r1; 58 | plot(x, y); 59 | 60 | while (y > x) 61 | { 62 | if (p < 0) 63 | { 64 | x++; 65 | p += 2*x+1; 66 | } 67 | else 68 | { 69 | y--; 70 | x++; 71 | p += 2*(x-y)+1; 72 | } 73 | plot(x, y); 74 | plot(x, -y); 75 | plot(-x, y); 76 | plot(-x, -y); 77 | plot(y, x); 78 | plot(-y, x); 79 | plot(y, -x); 80 | plot(-y, -x); 81 | } 82 | 83 | } 84 | void midPointCircleAlgo2() 85 | { 86 | int x = 0; 87 | int y = r2; 88 | float p = 5/4 - r2; 89 | plot2(x, y); 90 | 91 | while (y > x) 92 | { 93 | if (p < 0) 94 | { 95 | x++; 96 | p += 2*x+1; 97 | } 98 | else 99 | { 100 | y--; 101 | x++; 102 | p += 2*(x-y)+1; 103 | } 104 | plot2(x, y); 105 | plot2(x, -y); 106 | plot2(-x, y); 107 | plot2(-x, -y); 108 | plot2(y, x); 109 | plot2(-y, x); 110 | plot2(y, -x); 111 | plot2(-y, -x); 112 | } 113 | 114 | } 115 | void midPointCircleAlgo3() 116 | { 117 | int x = 0; 118 | int y = r3; 119 | float p = 5/4 - r3; 120 | plot3(x, y); 121 | 122 | while (y > x) 123 | { 124 | if (p < 0) 125 | { 126 | x++; 127 | p += 2*x+1; 128 | } 129 | else 130 | { 131 | y--; 132 | x++; 133 | p += 2*(x-y)+1; 134 | } 135 | plot3(x, y); 136 | plot3(x, -y); 137 | plot3(-x, y); 138 | plot3(-x, -y); 139 | plot3(y, x); 140 | plot3(-y, x); 141 | plot3(y, -x); 142 | plot3(-y, -x); 143 | } 144 | 145 | } 146 | 147 | void midPointCircleAlgo_off() 148 | { 149 | int x = 0; 150 | int y = r1; 151 | float p = 5/4 - r1; 152 | plot_off(x, y); 153 | 154 | while (y > x) 155 | { 156 | if (p < 0) 157 | { 158 | x++; 159 | p += 2*x+1; 160 | } 161 | else 162 | { 163 | y--; 164 | x++; 165 | p += 2*(x-y)+1; 166 | } 167 | plot_off(x, y); 168 | plot_off(x, -y); 169 | plot_off(-x, y); 170 | plot_off(-x, -y); 171 | plot_off(y, x); 172 | plot_off(-y, x); 173 | plot_off(y, -x); 174 | plot_off(-y, -x); 175 | } 176 | 177 | } 178 | 179 | void myDisplay(void) 180 | { 181 | glClear (GL_COLOR_BUFFER_BIT); 182 | glColor3f (0.0, 0.0, 0.0); 183 | glPointSize(1.0); 184 | 185 | midPointCircleAlgo(); 186 | midPointCircleAlgo2(); 187 | midPointCircleAlgo3(); 188 | glFlush (); 189 | while(1) 190 | { 191 | 192 | int temp = r2; 193 | r2 = r3; 194 | cout << "\nEnter new radius:\n\n" << endl; 195 | r3=r3+1; 196 | flag++; 197 | 198 | 199 | midPointCircleAlgo_off(); 200 | printf("\nOld circle removed"); 201 | glColor3f (0.0, 0.0, 0.0); 202 | glPointSize(1.0); 203 | midPointCircleAlgo3(); 204 | printf("\nNew circle drawn"); 205 | Sleep(200); 206 | r1 = temp; 207 | //glFlush (); 208 | glutSwapBuffers(); 209 | 210 | } 211 | glFlush(); 212 | } 213 | 214 | void main(int argc, char** argv) 215 | { 216 | cout << "Enter the coordinates of the center:\n\n" << endl; 217 | 218 | 219 | cout << "Enter X-coordinate : "; cin >> pntX1; 220 | cout << "Enter Y-coordinate : "; cin >> pntY1; 221 | cout << "\nEnter 1st radius : "; cin >> r1; 222 | cout << "\nEnter 2nd radius : "; cin >> r2; 223 | cout << "\nEnter 3rd radius : "; cin >> r3; 224 | 225 | 226 | 227 | glutInit(&argc, argv); 228 | glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 229 | glutInitWindowSize (640, 480); 230 | glutInitWindowPosition (100, 150); 231 | glutCreateWindow ("Line Drawing Alogrithms"); 232 | glutDisplayFunc(myDisplay); 233 | 234 | myInit (); 235 | glutMainLoop(); 236 | 237 | } -------------------------------------------------------------------------------- /07. Rotating ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int h,k; 7 | float a,b; 8 | 9 | void drawDot (GLint x, GLint y, GLfloat r, GLfloat g, GLfloat b) 10 | { glColor3f(r,g,b); 11 | glPointSize(1.0); 12 | glBegin (GL_POINTS); 13 | glVertex2i (x,y); 14 | glEnd(); 15 | } 16 | 17 | void symmetricPixels (int x, int y, int xc, int yc, float r, float g, float b) 18 | { drawDot (xc + x, yc + y, r,g,b); 19 | drawDot (xc - x, yc + y,r,g,b); 20 | drawDot (xc + x, yc - y,r,g,b); 21 | drawDot (xc - x, yc - y,r,g,b); 22 | } 23 | 24 | void Ellipse (int a, int b, int xc, int yc, float r, float g, float bl) 25 | { int aSq,bSq,twoASq,twoBSq,d,dx,dy,x,y; 26 | aSq = a*a; 27 | bSq = b*b; 28 | twoASq = 2*aSq; 29 | twoBSq = 2*bSq; 30 | d = bSq - b*aSq + aSq/4; 31 | dx = 0; 32 | dy = twoASq*b; 33 | x = 0; 34 | y = b; 35 | symmetricPixels(x,y,xc,yc,r,g,bl); 36 | while (dx < dy) 37 | { x++; 38 | dx += twoBSq; 39 | if (d >= 0) 40 | { y--; 41 | dy -= twoASq; 42 | } 43 | if (d < 0) 44 | d += bSq + dx; 45 | else 46 | d += bSq + dx - dy; 47 | symmetricPixels (x,y,xc,yc,r,g,bl); 48 | } 49 | d = (int)(bSq*(x+0.5)*(x+0.5) + aSq*(y-1)*(y-1) - 50 | aSq*bSq); 51 | while (y > 0) 52 | { y--; 53 | dy -= twoASq; 54 | if (d <= 0) 55 | { x++; 56 | dx += twoBSq; 57 | } 58 | if (d > 0) 59 | d += aSq - dy; 60 | else 61 | d += aSq -dy +dx; 62 | symmetricPixels(x,y,xc,yc,r,g,bl); 63 | } 64 | glFlush(); 65 | } 66 | 67 | 68 | void display(void) 69 | { 70 | 71 | double I,J; 72 | int i,j; 73 | glClear (GL_COLOR_BUFFER_BIT); 74 | glColor3f (1.0, 0.0, 0.0); 75 | glBegin(GL_POINTS); 76 | while(1) 77 | { 78 | Ellipse (a,b,h,k,1,1,1); 79 | Sleep(100); 80 | Ellipse (a,b,h,k,0,0,0); 81 | 82 | Ellipse (a-10,b,h,k,1,1,1); 83 | Sleep(100); 84 | Ellipse (a-10,b,h,k,0,0,0); 85 | 86 | Ellipse (a-20,b,h,k,1,1,1); 87 | Sleep(100); 88 | Ellipse (a-20,b,h,k,0,0,0); 89 | 90 | Ellipse (a-30,b,h,k,1,1,1); 91 | Sleep(100); 92 | Ellipse (a-30,b,h,k,0,0,0); 93 | } 94 | 95 | glEnd(); 96 | glFlush(); 97 | } 98 | void init(void) 99 | { 100 | glClearColor (0.0, 0.0, 0.0, 0.0); 101 | glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0); 102 | } 103 | int main(int argc, char** argv) 104 | { 105 | printf("Enter the center of ellipse:\n"); 106 | scanf("%d %d",&h,&k); 107 | printf("Enter the parameters a & b:\n"); 108 | scanf("%f %f",&a,&b); 109 | glutInit(&argc, argv); 110 | glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 111 | glutInitWindowSize (500, 500); 112 | glutInitWindowPosition (100, 100); 113 | glutCreateWindow ("Ellipse : Polynomial Method "); 114 | init (); 115 | glutDisplayFunc(display); 116 | glutMainLoop(); 117 | return 0; 118 | } -------------------------------------------------------------------------------- /08. globe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | int round(double number) 9 | { 10 | return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); 11 | } 12 | void delay(unsigned int ms){ 13 | clock_t goal = ms + clock(); 14 | while(goal>clock()); 15 | } 16 | 17 | void init(){ 18 | glClearColor(1.0,1.0,1.0,0.0); 19 | glMatrixMode(GL_PROJECTION); 20 | gluOrtho2D(-320,320,-240,240); 21 | } 22 | 23 | void middy_circle(float r, float xc, float yc){ 24 | float blue = 0.01; 25 | glPointSize(3.0); 26 | float d = 1-r; 27 | float x = 0, y = r; 28 | glBegin(GL_POINTS); 29 | glVertex2i(round(x+xc), round(y+yc)); 30 | glEnd(); 31 | glFlush(); 32 | glBegin(GL_POINTS); 33 | while(y>x){ 34 | if(d<0) 35 | { 36 | d+=((2*x)+3); 37 | x++; 38 | } 39 | else if(d>=0) 40 | { 41 | d+=(2*(x-y))+5; 42 | x++; 43 | y--; 44 | } 45 | glVertex2i(round(x+xc), round(y+yc)); 46 | glVertex2i(round(y+xc), round(x+yc)); 47 | glVertex2i(round(x+xc), round(-y+yc)); 48 | glVertex2i(round(y+xc), round(-x+yc)); 49 | glColor3f(0.0,blue,0.0); 50 | glVertex2i(round(-x+xc), round(-y+yc)); 51 | glVertex2i(round(-y+xc), round(-x+yc)); 52 | glVertex2i(round(-x+xc), round(y+yc)); 53 | glVertex2i(round(-y+xc), round(x+yc)); 54 | glColor3f(0.0,0.0,blue); 55 | blue = blue + 0.01; 56 | if(blue==1.0) 57 | blue = 0.01; 58 | } 59 | glEnd(); 60 | glFlush(); 61 | } 62 | 63 | void circly(){ 64 | int flager=0; 65 | for(float x=-160;x<=160;) 66 | { 67 | glClear(GL_COLOR_BUFFER_BIT); 68 | middy_circle(160.0,x,0.0); 69 | if(flager==0) 70 | x++; 71 | else 72 | x--; 73 | if(x>=160) 74 | flager=1; 75 | if(x<=-160) 76 | flager=0; 77 | delay(2); 78 | } 79 | } 80 | 81 | 82 | void drawDot (GLint x, GLint y, GLfloat r, GLfloat g, GLfloat b) 83 | { glColor3f(r,g,b); 84 | glPointSize(3.0); 85 | glBegin (GL_POINTS); 86 | glVertex2i (x,y); 87 | glEnd(); 88 | } 89 | 90 | void symmetricPixels (int x, int y, int xc, int yc, float r, float g, float b) 91 | { 92 | //double val = ((xc+x)*(xc+x) + (yc + y)*(yc+y) - 100*100); 93 | if(((xc+x)*(xc+x) + (yc + y)*(yc+y) - 100*100) <= 0) 94 | drawDot (xc + x, yc + y, r,g,b); 95 | if(((xc-x)*(xc-x) + (yc + y)*(yc+y) - 100*100) <= 0) 96 | drawDot (xc - x, yc + y,r,g,b); 97 | if(((xc+x)*(xc+x) + (yc - y)*(yc-y) - 100*100) <= 0) 98 | drawDot (xc + x, yc - y,r,g,b); 99 | if(((xc-x)*(xc-x) + (yc - y)*(yc-y) - 100*100) <= 0) 100 | drawDot (xc - x, yc - y,r,g,b); 101 | 102 | } 103 | 104 | void Ellipse (int a, int b, int xc, int yc, float r, float g, float bl) 105 | { int aSq,bSq,twoASq,twoBSq,d,dx,dy,x,y; 106 | aSq = a*a; 107 | bSq = b*b; 108 | twoASq = 2*aSq; 109 | twoBSq = 2*bSq; 110 | d = bSq - b*aSq + aSq/4; 111 | dx = 0; 112 | dy = twoASq*b; 113 | x = 0; 114 | y = b; 115 | symmetricPixels(x,y,xc,yc,r,g,bl); 116 | while (dx < dy) 117 | { x++; 118 | dx += twoBSq; 119 | if (d >= 0) 120 | { y--; 121 | dy -= twoASq; 122 | } 123 | if (d < 0) 124 | d += bSq + dx; 125 | else 126 | d += bSq + dx - dy; 127 | symmetricPixels (x,y,xc,yc,r,g,bl); 128 | } 129 | d = (int)(bSq*(x+0.5)*(x+0.5) + aSq*(y-1)*(y-1) - 130 | aSq*bSq); 131 | while (y > 0) 132 | { y--; 133 | dy -= twoASq; 134 | if (d <= 0) 135 | { x++; 136 | dx += twoBSq; 137 | } 138 | if (d > 0) 139 | d += aSq - dy; 140 | else 141 | d += aSq -dy +dx; 142 | symmetricPixels(x,y,xc,yc,r,g,bl); 143 | } 144 | glFlush(); 145 | } 146 | 147 | void ellipsy(){ 148 | glClear(GL_COLOR_BUFFER_BIT); 149 | Ellipse(100,100,0,0,0,1,1); 150 | int flager=0; 151 | float blue = 0.0; 152 | for(int x =0;x<=100;x=x+20){ 153 | Ellipse(100-x,100,0,0,0,1,1); 154 | } 155 | int x=0; 156 | for(int y = 10;y<=100;y=y+10){ 157 | x = x+50; 158 | Ellipse(x,y,0,-100,0,1,1); 159 | 160 | } 161 | int z=0; 162 | for(int y = 10;y<=100;y=y+10){ 163 | z = z+50; 164 | Ellipse(z,y,0,100,0,1,1); 165 | 166 | } 167 | 168 | } 169 | void display(){ 170 | glClear(GL_COLOR_BUFFER_BIT); 171 | //glutWireSphere(100,20,20); 172 | } 173 | void launcher(int argc, char** argv){ 174 | glutInit(&argc, argv); 175 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 176 | glutInitWindowSize(640,480); 177 | glutInitWindowPosition(200,200); 178 | glutCreateWindow("Attempt at drawing a globe"); 179 | glutDisplayFunc(ellipsy); 180 | init(); 181 | glutMainLoop(); 182 | } 183 | 184 | int main(int argc, char** argv){ 185 | launcher(argc,argv); 186 | return 0; 187 | } -------------------------------------------------------------------------------- /09. tranformation.cpp: -------------------------------------------------------------------------------- 1 | // C code to implement basic 2 | // transformations in OPENGL 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // window size 9 | #define maxWD 640 10 | #define maxHT 480 11 | 12 | // rotation speed 13 | #define thetaSpeed 0.05 14 | 15 | int matX[4],matY[4]; 16 | int new_matX[4],new_matY[4]; 17 | // this creates delay between two actions 18 | void delay(unsigned int mseconds) 19 | { 20 | clock_t goal = mseconds + clock(); 21 | while (goal > clock()) 22 | ; 23 | } 24 | 25 | // this is a basic init for the glut window 26 | void myInit(void) 27 | { 28 | glClearColor(1.0, 1.0, 1.0, 0.0); 29 | glMatrixMode(GL_PROJECTION); 30 | glLoadIdentity(); 31 | gluOrtho2D(0.0, maxWD, 0.0, maxHT); 32 | glClear(GL_COLOR_BUFFER_BIT); 33 | glFlush(); 34 | } 35 | 36 | // this function just draws a point 37 | void drawPoint(int x, int y) 38 | { 39 | glPointSize(4.0); 40 | glColor3f(0.0f, 0.0f, 1.0f); 41 | glBegin(GL_LINES); 42 | glVertex2i(x, y); 43 | glEnd(); 44 | } 45 | 46 | 47 | void drawP(int matX[4], int matY[4]) 48 | { 49 | glPointSize(1.0); 50 | glColor3f(0.0f, 0.0f, 1.0f); 51 | glBegin(GL_POLYGON); 52 | glVertex2i(matX[0], matY[0]); 53 | glVertex2i(matX[1], matY[1]); 54 | glVertex2i(matX[2], matY[2]); 55 | glVertex2i(matX[3], matY[3]); 56 | glEnd(); 57 | } 58 | void drawSq(int matX[4],int matY[4]) 59 | { 60 | int i,j; 61 | for(i=0;i<4;i++) 62 | { 63 | drawPoint(matX[i],matY[i]); 64 | } 65 | } 66 | 67 | 68 | void rotateAroundPt(int matX[4],int matY[4], int angle, int cx, int cy) 69 | { 70 | float theta = 0.0; 71 | // while (1) 72 | { 73 | delay(10); 74 | glClear(GL_COLOR_BUFFER_BIT); 75 | 76 | drawP(matX,matY); 77 | // update theta anticlockwise rotation 78 | int xf[4], yf[4]; 79 | theta = ((float)angle * 3.14159) / 180.0; 80 | // check overflow 81 | 82 | /* if (theta >= (2.0 * 3.14159)) 83 | theta = theta - (2.0 * 3.14159); 84 | */ 85 | // actual calculations.. 86 | for(int i=0;i<4;i++) 87 | { 88 | xf[i] = (matX[i] * cos(theta)) - (matY[i] * sin(theta)); 89 | yf[i] = (matX[i] * sin(theta)) + (matY[i] * cos(theta)); 90 | } 91 | 92 | // drawing the centre point 93 | drawP(xf, yf); 94 | 95 | // drawing the rotating point 96 | drawP(xf, yf); 97 | glFlush(); 98 | // creating a delay 99 | // so that the point can be noticed 100 | 101 | } 102 | } 103 | 104 | // this function will translate the point 105 | void translatePoint(int matX[4], int matY[4], int tx, int ty) 106 | { 107 | int fx[4],fy[4]; 108 | for(int i=0;i<4;i++) 109 | { 110 | fx[i] = matX[i]; 111 | fy[i] = matY[i]; 112 | } 113 | 114 | //while (1) 115 | { 116 | delay(10); 117 | glClear(GL_COLOR_BUFFER_BIT); 118 | drawP(matX, matY); // drawing the point 119 | // update 120 | for(int i=0;i<4;i++) 121 | { 122 | fx[i] = fx[i] + tx; 123 | fy[i] = fy[i] + ty; 124 | } 125 | 126 | // check overflow to keep point in screen 127 | /*if (px > maxWD || px < 0 || py > maxHT || py < 0) { 128 | px = fx; 129 | py = fy; 130 | }*/ 131 | 132 | drawP(fx, fy); // drawing the point 133 | 134 | glFlush(); 135 | // creating a delay 136 | // so that the point can be noticed 137 | 138 | } 139 | } 140 | 141 | // this function draws 142 | void scalePoint(int matX[4], int matY[4], int sx, int sy) 143 | { 144 | int fx[4], fy[4]; 145 | //while (1) 146 | { 147 | delay(500); 148 | glClear(GL_COLOR_BUFFER_BIT); 149 | drawP(matX,matY); 150 | // update 151 | for(int i=0;i<4;i++) 152 | { 153 | 154 | fx[i] = matX[i] * sx; 155 | fy[i] = matY[i] * sy; 156 | } 157 | 158 | drawP(fx, fy); // drawing the point 159 | 160 | glFlush(); 161 | // creating a delay 162 | // so that the point can be noticed 163 | 164 | } 165 | } 166 | 167 | // Actual display function 168 | void myDisplay(void) 169 | { 170 | int opt,i; 171 | int tx,ty; 172 | int rotX,rotY,angle; 173 | int sx,sy; 174 | printf("\nEnter the four coordinates of the square : \n"); 175 | for(i=0;i<4;i++) 176 | { 177 | scanf("%d",&matX[i]); 178 | scanf("%d",&matY[i]); 179 | } 180 | printf("\nThe four coordinates of the square are : \n"); 181 | 182 | glClear(GL_COLOR_BUFFER_BIT); 183 | drawP(matX,matY); 184 | glFlush(); 185 | 186 | while(1){ 187 | printf("\nEnter\n\t<1> for translation" 188 | "\n\t<2> for rotation" 189 | "\n\t<3> for scaling\n\t:"); 190 | scanf("%d", &opt); 191 | printf("\nGo to the window..."); 192 | switch (opt) { 193 | case 1: 194 | 195 | printf("\nEnter the X displacement : "); 196 | scanf("%d",&tx); 197 | printf("\nEnter the Y displacement : "); 198 | scanf("%d",&ty); 199 | translatePoint(matX, matY, tx, ty); 200 | break; 201 | case 2: 202 | printf("Enter the angle of rotation : "); 203 | scanf("%d",&angle); 204 | /*printf("\nEnter the point of rotation : \n"); 205 | scanf("%d",&rotX); 206 | scanf("%d",&rotY);*/ 207 | rotateAroundPt(matX,matY,angle,maxWD / 2, maxHT / 2); 208 | // point will circle around 209 | // the centre of the window 210 | break; 211 | case 3: 212 | printf("\nEnter the X scaling : "); 213 | scanf("%d",&sx); 214 | printf("\nEnter the Y scaling: "); 215 | scanf("%d",&sy); 216 | scalePoint(matX, matY, sx, sy); 217 | break; 218 | } 219 | } 220 | } 221 | 222 | void main(int argc, char** argv) 223 | { 224 | glutInit(&argc, argv); 225 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 226 | glutInitWindowSize(maxWD, maxHT); 227 | glutInitWindowPosition(100, 150); 228 | glutCreateWindow("Transforming point"); 229 | glutDisplayFunc(myDisplay); 230 | myInit(); 231 | glutMainLoop(); 232 | } -------------------------------------------------------------------------------- /10. reflection about an arbritrary line.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int x1, y1, x2, y2; 5 | float m; 6 | int c; 7 | int x,y,x_new,y_new; 8 | float mat[3][3]; 9 | void myInit() { 10 | glClear(GL_COLOR_BUFFER_BIT); 11 | glClearColor(0.0, 0.0, 0.0, 1.0); 12 | glMatrixMode(GL_PROJECTION); 13 | gluOrtho2D(0, 500, 0, 500); 14 | } 15 | 16 | 17 | void draw_line(float m,int c){ 18 | int x1,x2,y1,y2; 19 | x1 = 0; 20 | x2 = 400; 21 | if(m>=0){ 22 | y1 = m * x1 + c; 23 | y2 = m * x2 + c; 24 | 25 | } 26 | else 27 | { 28 | y1 = 400; 29 | y2 = 0; 30 | x1 = (y1 - c) / m; 31 | x2 = (y2 - c) / m; 32 | } 33 | glBegin(GL_LINES); 34 | glVertex2i(x1, y1); 35 | glVertex2i(x2, y2); 36 | glEnd(); 37 | 38 | mat[0][0] = (1-m*m) / (1+m*m); 39 | mat[0][1] = (2*m) / (1+m*m); 40 | mat[0][2] = ( -2 * c * m) / (1+m*m); 41 | mat[1][0] = (2*m) / (1+m*m); 42 | mat[1][1] = (m*m - 1) / (1+m*m); 43 | mat[1][2] = ( 2 * c ) / (1+m*m); 44 | mat[2][0] = 0; 45 | mat[2][1] = 0; 46 | mat[2][2] = 1; 47 | 48 | int i,j; 49 | x_new = mat[0][0]*x + mat[0][1]*y + mat[0][2]; 50 | y_new = mat[1][0]*x + mat[1][1]*y + mat[1][2]; 51 | 52 | glPointSize(5); 53 | glBegin(GL_POINTS); 54 | glVertex2i(x, y); 55 | glVertex2i(x_new, y_new); 56 | glEnd(); 57 | } 58 | 59 | 60 | void myDisplay() { 61 | draw_line(m,c); 62 | glFlush(); 63 | } 64 | 65 | void main(int argc, char **argv) { 66 | 67 | printf("\nEnter the value of m : "); 68 | scanf("%f",&m); 69 | printf("\nEnter the value of c : "); 70 | scanf("%d",&c); 71 | printf("\nEnter the x-coordinate of the point : "); 72 | scanf("%d",&x); 73 | printf("\nEnter the y-coordinate of the point : "); 74 | scanf("%d",&y); 75 | glutInit(&argc, argv); 76 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 77 | glutInitWindowSize(500, 500); 78 | glutInitWindowPosition(0, 0); 79 | glutCreateWindow("Bresenham's Line Drawing"); 80 | myInit(); 81 | glutDisplayFunc(myDisplay); 82 | glutMainLoop(); 83 | } -------------------------------------------------------------------------------- /11. composite transformation.txt: -------------------------------------------------------------------------------- 1 | // C code to implement basic 2 | // transformations in OPENGL 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // window size 9 | #define maxWD 640 10 | #define maxHT 480 11 | 12 | // rotation speed 13 | #define thetaSpeed 0.05 14 | 15 | int matX[4], matY[4]; 16 | int new_matX[4], new_matY[4]; 17 | // this creates delay between two actions 18 | /*void delay(unsigned int mseconds) 19 | { 20 | clock_t goal = mseconds + clock(); 21 | while (goal > clock()) 22 | ; 23 | }*/ 24 | 25 | // this is a basic init for the glut window 26 | void myInit(void) 27 | { 28 | glClearColor(0.0, 0.0, 0.0, 0.0); 29 | glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0); 30 | } 31 | 32 | void draw_pixel(int x, int y) { 33 | glBegin(GL_POINTS); 34 | glColor3f(1.0f, 1.0f, 1.0f); 35 | glVertex2i(x, y); 36 | glEnd(); 37 | } 38 | // this function just draws a point 39 | void drawPoint(int x, int y) 40 | { 41 | glPointSize(4.0); 42 | glColor3f(0.0f, 1.0f, 1.0f); 43 | glBegin(GL_LINES); 44 | glVertex2i(x, y); 45 | glEnd(); 46 | } 47 | 48 | 49 | void drawP(int matX[4], int matY[4],int r,int g, int b) 50 | { 51 | glPointSize(1.0); 52 | glColor3f(r,g,b); 53 | glBegin(GL_POLYGON); 54 | glVertex2i(matX[0], matY[0]); 55 | glVertex2i(matX[1], matY[1]); 56 | glVertex2i(matX[2], matY[2]); 57 | glVertex2i(matX[3], matY[3]); 58 | glEnd(); 59 | } 60 | void drawSq(int matX[4], int matY[4]) 61 | { 62 | int i, j; 63 | for (i = 0; i<4; i++) 64 | { 65 | drawPoint(matX[i], matY[i]); 66 | } 67 | } 68 | 69 | void drawaxis() 70 | { 71 | for (int i = -100; i <= 100; i++) //X and Y axis 72 | { 73 | draw_pixel(i, 0); 74 | draw_pixel(0, i); 75 | 76 | } 77 | for (int i = -2; i <= 2; i++) 78 | { 79 | draw_pixel(95 + i, 4 + i); 80 | draw_pixel(95 - i, 4 + i); 81 | 82 | } 83 | for (int i = 0; i <= 2; i++) 84 | { 85 | draw_pixel(4 + i, 95 + i); 86 | draw_pixel(4 - i, 95 + i); 87 | draw_pixel(4, 95 - i); 88 | 89 | } 90 | } 91 | void CT(int matX[4], int matY[4]) 92 | { 93 | glClear(GL_COLOR_BUFFER_BIT); 94 | drawP(matX, matY,0,0,1); 95 | int xf[4], yf[4]; 96 | float T[3][3]; 97 | T[0][0]=0.5; 98 | T[0][1]=0.5; 99 | T[0][2]=5; 100 | T[1][0]=-0.5; 101 | T[1][1]=0.5; 102 | T[1][2]=0; 103 | T[2][0]=0; 104 | T[2][1]=0; 105 | T[2][2]=1; 106 | 107 | double sum=0.0; 108 | 109 | for(int i=0;i<4;i++) 110 | { 111 | xf[i]=T[0][0]*matX[i]+T[0][1]*matY[i]+T[0][2]; 112 | yf[i]=T[1][0]*matX[i]+T[1][1]*matY[i]+T[1][2]; 113 | } 114 | drawP(xf,yf,1,0,0); 115 | drawaxis(); 116 | glFlush(); 117 | } 118 | 119 | 120 | 121 | void myDisplay(void) 122 | { 123 | int opt, i; 124 | int tx, ty; 125 | int rotX, rotY, angle; 126 | int sx, sy; 127 | printf("\nEnter the four coordinates of the square : \n"); 128 | for (i = 0; i<4; i++) 129 | { 130 | scanf("%d", &matX[i]); 131 | scanf("%d", &matY[i]); 132 | } 133 | printf("\nThe four coordinates of the square are : \n"); 134 | 135 | glClear(GL_COLOR_BUFFER_BIT); 136 | drawP(matX, matY,0,0,1); 137 | drawaxis(); 138 | 139 | glFlush(); 140 | CT(matX,matY); 141 | 142 | } 143 | 144 | void main(int argc, char** argv) 145 | { 146 | glutInit(&argc, argv); 147 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 148 | glutInitWindowSize(maxWD, maxHT); 149 | glutInitWindowPosition(100, 150); 150 | glutCreateWindow("Transforming point"); 151 | glutDisplayFunc(myDisplay); 152 | myInit(); 153 | glutMainLoop(); 154 | } -------------------------------------------------------------------------------- /12. bezier curve.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int x1, x2, x3, y11, y2, y3; 7 | 8 | void myInit() 9 | { 10 | glClear(GL_COLOR_BUFFER_BIT); 11 | glClearColor(1.0, 0.0, 0.0, 0.0); 12 | glMatrixMode(GL_PROJECTION); 13 | gluOrtho2D(0, 500, 0, 500); 14 | } 15 | 16 | void draw_pixel(int x, int y) 17 | { 18 | glBegin(GL_POINTS); 19 | glColor3f (1.0, 1.0, 1.0); 20 | glVertex2i(x, y); 21 | glEnd(); 22 | } 23 | void draw_gray(int x, int y) 24 | { 25 | glBegin(GL_POINTS); 26 | glColor3f (0.5, 0.5, 0.5); 27 | glVertex2i(x, y); 28 | glEnd(); 29 | } 30 | void draw_curve() 31 | { 32 | double u,x,y; 33 | draw_pixel(x1,y11); 34 | draw_pixel(x3,y3); 35 | for (u=0.01;u<1;u=u+0.01) 36 | { 37 | x=(1-u)*(1-u)*x1+2*(1-u)*u*x2+u*u*x3; 38 | y=(1-u)*(1-u)*y11+2*(1-u)*u*y2+u*u*y3; 39 | draw_pixel(x,y); 40 | } 41 | 42 | } 43 | 44 | 45 | void myDisplay() 46 | { 47 | //draw_line(0,500,250,250); 48 | //draw_line(250,250,0,500); 49 | draw_curve(); 50 | glFlush(); 51 | } 52 | 53 | void main(int argc, char **argv) 54 | { 55 | 56 | printf( "Enter (x1, y1, x2, y2, x3, y3)\n"); 57 | scanf(" %d %d %d %d %d %d", &x1, &y11, &x2, &y2, &x3, &y3); 58 | glutInit(&argc, argv); 59 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 60 | glutInitWindowSize(500, 500); 61 | glutInitWindowPosition(0, 0); 62 | glutCreateWindow("Bezier Curve Drawing"); 63 | myInit(); 64 | glutDisplayFunc(myDisplay); 65 | glutMainLoop(); 66 | } -------------------------------------------------------------------------------- /13. cubic bezier curve.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int x1, x2, x3, y11, y2, y3, x4, y4; 7 | 8 | void myInit() 9 | { 10 | glClear(GL_COLOR_BUFFER_BIT); 11 | glClearColor(1.0, 0.0, 0.0, 0.0); 12 | glMatrixMode(GL_PROJECTION); 13 | gluOrtho2D(0, 500, 0, 500); 14 | } 15 | 16 | void draw_pixel(int x, int y) 17 | { 18 | glBegin(GL_POINTS); 19 | glColor3f (1.0, 1.0, 1.0); 20 | glVertex2i(x, y); 21 | glEnd(); 22 | } 23 | void draw_gray(int x, int y) 24 | { 25 | glBegin(GL_POINTS); 26 | glColor3f (0.5, 0.5, 0.5); 27 | glVertex2i(x, y); 28 | glEnd(); 29 | } 30 | void draw_curve() 31 | { 32 | double u,x,y; 33 | draw_pixel(x1,y11); 34 | draw_pixel(x4,y4); 35 | for (u=0.01;u<1;u=u+0.01) 36 | { 37 | x=(1-u)*(1-u)*(1-u)*x1+3*(1-u)*(1-u)*u*x2+3*u*u*(1-u)*x3+u*u*u*x4; 38 | y=(1-u)*(1-u)*(1-u)*y11+3*(1-u)*(1-u)*u*y2+3*u*u*(1-u)*y3+u*u*u*y4; 39 | draw_pixel(x,y); 40 | } 41 | 42 | } 43 | 44 | 45 | void myDisplay() 46 | { 47 | //draw_line(0,500,250,250); 48 | //draw_line(250,250,0,500); 49 | draw_curve(); 50 | glFlush(); 51 | } 52 | 53 | void main(int argc, char **argv) 54 | { 55 | 56 | printf( "Enter (x1, y1, x2, y2, x3, y3, x4, y4)\n"); 57 | scanf("%d %d %d %d %d %d %d %d", &x1, &y11, &x2, &y2, &x3, &y3, &x4, &y4); 58 | glutInit(&argc, argv); 59 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 60 | glutInitWindowSize(500, 500); 61 | glutInitWindowPosition(0, 0); 62 | glutCreateWindow("Bezier Cubic Curve Drawing"); 63 | myInit(); 64 | glutDisplayFunc(myDisplay); 65 | glutMainLoop(); 66 | } -------------------------------------------------------------------------------- /14. orthographic projection.txt: -------------------------------------------------------------------------------- 1 | // Circle Drawing Algorithm 2 | // Editor: kirito_ys 3 | 4 | /* Tips for viewing the output: 5 | * Use Win +'+' to open magnifier. 6 | * Use Ctrl + Alt + 'F' for full screen 7 | * Use Ctrl + Alt + 'L' for lens mode 8 | * Use Win + Esc to exit magnifier 9 | */ 10 | 11 | #include 12 | //#include 13 | #include 14 | #include 15 | #include 16 | 17 | int WINDOW_WIDTH = 900; 18 | int WINDOW_HEIGHT = 600; 19 | 20 | float COLOR_GRAY = 0.5; 21 | 22 | double X1, Y1, X2, Y2; 23 | int x0=0; 24 | //int y0=0; 25 | 26 | //orthographic projection input variables 27 | //int x1=0,y1=0,z1=0, x2=0,y2=0,z2=100, x3=200,y3=0,z3=100, x4=200,y4=0,z4=0, h=50, x5,y5,z5; 28 | int p[][3]={{0,0,0},{0,0,100},{200,0,100},{200,0,0},{-1,-1,-1}}; 29 | int h=150; 30 | 31 | 32 | void draw_circle(float radius,int x_center,int y_center); 33 | void make_points(int x0,int y0,int x,int y); 34 | int give_rand(int MAX=WINDOW_HEIGHT); 35 | void project_plan(); 36 | void project_elevation(); 37 | void project_side(); 38 | 39 | void Init() 40 | { 41 | // Set clear color to white 42 | glClearColor(1.0,1.0,1.0,0); 43 | // Set fill color to black 44 | glColor3f(1.0,1.0,1.0); 45 | //glViewport(0 , 0 , 500 , 500); 46 | // glMatrixMode(GL_PROJECTION); 47 | // glLoadIdentity(); 48 | // gluOrtho2d(left,right,bottom,top) 49 | gluOrtho2D(-WINDOW_WIDTH/4 , 3*WINDOW_WIDTH/4 , -WINDOW_HEIGHT/4 , 3*WINDOW_HEIGHT/4); 50 | } 51 | 52 | void make_axis(){ 53 | 54 | glColor3f(0.4,0.4,0.4); 55 | glBegin(GL_LINES); 56 | glVertex2f(0,WINDOW_HEIGHT/2); 57 | glVertex2f(0,-WINDOW_HEIGHT/2); 58 | glVertex2f(WINDOW_WIDTH/2,0); 59 | glVertex2f(-WINDOW_WIDTH/2,0); 60 | glEnd(); 61 | } 62 | 63 | 64 | int give_rand(int MAX){ 65 | return (MAX*rand())/RAND_MAX; 66 | } 67 | 68 | void project_plan(){ 69 | 70 | 71 | glColor3f(0,0,0); 72 | 73 | 74 | glBegin(GL_TRIANGLE_STRIP); 75 | for(int i=0;i<5;i++){ 76 | for(int j=i;j<5;j++){ 77 | glVertex2i(p[i][0],p[i][2]); 78 | glVertex2i(p[j][0],p[j][2]); 79 | } 80 | } 81 | glEnd(); 82 | 83 | glColor3f(0.2,0.2,0.2); 84 | glBegin(GL_LINES); 85 | for(int i=0;i<5;i++){ 86 | for(int j=i;j<5;j++){ 87 | glVertex2i(p[i][0],p[i][2]); 88 | glVertex2i(p[j][0],p[j][2]); 89 | } 90 | } 91 | glEnd(); 92 | 93 | glColor3f(1,0,0); 94 | //glPointSize(5); 95 | glBegin(GL_POINTS); 96 | for(int i=0;i<5;i++) 97 | glVertex2i(p[i][0],p[i][2]); 98 | glEnd(); 99 | } 100 | 101 | void project_elevation(){ 102 | int t_x = 0; 103 | int t_y = 200; 104 | 105 | /*glBegin(GL_POLYGON); 106 | for(int i=0;i<5;i++) 107 | glVertex2i(p[i][0],p[i][1]); 108 | glEnd();*/ 109 | 110 | glColor3f(0,0,0); 111 | glBegin(GL_TRIANGLE_STRIP); 112 | for(int i=0;i<5;i++){ 113 | for(int j=i;j<5;j++){ 114 | glVertex2i(p[i][0] +t_x,p[i][1] +t_y); 115 | glVertex2i(p[j][0] +t_x,p[j][1] +t_y); 116 | } 117 | } 118 | glEnd(); 119 | 120 | glColor3f(0.8,0.8,0.8); 121 | glBegin(GL_LINES); 122 | for(int i=0;i<5;i++){ 123 | for(int j=i;j<5;j++){ 124 | glVertex2i(p[i][0] +t_x,p[i][1] +t_y); 125 | glVertex2i(p[j][0] +t_x,p[j][1] +t_y); 126 | } 127 | } 128 | glEnd(); 129 | 130 | 131 | } 132 | 133 | void project_side(){ 134 | int t_x = 400; 135 | int t_y = 200; 136 | 137 | glColor3f(0,0,0); 138 | glBegin(GL_TRIANGLE_STRIP); 139 | for(int i=0;i<5;i++){ 140 | for(int j=0;j<5;j++){ 141 | glVertex2i(-p[i][2] +t_x,p[i][1] +t_y); 142 | glVertex2i(-p[j][2] +t_x,p[j][1] +t_y); 143 | } 144 | } 145 | glEnd(); 146 | 147 | glColor3f(0.8,0.8,0.8); 148 | glBegin(GL_LINES); 149 | for(int i=0;i<5;i++){ 150 | for(int j=i;j<5;j++){ 151 | glVertex2i(-p[i][2] +t_x,p[i][1] +t_y); 152 | glVertex2i(-p[j][2] +t_x,p[j][1] +t_y); 153 | } 154 | } 155 | glEnd(); 156 | 157 | 158 | } 159 | 160 | void graphics_draw(void){ 161 | glClear(GL_COLOR_BUFFER_BIT); 162 | make_axis(); 163 | glColor3f(1,1,1); 164 | 165 | //finding Coordinates of tip of pyramid 166 | p[4][0]=p[2][0]/2; 167 | p[4][1]=h; 168 | p[4][2]=p[2][2]/2; 169 | 170 | project_plan(); 171 | project_elevation(); 172 | project_side(); 173 | 174 | 175 | 176 | glFlush(); 177 | } 178 | 179 | 180 | void make_points(int x0,int y0,int x,int y){ 181 | glBegin(GL_POINTS); 182 | glVertex2d(x0+x,y0+y); 183 | glVertex2d(x0+x,y0-y); 184 | glVertex2d(x0-x,y0+y); 185 | glVertex2d(x0-x,y0-y); 186 | glVertex2d(x0+y,y0+x); 187 | glVertex2d(x0+y,y0-x); 188 | glVertex2d(x0-y,y0+x); 189 | glVertex2d(x0-y,y0-x); 190 | glEnd(); 191 | } 192 | 193 | 194 | void main(int argc, char **argv) 195 | { 196 | printf("ORTHO\n"); 197 | 198 | //make random really random 199 | srand(time(0)); 200 | 201 | 202 | glFlush(); 203 | // Initialise GLUT library 204 | glutInit(&argc,argv); 205 | // Set the initial display mode 206 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 207 | // Set the initial window position and size 208 | //position means the position of the output screen with r2espect to the monitor screen 209 | glutInitWindowPosition(0,0); 210 | glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT); 211 | // Create the window with title "DDA_Line" 212 | glutCreateWindow("Orthographic Projection"); 213 | // Initialize drawing colors 214 | Init(); 215 | // Call the displaying function 216 | glutDisplayFunc(graphics_draw); 217 | // Keep displaying untill the program is closed 218 | glutMainLoop(); 219 | } -------------------------------------------------------------------------------- /15. Blending Function.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int x1, y1, x2, y2,x_1,y_1,x_2,y_2; 6 | 7 | 8 | 9 | void myInit(void) 10 | { 11 | glClearColor(0.0, 0.0, 0.0, 0.0); 12 | glOrtho(0.0, 100.0, -100.0, 100.0, -1.0, 1.0); 13 | } 14 | 15 | void draw_pixel(int x, int y,int r, int g, int b) { 16 | glBegin(GL_POINTS); 17 | glColor3f(1,1,1); 18 | glVertex2i(x, y); 19 | glEnd(); 20 | } 21 | 22 | void drawP() 23 | { 24 | glPointSize(1.0); 25 | glColor3f(1,1,1); 26 | glBegin(GL_LINES); 27 | glVertex2i(x1,y1); 28 | glVertex2i(x2,y2); 29 | glEnd(); 30 | 31 | /*glBegin(GL_LINES); 32 | glVertex2i(x2,y2); 33 | glVertex2i(x3,y3); 34 | glEnd(); 35 | */ 36 | 37 | 38 | } 39 | void draw_curve() { 40 | //drawP(); 41 | double u,x,y; 42 | //draw_pixel(x1, y1); 43 | //draw_pixel(x2, y2); 44 | double f1,f2,f3,f4; 45 | for(u=0;u<=1;u+=0.01) 46 | { 47 | f1 = ( 2 * u * u - 3 * u + 1 ) ; 48 | f2 = ( -4 * u * u + 4 * u ) ; 49 | f3 = ( 2 * u * u - u) ; 50 | draw_pixel(u*100, f1*100,1,0,0); 51 | draw_pixel(u*100, f2*100,0,1,0); 52 | draw_pixel(u*100, f3*100,0,0,1); 53 | printf("\n%f\t%f\t%f\t%f",u,f1,f2,f3); 54 | 55 | } 56 | 57 | for (int i = -100; i <= 100; i++) //X and Y axis 58 | { 59 | draw_pixel(i, 0,1,1,1); 60 | draw_pixel(0, i,1,1,1); 61 | 62 | } 63 | 64 | 65 | 66 | } 67 | 68 | void myDisplay() { 69 | draw_curve(); 70 | glFlush(); 71 | } 72 | 73 | void main(int argc, char **argv) { 74 | 75 | /*printf("Enter (x1, y1, x2, y2,x1',y1',x2',y2')\n"); 76 | scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x_1,&y_1, &x_2, &y_2); 77 | */ 78 | glutInit(&argc, argv); 79 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 80 | glutInitWindowSize(500, 500); 81 | glutInitWindowPosition(0, 0); 82 | glutCreateWindow("Blending Functions"); 83 | myInit(); 84 | glutDisplayFunc(myDisplay); 85 | glutMainLoop(); 86 | } -------------------------------------------------------------------------------- /16. Cubic Bezier_taylor's.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int x1, x2, x3, y11, y2, y3, x4, y4; 7 | 8 | void myInit() 9 | { 10 | 11 | glClearColor(1.0, 1.0, 1.0, 0.0); 12 | glClear(GL_COLOR_BUFFER_BIT); 13 | glMatrixMode(GL_PROJECTION); 14 | gluOrtho2D(0, 500, 0, 500); 15 | } 16 | 17 | void drawP() 18 | { 19 | glColor3f(0,0,0); 20 | glBegin(GL_LINES); 21 | glVertex2i(x1,y11); 22 | glVertex2i(x2,y2); 23 | glEnd(); 24 | 25 | glBegin(GL_LINES); 26 | glVertex2i(x2,y2); 27 | glVertex2i(x3,y3); 28 | glEnd(); 29 | 30 | glBegin(GL_LINES); 31 | glVertex2i(x3,y3); 32 | glVertex2i(x4,y4); 33 | glEnd(); 34 | } 35 | void draw_pixel(int x, int y) 36 | { 37 | glBegin(GL_POINTS); 38 | glColor3f (0.0, 0.0, 0.0); 39 | glVertex2i(x, y); 40 | glEnd(); 41 | } 42 | void draw_gray(int x, int y) 43 | { 44 | glBegin(GL_POINTS); 45 | glColor3f (0.5, 0.5, 0.5); 46 | glVertex2i(x, y); 47 | glEnd(); 48 | } 49 | void draw_curve() 50 | { 51 | drawP(); 52 | double dt=0.0005; 53 | double x,y; 54 | double dfx,dfy,ddfx,ddfy,dddfx,dddfy; 55 | x=x1; 56 | y=y11; 57 | dfx=(3*x2-3*x1)*dt+(3*x1-6*x2+3*x3)*dt*dt+(-x1+3*x2-3*x3+x4)*dt*dt*dt; 58 | dfy=(3*y2-3*y11)*dt+(3*y11-6*y2+3*y3)*dt*dt+(-y11+3*y2-3*y3+y4)*dt*dt*dt; 59 | ddfx=2*(3*x1-6*x2+3*x3)*dt*dt+6*(-x1+3*x2-3*x3+x4)*dt*dt*dt; 60 | ddfy=2*(3*y11-6*y2+3*y3)*dt*dt+6*(-y11+3*y2-3*y3+y4)*dt*dt*dt; 61 | dddfx=6*(-x1+3*x2-3*x3+x4)*dt*dt*dt; 62 | dddfy=6*(-y11+3*y2-3*y3+y4)*dt*dt*dt; 63 | for (double dt=0.0005;dt<1;dt=dt+0.0005) 64 | { 65 | draw_pixel(x,y); 66 | x=x+dfx; 67 | y=y+dfy; 68 | dfx=dfx+ddfx; 69 | dfy=dfy+ddfy; 70 | ddfx=ddfx+dddfx; 71 | ddfy=ddfy+dddfy; 72 | } 73 | 74 | } 75 | 76 | 77 | void myDisplay() 78 | { 79 | draw_curve(); 80 | glFlush(); 81 | } 82 | 83 | void main(int argc, char **argv) 84 | { 85 | 86 | printf( "Enter (x1, y1, x2, y2, x3, y3, x4, y4)\n"); 87 | scanf("%d %d %d %d %d %d %d %d", &x1, &y11, &x2, &y2, &x3, &y3, &x4, &y4); 88 | glutInit(&argc, argv); 89 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 90 | glutInitWindowSize(500, 500); 91 | glutInitWindowPosition(0, 0); 92 | glutCreateWindow("Bezier Cubic Curve Drawing by using Taylor series method"); 93 | myInit(); 94 | glutDisplayFunc(myDisplay); 95 | glutMainLoop(); 96 | } -------------------------------------------------------------------------------- /17. Hermite Curve.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int x1, y1, x2, y2,x_1,y_1,x_2,y_2; 6 | 7 | 8 | 9 | void myInit(void) 10 | { 11 | glClearColor(0.0, 0.0, 0.0, 0.0); 12 | glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); 13 | } 14 | 15 | void draw_pixel(int x, int y) { 16 | glBegin(GL_POINTS); 17 | glVertex2i(x, y); 18 | glEnd(); 19 | } 20 | 21 | void drawP() 22 | { 23 | glPointSize(1.0); 24 | glColor3f(1,1,1); 25 | glBegin(GL_LINES); 26 | glVertex2i(x1,y1); 27 | glVertex2i(x2,y2); 28 | glEnd(); 29 | 30 | /*glBegin(GL_LINES); 31 | glVertex2i(x2,y2); 32 | glVertex2i(x3,y3); 33 | glEnd(); 34 | */ 35 | 36 | 37 | } 38 | void draw_curve() { 39 | //drawP(); 40 | double u,x,y; 41 | draw_pixel(x1, y1); 42 | draw_pixel(x2, y2); 43 | double f1,f2,f3,f4; 44 | for(u=0.01;u<1;u+=0.01) 45 | { 46 | f1 = ( 2 * u * u * u - 3 * u * u + 1 ); 47 | f2 = ( -2 * u * u * u + 3 * u * u ); 48 | f3 = ( u * u * u - 2 * u * u + u ); 49 | f4 = ( u * u * u - u * u ); 50 | x = f1 * x1 + f2 * x2 + f3 * x_1 + f4 * x_2; 51 | y = f1 * y1 + f2 * y2 + f3 * y_1 + f4 * y_2; 52 | draw_pixel(x, y); 53 | } 54 | 55 | 56 | for (int i = -100; i <= 100; i++) //X and Y axis 57 | { 58 | draw_pixel(i, 0); 59 | draw_pixel(0, i); 60 | 61 | } 62 | for (int i = -2; i <= 2; i++) 63 | { 64 | draw_pixel(95 + i, 4 + i); 65 | draw_pixel(95 - i, 4 + i); 66 | 67 | } 68 | for (int i = 0; i <= 2; i++) 69 | { 70 | draw_pixel(4 + i, 95 + i); 71 | draw_pixel(4 - i, 95 + i); 72 | draw_pixel(4, 95 - i); 73 | 74 | } 75 | 76 | } 77 | 78 | void myDisplay() { 79 | draw_curve(); 80 | glFlush(); 81 | } 82 | 83 | void main(int argc, char **argv) { 84 | 85 | printf("Enter (x1, y1, x2, y2,x1',y1',x2',y2')\n"); 86 | scanf("%d %d %d %d %d %d %d %d ", &x1, &y1, &x2, &y2, &x_1,&y_1, &x_2, &y_2); 87 | 88 | glutInit(&argc, argv); 89 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 90 | glutInitWindowSize(500, 500); 91 | glutInitWindowPosition(0, 0); 92 | glutCreateWindow("Hermite Curve"); 93 | myInit(); 94 | glutDisplayFunc(myDisplay); 95 | glutMainLoop(); 96 | } -------------------------------------------------------------------------------- /18 Plane Curve.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int x1, y1, x2, y2,x3,y3; 5 | 6 | 7 | /* 8 | void myInit(void) 9 | { 10 | glClearColor(0.0, 0.0, 0.0, 0.0); 11 | glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); 12 | } 13 | */ 14 | void myInit(void) 15 | { 16 | glClearColor(0.0, 0.0, 0.0, 0.0); 17 | gluOrtho2D(0.0, 500.0, 0.0, 500.0); 18 | } 19 | 20 | void draw_pixel(int x, int y) { 21 | glBegin(GL_POINTS); 22 | glVertex2i(x, y); 23 | glEnd(); 24 | } 25 | 26 | void drawP() 27 | { 28 | glPointSize(1.0); 29 | glColor3f(1,1,1); 30 | glBegin(GL_LINES); 31 | glVertex2i(x1,y1); 32 | glVertex2i(x2,y2); 33 | glEnd(); 34 | 35 | glBegin(GL_LINES); 36 | glVertex2i(x2,y2); 37 | glVertex2i(x3,y3); 38 | glEnd(); 39 | 40 | 41 | } 42 | void draw_curve() { 43 | //drawP(); 44 | double u,x,y,f1,f2,f3; 45 | draw_pixel(x1, y1); 46 | draw_pixel(x3, y3); 47 | for(u=0.01;u<=1;u+=0.01) 48 | { 49 | f1 = 2*u*u - 3*u + 1; 50 | f2 = -4*u*u + 4*u ; 51 | f3 = 2*u*u - u; 52 | x = f1*x1 + f2*x2 + f3*x3; 53 | y = f1*y1 + f2*y2 + f3*y3; 54 | draw_pixel(x, y); 55 | } 56 | 57 | /* 58 | for (int i = -100; i <= 100; i++) //X and Y axis 59 | { 60 | draw_pixel(i, 0); 61 | draw_pixel(0, i); 62 | 63 | } 64 | for (int i = -2; i <= 2; i++) 65 | { 66 | draw_pixel(95 + i, 4 + i); 67 | draw_pixel(95 - i, 4 + i); 68 | 69 | } 70 | for (int i = 0; i <= 2; i++) 71 | { 72 | draw_pixel(4 + i, 95 + i); 73 | draw_pixel(4 - i, 95 + i); 74 | draw_pixel(4, 95 - i); 75 | 76 | }*/ 77 | 78 | } 79 | 80 | void myDisplay() { 81 | draw_curve(); 82 | glFlush(); 83 | } 84 | 85 | void main(int argc, char **argv) { 86 | 87 | printf("Enter (x1, y1, x2, y2,x3,y3)\n"); 88 | scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2,&x3,&y3); 89 | 90 | glutInit(&argc, argv); 91 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 92 | glutInitWindowSize(500, 500); 93 | glutInitWindowPosition(0, 0); 94 | glutCreateWindow("Plane Curve"); 95 | myInit(); 96 | glutDisplayFunc(myDisplay); 97 | glutMainLoop(); 98 | } -------------------------------------------------------------------------------- /19. Space curve.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int x1, y1, x2, y2,x3,y3,x4,y4; 5 | 6 | 7 | /* 8 | void myInit(void) 9 | { 10 | glClearColor(0.0, 0.0, 0.0, 0.0); 11 | glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); 12 | } 13 | */ 14 | void myInit(void) 15 | { 16 | glClearColor(0.0, 0.0, 0.0, 0.0); 17 | gluOrtho2D(0.0, 500.0, 0.0, 500.0); 18 | } 19 | 20 | void draw_pixel(int x, int y) { 21 | glBegin(GL_POINTS); 22 | glVertex2i(x, y); 23 | glEnd(); 24 | } 25 | 26 | void drawP() 27 | { 28 | glPointSize(1.0); 29 | glColor3f(1,1,1); 30 | glBegin(GL_LINES); 31 | glVertex2i(x1,y1); 32 | glVertex2i(x2,y2); 33 | glEnd(); 34 | 35 | glBegin(GL_LINES); 36 | glVertex2i(x2,y2); 37 | glVertex2i(x3,y3); 38 | glEnd(); 39 | 40 | 41 | } 42 | void draw_curve() { 43 | //drawP(); 44 | double u,x,y,f1,f2,f3,f4; 45 | draw_pixel(x1, y1); 46 | draw_pixel(x3, y3); 47 | for(u=0.01;u<=1;u+=0.01) 48 | { 49 | f1 = -9*u*u*u/2 + 9*u*u - 11*u/2 + 1; 50 | f2 = 27*u*u*u/2 - 45*u*u/2 + 9*u; 51 | f3 * -27*u*u*u/2 + 18*u*u - 9*u/2; 52 | f4 = 9*u*u*u/2 - 9*u*u/2 + u; 53 | x = f1*x1 + f2*x2 + f3*x3 + f4*x4; 54 | y = f1*y1 + f2*y2 + f3*y3 + f4*y4; 55 | draw_pixel(x, y); 56 | } 57 | 58 | /* 59 | for (int i = -100; i <= 100; i++) //X and Y axis 60 | { 61 | draw_pixel(i, 0); 62 | draw_pixel(0, i); 63 | 64 | } 65 | for (int i = -2; i <= 2; i++) 66 | { 67 | draw_pixel(95 + i, 4 + i); 68 | draw_pixel(95 - i, 4 + i); 69 | 70 | } 71 | for (int i = 0; i <= 2; i++) 72 | { 73 | draw_pixel(4 + i, 95 + i); 74 | draw_pixel(4 - i, 95 + i); 75 | draw_pixel(4, 95 - i); 76 | 77 | }*/ 78 | 79 | } 80 | 81 | void myDisplay() { 82 | draw_curve(); 83 | glFlush(); 84 | } 85 | 86 | void main(int argc, char **argv) { 87 | 88 | printf("Enter (x1, y1, x2, y2,x3,y3)\n"); 89 | scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2,&x3,&y3,&x4, &y4); 90 | 91 | glutInit(&argc, argv); 92 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 93 | glutInitWindowSize(500, 500); 94 | glutInitWindowPosition(0, 0); 95 | glutCreateWindow("Space Curve"); 96 | myInit(); 97 | glutDisplayFunc(myDisplay); 98 | glutMainLoop(); 99 | } -------------------------------------------------------------------------------- /20. Flag using curves.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define NUMSTEPS 0.01 8 | #define tension 0.00 9 | GLfloat P[4][4][3] = { 10 | {{-4.5, -4.5, 12.0}, {-1.5, -4.5, 6.0}, 11 | {1.5, -4.5, -3.0}, {4.5, -4.5, 6.0}}, 12 | {{-4.5, -1.5, 3.0}, {-1.5, -1.5, 9.0}, 13 | {1.5, -1.5, 0.0}, {4.5, -1.5, -3.0}}, 14 | {{-4.5, 1.5, 12.0}, {-1.5, 1.5, 0.0}, 15 | {1.5, 1.5, 9.0}, {4.5, 1.5, 12.0}}, 16 | {{-4.5, 4.5, -6.0}, {-1.5, 4.5, -6.0}, 17 | {1.5, 4.5, 0.0}, {4.5, 4.5, -3.0}} 18 | }; 19 | 20 | 21 | GLfloat D[] = {1.0f, 1.0f, 0.0f, 0.0f, 22 | 0.0f, 0.0f, 1.0f, 1.0f, 23 | 0.0f, 0.33333333333333f, 0.0f, 0.0f, 24 | 0.0f, 0.0f, -0.33333333333333f, 0.0f}; 25 | GLfloat DT[] = {1.0f, 0.0f, 0.0f, 0.0f, 26 | 1.0f, 0.0f, 0.33333333333333f, 0.0f, 27 | 0.0f, 1.0f, 0.0f, -0.33333333333333f, 28 | 0.0f, 1.0f, 0.0f, 0.0f}; 29 | 30 | GLfloat StartPt_u(int ii,int jj){ 31 | 32 | return P[jj][1][ii]; 33 | } 34 | 35 | GLfloat EndPt_u(int ii,int jj){ 36 | return P[jj][2][ii]; 37 | } 38 | 39 | GLfloat StartTan_u(int ii,int jj){ 40 | return( (1-tension)*(P[jj][2][ii] - P[jj][0][ii]) /2 ); 41 | } 42 | 43 | GLfloat EndTan_u(int ii,int jj){ 44 | return( (1-tension)*(P[jj][3][ii] - P[jj][1][ii])/2 ); 45 | } 46 | 47 | GLfloat StartPt_w(int ii,int jj){ 48 | return P[1][jj][ii]; 49 | } 50 | 51 | GLfloat EndPt_w(int ii,int jj){ 52 | return P[2][jj][ii]; 53 | } 54 | 55 | GLfloat StartTan_w(int ii,int jj){ 56 | return( (1-tension)*(P[2][jj][ii] - P[0][jj][ii]) /2 ); 57 | } 58 | 59 | GLfloat EndTan_w(int ii,int jj){ 60 | return( (1-tension)*(P[3][jj][ii] - P[1][jj][ii])/2 ); 61 | } 62 | 63 | 64 | 65 | 66 | void Spline_u(int piece_s,int piece_e){ 67 | int i,j; 68 | double t, vv[3]; 69 | double inc; 70 | inc = NUMSTEPS; 71 | 72 | for(j=piece_s-1;j///#include 3 | #include 4 | #include ///#include 5 | #include ///#include 6 | //#include 7 | #include 8 | #include 9 | 10 | #define FROM_RIGHT 1 11 | #define FROM_LEFT 2 12 | #define FROM_TOP 3 13 | #define FROM_BOTTOM 4 14 | 15 | static int WINDOW_WIDTH, WINDOW_HEIGHT; 16 | 17 | int playerResult = 0; 18 | static float Xspeed = 2, Yspeed = 2; 19 | static float delta = 2; 20 | int flag_pg = 0; 21 | char string[100]; 22 | 23 | struct RECTA 24 | { 25 | float left, top, right, bottom; 26 | }; 27 | 28 | RECTA ball = { 100, 100, 120, 120 }; 29 | RECTA wall; 30 | RECTA player_1 = { 0, 790, 80, 800 }; 31 | 32 | void DrawRectangle(RECTA rect) 33 | { 34 | glBegin(GL_QUADS); 35 | glVertex2f(rect.left, rect.bottom); //Left - Bottom 36 | glVertex2f(rect.right, rect.bottom); 37 | glVertex2f(rect.right, rect.top); 38 | glVertex2f(rect.left, rect.top); 39 | glEnd(); 40 | } 41 | 42 | void Timer(int v) 43 | { 44 | /* ??? ???? ????? ?? ?? 5 ?? ????? ?? ??????? */ 45 | 46 | ball.left += Xspeed; 47 | ball.right += Xspeed; 48 | ball.top += Yspeed; 49 | ball.bottom += Yspeed; 50 | 51 | glutTimerFunc(1, Timer, 1); // ??? ????? ???? ??? ?? ??? ?????? ?????? ?????? 52 | } 53 | void drawText(char*string, int x, int y) 54 | { 55 | char *c; 56 | glPushMatrix(); 57 | glTranslatef(x, y, 0); 58 | glScalef(0.1, -0.1, 1); 59 | 60 | for (c = string; *c != '\0'; c++) 61 | { 62 | glutStrokeCharacter(GLUT_STROKE_ROMAN, *c); 63 | } 64 | glPopMatrix(); 65 | 66 | } 67 | 68 | int Test_Ball_Wall(RECTA ball, RECTA wall) 69 | { 70 | if (ball.right >= wall.right) 71 | return FROM_RIGHT; 72 | if (ball.left <= wall.left) 73 | return FROM_LEFT; 74 | if (ball.top <= wall.top) 75 | return FROM_TOP; 76 | if (ball.bottom >= wall.bottom) 77 | return FROM_BOTTOM; 78 | 79 | else return 0; 80 | } 81 | 82 | bool Test_Ball_Player(RECTA ball, RECTA player) 83 | { 84 | 85 | if (ball.bottom >= player.top && ball.left >= player.left && ball.right <= player.right) 86 | { 87 | playerResult++; 88 | if ((playerResult + 1) % 2 == 0 ) 89 | delta += 1; 90 | 91 | return true; 92 | } 93 | else if (ball.bottom >= player.bottom && (ball.left < player.left || ball.right > player.right)) 94 | { 95 | flag_pg = 1; 96 | glutPostRedisplay(); 97 | 98 | } 99 | return false; 100 | } 101 | 102 | static int mouse_x = 0; 103 | void MouseMotion(int x, int y) 104 | { 105 | 106 | mouse_x = x; 107 | } 108 | 109 | 110 | // OpenGL Setting 111 | void Setting(void) 112 | { 113 | glClearColor(0.0, 0.0, 0.0, 0.0); 114 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 115 | 116 | } 117 | 118 | 119 | // OnWindowResize 120 | void reshape(int w, int h) 121 | { 122 | WINDOW_WIDTH = w; 123 | WINDOW_HEIGHT = h; 124 | 125 | glViewport(0, 0, (GLsizei)w, (GLsizei)h); 126 | 127 | glMatrixMode(GL_PROJECTION); 128 | glLoadIdentity(); 129 | gluOrtho2D(0, w, h, 0); 130 | 131 | 132 | glMatrixMode(GL_MODELVIEW); 133 | glLoadIdentity(); 134 | 135 | } 136 | 137 | int pcResult = 0; 138 | 139 | void Render() 140 | { 141 | glClear(GL_COLOR_BUFFER_BIT); 142 | glLoadIdentity(); 143 | if (flag_pg == 0) 144 | { 145 | 146 | //sprintf(string, "PC : %d ", pcResult); 147 | //drawText(string, 10, 80); 148 | sprintf(string, "Player : %d ", playerResult); 149 | drawText(string, 10, 120); 150 | 151 | wall.left = wall.top = 0; 152 | wall.right = WINDOW_WIDTH; 153 | wall.bottom = WINDOW_HEIGHT; 154 | 155 | DrawRectangle(ball); 156 | 157 | if (Test_Ball_Wall(ball, wall) == FROM_RIGHT) 158 | Xspeed = -delta; 159 | 160 | if (Test_Ball_Wall(ball, wall) == FROM_LEFT) 161 | Xspeed = delta; 162 | 163 | if (Test_Ball_Wall(ball, wall) == FROM_TOP) 164 | Yspeed = delta; 165 | 166 | if (Test_Ball_Wall(ball, wall) == FROM_BOTTOM) 167 | { 168 | Yspeed = -delta; 169 | pcResult += 1; 170 | } 171 | 172 | DrawRectangle(player_1); 173 | player_1.left = mouse_x - 60; 174 | player_1.right = mouse_x + 60; 175 | 176 | 177 | if (Test_Ball_Player(ball, player_1) == true) 178 | Yspeed = -delta; 179 | } 180 | else if (flag_pg == 1) 181 | { 182 | drawText("GAME OVER", 450, 200); 183 | sprintf(string, "SCORE IS %d ", playerResult); 184 | drawText(string, 450, 250); 185 | } 186 | glutSwapBuffers(); 187 | } 188 | 189 | 190 | int main(int argc, char** argv) 191 | { 192 | glutInit(&argc, argv); 193 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 194 | glutInitWindowSize(1400, 1000); 195 | glutInitWindowPosition(200, 200); 196 | glutCreateWindow("DX BALL OpenGL game"); 197 | 198 | 199 | Setting(); 200 | 201 | 202 | 203 | glutDisplayFunc(Render); 204 | glutIdleFunc(Render); 205 | 206 | glutTimerFunc(1, Timer, 1); 207 | 208 | glutReshapeFunc(reshape); 209 | 210 | //glutKeyboardFunc(keyboard); 211 | 212 | //glutSpecialFunc(inputKey); 213 | 214 | glutPassiveMotionFunc(MouseMotion); 215 | 216 | 217 | glutMainLoop(); 218 | return 0; 219 | } 220 | -------------------------------------------------------------------------------- /22. Flag using Bezier Surface.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | double XMat[4][4] = {{ 100, 220, 240, 260}, 6 | {105, 225, 235, 255 }, 7 | {105, 225, 235, 255}, 8 | {100, 220, 240, 260}}; 9 | double YMat[4][4] = {{200, 200, 240, 150}, 10 | {230,250,250,230}, 11 | {280,260,260,230}, 12 | {300,320,320,200}}; 13 | double ZMat[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; 14 | 15 | void myInit(void) 16 | { 17 | glClearColor(0.0, 0.0, 0.0, 0.0); 18 | gluOrtho2D(0.0, 500.0, 0.0, 500.0); 19 | } 20 | 21 | void draw_pixel(int x, int y,float r, float g, float b ) { 22 | glBegin(GL_POINTS); 23 | glColor3f(r,g,b); 24 | glVertex2i(x, y); 25 | glEnd(); 26 | } 27 | 28 | 29 | void draw_curve() { 30 | //drawP(); 31 | double u,w; 32 | 33 | double x,y; 34 | double A[1][4],B[4][1]; 35 | for(u=0;u<=0.5;u=u+0.001) 36 | { 37 | for(w=0;w<=0.5;w=w+0.001) 38 | { 39 | A[0][0] = (1-u)*(1-u)*(1-u); 40 | A[0][1] = 3*u*(1-u)*(1-u); 41 | A[0][2] = 3*u*u*(1-u); 42 | A[0][3] = u*u*u; 43 | 44 | B[0][0] = (1-w)*(1-w)*(1-w); 45 | B[1][0] = 3*w*(1-w)*(1-w); 46 | B[2][0] = 3*w*w*(1-w); 47 | B[3][0] = 3*w*w*w; 48 | 49 | x = (A[0][0] * XMat[0][0] + A[0][1] * XMat[1][0] + A[0][2] * XMat[2][0] + A[0][3] * XMat[3][0]) * B[0][0] + (A[0][0] * XMat[0][1] + A[0][1] * XMat[1][1] + A[0][2] * XMat[2][1] + A[0][3] * XMat[3][1]) * B[1][0] + (A[0][0] * XMat[0][2] + A[0][1] * XMat[1][2] + A[0][2] * XMat[2][2] + A[0][3] * XMat[3][2]) * B[2][0] + (A[0][0] * XMat[0][3] + A[0][1] * XMat[1][3] + A[0][2] * XMat[2][3] + A[0][3] * XMat[3][3]) * B[3][0]; 50 | y = (A[0][0] * YMat[0][0] + A[0][1] * YMat[1][0] + A[0][2] * YMat[2][0] + A[0][3] * YMat[3][0]) * B[0][0] + (A[0][0] * YMat[0][1] + A[0][1] * YMat[1][1] + A[0][2] * YMat[2][1] + A[0][3] * YMat[3][1]) * B[1][0] + (A[0][0] * YMat[0][2] + A[0][1] * YMat[1][2] + A[0][2] * YMat[2][2] + A[0][3] * YMat[3][2]) * B[2][0] + (A[0][0] * YMat[0][3] + A[0][1] * YMat[1][3] + A[0][2] * YMat[2][3] + A[0][3] * YMat[3][3]) * B[3][0]; 51 | if(u<0.17) 52 | draw_pixel(x, y,0,1,0); 53 | else if(u>=0.17 && u<0.34) 54 | draw_pixel(x, y,1,1,1); 55 | else 56 | draw_pixel(x, y,0.74,0.17,0.07); 57 | } 58 | } 59 | 60 | 61 | 62 | 63 | } 64 | 65 | void myDisplay() { 66 | draw_curve(); 67 | glFlush(); 68 | } 69 | 70 | void main(int argc, char **argv) { 71 | 72 | //printf("Enter (x1, y1, x2, y2,x3,y3,x4,y4)\n"); 73 | //scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2,&x3,&y3,&x4,&y4); 74 | 75 | glutInit(&argc, argv); 76 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 77 | glutInitWindowSize(500, 500); 78 | glutInitWindowPosition(0, 0); 79 | glutCreateWindow("Cubic Bezier Curve"); 80 | myInit(); 81 | glutDisplayFunc(myDisplay); 82 | glutMainLoop(); 83 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Computer-Graphics-using-OpenGL 2 | This is a collection of the various programs that were written using the OpenGL library as a part of the Computer Graphics Lab. 3 | -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | #define ROUND(x) ((int)(x+0.5)) 10 | int xa,xb,ya,yb; 11 | void display (void) 12 | { 13 | int dx=xb-xa,dy=yb-ya,steps,k; 14 | float xIncrement,yIncrement,x=xa,y=ya; 15 | glClear (GL_COLOR_BUFFER_BIT); 16 | glColor3f (1.0, 0.0, 0.0); 17 | if(abs(dx)>abs(dy)) 18 | steps=abs(dx); 19 | else steps=abs(dy); 20 | xIncrement=dx/(float)steps; 21 | yIncrement=dy/(float)steps; 22 | glBegin(GL_POINTS); 23 | glVertex2s(ROUND(x),ROUND(y)); 24 | for(k=0;k 4 | #include 5 | #include 6 | #include 7 | 8 | // window size 9 | #define maxWD 640 10 | #define maxHT 480 11 | 12 | // rotation speed 13 | #define thetaSpeed 0.05 14 | 15 | int matX[4]={0,10,10,0}, matY[4] = {0,0,10,10}; 16 | int new_matX[4], new_matY[4]; 17 | int theta; 18 | // this creates delay between two actions 19 | /*void delay(unsigned int mseconds) 20 | { 21 | clock_t goal = mseconds + clock(); 22 | while (goal > clock()) 23 | ; 24 | }*/ 25 | 26 | // this is a basic init for the glut window 27 | void myInit(void) 28 | { 29 | glClearColor(1.0, 1.0, 1.0, 0.0); 30 | glOrtho(-1000.0, 1000.0, -1000.0, 1000.0, -1.0, 1.0); 31 | } 32 | 33 | void draw_pixel(int x, int y,float r,float g,float b) { 34 | glBegin(GL_POINTS); 35 | glColor3f(r, g, b); 36 | glVertex2i(x, y); 37 | glEnd(); 38 | } 39 | // this function just draws a point 40 | void drawPoint(int x, int y,float r,float g,float b) 41 | { 42 | glPointSize(4.0); 43 | glColor3f(r, g, b); 44 | glBegin(GL_LINES); 45 | glVertex2i(x, y); 46 | glEnd(); 47 | } 48 | 49 | 50 | 51 | void drawSq() 52 | { 53 | int i,j; 54 | for(i=-500;i<=500;i++) 55 | { 56 | for(j=-500;j<=500;j++) 57 | { 58 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 59 | draw_pixel(i,j,1,1,0); 60 | else 61 | draw_pixel(i,j,0,1,0); 62 | } 63 | } 64 | } 65 | 66 | void rotateSq(){ 67 | int i, j,x,y; 68 | float angle = 0.0; 69 | angle = ((float)theta * 3.14159) / 180.0; 70 | float radius=70.7; 71 | float ang_sec=angle/10; 72 | float pos; 73 | for(i=-500;i<=500;i++){ 74 | for(j=-500;j<=500;j++){ 75 | pos=i*i+j*j; 76 | if(pos<=(radius*radius)){ 77 | x = (i * cos(ang_sec)) - (j * sin(ang_sec)); 78 | y = (j * cos(ang_sec)) + (i * sin(ang_sec)); 79 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 80 | draw_pixel(x,y,1,1,0); 81 | else 82 | draw_pixel(x,y,0,1,0); 83 | }else if(pos>(radius*radius) && pos<=(radius*radius*4)){ 84 | x = (i * cos(ang_sec*2)) - (j * sin(ang_sec*2)); 85 | y = (j * cos(ang_sec*2)) + (i * sin(ang_sec*2)); 86 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 87 | draw_pixel(x,y,1,1,0); 88 | else 89 | draw_pixel(x,y,0,1,0); 90 | }else if(pos>(radius*radius*4) && pos<=(radius*radius*9)){ 91 | x = (i * cos(ang_sec*3)) - (j * sin(ang_sec*3)); 92 | y = (j * cos(ang_sec*3)) + (i * sin(ang_sec*3)); 93 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 94 | draw_pixel(x,y,1,1,0); 95 | else 96 | draw_pixel(x,y,0,1,0); 97 | }else if(pos>(radius*radius*9) && pos<=(radius*radius*16)){ 98 | x = (i * cos(ang_sec*4)) - (j * sin(ang_sec*4)); 99 | y = (j * cos(ang_sec*4)) + (i * sin(ang_sec*4)); 100 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 101 | draw_pixel(x,y,1,1,0); 102 | else 103 | draw_pixel(x,y,0,1,0); 104 | }else if(pos>(radius*radius*16) && pos<=(radius*radius*25)){ 105 | x = (i * cos(ang_sec*5)) - (j * sin(ang_sec*5)); 106 | y = (j * cos(ang_sec*5)) + (i * sin(ang_sec*5)); 107 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 108 | draw_pixel(x,y,1,1,0); 109 | else 110 | draw_pixel(x,y,0,1,0); 111 | }else if(pos>(radius*radius*25) && pos<=(radius*radius*36)){ 112 | x = (i * cos(ang_sec*6)) - (j * sin(ang_sec*6)); 113 | y = (j * cos(ang_sec*6)) + (i * sin(ang_sec*6)); 114 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 115 | draw_pixel(x,y,1,1,0); 116 | else 117 | draw_pixel(x,y,0,1,0); 118 | }else if(pos>(radius*radius*36) && pos<=(radius*radius*49)){ 119 | x = (i * cos(ang_sec*7)) - (j * sin(ang_sec*7)); 120 | y = (j * cos(ang_sec*7)) + (i * sin(ang_sec*7)); 121 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 122 | draw_pixel(x,y,1,1,0); 123 | else 124 | draw_pixel(x,y,0,1,0); 125 | }else if(pos>(radius*radius*49) && pos<=(radius*radius*64)){ 126 | x = (i * cos(ang_sec*8)) - (j * sin(ang_sec*8)); 127 | y = (j * cos(ang_sec*8)) + (i * sin(ang_sec*8)); 128 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 129 | draw_pixel(x,y,1,1,0); 130 | else 131 | draw_pixel(x,y,0,1,0); 132 | }else if(pos>(radius*radius*64) && pos<=(radius*radius*81)){ 133 | x = (i * cos(ang_sec*9)) - (j * sin(ang_sec*9)); 134 | y = (j * cos(ang_sec*9)) + (i * sin(ang_sec*9)); 135 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 136 | draw_pixel(x,y,1,1,0); 137 | else 138 | draw_pixel(x,y,0,1,0); 139 | }else if(pos>(radius*radius*81) && pos<=(radius*radius*100)){ 140 | x = (i * cos(ang_sec*10)) - (j * sin(ang_sec*10)); 141 | y = (j * cos(ang_sec*10)) + (i * sin(ang_sec*10)); 142 | if((i>=-50 && i<= 50) || (j>=-50 && j<=50)) 143 | draw_pixel(x,y,1,1,0); 144 | else 145 | draw_pixel(x,y,0,1,0); 146 | } 147 | } 148 | } 149 | } 150 | void drawaxis() 151 | { 152 | for (int i = -100; i <= 100; i++) //X and Y axis 153 | { 154 | draw_pixel(i, 0,1,1,1); 155 | draw_pixel(0, i,1,1,1); 156 | 157 | } 158 | for (int i = -2; i <= 2; i++) 159 | { 160 | draw_pixel(95 + i, 4 + i,1,1,1); 161 | draw_pixel(95 - i, 4 + i,1,1,1); 162 | 163 | } 164 | for (int i = 0; i <= 2; i++) 165 | { 166 | draw_pixel(4 + i, 95 + i,1,1,1); 167 | draw_pixel(4 - i, 95 + i,1,1,1); 168 | draw_pixel(4, 95 - i,1,1,1); 169 | 170 | } 171 | } 172 | 173 | 174 | // Actual display function 175 | void myDisplay(void) 176 | { 177 | int opt, i; 178 | int tx, ty; 179 | int rotX, rotY; 180 | int sx, sy; 181 | 182 | 183 | 184 | glClear(GL_COLOR_BUFFER_BIT); 185 | //drawP(matX, matY,0,0,1); 186 | drawaxis(); 187 | //drawSq(); 188 | printf("Enter the angle: "); 189 | scanf("%d", &theta); 190 | rotateSq(); 191 | glFlush(); 192 | 193 | 194 | 195 | 196 | 197 | } 198 | 199 | void main(int argc, char** argv) 200 | { 201 | glutInit(&argc, argv); 202 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 203 | glutInitWindowSize(maxWD, maxHT); 204 | glutInitWindowPosition(100, 150); 205 | glutCreateWindow("Transforming point"); 206 | glutDisplayFunc(myDisplay); 207 | myInit(); 208 | glutMainLoop(); 209 | } --------------------------------------------------------------------------------