0% found this document useful (0 votes)
48 views

Task All

The document contains C++ code for drawing a scene using OpenGL that includes: 1) Functions to draw various scene elements like grass, a road, mountains, and crosses using OpenGL drawing commands. 2) Functions to draw objects like a car that moves across the screen, windmills in the background, and a rotating windmill structure. 3) A main function that initializes an OpenGL window, calls the drawing functions to render the scene, and includes an idle callback to continuously redraw the scene and animate elements over time.

Uploaded by

Abdul Rahim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Task All

The document contains C++ code for drawing a scene using OpenGL that includes: 1) Functions to draw various scene elements like grass, a road, mountains, and crosses using OpenGL drawing commands. 2) Functions to draw objects like a car that moves across the screen, windmills in the background, and a rotating windmill structure. 3) A main function that initializes an OpenGL window, calls the drawing functions to render the scene, and includes an idle callback to continuously redraw the scene and animate elements over time.

Uploaded by

Abdul Rahim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Task All

SS

Code
#include <Windows.h>
#include <gl/GL.h>
#include<gl/GLU.h>
#include <gl/glut.h>
#include<math.h>

void myInit()
{
glClearColor(0.5,0.5,0.5,1); //Background Color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,640,0,640);
glMatrixMode(GL_MODELVIEW);

}
void setWindow() {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 640);
}
void grass()
{
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(0, 0);
glVertex2i(640, 0);
glVertex2i(640, 50);
glVertex2i(0, 50);
glEnd();
glFlush();
}

void drawroad()
{
glBegin(GL_QUADS);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex2i(0, 50);
glVertex2i(640, 50);
glVertex2i(640, 100);
glVertex2i(0, 100);
glEnd();
glFlush();
}

void drawcross()
{
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(0, 75);
glVertex2i(50, 75);
glVertex2i(50, 80);
glVertex2i(0, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(60, 75);
glVertex2i(100, 75);
glVertex2i(100, 80);
glVertex2i(60, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(110, 75);
glVertex2i(150, 75);
glVertex2i(150, 80);
glVertex2i(110, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(160, 75);
glVertex2i(200, 75);
glVertex2i(200, 80);
glVertex2i(160, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(210, 75);
glVertex2i(250, 75);
glVertex2i(250, 80);
glVertex2i(210, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(260, 75);
glVertex2i(300, 75);
glVertex2i(300, 80);
glVertex2i(260, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(310, 75);
glVertex2i(350, 75);
glVertex2i(350, 80);
glVertex2i(310, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(360, 75);
glVertex2i(400, 75);
glVertex2i(400, 80);
glVertex2i(360, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(410, 75);
glVertex2i(450, 75);
glVertex2i(450, 80);
glVertex2i(410, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(460, 75);
glVertex2i(500, 75);
glVertex2i(500, 80);
glVertex2i(460, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(510, 75);
glVertex2i(550, 75);
glVertex2i(550, 80);
glVertex2i(510, 80);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(560, 75);
glVertex2i(600, 75);
glVertex2i(600, 80);
glVertex2i(560, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(610, 75);
glVertex2i(650, 75);
glVertex2i(650, 80);
glVertex2i(610, 80);
glEnd();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(660, 75);
glVertex2i(680, 75);
glVertex2i(680, 80);
glVertex2i(660, 80);
glEnd();

glFlush();
}

void Mountain()
{
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(0, 100);
glVertex2i(100, 100);
glVertex2i(50, 300);
glVertex2i(100, 100);
glVertex2i(200, 100);
glVertex2i(150, 350);
glVertex2i(200, 100);
glVertex2i(300, 100);
glVertex2i(250, 320);
glVertex2i(300, 100);
glVertex2i(400, 100);
glVertex2i(350, 300);
glVertex2i(400, 100);
glVertex2i(500, 100);
glVertex2i(450, 290);
glVertex2i(500, 100);
glVertex2i(680, 100);
glVertex2i(580, 250);
glEnd();
glFlush();
}

void base() {
glColor3f(0, 0, 0);
glBegin(GL_QUADS);

glVertex2f(100, 200);
glVertex2f(110 , 200);
glVertex2f(110 , 200 + 120);
glVertex2f(100, 200 + 120);

glEnd();
}

void drawCircle(int x, int y, float rad)


{
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i<360; i++)
{
double angle = i * (2 * 3.14) / 180;
glVertex2f(x + cos(angle)*rad, y + sin(angle)*rad);
}
glEnd();
glFlush();
}
void drawCirclepoly(int x, int y, float rad)
{
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i<360; i++)
{
double angle = i * (2 * 3.14) / 100;
glVertex2f(x + cos(angle)*rad, y + sin(angle)*rad);
}
glEnd();
glFlush();
}
float counter=0.0;

void drawcar()
{
glPushMatrix();

counter=counter+0.09;
if(counter>640)
counter=0.0;
glTranslated(counter,0.0,0.0);
glColor3f(0, 0, 1);
glBegin(GL_QUADS);

glVertex2f(60, 70);
glVertex2f(80 , 70);
glVertex2f(80 , 120);
glVertex2f(60, 120);

glEnd();
glColor3f(255, 165, 0);
glBegin(GL_QUADS);

glVertex2f(10, 70);
glVertex2f(60 , 70);
glVertex2f(60 , 150);
glVertex2f(10, 150);

glEnd();
drawCirclepoly(25,60,10);
drawCirclepoly(50,60,10);
glPopMatrix();
}
void drawScene()
{

grass();
drawroad();
drawcross();

Mountain();

void drawFan()
{
glBegin(GL_TRIANGLES);
glColor3f(10,0,10);
glVertex2i(300,300);
glVertex2i(450,350);
glVertex2i(450,250);
glEnd();
glFlush();
}

void drawStand()
{
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,0.0);
glVertex2i(300,300);
glVertex2i(280,0);
glVertex2i(320,0);
glEnd();
glFlush();
}

int a=0;
float angle =360/3;
void drawMill()
{
drawStand();

a+=2;
glTranslatef(300,300,0);
glRotatef(a,0,0,1);
glTranslatef(-300,-300,0);

drawFan();
for (int i = 0; i<5; i++){
glTranslatef(300,300,0);
glRotatef(60,0,0,1);
glTranslatef(-300,-300,0);

drawFan();

void mil()
{

glPushMatrix();
glTranslatef(0,120,0);
drawMill();
glPopMatrix();
glPushMatrix();
glTranslatef(120,120,0);
glScalef(0.5,0.5,0);
glTranslatef(120,120,0);
glTranslatef(290,120,0);
drawMill();

glPopMatrix();
glPushMatrix();
glTranslatef(-120,120,0);
glScalef(0.5,0.5,0);
glTranslatef(-120,120,0);
glTranslatef(290,120,0);
drawMill();

glPopMatrix();
}
void Timer(int iUnused)
{angle-=0.5;
glutTimerFunc(35,Timer,-1);

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawCircle(10,10,2);

drawScene();
mil();
//glPopMatrix();
drawcar();

glutPostRedisplay();
glutSwapBuffers();
glFlush();
}

int main(int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(700,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Car Moving");
myInit();
glutDisplayFunc(myDisplay);
glutIdleFunc(myDisplay);
glutTimerFunc(30,Timer,1);
glutMainLoop();
return 0;
}

You might also like