CG2
CG2
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glVertex2i(400, 700);
glEnd();
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(0.0f, 1.07f, 0.0f);
glVertex3f(1.07f, 0.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
void initOpenGL() {
glEnable(GL_DEPTH_TEST);
glClearColor(0.2, 0.2, 0.2, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 800.0 / 600.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
initOpenGL();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#include <glut.h>
#include <stdio.h>
void init(void) {
GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.2 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(20.0, 1.0, 0.0, 0.0);
//CONE
glPushMatrix();
glTranslatef(-0.7, 0.5, 0.0);
glRotatef(270.0, 1.0, 0.0, 0.0);
glutSolidCone(0.8, 2.1, 15, 15);
glPopMatrix();
//CUBE
glPushMatrix();
glTranslatef(-0.75, -0.5, 0.0);
glRotatef(85, 1.0, 0.0, 0.0);
glRotatef(-20, 0.0, 0.0, 1.0);
glutSolidCube(1.7);
glPopMatrix();
//SPHERE
glPushMatrix();
glTranslatef(0.5, 0.0, 3.0);
glutSolidSphere(1.0, 30, 30);
glPopMatrix();
//CYLINDER
GLUquadricObj* qobj;
qobj = gluNewQuadric();
glPushMatrix();
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricNormals(qobj, GLU_SMOOTH);
glTranslatef(0.7, 0.0, -4.5);
glRotatef(90, 1.0, 0.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
gluCylinder(qobj, 0.85, 0.85, 3.0, 6, 6);
glPopMatrix();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.5, 2.5, -2.5 * (GLfloat)h / (GLfloat)w, 2.5 * (GLfloat)h /
(GLfloat)w, -10.0, 10.0);
else
glOrtho(-2.5 * (GLfloat)w / (GLfloat)h, 2.5 * (GLfloat)w / (GLfloat)h,
-2.5, 2.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Solid objects");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#include<glut.h>
void obj(double tx, double ty, double tz, double sx, double sy, double sz)
{
glRotated(50, 0, 1, 0);
glRotated(10, -1, 0, 0);
glRotated(11.7, 0, 0, -1);
glTranslated(tx, ty, tz);
glScaled(sx, sy, sz);
glutSolidCube(1);
glLoadIdentity();
}
void display()
{
glViewport(0, 0, 700, 700);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
obj(0, 0, 0.5, 1, 1, 0.04); // 3 walls
obj(0, -0.5, 0, 1, 0.04, 1);
obj(-0.5, 0, 0, 0.04, 1, 1);
obj(0, -0.3, 0, 0.02, 0.2, 0.02); //4 table legs
obj(0, -0.3, -0.4, 0.02, 0.2, 0.02);
obj(0.4, -0.3, 0, 0.02, 0.2, 0.02);
obj(0.4, -0.3, -0.4, 0.02, 0.2, 0.02);
obj(0.2, -0.18, -0.2, 0.6, 0.02, 0.6); //table surface
glRotated(50, 0, 1, 0);
glRotated(10, -1, 0, 0);
glRotated(11.7, 0, 0, -1);
glTranslated(0.3, -0.1, -0.3);
glutSolidTeapot(0.15);
glFlush();
glLoadIdentity();
}
void main(int argc, char** argv)
{
float ambient[] = { 1,1,1,1 };
float light_pos[] = { 27,80,2,3 };
glutInitWindowSize(700, 700);
glutCreateWindow("Teapot");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
#include <stdio.h>
#include <glut.h>
double xmin = 50, ymin = 50, xmax = 100, ymax = 100;
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300;
const int RIGHT = 8;
const int LEFT = 2;
const int TOP = 4;
const int BOTTOM = 1;
int ComputeOutCode(double x, double y)
{
int code = 0; if (y > ymax)
code |= TOP;
//above the clip window
else if (y < ymin)
code |= BOTTOM;
//below the clip window
if (x > xmax)
code |= RIGHT;
//to the right of clip window
else if (x < xmin)
code |= LEFT;
//to the left of clip window
return code;
}
void CohenSutherland(double x0, double y0, double x1, double y1)
{
int outcode0, outcode1, outcodeOut;
int accept = 0, done = 0;
outcode0 = ComputeOutCode(x0, y0);
outcode1 = ComputeOutCode(x1, y1);
do {
if (!(outcode0 | outcode1))
{
accept = 1; done = 1;
}
else if (outcode0 & outcode1) done = 1;
else {
double x, y;
outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP)
{
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM)
{
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
{
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
}
else
{
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0)
{
x0 = x; y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else {
x1 = x; y1 = y;
outcode1 = ComputeOutCode(x1, y1);
}
}
} while (!done);
if (accept)
{
double sx = (xvmax - xvmin) / (xmax - xmin);
double sy = (yvmax - yvmin) / (ymax - ymin);
double vx0 = xvmin + (x0 - xmin) * sx;
double vy0 = yvmin + (y0 - ymin) * sy;
double vx1 = xvmin + (x1 - xmin) * sx;
double vy1 = yvmin + (y1 - ymin) * sy;
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd(); glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2d(vx0, vy0);
glVertex2d(vx1, vy1);
glEnd();
}
}
void display()
{
double x0 = 60, y0 = 20, x1 = 80, y1 = 120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES); glVertex2d(x0, y0);
glVertex2d(x1, y1); glEnd();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherland(x0, y0, x1, y1);
glFlush();
}
void myinit()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Cohen Suderland Line Clipping Algorithm");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
#include<stdlib.h>
#include<glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
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);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
glFlush();
glutSwapBuffers();
}
void spinCube()
{
theta[axis] += 0.05;
if (theta[axis] > 360.0) theta[axis] -= 360.0;
glutPostRedisplay();
}
#include <glut.h>
GLfloat vertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
};
GLubyte cubeIndices[] = {
0, 3, 2, 1,
2, 3, 7, 6,
0, 4, 7, 3,
1, 2, 6, 5,
4, 5, 6, 7,
0, 1, 5, 4
};
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
if (w <= h)
glOrtho(-2, 2, -2 * (GLfloat)h / (GLfloat)w, 2 * (GLfloat)h / (GLfloat)w,
-10, 10);
else
glOrtho(-2 * (GLfloat)w / (GLfloat)h, 2 * (GLfloat)w / (GLfloat)h, -2, 2,
-10, 10);
glMatrixMode(GL_MODELVIEW);
}
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
7. Draw a colored cube and allow the user to move the camera suitably to experiment
with perspective viewing.
#include <glut.h>
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(squareX, squareY, 0.0f);
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.1f, -0.1f);
glVertex2f(0.1f, -0.1f);
glVertex2f(0.1f, 0.1f);
glVertex2f(-0.1f, 0.1f);
glEnd();
glutSwapBuffers();
}
lastX = x;
lastY = y;
glutPostRedisplay();
}
}
void initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
}
initOpenGL();
glutDisplayFunc(display);
glutMotionFunc(mouseMotion);
glutMouseFunc(mouseButton);
glutMainLoop();
return 0;
}
9. Implement a program to display the wireframe model of a sphere.
#include <glut.h>
GLsizei winWidth = 500, winHeight = 500;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void wireQuadSurfs(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(1.0, 1.0, 0.0);
glutWireSphere(1.0, 8, 6);
glPopMatrix();
glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glOrtho(-2.0, 2.0, -2.0, 2.0, 0.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Wire-Frame Quadric Surfaces");
init();
glutDisplayFunc(wireQuadSurfs);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
}