#include <GL/gl.h> #include<stdio.h> #include<math.h> #include <GL/glut.h> void…
- User
#include <GL/gl.h> #include<stdio.h> #include<math.h> #include <GL/glut.h>
void circle(GLfloat rx, GLfloat ry, GLfloat cx, GLfloat 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 sun(GLfloat rx, GLfloat ry, GLfloat cx, GLfloat 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.0, 0.0, 0.9, 0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 500, 0.0, 500); // window size } float bx = 10;
void hills() { // Hills 1 glColor3ub(184, 134, 11); glBegin(GL_POLYGON); glVertex2d(-40, 300); glVertex2d(200, 300); glVertex2d(100, 450); glEnd();
// Hills 2 glColor3ub(218, 165, 32); glBegin(GL_POLYGON); glVertex2d(150, 300); glVertex2d(350, 300); glVertex2d(250, 450); glEnd(); // Hills 3 glColor3ub(184, 134, 11); glBegin(GL_POLYGON); glVertex2d(300, 300); glVertex2d(520, 300); glVertex2d(400, 450); glEnd();}
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, 150); glVertex2d(0, 150); glEnd(); // road glColor3ub(255, 255, 255); glBegin(GL_POLYGON); glVertex2d(0, 55); glVertex2d(500, 55); glVertex2d(500, 115); glVertex2d(0, 115); glEnd(); glColor3ub(0, 0, 0); glBegin(GL_POLYGON); glVertex2d(0, 60); glVertex2d(500, 60); glVertex2d(500, 110); glVertex2d(0, 110); glEnd(); // hills hills(); //sun design glColor3ub(255, 215, 0); sun(20, 20, 175, 450); glPushMatrix(); glTranslatef(bx, 0, 0); glColor3ub(255, 0, 0); glBegin(GL_POLYGON); glVertex2d(410, 100); glVertex2d(490, 100); glVertex2d(485, 130); glVertex2d(410, 130); glEnd(); glColor3ub(255, 0, 0); glBegin(GL_POLYGON); glVertex2d(420, 130); glVertex2d(475, 130); glVertex2d(465, 160); glVertex2d(430, 160); glEnd(); // car window glColor3ub(220, 220, 220); glBegin(GL_POLYGON); glVertex2d(425, 130); glVertex2d(445, 130); glVertex2d(445, 150); glVertex2d(430, 150); glEnd(); // car window glColor3ub(220, 220, 220); glBegin(GL_POLYGON); glVertex2d(450, 130); glVertex2d(470, 130); glVertex2d(465, 150); glVertex2d(450, 150); glEnd(); // car wheel glColor3ub(0, 0, 0); circle(10, 14, 435, 100); circle(10, 14, 465, 100); glColor3ub(245, 245, 245); circle(6, 10, 435, 100); circle(6, 10, 465, 100); glPopMatrix(); bx += .05; if (bx > 0) bx = -500; glutPostRedisplay(); glFlush(); glutSwapBuffers();}
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(1000, 600); glutInitWindowPosition(300, 50); glutCreateWindow("A Moving Car Scenario"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
- Assistant
The code is a simple OpenGL program that displays a moving car scenario. The program uses basic shapes like polygons and circles to draw the elements of the scene, such as the ground, road, hills, car body, car wheels, and the sun. The car moves horizontally across the screen, giving the impression of a moving car. The code also sets up the necessary OpenGL settings and functions for the program to run.
- Assistant
The program begins by including the necessary header files for OpenGL and GLUT. It then defines the circle and sun functions, which use a set of predefined vertices to draw a circle on the screen.
The init function sets up the initial OpenGL settings, such as the clear color and projection matrix.
The hills function is a helper function that draws three hills using different shades of brown.
The display function is where all the drawing code is located. It starts by clearing the color buffer using glClear and then sets the color for the ground and road. It uses glBegin and glEnd to draw polygons for the ground and road.