抛物运动 小球 二维 OpenGL C++

// DrawBall.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<gl/glut.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<time.h>
using namespace std;
double v = 0.025;//初速度大小
double vx = 0.5*v;
double vy = 3 * v;
double a = -0.2;//向下加速度
static double x, y;//圆心坐标
double t = 0.0;

void MoveBall(void)
{
    t = t + 0.001;
    x = x + vx * t;
    y = y +vy*t + a*t*t;
}
//画一次圆
void DrawFilledCircle(double x, double y, double radius, int sections)
{
    float alpha;
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(x, y);
    for (int count = 0; count <= sections; count++)
    {
        alpha = count * 2 * 3.1415926 / sections;//圆心角
        glVertex2f(x + radius*cos(alpha), y + radius*sin(alpha));
    }
    glEnd();
}

void init(void)//初始化
{
    x = -1.0;
    y = -1.0;
    glClearColor(1.0, 1.0, 1.0, 0.0);//清屏色
    glOrtho(-2.0, 2.0, -2.0, 2.0, 2.0, -2.0);//视见体

}


void display(void)//重绘屏幕
{
    glClear(GL_COLOR_BUFFER_BIT);//清除颜色缓存
    glColor3f(0.0, 0.0, 1.0);//当前颜色
    DrawFilledCircle(x, y, 0.2,100);//在这里写绘制函数

                            glutSwapBuffers();//动画显示
    //glFlush();//静态显示 赶紧显示
}
void myTime(int value)
{
    MoveBall();
    glutPostRedisplay();
    glutTimerFunc(1, myTime, 1);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);//glut库与控制台初始化函数
                          glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//显示模式 单双缓存 颜色模式 深度
    //glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(500, 200);//窗口的位置 窗口左上角距离屏幕左上角
    glutInitWindowSize(500, 500);
    glutCreateWindow(" BallMovement ");//窗口名字
    init();//调用自定义的初始化函数

    glutDisplayFunc(display);//注册显示回调函数
    glutTimerFunc(1, myTime, 1);
    glutMainLoop();//进入事件循环
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/turningpoint/p/8970497.html