Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/freeglut.h>
- #define _USE_MATH_DEFINES
- #include <math.h>
- double WINDOW_HEIGHT = 500;
- double aspect_ratio = 4.0 / 3.0;
- double HEIGHT = 10.0;
- double WIDTH = HEIGHT * aspect_ratio;
- void drawCircle(GLfloat x, GLfloat y, GLfloat r) {
- const size_t points = 30;
- glBegin(GL_POLYGON);
- for (size_t i = 0; i < points; i++) {
- glVertex3f(x + r * cos(2 * i * M_PI / points), y + r * sin(2 * i * M_PI / points), 0.0);
- }
- glEnd();
- }
- void drawPoint(GLfloat x, GLfloat y) {
- glBegin(GL_POINTS);
- glVertex3f(x, y, 0.0);
- glEnd();
- }
- void drawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {
- glBegin(GL_LINES);
- glVertex3f(x1, y1, 0.0);
- glVertex3f(x2, y2, 0.0);
- glEnd();
- }
- GLfloat sunR = 1.0;
- GLfloat sunX = WIDTH / 2;
- GLfloat sunY = HEIGHT * 0.9 - sunR;
- GLfloat sunCenterX = WIDTH / 2, sunCenterY = 0.0, sunRotationAngle = 0.05;
- void drawSun() {
- glColor3f(1.0, 1.0, 0.0);
- drawCircle(sunX, sunY, sunR);
- }
- GLfloat wmW = 3.0, wmH = 6.0;
- GLfloat wmX = WIDTH / 2, wmY = wmH * 0.8, wmRotationAngle = 0.05, wmAngle = 0.0;
- GLfloat wmSize = 2.6;
- void drawWindmill() {
- wmAngle += wmRotationAngle;
- glColor3ub(163, 104, 8);
- glRectf(WIDTH / 2 - wmW / 2, 0.0, WIDTH / 2 + wmW / 2, wmH);
- glColor3ub(201, 201, 201);
- for (size_t i = 0; i < 4; i++)
- {
- glBegin(GL_POLYGON);
- glVertex3f(wmX, wmY, 0.0);
- glVertex3f(wmX + cos(i * (M_PI / 2) - M_PI / 8 + wmAngle) * wmSize, wmY + sin(i * (M_PI / 2) - M_PI / 8 + wmAngle) * wmSize, 0.0);
- glVertex3f(wmX + cos(i * (M_PI / 2) + M_PI / 8 + wmAngle) * wmSize, wmY + sin(i * (M_PI / 2) + M_PI / 8 + wmAngle) * wmSize, 0.0);
- glEnd();
- }
- }
- void moveSun() {
- sunX -= sunCenterX;
- sunY -= sunCenterY;
- GLfloat newX = sunX * cos(sunRotationAngle) - sunY * sin(sunRotationAngle);
- GLfloat newY = sunX * sin(sunRotationAngle) + sunY * cos(sunRotationAngle);
- sunX = newX + sunCenterX;
- sunY = newY + sunCenterY;
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- drawSun();
- drawWindmill();
- Sleep(30);
- glFlush();
- glutPostRedisplay();
- }
- void onMouseClick(int button, int state, int x, int y) {
- switch (button)
- {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- glutIdleFunc(moveSun);
- }
- else {
- glutIdleFunc(NULL);
- }
- break;
- default:
- break;
- }
- }
- void onReshape(int w, int h) {
- if (w / h) {
- WINDOW_HEIGHT = w / aspect_ratio;
- }
- else {
- WINDOW_HEIGHT = h;
- }
- glViewport(0, 0, WINDOW_HEIGHT * aspect_ratio, WINDOW_HEIGHT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0);
- }
- void init()
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 5.0, 0.0, 5.0, -5.0, 5.0);
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(WINDOW_HEIGHT * aspect_ratio, WINDOW_HEIGHT);
- glutInitWindowPosition(300, 180);
- glutCreateWindow("hello");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(onReshape);
- glutMouseFunc(onMouseClick);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement