Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CG Lab Experiments

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Lab Experiments

Part 1

Course Details

Course Code: CSI 414


Course Title: Computer Graphics Sessional

Student Details

CSE 068 07941 Md. Fardeen Ehsan Shawon


Experiment 1

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>

GLfloat i=0.0f, r=0;


void idle(){
glutPostRedisplay();
}
static void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor (1, 1, 1, 1);

//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();

//propellers and rotation


glPushMatrix();
glRotatef(i, 0, 0, 30);

//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();

//rotation state check


if (r==1){
i-=0.02f;
}else if (r==2){
i+=0.02f;
}
glFlush();
}

void menu(int id){


switch(id){
case 0:
r = 0;
break;
case 1:
r = 1;
break;
case 2:
r = 2;
break;
default:
exit(0);
break;
}
glutPostRedisplay();
}

void init (void){


glOrtho(-50,50,-110,50,-1.0,1.0);
}

int main(int argc, char *argv[])


{
glutInit(&argc, argv);
glutInitWindowSize(500,800);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

glutCreateWindow("CG Lab Task 1");


init();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutCreateMenu(menu);
glutAddMenuEntry("STOP", 0);
glutAddMenuEntry("Clockwise", 1);
glutAddMenuEntry("Anti Clockwise", 2);
glutAddMenuEntry("QUIT", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();

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.

For the keypresses, we will use keyboard function.

Code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include<math.h>

GLfloat a1=128.0, b1=128.0, c1=128.0;


GLfloat m1=255.0, m2=255.0, m3=255.0;
GLfloat r1=255.0, r2=255.0, r3=255.0;
GLfloat bg1=0, bg2=0, bg3=0, bg4=0;
GLfloat walk=0.0, isUmbrella=0;
GLfloat bodyX=-80, bodyY = -25;
GLfloat rainX = -100, rainY = 50;
GLfloat leg1X1=bodyX, leg1Y1=bodyY-5, leg1X2=bodyX-3, leg1Y2=bodyY-15;
GLfloat leg2X1=bodyX, leg2Y1=bodyY-5, leg2X2=bodyX+3, leg2Y2=bodyY-15;

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 walkMan(int value){


if(walk==1){
bodyX+=5;
if(bodyX>100){
bodyX=-100;
}
leg1X1=bodyX, leg1Y1=bodyY-5, leg1X2=bodyX-1, leg1Y2=bodyY-15;
leg2X1=bodyX, leg2Y1=bodyY-5, leg2X2=bodyX+1, leg2Y2=bodyY-15;
display();
glutPostRedisplay();
glutTimerFunc(500,walkMan2, 0);
}
}

void walkMan2(int value){


if(walk==1){
bodyX+=5.0;
if(bodyX>100){
bodyX=-100;
}
leg1X1=bodyX, leg1Y1=bodyY-5, leg1X2=bodyX-3, leg1Y2=bodyY-15;
leg2X1=bodyX, leg2Y1=bodyY-5, leg2X2=bodyX+3, leg2Y2=bodyY-15;
display();
glutPostRedisplay();
glutTimerFunc(500,walkMan, 0);
}
}

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 drawRain2(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, drawRain, 0);
}

void display(){
glClearColor(bg1,bg2,bg3,bg4);
glClear(GL_COLOR_BUFFER_BIT);

//house bottom right


glBegin(GL_QUADS);
glColor3ub(a1,b1,c1);
glVertex2f(30.0,10.0);
glVertex2f(-10.0,10.0);
glVertex2f(-10.0,-20.0);
glVertex2f(30.0,-20.0);
glEnd();

//house bottom left


glBegin(GL_QUADS);
glColor3ub(a1,b1,c1);
glVertex2f(-36.0,10.0);
glVertex2f(-10.0,10.0);
glVertex2f(-10.0,-20.0);
glVertex2f(-36.0,-20.0);
glEnd();

//house upper right


glBegin(GL_QUADS);
glColor3ub(a1,b1,c1);
glVertex2f(-23,30);
glVertex2f(17,30);
glVertex2f(30,10);
glVertex2f(-10,10);
glEnd();

//house upper left


glBegin(GL_TRIANGLES);
glColor3ub(a1,b1,c1);
glVertex2f(-23,30);
glVertex2f(-36,10);
glVertex2f(-10,10);
glEnd();

//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 init (void)


{
glOrtho(-100,100,-50,50,-1.0,1.0);
}

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;
}
}

void keyboard(unsigned char key, int x, int y){


switch(key){
case 'h':
case 'H':
if (a1==128.0){
a1 = 255;
b1 = 0;
c1 = 0;
}else{
a1 = 128;
b1 = 128;
c1 = 128;
}
display();
break;
case 'm':
case 'M':
if (m1==255.0){
m1 = 254.0;
m2 = 165.0;
m3 = 0.0;
}else{
m1 = 255.0;
m2 = 255.0;
m3 = 255.0;
}
display();
break;
case 'b':
case 'B':
if (bg1==0){
bg1 = 1;
bg2 = 1;
bg3 = 1;
bg4 = 1;
}else{
bg1 = 0.0;
bg2 = 0.0;
bg3 = 0.0;
bg4 = 0.0;
}
display();
break;
case 'w':
case 'W':
case 'r':
case 'R':
if(walk==0){
walk=1;
walkMan(0);
}else{
walk=0;
}
display();
break;
case 'a':
case 'A':
changeAll();
display();
break;
case 'u':
case 'U':
if(isUmbrella ==1){
isUmbrella=0;
}else{
isUmbrella=1;
}
display();
break;
case 'e':
case 'E':
exit(0);
break;
}
}

int main(int a, char * *b){


glutInit(&a, b);
glutInitWindowSize(800,400);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutCreateWindow("CG Lab Task 2");


init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Output:

You might also like