Untitled
#include <GL/glut.h> #include <cmath> // For sin and cos functions // Global variable to track mode bool isDay = true; float car1X = -1.0f, car2X = -0.5f, car3X = 0.2f; // Car positions void drawText(float x, float y, const char* text) { glRasterPos2f(x, y); for (const char* c = text; *c != '\0'; c++) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c); } } void drawCloud(float x, float y) { glColor3f(1.0, 1.0, 1.0); // White clouds glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.1f * cos(theta), y + 0.05f * sin(theta)); } glEnd(); glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f((x + 0.12f) + 0.1f * cos(theta), y + 0.07f * sin(theta)); } glEnd(); glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f((x - 0.12f) + 0.1f * cos(theta), y + 0.07f * sin(theta)); } glEnd(); } void drawBuilding(float x, float y, float width, float height, bool hasSolar, const char* label) { // Draw the building glColor3f(0.6, 0.2, 0.2); // Default building color glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x + width, y); glVertex2f(x + width, y + height); glVertex2f(x, y + height); glEnd(); // Add windows if (!isDay && hasSolar) { glColor3f(1.0, 1.0, 0.0); // Yellow for lit windows at night with solar } else { glColor3f(0.2, 0.2, 0.2); // Dark for unlit windows } glBegin(GL_QUADS); glVertex2f(x + width * 0.1f, y + height * 0.5f); glVertex2f(x + width * 0.4f, y + height * 0.5f); glVertex2f(x + width * 0.4f, y + height * 0.7f); glVertex2f(x + width * 0.1f, y + height * 0.7f); glEnd(); // Add solar panels if applicable if (hasSolar) { glColor3f(0.0, 0.0, 1.0); // Blue for solar panels glBegin(GL_QUADS); glVertex2f(x + width * 0.1f, y + height); glVertex2f(x + width * 0.4f, y + height); glVertex2f(x + width * 0.35f, y + height * 1.1f); glVertex2f(x + width * 0.15f, y + height * 1.1f); glEnd(); glBegin(GL_QUADS); glVertex2f(x + width * 0.6f, y + height); glVertex2f(x + width * 0.9f, y + height); glVertex2f(x + width * 0.85f, y + height * 1.1f); glVertex2f(x + width * 0.65f, y + height * 1.1f); glEnd(); } glColor3f(1.0, 1.0, 1.0); // White text drawText(x + width * 0.25f, y + height * 0.1f, label); } void drawSun(float x, float y) { glColor3f(1.0, 1.0, 0.0); // Yellow sun glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.1f * cos(theta), y + 0.1f * sin(theta)); } glEnd(); } void drawMoon(float x, float y) { glColor3f(1.0, 1.0, 1.0); // White moon glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.1f * cos(theta), y + 0.1f * sin(theta)); } glEnd(); } void drawCar1(float x, float y) { glColor3f(1.0, 0.0, 0.0); // Red car glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x + 0.3f, y); glVertex2f(x + 0.3f, y + 0.1f); glVertex2f(x, y + 0.1f); glEnd(); glColor3f(0.0, 0.0, 0.0); // Wheels glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.05f + 0.03f * cos(theta), y - 0.03f + 0.03f * sin(theta)); } glEnd(); glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.25f + 0.03f * cos(theta), y - 0.03f + 0.03f * sin(theta)); } glEnd(); } void drawCar2(float x, float y) { glColor3f(0.0, 0.0, 1.0); // Blue car glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x + 0.4f, y); glVertex2f(x + 0.4f, y + 0.12f); glVertex2f(x, y + 0.12f); glEnd(); glColor3f(0.0, 0.0, 0.0); // Wheels glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.1f + 0.04f * cos(theta), y - 0.03f + 0.04f * sin(theta)); } glEnd(); glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.3f + 0.04f * cos(theta), y - 0.03f + 0.04f * sin(theta)); } glEnd(); } void drawCar3(float x, float y) { glColor3f(0.0, 1.0, 0.0); // Green truck glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x + 0.5f, y); glVertex2f(x + 0.5f, y + 0.15f); glVertex2f(x, y + 0.15f); glEnd(); glColor3f(0.0, 0.5, 0.0); // Cabin glBegin(GL_QUADS); glVertex2f(x + 0.35f, y + 0.15f); glVertex2f(x + 0.5f, y + 0.15f); glVertex2f(x + 0.5f, y + 0.25f); glVertex2f(x + 0.35f, y + 0.25f); glEnd(); glColor3f(0.0, 0.0, 0.0); // Wheels glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.1f + 0.04f * cos(theta), y - 0.03f + 0.04f * sin(theta)); } glEnd(); glBegin(GL_POLYGON); for (int i = 0; i < 360; i += 10) { float theta = i * 3.14159 / 180; glVertex2f(x + 0.4f + 0.04f * cos(theta), y - 0.03f + 0.04f * sin(theta)); } glEnd(); } void display() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(isDay ? 0.53f : 0.0f, isDay ? 0.81f : 0.0f, isDay ? 0.92f : 0.2f); // Background glBegin(GL_QUADS); glVertex2f(-1.0f, -1.0f); glVertex2f(1.0f, -1.0f); glVertex2f(1.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); if (isDay) drawSun(0.8f, 0.8f); else drawMoon(0.8f, 0.8f); drawCloud(-0.5f, 0.7f); drawCloud(0.2f, 0.8f); glColor3f(0.2f, 0.2f, 0.2f); // Road glBegin(GL_QUADS); glVertex2f(-1.0f, -0.5f); glVertex2f(1.0f, -0.5f); glVertex2f(1.0f, -0.7f); glVertex2f(-1.0f, -0.7f); glEnd(); drawBuilding(-0.8f, -0.7f, 0.4f, 0.6f, true, "OFFICE"); drawBuilding(-0.2f, -0.7f, 0.4f, 0.6f, true, "Alpha Residency"); drawBuilding(0.4f, -0.7f, 0.4f, 0.6f, false, "HOME"); drawCar1(car1X, -0.65f); drawCar2(car2X, -0.65f); drawCar3(car3X, -0.65f); glFlush(); } void update(int value) { car1X += 0.02f; car2X += 0.02f; car3X += 0.02f; if (car1X > 1.2f) car1X = -1.2f; if (car2X > 1.2f) car2X = -1.2f; if (car3X > 1.2f) car3X = -1.2f; glutPostRedisplay(); glutTimerFunc(30, update, 0); } void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { float mx = (float)x / 400.0f - 1.0f; float my = 1.0f - (float)y / 300.0f; if (mx >= -0.9f && mx <= -0.6f && my >= -0.9f && my <= -0.8f) isDay = true; if (mx >= -0.5f && mx <= -0.2f && my >= -0.9f && my <= -0.8f) isDay = false; glutPostRedisplay(); } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800, 600); glutCreateWindow("Day and Night Mode with Cars"); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 1.0, -1.0, 1.0); glutDisplayFunc(display); glutMouseFunc(mouse); glutTimerFunc(30, update, 0); glutMainLoop(); return 0; }
Leave a Comment