#define _CRT_SECURE_NO_WARNINGS
#include <GL/glut.h>
#include <stdio.h>
#include<windows.h>
#include<iostream>
#include<math.h>
#define GI 9.8
#define Length 120
#define PI 3.1415
#define CX -200
#define CY 100
#define Radius 50
#define JX 0
#define JY 0
int t = 0;
int direction = 1;
int direction2;
using namespace std;
float dt;
float circle;
int yspeed = 0;
int xspeed = 0;
float cirangle = 0;
float i = PI / 60;
int mini = 1;
void Wheel(float circle)
{
glBegin(GL_LINE_LOOP);
glVertex2f(CX, CY);
glVertex2f(CX + 30, CY + 70);
glVertex2f(CX - 30, CY + 70);
glEnd();
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 12; i++)
{
glColor3f(1.0, 1.0, 1.0);
glVertex2f(CX + Radius * cos(float(PI * i / 6.0 + circle)), CY + Radius * sin(float((PI * i / 6.0 + circle))));
}
glEnd();
for (int i = 0; i < 12; i++)
{
glColor3f(((((rand() + i) % 255) / 255.0)), ((((rand() + i) % 255) / 255.0)), ((((rand() + i) % 255) / 255.0)));
glBegin(GL_LINE_LOOP);
glVertex2f(CX + Radius * cos(float(PI * i / 6.0 + circle)) - 5, CY + Radius * sin(float((PI * i / 6.0 + circle))));
glVertex2f(CX + Radius * cos(float(PI * i / 6.0 + circle)) + 5, CY + Radius * sin(float((PI * i / 6.0 + circle))));
glVertex2f(CX + Radius * cos(float(PI * i / 6.0 + circle)) + 5, CY + Radius * sin(float((PI * i / 6.0 + circle))) + 10);
glVertex2f(CX + Radius * cos(float(PI * i / 6.0 + circle)) - 5, CY + Radius * sin(float((PI * i / 6.0 + circle))) + 10);
glEnd();
}
glColor3f(1.0, 1.0, 1.0);
}
void Tower(float height)
{
glBegin(GL_LINE_LOOP);
glVertex2f(220, -30);
glVertex2f(230, -30);
glVertex2f(230, 200);
glVertex2f(220, 200);
glEnd();
glColor3f(rand() % 10 / 10.0, rand() % 10 / 10.0, rand() % 10 / 10.0);
glBegin(GL_LINE_LOOP);
glVertex2f(210, height);
glVertex2f(240, height);
glVertex2f(240, height - 20);
glVertex2f(210, height - 20);
glColor3f(1.0, 1.0, 1.0);
}
void Boat(float angle)
{
float angle1 = angle + PI / 10;
float angle2 = angle - PI / 10;
float angle3 = angle + PI / 45;
float angle4 = angle - PI / 45;
glBegin(GL_LINE_LOOP);
glVertex2f(30, 0);
glVertex2f(80, 150);
glVertex2f(-20, 150);
glEnd();
glColor3f(rand() % 5 / 5.0, rand() % 5 / 5.0, rand() % 5 / 5.0);
glBegin(GL_LINE_LOOP);
glVertex2f(Length * cos(angle1) + 30, Length * sin(angle1));
glVertex2f(Length * cos(angle2) + 30, Length * sin(angle2));
glVertex2f((Length + 10) * cos(angle4) + 30, (Length + 10) * sin(angle4));
glVertex2f((Length + 10) * cos(angle3) + 30, (Length + 10) * sin(angle3));
glColor3f(1.0, 1.0, 1.0);
glEnd();
glBegin(GL_LINES);
glVertex2f(30, 0);
glVertex2f((Length - 3) * cos(angle3) + 30, (Length - 3) * sin(angle3));
glVertex2f(30, 0);
glVertex2f((Length - 3) * cos(angle4) + 30, (Length - 3) * sin(angle4));
glEnd();
}
void Init()
{
glClearColor(0.9f, 0.6f, 0.7f, 1.0f);
}
void Reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-(double)w / 2, (double)w / 2, (double)h / 2, -(double)h / 2);
}
void Draw(float angle, float height, float circle)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
Boat(angle);
Wheel(circle);
Tower(height);
glEnd();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
// 获取键盘输入
switch (key) {
case 's':
yspeed += 10;
break;
case 'a':
xspeed -= 10;
break;
case 'w':
yspeed -= 10;
break;
case 'd':
xspeed += 10;
break;
case 'q':
case 0x1b:
exit(0);
default:
break;
}
// 重新绘制图形
glutPostRedisplay();
}
void timefunction(int value)
{
//float i2 = PI*3/4;
//direction2 = -1;
cirangle += PI / 180;
t += mini;
Draw(i, GI / 2 * t * t / 50.0, cirangle);
glutSwapBuffers();
i += direction * sqrt(abs(sin(i * 3 / 2 - PI / 4))) * PI / 60.0;
//i2 += direction2 *sqrt(abs(sin(i*2-PI/2)))* PI / 180.0;
if (t == 40 || t == 0)
{
mini = -mini;
}
if ((i < PI / 6 && direction < 0) || (i > PI * 5 / 6 && direction > 0))
{
direction = -direction;
//direction2 = -direction2;
}
glutPostRedisplay();
glutTimerFunc(10, timefunction, 1);
}
void myDisplay(void)
{
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);//初始化glut
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);//1显示窗口左上角初始位置
glutInitWindowSize(640, 480);//显示窗口大小
glutCreateWindow("FirstDemo");// 显示窗口的名称
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(myDisplay);
glutKeyboardFunc(keyboard);
glutTimerFunc(20, timefunction, 1);
// 进入主循环
glutMainLoop();
return 0;
}
06-07
204