C语言300行-投篮
#include "stdio。h"
#ifdef __APPLE__
#include 〈GLUT/glut。h〉
#else
#include <GL/glut.h>
#endif
#include 〈stdlib.h>
#include <stdio。h〉
#include <math.h〉
#include 〈windows.h〉
#include 〈time。h〉
static double a=0;
static double b=0.2;
static double t=0;
static double t1=0;
static double v=0;
static double i=0;
static double m=0;
static double n=0;
static double p,q,r,r1=1;
static void resize(int width,int height)//重绘回调函数
{
const float ar=(float)width/(float)height;
glViewport(0,0,width,height);//利用函数glViewport定义视区
glMatrixMode(GL_PROJECTION);//投影矩阵
glLoadIdentity();// 重置坐标系统,使投影变换复位
glFrustum(-ar,ar,—1。0,1。0,2。0,100.0);//利用函数glFrustum定义修剪区
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void display(void)//显示回调
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜色缓冲区和深度缓冲区
t=t+t1*0.0001;
glColor3d(1,0。6,0.6);//画图颜色灰红
//绘制一个实心球,默认原点在屏幕中心与照相机重合
glPushMatrix();
glTranslated(—3+0。5*cos(a),-0.6+0。5*sin(a),-5);
glRotated(0,0,0,0);
glutSolidSphere(b,16,16);
glPopMatrix();
glColor3d(1,0,0);//画图颜色红
//绘制一个实心球,默认原点在屏幕中心与照相机重合
glPushMatrix();
glTranslated(—3+t*cos(a)*v*t1,-0。6+(t*sin(a)*v—45*t*t)*t1,—5);
glRotated(0,0,0,0);
glutSolidSphere(0。2*r1,16,16);
glPopMatrix();
glColor3d(1,1,1);//画图颜色灰
//绘制一个线框圆环
glPushMatrix();
glTranslated(m+2。3,n—0。6,-5);
glRotated(100,1,0,0);
glRotated(7。5,0,1,0);
glutWireTorus(0。1*r1,0。6,16,16);
glPopMatrix();
if(pow(3—t*cos(a)*v*t1+m+2。3,2)+pow(0.6-(t*sin(a)*v-45*t*t)*t1+n—0。6,2)〈0.024)
{
t=t-t1*0。0001;
r1=3。5;
}
else r1=1;
for(p=-3。5,q=0.5;p<3。6;p=p+0.5)
{
if(pow(3-t*cos(a)*v*t1+p,2)+pow(0。6—(t*sin(a)*v—45*t*t)*t1+q,2)<0。2)r=0.4;
else r=0。05;
glColor3d(0,0,0);//画图颜色黑
//绘制一个线框圆环
glPushMatrix();
glTranslated(p,q,—5);
glRotated(t*5000,0,1,0);
glutWireTorus(r,0.15,16,16);
glPopMatrix();
}
for(p=-3.5,q=0;p〈3.6;p=p+0。5)
{
if(pow(3—t*cos(a)*v*t1+p,2)+pow(0.6-(t*sin(a)*v—45*t*t)*t1+q,2)<0。2)r=0.4;
else r=0.05;
glColor3d(0,0,0);//画图颜色黑
//绘制一个线框圆环
glPushMatrix();
glTranslated(p,q,—5);
glRotated(t*5000,0,1,0);
glutWireTorus(r,0.15,16,16);
glPopMatrix();
}
for(p=—3。5,q=1;p〈3。6;p=p+0.5)
{
if(pow(3-t*cos(a)*v*t1+p,2)+pow(0。6-(t*sin(a)*v—45*t*t)*t1+q,2)<0。2)r=0。4;
else r=0.05;
glColor3d(0,0,0);//画图颜色黑
//绘制一个线框圆环
glPushMatrix();
glTranslated(p,q,—5);
glRotated(t*5000,0,1,0);
glutWireTorus(r,0.15,16,16);
glPopMatrix();
}
glutSwapBuffers();//交换缓冲区
}
static void key(unsigned char key, int x, int y)//键盘回调函数
{
switch(key)
{
case 27://Esc键的ASCII码为27
case 13://回车的ASCII码为13
exit(0);
break;
case 43://加号的ASCII码为43
if (a<1.4)
{
a=a+0。1;
}
break;
case 45://减号的ASCII码为45
if (a〉0)
{
a=a-0.1;
}
break;
case 49://1的ASCII码为49
v=4;
break;
case 50://2的ASCII码为50
v=8;
break;
case 51://3的ASCII码为51
v=12;
break;
case 52://4的ASCII码为52
v=16;
break;
case 53://5的ASCII码为53
v=20;
break;
case 54://6的ASCII码为54
v=24;
break;
case 55://7的ASCII码为55
v=28;
break;
case 56://8的ASCII码为56
v=32;
break;
case 57://9的ASCII码为57
v=36;
break;
case 32://空格的ASCII码为32
if(t〈0.1)
{
t1=1;
b=0.001;
}
else
{
b=0.2;
v=0;
t1=0;
t=0;
m=0;
n=0;
}
break;
case 97://a的ASCII码为97
m=m-0。2;
break;
case 119://w的ASCII码为119
n=n+0。2;
break;
case 100://d的ASCII码为100
m=m+0。2;
break;
case 115://s的ASCII码为115
n=n-0.2;
break;
}
glutPostRedisplay();//标记当前窗口需要重新绘制
}
static void idle(void)//空闲回调函数
{
glutPostRedisplay();//标记当前窗口需要重新绘制
}
//定义一个默认的光源的属性值,使用数组来存放光源的颜色值
const GLfloat light_ambient[] = { 0。0f, 0.0f, 0.0f, 1。0f };//光源中环境光的颜色,黑色,第四个分量1。0表示不透明。
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1。0f };//光源中漫射光的颜色,白色
const GLfloat light_specular[] = { 1.0f, 1.0f, 1。0f, 1。0f };//光源中镜面反射光的颜色
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0。0f };//定向光源,指定了光源的方向
//定义材质属性值,使用数组来存放材质的颜色值
const GLfloat mat_ambient[] = { 0。7f, 0.7f, 0。7f, 1。0f }; //材质的环境颜色
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0。8f, 1。0f }; //材质的漫射光颜色
const GLfloat mat_specular[] = { 1.0f, 1。0f, 1。0f, 1。0f };//材质镜面反射光的颜色
const GLfloat high_shininess[] = { 100。0f };//材质的镜面反射指数
int main(int argc, char *argv[])
{
MessageBox(NULL,"\”+、-\”控制角度\n\"1~9\"控制速度\n\"wasd\"控制框移动\n按空格发射",”介绍",MB_OK);
glutInit(&argc, argv);//对GLUT 进行初始化
glutInitWindowPosition(15,15);//窗口位置
glutInitWindowSize(960,640);//窗口大小
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);//使用双缓冲区窗口,窗口使用深度缓存
glutCreateWindow("GLUT Shapes”);//参数将被作为窗口的标题
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);//注册回调函数
glClearColor(0。1f,0.1f,1。0f,1);//参数可以在0、1改动
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);// 开启剔除操作效果
glDepthFunc(GL_LESS);// 指定深度缓冲比较值,深度值小于参考值,则通过
glEnable(GL_LIGHT0);//启用0号光源
glEnable(GL_NORMALIZE);// 启用自动单位化法向量
glEnable(GL_COLOR_MATERIAL);//启用材质颜色
glEnable(GL_LIGHTING);//启用光照
//设置0号光源的特性值
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//设置材质的特性值,本题只设置了物体前面的材质值
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();//启动glut程序进入事件循环
return EXIT_SUCCESS;
}