Untitled

 avatar
unknown
plain_text
5 months ago
16 kB
6
Indexable
#include <GL/gl.h>
#include<math.h>
#include <GL/glut.h>


void sun(float rx, float ry, float cx, float cy) {
    glBegin(GL_POLYGON);
    glVertex2f(cx, cy);
    for (int i = 0; i <= 360; i++) {
        float angle = i * 3.1416 / 180;
        float x = rx * cos(angle);
        float y = ry * sin(angle);
        glVertex2f((x + cx), (y + cy));
    }
    glEnd();
}


void init(void) {
    glClearColor(0.53, 0.81, 0.92, 1.0); // Sky blue
    gluOrtho2D(0, 500, 0, 500);
}

float bx = 0;
void clouds()
{
    // 1st cloud
    glColor3ub(220, 220, 220); // Light gray
    sun(20, 30, 460 + bx, 460);
    sun(15, 20, 445 + bx, 460);
    sun(15, 20, 475 + bx, 460);

    // 2nd cloud
    sun(20, 30, 390 + bx, 420);
    sun(15, 20, 405 + bx, 420);
    sun(15, 20, 375 + bx, 420);

    // cng cloud pos for animation
    bx += .3;
    if (bx > 0) bx = -550;

    glutPostRedisplay();
}

void drawH(float x, float y) {
    // Head
    glColor3ub(0, 0, 0);
    sun(5, 5, x, y + 20); // Head pos

    // Body
    glColor3ub(0, 0, 255); // Blue
    glBegin(GL_LINES);
    glVertex2f(x, y + 15); // Top of body
    glVertex2f(x, y);      // Bottom of body
    glEnd();

    // Arms
    glBegin(GL_LINES);
    glVertex2f(x, y + 10); // Left arm
    glVertex2f(x - 10, y + 5);
    glVertex2f(x, y + 10); // Right arm
    glVertex2f(x - 5, y + 5);
    glEnd();

    //stick
    glColor3ub(0, 0, 0);
    glBegin(GL_LINES);
    glVertex2f(x - 3, y + 15);
    glVertex2f(x - 10, y - 30);
    glEnd();
}


float bxx = 10;
void boat() {
    glColor3ub(0, 0, 0);
    glBegin(GL_POLYGON);
    glVertex2d(325 + bxx, 220);
    glVertex2d(400 + bxx, 220);
    glVertex2d(425 + bxx, 250);
    glVertex2d(300 + bxx, 250);
    glEnd();

    glColor3ub(205, 133, 63);
    glBegin(GL_POLYGON);
    glVertex2d(325 + bxx, 250);
    glVertex2d(400 + bxx, 250);
    glVertex2d(390 + bxx, 280);
    glVertex2d(335 + bxx, 280);
    glEnd();
    drawH(320 + bxx, 250);

    bxx += .81;
    if (bxx > 0) bxx = -550;

    glColor3ub(0, 0, 0);
    glBegin(GL_POLYGON);
    glVertex2d(325, 200);
    glVertex2d(400, 200);
    glVertex2d(425, 230);
    glVertex2d(300, 230);
    glEnd();

    glColor3ub(215, 215, 55);
    glBegin(GL_POLYGON);
    glVertex2d(325, 230);
    glVertex2d(400, 230);
    glVertex2d(390, 260);
    glVertex2d(335, 260);
    glEnd();
    //khuti
    glColor3ub(0.5, 0.5, 0.5);
    glBegin(GL_POLYGON);
    glVertex2d(300, 200);
    glVertex2d(303, 200);
    glVertex2d(303, 260);
    glVertex2d(300, 260);
    glEnd();

    glutPostRedisplay();
}


void drawHuman(float x, float y) {
    // Head
    glColor3ub(0, 0, 0);
    sun(10, 10, x, y + 40);
    glLineWidth(2);
    // Body
    glColor3ub(0, 0, 255);
    glBegin(GL_LINES);
    glVertex2f(x, y + 30); // Top of body
    glVertex2f(x, y);      // Bottom of body
    glEnd();

    // Arms
    glBegin(GL_LINES);
    glVertex2f(x, y + 25); // Left arm
    glVertex2f(x - 15, y + 20);
    glVertex2f(x, y + 25); // Right arm
    glVertex2f(x + 15, y + 20);
    glEnd();

    // Legs
    glBegin(GL_LINES);
    glVertex2f(x, y); // Left leg
    glVertex2f(x - 10, y - 30);
    glVertex2f(x, y); // Right leg
    glVertex2f(x + 10, y - 30);
    glEnd();
    glLineWidth(1);
}

float birdX1 = 100, birdY1 = 400; // First bird pos
float birdX2 = 200, birdY2 = 450; // Second bird pos
void drawBird(float x, float y) {
    glColor3ub(0, 0, 0); // Color for birds
    glLineWidth(1.2);
    glBegin(GL_LINES);
    glVertex2f(x, y);
    glVertex2f(x - 10, y + 10);
    glVertex2f(x, y);
    glVertex2f(x + 10, y + 10);
    glEnd();
    glLineWidth(1);
}

void drawFence(float x, float y) {
    glColor3ub(255, 255, 0); // Yellow color for the fence
    glLineWidth(5); //line width baraisi
    // Draw horizontal fence slats (multiple horizontal lines)
    for (int i = 1; i < 3; i++) {
        glBegin(GL_LINES);
        glVertex2f(x, y + i * 20);           // Start point of the horizontal line
        glVertex2f(x + 100, y + i * 20);     // End point of horizontal line (width of the fence)
        glEnd();


    }

    // Draw vertical fence posts (lines at the start and end of each slat)
    glBegin(GL_LINES);
    glVertex2f(x, y);                        // Left post at the starting point
    glVertex2f(x, y + 50);                   // Left post at the end of the fence

    glVertex2f(x + 100, y);                  // Right post at the starting point
    glVertex2f(x + 100, y + 50);             // Right post at the end of the fence
    glEnd();

    glLineWidth(1);
}

void flower(float x, float y) {
    glColor3ub(0, 128, 0); // Yellow color for the fence
    glLineWidth(2);
    glBegin(GL_LINES);
    glVertex2f(x, y);
    glVertex2f(x + 20, y + 20 - 10);

    glVertex2f(x, y);
    glVertex2f(x + 15, y + 25 - 10);

    glVertex2f(x, y);
    glVertex2f(x - 20, y + 20 - 10);

    glVertex2f(x, y);
    glVertex2f(x - 15, y + 25 - 10);

    glVertex2f(x, y);
    glVertex2f(x + 5, y + 30 - 10);

    glVertex2f(x, y);
    glVertex2f(x - 5, y + 30 - 10);
    glEnd();
    glColor3ub(225, 223, 0);
    sun(3, 3, x + 15, y + 25 - 10);
    glColor3ub(225, 105, 180);
    sun(3, 3, x - 5, y + 30 - 10);
    glColor3ub(0, 128, 0);
    sun(3, 3, x - 20, y + 20 - 10);
    glColor3ub(150, 123, 182);
    sun(3, 3, x + 20, y + 20 - 10);
    glColor3ub(225, 223, 0);
    sun(3, 3, x - 15, y + 25 - 10);
    glColor3ub(150, 123, 182);
    sun(3, 3, x + 5, y + 30 - 10);
    glLineWidth(1);

}
float carX = 0.0f, velocity = 0.0f, acceleration = 0.05f, maxSpeed = 5.0f;
void drawCar(float x, float y) {
    // Bus body
    glColor3ub(255, 165, 0); // Orange color->bus body
    glBegin(GL_POLYGON);
    glVertex2f(x, y);       // Bottom-left corner
    glVertex2f(x + 160, y);  // Bottom-right corner (increased width for bus)
    glVertex2f(x + 160, y + 40); // Top-right corner
    glVertex2f(x, y + 40);  // Top-left corner
    glEnd();

    // Windows (more windows for the bus)
    glColor3ub(173, 216, 230); // LightBlue color for the windows
    // Add windows along the body of the bus
    for (int i = 0; i < 6; i++) {
        glBegin(GL_POLYGON);
        glVertex2f(x + 10 + i * 25, y + 15);  // Bottom-left corner of the window
        glVertex2f(x + 30 + i * 25, y + 15);  // Bottom-right corner of the window
        glVertex2f(x + 30 + i * 25, y + 30);  // Top-right corner of the window
        glVertex2f(x + 10 + i * 25, y + 30);  // Top-left corner of the window
        glEnd();
    }

    // Wheels of the bus
    glColor3ub(0, 0, 0); // Black color for the wheels
    sun(12, 12, x + 30, y - 5); // Left wheel
    sun(12, 12, x + 120, y - 5); // Right wheel
}


void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    //Ground Color
    glColor3ub(0, 255, 0);
    glBegin(GL_POLYGON);
    glVertex2d(0, 0);
    glVertex2d(500, 0);
    glVertex2d(500, 300);
    glVertex2d(0, 300);
    glEnd();

    //road
    glColor3ub(110, 110, 110);
    glBegin(GL_POLYGON);
    glVertex2d(300, 0);
    glVertex2d(350, 0);
    glVertex2d(400, 300);
    glVertex2d(400, 300);
    glEnd();

    glColor3ub(110, 110, 110);
    glBegin(GL_POLYGON);
    glVertex2d(0, 0);
    glVertex2d(800, 0);
    glVertex2d(800, 18);
    glVertex2d(0, 10);
    glEnd();

    glColor3ub(150, 150, 150);
    glBegin(GL_POLYGON);
    glVertex2d(331, 90);
    glVertex2d(75, 140);
    glVertex2d(75, 150);
    glVertex2d(337, 110);
    glEnd();

    glColor3ub(10, 149, 237);
    glBegin(GL_POLYGON);
    glVertex2d(-40, 200);
    glVertex2d(0, 300);
    glVertex2d(500, 300);
    glVertex2d(500, 200);
    glEnd();

    // Hill 1
    glColor3ub(144, 238, 144); // Light green
    glBegin(GL_POLYGON);
    glVertex2d(-40, 300);
    glVertex2d(200, 300);
    glVertex2d(100, 450);
    glEnd();

    // Hill 11
    glColor3ub(205, 133, 63);
    glBegin(GL_POLYGON);
    glVertex2d(-100, 300);
    glVertex2d(100, 300);
    glVertex2d(0, 400);
    glEnd();

    // Hill 2
    glColor3ub(139, 69, 19); // SaddleBrown
    glBegin(GL_POLYGON);
    glVertex2d(150, 300);
    glVertex2d(350, 300);
    glVertex2d(250, 450);
    glEnd();

    // Hill 22
    glColor3ub(184, 134, 11); // Dark Goldenrod (different from Peru)
    glBegin(GL_POLYGON);
    glVertex2d(250, 300);
    glVertex2d(450, 300);
    glVertex2d(350, 400);
    glEnd();

    // Hill 3
    glColor3ub(205, 166, 33); // Forest Green
    glBegin(GL_POLYGON);
    glVertex2d(300, 300);
    glVertex2d(520, 300);
    glVertex2d(400, 450);
    glEnd();

    // Hill 33
    glColor3ub(0, 100, 0); // Dark Green
    glBegin(GL_POLYGON);
    glVertex2d(400, 300);
    glVertex2d(700, 300);
    glVertex2d(550, 420);
    glEnd();

    // Boat design function call
    boat();

    // House Tree
    glColor3ub(139, 69, 19);
    glBegin(GL_POLYGON);
    glVertex2d(50, 150);
    glVertex2d(70, 150);
    glVertex2d(70, 300);
    glVertex2d(50, 300);
    glEnd();

    // Tree leaf
    glColor3ub(0, 128, 0);
    sun(30, 40, 35, 320);

    glColor3ub(0, 128, 0);
    sun(30, 40, 85, 320);

    glColor3ub(0, 128, 0);
    sun(25, 30, 45, 370);

    glColor3ub(0, 128, 0);
    sun(30, 30, 70, 370);

    glColor3ub(0, 128, 0);
    sun(25, 30, 55, 400);

    ///
    // House Tree 2
    glColor3ub(110, 50, 15);
    glBegin(GL_POLYGON);
    glVertex2d(100, 150);
    glVertex2d(120, 150);
    glVertex2d(120, 320);
    glVertex2d(100, 320);
    glEnd();

    // Tree leaves
    glColor3ub(0, 100, 0);
    sun(30, 40, 85, 335);
    glColor3ub(0, 100, 0);
    sun(30, 40, 135, 335);
    glColor3ub(0, 100, 0);
    sun(25, 30, 95, 385);
    glColor3ub(0, 100, 0);
    sun(30, 30, 120, 385);
    glColor3ub(0, 100, 0);
    sun(25, 30, 105, 415);


    /////////
    //House tree 3
    glColor3ub(139, 69, 19);
    glBegin(GL_POLYGON);
    glVertex2d(430, 150);
    glVertex2d(450, 150);
    glVertex2d(450, 300);
    glVertex2d(430, 300);
    glEnd();

    // Tree leaf
    glColor3ub(0, 144, 0);
    sun(30, 40, 415, 320);

    glColor3ub(0, 144, 0);
    sun(30, 40, 465, 320);

    glColor3ub(0, 144, 0);
    sun(25, 30, 425, 370);

    glColor3ub(0, 144, 0);
    sun(30, 30, 450, 370);

    glColor3ub(0, 144, 0);
    sun(25, 30, 435, 400);


    // 2nd House
    glColor3ub(173, 216, 230); // LightBlue
    glBegin(GL_POLYGON);
    glVertex2d(100, 220);
    glVertex2d(200, 220);
    glVertex2d(175, 270);
    glVertex2d(130, 270);
    glEnd();


    glColor3ub(244, 164, 96);
    glBegin(GL_POLYGON);
    glVertex2d(100, 170);
    glVertex2d(185, 170);
    glVertex2d(185, 220);
    glVertex2d(100, 220);
    glEnd();


    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(100, 170);
    glVertex2d(190, 170);
    glVertex2d(190, 160);
    glVertex2d(100, 160);
    glEnd();


    // 2nd house door and window
    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(140, 170);
    glVertex2d(165, 170);
    glVertex2d(165, 200);
    glVertex2d(140, 200);
    glEnd();


    // 1st House
    glColor3ub(255, 248, 202); // Bisque
    glBegin(GL_POLYGON);
    glVertex2d(0, 220);
    glVertex2d(135, 220);
    glVertex2d(110, 270);
    glVertex2d(25, 270);
    glEnd();

    glColor3ub(255, 222, 173);
    glBegin(GL_POLYGON);
    glVertex2d(10, 220);
    glVertex2d(50, 220);
    glVertex2d(25, 255);
    glEnd();


    glColor3ub(255, 222, 173);
    glBegin(GL_POLYGON);
    glVertex2d(10, 150);
    glVertex2d(50, 150);
    glVertex2d(50, 220);
    glVertex2d(10, 220);
    glEnd();


    glColor3ub(222, 184, 135);
    glBegin(GL_POLYGON);
    glVertex2d(50, 150);
    glVertex2d(125, 150);
    glVertex2d(125, 220);
    glVertex2d(50, 220);
    glEnd();


    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(10, 150);
    glVertex2d(125, 150);
    glVertex2d(125, 140);
    glVertex2d(10, 140);
    glEnd();

    // 1st house door and window
    glColor3ub(160, 66, 45);
    glBegin(GL_POLYGON);
    glVertex2d(75, 150);
    glVertex2d(95, 150);
    glVertex2d(95, 195);
    glVertex2d(75, 195);
    glEnd();

    glColor3ub(160, 66, 45);
    glBegin(GL_POLYGON);
    glVertex2d(20, 200);
    glVertex2d(35, 200);
    glVertex2d(35, 175);
    glVertex2d(20, 175);
    glEnd();

    //////////////3rd house 
    glColor3ub(144, 238, 144); // LightGreen
    glBegin(GL_POLYGON);
    glVertex2d(373, 220);
    glVertex2d(508, 220);
    glVertex2d(483, 270);
    glVertex2d(398, 270);
    glEnd();

    glColor3ub(255, 222, 173);
    glBegin(GL_POLYGON);
    glVertex2d(383, 220);
    glVertex2d(423, 220);
    glVertex2d(398, 255);
    glEnd();

    glColor3ub(255, 222, 173);
    glBegin(GL_POLYGON);
    glVertex2d(383, 150);
    glVertex2d(423, 150);
    glVertex2d(423, 220);
    glVertex2d(383, 220);
    glEnd();

    glColor3ub(222, 184, 135);
    glBegin(GL_POLYGON);
    glVertex2d(423, 150);
    glVertex2d(498, 150);
    glVertex2d(498, 220);
    glVertex2d(423, 220);
    glEnd();

    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(383, 150);
    glVertex2d(498, 150);
    glVertex2d(498, 140);
    glVertex2d(383, 140);
    glEnd();

    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(448, 150);
    glVertex2d(468, 150);
    glVertex2d(468, 195);
    glVertex2d(448, 195);
    glEnd();

    glColor3ub(160, 82, 45);
    glBegin(GL_POLYGON);
    glVertex2d(393, 200);
    glVertex2d(408, 200);
    glVertex2d(408, 175);
    glVertex2d(393, 175);
    glEnd();

    //sun design
    glColor3ub(255, 215, 0);
    sun(25, 30, 365, 450);

    drawHuman(290, 220);
    // counds function
    clouds();
    // Draw Birds
    drawBird(birdX1, birdY1);
    drawBird(birdX2, birdY2);

    // Update bird positions to create flying effect
    birdX1 += 0.25; // Move first bird to right
    birdX2 += 0.23; // Move second bird to right

    // Reset birds when they move out of view
    if (birdX1 > 500) birdX1 = 0;
    if (birdX2 > 500) birdX2 = 0;

    //flower
    flower(20, 70);
    flower(40, 70);
    flower(70, 70);
    flower(90, 70);
    flower(100, 70);
    flower(130, 70);
    flower(150, 70);
    flower(180, 70);
    flower(210, 65);
    flower(240, 65);


    flower(100, 50);
    flower(20, 50);
    flower(50, 55);
    flower(80, 50);
    flower(100, 30);
    flower(20, 25);
    flower(50, 35);
    flower(80, 30);
    flower(110, 35);
    flower(130, 30);
    flower(150, 30);
    flower(170, 30);
    flower(190, 30);
    flower(210, 30);
    flower(230, 30);
    flower(250, 30);
    flower(270, 30);
    flower(285, 30);
    flower(130, 55);
    flower(150, 55);
    flower(170, 55);
    flower(190, 55);
    flower(210, 55);
    flower(230, 55);
    flower(250, 55);
    flower(270, 55);
    flower(285, 55);


    flower(450, 70);
    flower(450 + 30, 65);
    flower(450 - 30, 50);
    flower(450, 50);
    flower(450 - 50, 50);
    flower(450 - 50, 70);
    flower(480, 50);
    flower(440, 35);
    flower(415, 30);
    flower(465, 35);
    flower(480, 30);
    flower(430, 30);flower(400, 30);flower(380, 30);


    // Draw face
    for (float x = 0; x < 200; x += 5) {
        drawFence(x, 20); // Draw fance along the ground
    }
    for (float x = 370; x < 500; x += 5) {
        drawFence(x, 20); // Draw fance along the ground
    }


    ///car
    velocity += acceleration; // Increase velocity by the acceleration value
    // Limit the velocity to max speed
    if (velocity > maxSpeed) {
        velocity = 0.1;
    }
    carX += velocity; // // Update the car's position based on the velocity
    drawCar(carX, 0.0f); // Draw the car at the new position
    glFlush();
    if (carX > 600)carX = -600;


    glutSwapBuffers();
}


int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(800, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Village Scenery");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Editor is loading...
Leave a Comment