OpengL球体跳跃前进例子

#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();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值