CG Lab Experiments
CG Lab Experiments
CG Lab Experiments
Part 1
Course Details
Student Details
Problem: Write a program using OpenGL to rotate the wind generator using the menu options: •
Rotate clockwise • Rotate anti-clockwise • Stop
Aim: To design a wind generator using various basic shapes and implementation of menu options
using opengl.
Description: A Wind generator can be designed in various way. We will use rectangles, circles and
triangle to draw a wind generator.
Steps:
● Create the base using multiple GL_QUADS functions.
● Create the center circle of the propellers using circle drawing algorithm.
● Create Four propellers using GL_TRIANGLES.
● Add Menu options to stop and rotate the propellers clockwise and anti-clockwise.
● Implement the rotation of the propellers.
Code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include<math.h>
//base
glBegin(GL_QUADS);
glColor3ub(128,128,128);
glVertex2f(-25.0,-80.0);
glVertex2f(25.0,-80.0);
glVertex2f(25.0,-90.0);
glVertex2f(-25.0,-90.0);
glEnd();
//stand
glBegin(GL_QUADS);
glColor3ub(128,128,128);
glVertex2f(-20,-30.0);
glVertex2f(20,-30.0);
glVertex2f(4,-5.0);
glVertex2f(-4,-5.0);
glEnd();
glBegin(GL_QUADS);
glColor3ub(128,128,128);
glVertex2f(-20,-30.0);
glVertex2f(20,-30.0);
glVertex2f(25,-80.0);
glVertex2f(-25,-80.0);
glEnd();
//circle
int j;
int triangleAmount=40;
GLfloat twicePi = 2.0*3.1416;
glBegin(GL_TRIANGLE_FAN);
glColor3ub(0,0,0);
GLfloat x = 0, y = 0, radius = 3;
glVertex2f(x,y);
for(j=0;j<=triangleAmount;j++){
glVertex2f(
x+(radius*cos(j*twicePi/triangleAmount)),
y+(radius*sin(j*twicePi/triangleAmount))
);
}
glEnd();
//right propeller
glBegin(GL_TRIANGLES);
glColor3ub(0,0,0);
glVertex2f(5,00);
glVertex2f(30,5);
glVertex2f(30,-5);
glEnd();
//up propeller
glBegin(GL_TRIANGLES);
glColor3ub(0,0,0);
glVertex2f(0,5);
glVertex2f(-5,30);
glVertex2f(5,30);
glEnd();
//left propeller
glBegin(GL_TRIANGLES);
glColor3ub(0,0,0);
glVertex2f(-5,0);
glVertex2f(-30,5);
glVertex2f(-30,-5);
glEnd();
//bottom propeller
glBegin(GL_TRIANGLES);
glColor3ub(0,0,0);
glVertex2f(0,-5);
glVertex2f(-5,-30);
glVertex2f(5,-30);
glEnd();
glPopMatrix();
return EXIT_SUCCESS;
}
Output:
Experiment 2
Problem: Write a program to design a scenario as shown in Figure 1. Design the user interface such
as shown below:
● Press 'w' or W' to make the man walk in rain.
● Keypress 'u' or 'U' so that the man can walk in rain using an umbrella
● Keypress 'R' or 'r' to start and stop the rain
● Press any other keys to assign appropriate colours in different parts of the scene ( e.g.
background, house, umbrella, sun/moon).
Aim: To draw a scene including a house, moon, man, umbrella and rain. And change colors of
different element, walk the man with umbrella or not, start and stop the rain on different keypresses.
Description: This scenery contains multiple contents that requires multiple shapes. We will draw
different elements using a single or multiple basic shapes.
● House: We will use Quads and Triangles to draw the house, and lines to show the
borders/outlines.
● Moon: We will use circle drawing algorithm to draw the moon.
● Man: We will use lines to draw the man’s body and circle to draw the head. We will calculate
the coordinates of the man’s body, leg, and head from a single point, this technique will help
us to later walk the man.
● Umbrella: We will use half circle and a line to draw the umbrella. The coordinates of the
umbrella will also be calculated from the single point , that is being used for the man’s body.
● Rain: We will use hundreds of randomly generated small lines as the rain, and update the
coordinated randomly to show raining.
Code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
void display();
void walkMan(int);
void walkMan2(int);
void drawLine(int x1,int y1, int x2, int y2){
glColor3ub(r1,r2,r3);
glBegin(GL_LINES);
{
glVertex3f(x1,y1,0.0);
glVertex3f(x2,y2,0.0);
}
glEnd();
}
void drawCircle(int x,int y,int radius, GLfloat cc1, GLfloat cc2, GLfloat cc3){
int i;
int triangleAmount=40;
GLfloat twicePi = 2.0*3.1416;
glBegin(GL_TRIANGLE_FAN);
glColor3ub(cc1,cc2,cc3);
glVertex2f(x,y);
for(i=0;i<=triangleAmount;i++){
glVertex2f(
x+(radius*cos(i*twicePi/triangleAmount)),
y+(radius*sin(i*twicePi/triangleAmount))
);
}
glEnd();
}
void drawUmbrella(){
int i;
int triangleAmount=300;
GLfloat twicePi = 2.0*3.1416;
int x = bodyX+10, y = bodyY+22, radius = 15;
glBegin(GL_TRIANGLE_FAN);
glColor3ub(m1,m2,m3);
glVertex2f(x,y);
for(i=0;i<=triangleAmount;i++){
float t = y+(radius*sin(i*twicePi/triangleAmount));
if (t<(y-1)){
continue;
}
glVertex2f(
x+(radius*cos(i*twicePi/triangleAmount)),
y+(radius*sin(i*twicePi/triangleAmount))
);
}
glEnd();
drawLine(bodyX, bodyY+5,(bodyX+x)/2,bodyY+2.5);
drawLine((bodyX+x)/2, bodyY+2.5,x,bodyY+5);
drawLine(x,y,x,bodyY+5);
}
void drawMan(){
drawLine(bodyX, bodyY+10, bodyX, bodyY-5);
drawLine(leg1X1, leg1Y1, leg1X2, leg1Y2);
drawLine(leg2X1, leg2Y1, leg2X2, leg2Y2);
drawCircle(bodyX, bodyY+12, 4, r1,r2,r3);
}
void drawRain2(int);
void drawRain(int value){
for(int i=0; i<=300; i++){
rainX = -100 + (rand() % 201);
rainY = -100 + (rand() % 201);
drawLine(rainX, rainY, rainX, rainY+2);
}
glutTimerFunc(200, drawRain2, 0);
}
void display(){
glClearColor(bg1,bg2,bg3,bg4);
glClear(GL_COLOR_BUFFER_BIT);
//house door
glBegin(GL_QUADS);
glColor3ub(0,0,0);
glVertex2f(-18,-20);
glVertex2f(-28,-20);
glVertex2f(-28,-5);
glVertex2f(-18,-5);
glEnd();
//drawing lines
drawLine(30,-20,30,10);
drawLine(-10,-20,-10,10);
drawLine(-36,-20,-36,10);
drawLine(-36,-20,30,-20);
drawLine(-36,10,30,10);
drawLine(-23,30,-10,10);
drawLine(-23,30,-36,10);
drawLine(-23,30,17,30);
drawLine(17,30,30,10);
//home end
//draw road
drawLine(-90,-40,90,-40);
//drawMan
drawMan();
if(isUmbrella==1){
drawUmbrella();
}
//moon
drawCircle(80,35,7, m1, m2, m3);
drawRain(0);
glFlush();
}
void changeAll(void){
if (a1==128.0){
a1 = 255;
b1 = 0;
c1 = 0;
}else{
a1 = 128;
b1 = 128;
c1 = 128;
}
if (m1==255.0){
m1 = 254.0;
m2 = 165.0;
m3 = 0.0;
}else{
m1 = 255.0;
m2 = 255.0;
m3 = 255.0;
}
if (bg1==0){
bg1 = 1;
bg2 = 1;
bg3 = 1;
bg4 = 1;
}else{
bg1 = 0.0;
bg2 = 0.0;
bg3 = 0.0;
bg4 = 0.0;
}
if(r1==255){
r1 =0;
r2 =0;
r3 =0;
}else{
r1=255;
r2=255;
r3=255;
}
}
Output: