CG SEA
CG SEA
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if (showCone) {
glPushMatrix();
glTranslatef(0.0f, 0.2f, 0.0f);
glRotatef(rotationY, 0.0f, 1.0f, 0.0f);
glRotatef(rotationX, 1.0f, 0.0f, 0.0f);
glColor3f(0.7f, 0.3f, 0.3f);
glutSolidCone(1.0, 2.0, 20, 20);
glPopMatrix();
}
else {
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
gluDisk(qobj, 0.0, 0.7, 20, 1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 0.0f, 2.0f);
gluDisk(qobj, 0.0, 0.7, 20, 1);
glPopMatrix();
gluDeleteQuadric(qobj);
glPopMatrix();
}
glFlush();
glutSwapBuffers();
}
void init() {
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);
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#include <glut.h>
float rotationX = 0.0f;
float rotationY = 0.0f;
float scale = 1.0f;
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();
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(rotationX, 1.0f, 0.0f, 0.0f);
glRotatef(rotationY, 0.0f, 1.0f, 0.0f);
glScalef(scale, scale, scale);
glutSolidSphere(1.0, 30, 30);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(0.0f, 1.07f, 0.0f);
glVertex3f(1.07f, 0.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
rotationX -= 5.0f;
break;
case 's':
rotationX += 5.0f;
break;
case 'a':
rotationY -= 5.0f;
break;
case 'd':
rotationY += 5.0f;
break;
case '+':
scale += 0.1f;
break;
case '-':
scale = (scale > 0.1f) ? scale - 0.1f : scale;
break;
}
glutPostRedisplay();
}
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);
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Sphere Interaction");
initOpenGL();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
3. Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing
#include <glut.h>
float v[] = { -1,-1,-1,
1,1,1,
1,-1,1 };-1,1,-1, 1,1,-1,
1,-1,-1, -1,-1,1, -1,1,1,
float c[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, };
GLubyte list[] = { 0,1,2,3, 2,3,7,6, 4,5,6,7, 4,5,1,0, 5,6,2,1, 0,3,7,4
};
int gx = 0, gy = 0, gz = 1;
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);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, list);
glFlush();
}
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 main()
{
glutInitWindowSize(700, 700);
glutCreateWindow("Colorcube Viewer");
glMatrixMode(GL_PROJECTION);
glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, v);
glColorPointer(3, GL_FLOAT, 0, c);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
#include <glut.h>
#include <stdio.h>
float x1 = 150.0, y1 = 150.0, x2 = 250.0, y2 = 150.0, x3 = 250.0, y3 = 250.0, x4 =
150.0, y4 = 250.0;
float tX = 0.0, tY = 0.0, sX=0.0, sY=0.0;
bool translated = false;
bool scaled = false;
void drawSquare(float x1, float y1, float x2, float y2, float x3, float y3, float
x4, float y4) {
glBegin(GL_QUADS);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glVertex2f(x4, y4);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(tX, tY, 0.0);
glColor3f(1.0, 0.0, 0.0);
drawSquare(x1, y1, x2, y2, x3, y3, x4, y4);
glPopMatrix();
}
if (scaled)
{
glPushMatrix();
glScalef(sX, sY, 0.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare(x1, y1, x2, y2, x3, y3, x4, y4);
glPopMatrix();
}
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
}
void onKeyPress(unsigned char key, int x, int y) {
if (key == 't') {
printf("Enter the tX and tY value for translation: ");
scanf_s("%f %f", &tX, &tY);
translated = true;
scaled = false;
glutPostRedisplay();
}
if(key == 's') {
printf("Enter the sX and sY value for scaling: ");
scanf_s("%f %f", &sX, &sY);
scaled = true;
translated = false;
glutPostRedisplay();
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("2D Object Translation");
init();
glutDisplayFunc(display);
glutKeyboardFunc(onKeyPress);
glutMainLoop();
return 0;
}
#include<stdlib.h>
#include<glut.h>
GLfloat vertices[] = {
-1.0f, -1.0f, -1.0f, // 0
1.0f, -1.0f, -1.0f, // 1
1.0f, 1.0f, -1.0f, // 2
-1.0f, 1.0f, -1.0f, // 3
-1.0f, -1.0f, 1.0f, // 4
1.0f, -1.0f, 1.0f, // 5
1.0f, 1.0f, 1.0f, // 6
-1.0f, 1.0f, 1.0f // 7
};
GLfloat colors[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0,0.0,
1.0, 1.0, 1.0,1.0, 0.0,1.0,1.0 };
GLubyte cubeIndices[] = {
0, 1, 2, 3, // Front face
4, 5, 6, 7, // Back face
0, 4, 7, 3, // Left face
1, 5, 6, 2, // Right face
3, 2, 6, 7, // Top face
0, 1, 5, 4 // Bottom face
};
static GLfloat theta[] = { 0.0,0.0,0.0 };
static GLint axis = 2;
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();
}
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 main(int argc, char** argv)
{
glutInitWindowSize(500, 500);
glutCreateWindow("Spin a color cube");
glMatrixMode(GL_PROJECTION);
glOrtho(-2.0, 2.0, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glutMainLoop();
}
#include <glut.h>
#include <cmath>
void bresenhamLine(float x1, float y1, float x2, float y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x2 >= x1) ? 1 : -1;
int sy = (y2 >= y1) ? 1 : -1;
bool steep = dy > dx;
if (steep) {
int t = dx;
dx = dy;
dy = t;
}
int p = 2 * dy - dx;
int x = x1;
int y = y1;
glBegin(GL_POINTS);
for (int i = 0; i <= dx; i++) {
glVertex2i(x, y);
if (p >= 0) {
if (steep)
x += sx;
else
y += sy;
p -= 2 * dx;
}
if (steep)
y += sy;
else
x += sx;
p += 2 * dy;
}
glEnd();
glFlush();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
bresenhamLine(150,150,300,200); //m<1
bresenhamLine(150,150,300,300); //m=1
bresenhamLine(150,150,200,300); //m>1
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(2.0);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
7. Create and rotate a triangle about the origin and a fixed point.
#include<glut.h>
#include<stdio.h>
int x, y, rFlag = 0;
void draw_pixel(float x1, float y1)
{
glColor3f(0.0, 0.0, 1.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2f(x1, y1);
glEnd();
}
void triangle()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(100, 100);
glVertex2f(250, 400);
glVertex2f(400, 100);
glEnd();
}
float th = 0.0;
float trX = 0.0, trY = 0.0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
if (rFlag == 1)
{
trX = 0.0;
trY = 0.0;
th += 0.1;
draw_pixel(0.0, 0.0);
}
if (rFlag == 2)
{
trX = x;
trY = y;
th += 0.1;
draw_pixel(x,y);
}
glTranslatef(trX, trY, 0.0);
glRotatef(th, 0.0, 0.0, 1.0);
glTranslatef(-trX, -trY, 0.0);
triangle();
glutPostRedisplay();
glutSwapBuffers();
}
void myInit()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-500, 500, -500, 500);
glMatrixMode(GL_MODELVIEW);
}
void rotateMenu(int option)
{
if (option == 1)
rFlag = 1;
else if (option == 2)
rFlag = 2;
else if (option == 3)
rFlag = 3;
}
void main(int argc, char** argv)
{
printf("Enter Fixed points(x,y) for Rotation: ");
scanf_s("%d %d", &x, &y);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Create and Rotate Triangle");
myInit();
glutDisplayFunc(display);
glutCreateMenu(rotateMenu);
glutAddMenuEntry("Rotate around origin", 1);
glutAddMenuEntry("Rotate around fixed point", 2);
glutAddMenuEntry("Stop Rotation", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
#include <glut.h>
#include<stdio.h>
int xc, yc, r;
void plotPoints(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y);
glVertex2i(xc - x, yc + y);
glVertex2i(xc + x, yc - y);
glVertex2i(xc - x, yc - y);
glVertex2i(xc + y, yc + x);
glVertex2i(xc - y, yc + x);
glVertex2i(xc + y, yc - x);
glVertex2i(xc - y, yc - x);
glEnd();
}
void drawCircle() {
int x = 0;
int y = r;
int d = 1 - r;
plotPoints(x, y);
while (y > x) {
if (d < 0) {
d = d + 2 * x + 3;
}
else {
d = d + 2 * (x - y) + 5;
y--;
}
x++;
plotPoints(x, y);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.5, 0.5);
drawCircle();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 400);
}
void main(int argc, char** argv) {
printf("Enter the center of the circle (xc, yc): ");
scanf_s("%d %d", &xc, &yc);
printf("Enter the radius of the circle: ");
scanf_s("%d", &r);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Midpoint Circle Drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
}
if (x < xMin)
code |= LEFT;
else if (x > xMax)
code |= RIGHT;
if (y < yMin)
code |= BOTTOM;
else if (y > yMax)
code |= TOP;
return code;
}
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
}
else if (code1 & code2) {
break;
}
else {
int codeOut;
float x, y;
if (codeOut == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
glColor3f(0.0, 1.0, 0.0); // Green for clipped line
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
}
void draw()
{
glColor3f(0.0, 0.0, 1.0); // Red for the window
glBegin(GL_LINE_LOOP);
glVertex2f(xMin, yMin);
glVertex2f(xMax, yMin);
glVertex2f(xMax, yMax);
glVertex2f(xMin, yMax);
glEnd();
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
showOriginal = false;
glViewport(0, 0, 500, 500);
draw();
glFlush();
}
void initOpenGL()
{
glClearColor(1.0, 1.0, 1.0, 1.0); // White background
gluOrtho2D(-200, 200, -200, 200);
}
#include <glut.h>
void init() {
glClearColor(0.5f, 0.8f, 0.95f, 1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
void drawGround() {
glColor3f(0.0f, 0.8f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(500, 0);
glVertex2f(500, 150);
glVertex2f(0, 150);
glEnd();
}
void drawHouse() {
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawGround();
drawHouse();
glFlush();
}