#include <windows.h>
#include "gl/glut.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
// 参数指定正方形的位置和大小
float xsite = 100.0; //圆心坐标
float ysite = 200.0;
GLsizei rsize = 20;
// 正方形运动变化的步长
float xstep = 2.0f;
float ystep = 0.0f;
// 窗口的大小
float windowWidth = 500;
float windowHeight = 400;
int TIME=13;
void TimerFunction(int value);
inline void myArc(int x,int y,int a,int b,int start_angle,int end_angle,double fr,double fg,double fb)
{
int angle;
const double pi = acos(-1.0f);
if(fr>=0)
{
glBegin(GL_TRIANGLE_FAN);
glColor3d(fr,fg,fb);
for(angle = start_angle; angle <= end_angle; angle = angle + 1)
{
int px = x + a * cos(angle / 180.0 * pi);
int py = y + b * sin(angle / 180.0 * pi);
glVertex2i(px, py);
}
glEnd();
// TimerFunction(0);
}
}
void drawLine(int Linex1,int Liney1,int Linex2,int Liney2)
{
glColor3f(1.0f , 0.0f , 0.0f); //RGB
glBegin(GL_LINES);
{
glVertex2i(Linex1,Liney1);
glVertex2i(Linex2,Liney2);
}
glEnd();
glFlush();
}
inline void myCircle(int x,int y,int radius,double fr,double fg,double fb)
{
myArc(x,y,radius,radius,0,360,fr,fg,fb);
drawLine(0,y,windowWidth,y);
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
myCircle(xsite, ysite, rsize, 0, 1.0, 1.0);
drawLine(0, 200, windowWidth, 200);
glutSwapBuffers();
}
void ChangeSize(GLsizei w, GLsizei h)
{
if(h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
{
windowHeight = 300.0f*h/w;
windowWidth = 300.0f;
}
else
{
windowWidth = 300.0f*w/h;
windowHeight = 300.0f;
}
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void TimerFunction(int value)
{
// 处理到达窗口边界的球,使之反弹
if(xsite + rsize > windowWidth || xsite < 0) { //right,rebound to the left
xstep = -xstep;
}
if(xsite - rsize < 0) {
xstep = -xstep;
}
if(ysite + rsize > windowHeight || ysite < 0) { //
ystep = -ystep;
}
if (ysite - rsize < 0) {
ystep = -ystep;
}
// 根据步长修改正方形的位置
ystep = ystep - 0.5f * 450.0f * 0.03f * 0.03f;
xsite += xstep;//重设x, y坐标
ysite += ystep;
// 用新坐标重新绘图
glutPostRedisplay();
glutTimerFunc(TIME,TimerFunction, 1);
}
void main()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(TIME, TimerFunction, 1);
glutMainLoop();
}
OpengL球体跳跃前进例子
最新推荐文章于 2022-04-28 16:09:42 发布