Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 kB
1
Indexable
Never
1 #include<stdio.h> 
#include<math.h> 
#include<GL/glut.h> 
GLint xStart, yStart, xEnd, yEnd; 
void init(); 
void LineBres_L1(GLint, GLint, GLint, GLint, GLfloat); 
void LineBres_GE1(GLint, GLint, GLint, GLint, GLfloat); 
void setPixel(GLint, GLint); 
void display(); 
void main(int argc, char** argv) 
{ 
printf("enter two points for draw lineBresenham:\n"); 
printf("\n enter point1(xStart,yStart):"); 
scanf_s("%d%d", &xStart, &yStart); 
printf("\n enter point2(xEnd,yEnd):"); 
scanf_s("%d%d", &xEnd, &yEnd); 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(300, 400); 
glutInitWindowPosition(0, 0); 
glutCreateWindow("LineBresenham"); 
init(); 
glutDisplayFunc(display); 
glutMainLoop(); 
} 
void display() 
{ 
glClear(GL_COLOR_BUFFER_BIT); 
GLfloat m; 
m = (float)(yEnd - yStart) / (xEnd - xStart); 
if (fabs(m) >= 1) 
LineBres_GE1(xStart, yStart, xEnd, yEnd, m); 
else 
LineBres_L1(xStart, yStart, xEnd, yEnd, m); 
} 
void LineBres_L1(GLint x0, GLint y0, GLint x1, GLint y1, GLfloat m) { 
GLint dx = abs(x1 - x0); 
GLint dy = abs(y1 - y0); 
GLint p = 2 * dy - dx; 
GLint twoDy = 2 * dy; 
GLint twoDyMinusDx = 2 * (dy - dx); 
GLint x = x0, y = y0; 
if (x0 > x1) 
{ 
x = x1; 
y = y1; 
x1 = x0; 
} 
else
{ 
x = x0; 
y = y0; 
} 
setPixel(x, y); 
while (x < x1) 
{ 
if (p < 0) 
{ 
p += twoDy; 
x++; 
} 
else 
{ 
p += twoDyMinusDx; 
x++; 
if (m < 0) 
y--; 
else 
y++; 
} 
setPixel(x, y); 
} 
} 
void LineBres_GE1(GLint x0, GLint y0, GLint x1, GLint y1, GLfloat m) { 
GLint dx = abs(x1 - x0); 
GLint dy = abs(y1 - y0); 
GLint p = 2 * dx - dy; 
GLint twoDx = 2 * dx; 
GLint twoDxMinusDy = 2 * (dx - dy); 
GLint x = x0, y = y0; 
if (y0 > y1) 
{ 
x = x1; 
y = y1; 
y1 = y0; 
} 
else 
{ 
x = x0; 
y = y0; 
} 
setPixel(x, y); 
while (y < y1) 
{ 
if (p < 0) 
{ 
p += twoDx; 
y++; 
} 
else 
{ 
p += twoDxMinusDy;
y++; 
if (m < 0) 
x--; 
else 
x++; 
} 
setPixel(x, y); 
} 
} 
void setPixel(GLint xCoordinate, GLint yCoordinate) { 
glBegin(GL_POINTS); 
glVertex2i(xCoordinate, yCoordinate); 
glEnd(); 
glFlush(); 
} 
void init() 
{ 
glClearColor(1.0, 1.0, 1.0, 0); 
glColor3f(0.0, 0.0, 0.0); 
glPointSize(8.0); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D(-50, 50, -50, 50); 
}
























2
#include<stdio.h> 
#include<math.h> 
#include<GL/glut.h> 
GLfloat t[3][3] = { { 10,20,20},{ 10,10,20}, {1,1,1} }; 
GLfloat rt[3][3] = { { 0 },{ 0 },{ 0 } }; 
GLfloat res[3][3] = { { 0 },{ 0 },{ 0 } }; 
GLfloat xr = 10; 
GLfloat yr = 20; 
GLfloat th; 
GLint ch; 
void multiply() 
{ 
int i, j, k; 
for (i = 0; i < 3; i++) 
for (j = 0; j < 3; j++) 
{ 
res[i][j] = 0; 
for (k = 0; k < 3; k++) 
res[i][j] = res[i][j] + rt[i][k] * t[k][j]; 
} 
} 
void rotate_about_origin() 
{ 
 rt[0][0] = cos(th); 
rt[0][1] = -sin(th); 
rt[0][2] = 0; 
rt[1][0] = sin(th); 
rt[1][1] = cos(th); 
rt[1][2] = 0; 
rt[2][0] = 0; 
rt[2][1] = 0; 
rt[2][2] = 1; 
multiply(); 
} 
void rotate_about_fixed_point() 
{ 
GLfloat m, n; 
m = xr * (1 - cos(th)) + yr * sin(th); 
n = yr * (1 - cos(th)) - xr * sin(th); 
rt[0][0] = cos(th); 
rt[0][1] = -sin(th); 
rt[0][2] = m; 
rt[1][0] = sin(th); 
rt[1][1] = cos(th); 
rt[1][2] = n; 
rt[2][0] = 0; 
rt[2][1] = 0; 
rt[2][2] = 1; 
multiply();
} 
void draw_triangle() 
{ 
glLineWidth(5); 
glBegin(GL_LINE_LOOP); 
glColor3f(1, 0, 0); 
glVertex2f(t[0][0], t[1][0]); 
glColor3f(0, 1, 0); 
glVertex2f(t[0][1], t[1][1]); 
glColor3f(0, 0, 1); 
glVertex2f(t[0][2], t[1][2]); 
glEnd(); 
glFlush(); 
} 
void draw_rotated_triangle() 
{ 
glLineWidth(5); 
glBegin(GL_LINE_LOOP); 
glColor3f(1, 0, 0); 
glVertex2f(res[0][0], res[1][0]); glColor3f(0, 1, 0); 
glVertex2f(res[0][1], res[1][1]); glColor3f(0, 0, 1); 
glVertex2f(res[0][2], res[1][2]); glEnd(); 
glFlush(); 
} 
void display() 
{ 
glClear(GL_COLOR_BUFFER_BIT); 
if (ch == 1) 
{ 
draw_triangle(); 
rotate_about_origin(); 
draw_rotated_triangle(); 
glFlush(); 
} 
if (ch == 2) 
{ 
draw_triangle(); 
rotate_about_fixed_point(); 
draw_rotated_triangle(); 
glFlush(); 
} 
} 
void myinit() 
{ 
glClearColor(1, 1, 1, 1); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D(-50, 50, -50, 50); 
} 
int main(int argc, char** argv)
{ 
printf("***Rotation***\n1.Rotation about origin\n2.Rotation about a fixed point(xr, yr)\n"); 
printf("Enter the choice\n"); 
scanf_s("%d", &ch); 
printf("Enter rotation angle\n"); 
scanf_s("%f", &th); 
th = th * 3.14 / 180; 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(500, 500); 
glutInitWindowPosition(0, 0); 
glutCreateWindow("Rotation of Triangle"); 
glutDisplayFunc(display); 
myinit(); 
glutMainLoop(); 
return 0; 
}



































3.#include<stdlib.h> 
#include<GL/glut.h> 
GLfloat ver[8][3] = {{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1},{-1,-1,-1},{-1,1,-1},{1,1,-1  },{ 1,-1,-1}}; 
GLfloat col[][3] = {{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}}; 
void polygon(int a, int b, int c, int d) 
{ 
glBegin(GL_POLYGON); 
glColor3fv(col[a]); 
glVertex3fv(ver[a]); 
glColor3fv(col[b]); 
glVertex3fv(ver[b]); 
glColor3fv(col[c]); 
glVertex3fv(ver[c]); 
glColor3fv(col[d]); 
glVertex3fv(ver[d]); 
glEnd(); 
} 
void colorCube(void) 
{ 
polygon(0, 3, 2, 1); 
polygon(4, 5, 6, 7); 
polygon(2, 3, 7, 6); 
polygon(1, 5, 4, 0); 
polygon(1, 2, 6, 5); 
polygon(0, 4, 7, 3); 
} 
static GLfloat th[] = {0,0,0}; 
static GLint axis = 0; 
void display(void) 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity(); 
glRotatef(th[0], 1, 0, 0); 
glRotatef(th[1], 0, 1, 0); 
glRotatef(th[2], 0, 0, 1); 
colorCube(); 
glFlush(); 
glutSwapBuffers(); 
} 
void spinCube() 
{ 
th[axis] = th[axis] + 0.1; 
if (th[axis] > 360) 
th[axis] = th[axis] - 360; 
glutPostRedisplay(); 
} 
void mouse(int btn, int state, int x, int y) 
{ 
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
axis = 0; 
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) 
axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; 
} 
void myReshape(int w, int h) 
{ 
glViewport(0, 0, w, h); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(-2, 2, -2, 2, -10, 10); 
glMatrixMode(GL_MODELVIEW); 
} 
void main(int argc, char** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("colorCube"); 
glutInitWindowSize(700, 700); 
glutReshapeFunc(myReshape); 
glutDisplayFunc(display); 
glutIdleFunc(spinCube); 
glutMouseFunc(mouse); 
glEnable(GL_DEPTH_TEST); 
glutMainLoop(); 
}



























4.#include<stdlib.h> 
#include<GL/glut.h> 
GLfloat ver[][3] = { {-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1},{-1,-1,-1},{-1,1,-1},{1,1,-1  },{ 1,-1,-1} }; 
GLfloat col[][3] = { {0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}  }; 
void polygon(int a, int b, int c, int d) 
{ 
glBegin(GL_POLYGON); 
glColor3fv(col[a]); 
glVertex3fv(ver[a]); 
glColor3fv(col[b]); 
glVertex3fv(ver[b]); 
glColor3fv(col[c]); 
glVertex3fv(ver[c]); 
glColor3fv(col[d]); 
glVertex3fv(ver[d]); 
glEnd(); 
} 
void colorcube() 
{ 
polygon(0, 3, 2, 1); 
polygon(4, 5, 6, 7); 
polygon(2, 3, 7, 6); 
polygon(1, 5, 4, 0); 
polygon(1, 2, 6, 5); 
polygon(0, 4, 7, 3); 
} 
static GLfloat theta[] = { 0.0,0.0,0.0 }; 
static GLint axis = 2; 
static GLdouble viewer[] = { 0.0, 0.0, 5.0 }; 
void display(void) 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity(); 
gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glRotatef(theta[0], 1.0, 0.0, 0.0); 
glRotatef(theta[1], 0.0, 1.0, 0.0); 
glRotatef(theta[2], 0.0, 0.0, 1.0); 
colorcube(); 
glFlush(); 
glutSwapBuffers(); 
} 
void mouse(int btn, int state, int x, int y) 
{ 
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; 
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; 
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; 
theta[axis] += 2.0; 
if (theta[axis] > 360.0) theta[axis] -= 360.0; 
display(); 
}
void keys(unsigned char key, int x, int y) 
{ 
if (key == 'x') viewer[0] -= 1.0; 
if (key == 'X') viewer[0] += 1.0; 
if (key == 'y') viewer[1] -= 1.0; 
if (key == 'Y') viewer[1] += 1.0; 
if (key == 'z') viewer[2] -= 1.0; 
if (key == 'Z') viewer[2] += 1.0; 
display(); 
} 
void myReshape(int w, int h) 
{ 
glViewport(0, 0, w, h); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
if (w <= h) 
{ 
glFrustum(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w, 2.0 * (GLfloat)h /  (GLfloat)w, 
2.0, 20.0); 
} 
else 
{ 
glFrustum(-2.0, 2.0, -2.0 * (GLfloat)w / (GLfloat)h, 2.0 * (GLfloat)w /  (GLfloat)h, 
2.0, 20.0); 
} 
glMatrixMode(GL_MODELVIEW); 
} 
void main(int argc, char** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
glutInitWindowSize(750, 750); 
glutCreateWindow("Colorcube Viewer"); 
glutReshapeFunc(myReshape); 
glutDisplayFunc(display); 
glutMouseFunc(mouse); 
glutKeyboardFunc(keys); 
glEnable(GL_DEPTH_TEST); 
glutMainLoop(); 
}















5 #include<stdio.h> 
#include<GL/glut.h> 
float xmin = 50, ymin = 50, xmax = 100, ymax = 100; /Window boundaries float xvmin = 200, yvmin = 200, xvmax = 400, yvmax = 400; /Viewport boundaries 
int RIGHT = 8, LEFT = 2, TOP = 4, BOTTOM = 1; //code for TBRL 
float sx, sy, vx1, vy1, vx2, vy2; 
float X1, Y1, X2, Y2; 
int compute(float x, float y) 
{ 
int code = 0; 
if (y > ymax) 
code = TOP; 
else if (y < ymin) 
code = BOTTOM; 
if (x > xmax) 
code = RIGHT; 
else if (x < xmin) 
code = LEFT; 
return code; 
} 
void cohen(float X1, float Y1, float X2, float Y2) 
{ 
float x, y; 
int accept = 0, done = 0, code_p, code_q, code; 
code_p = compute(X1, Y1); 
code_q = compute(X2, Y2); 
do 
{ 
if (!(code_p | code_q)) 
{ 
accept = 1; 
done = 1; 
} 
else if (code_p & code_q) 
done = 1; 
else 
{ 
code = code_p ? code_p : code_q; 
if (code & TOP) 
{ 
x = X1 + (X2 - X1) * (ymax - Y1) / (Y2 - Y1); 
y = ymax; 
} 
else if (code & BOTTOM) 
{ 
x = X1 + (X2 - X1) * (ymin - Y1) / (Y2 - Y1);
y = ymin; 
} 
else if (code & RIGHT) 
{ 
y = Y1 + (Y2 - Y1) * (xmax - X1) / (X2 - X1); 
x = xmax; 
} 
else 
{ 
y = Y1 + (Y2 - Y1) * (xmin - X1) / (X2 - X1); 
x = xmin; 
} 
if (code == code_p) 
{ 
X1 = x; 
Y1 = y; 
code_p = compute(X1, Y1); 
} 
else 
{ 
X2 = x; 
Y2 = y; 
code_q = compute(X2, Y2); 
} 
} 
} while (!done); 
if (accept) 
{ 
sx = (xvmax - xvmin) / (xmax - xmin); 
sy = (yvmax - yvmin) / (ymax - ymin); 
vx1 = xvmin + (X1 - xmin) * sx; 
vy1 = xvmin + (Y1 - ymin) * sy; 
vx2 = xvmin + (X2 - xmin) * sx; 
vy2 = xvmin + (Y2 - ymin) * sy; 
} 
} 
void display() 
{ 
glClear(GL_COLOR_BUFFER_BIT); 
glColor3f(1, 1, 1); 
glLineWidth(2); 
// The below code is used to draw entered lines 
glBegin(GL_LINES); 
glVertex2d(X1, Y1); 
glVertex2d(X2, Y2); 
glEnd(); 
glColor3f(1, 1, 1); 
// The below code is used to draw a window. 
glBegin(GL_LINE_LOOP); 
glVertex2f(xmin, ymin); 
glVertex2f(xmax, ymin); 
glVertex2f(xmax, ymax); 
glVertex2f(xmin, ymax);
glEnd(); 
cohen(X1, Y1, X2, Y2); 
glColor3f(1, 1, 1); 
// The below code is for the view port  glBegin(GL_LINE_LOOP); 
glVertex2f(xvmin, yvmin); 
glVertex2f(xvmax, yvmin); 
glVertex2f(xvmax, yvmax); 
glVertex2f(xvmin, yvmax); 
glEnd(); 
glColor3f(1, 1, 1); 
// The clipped coordinates at the viewport. glBegin(GL_LINES); 
glVertex2d(vx1, vy1); 
glVertex2d(vx2, vy2); 
glEnd(); 
glFlush(); 
} 
void myinit() 
{ 
glClearColor(0, 0, 0, 1); 
gluOrtho2D(0, 500, 0, 500); 
} 
void main(int argc, char** argv) 
{ 
printf("\n Enter the points\n"); 
scanf_s("%f%f%f%f", &X1, &Y1, &X2, &Y2); glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); 
glutCreateWindow("cohen sutherland"); 
glutDisplayFunc(display); 
myinit(); 
glutMainLoop(); 
}