#define WIN32_LEAN_AND_MEAN //裁减过大的函数库
#include <windows.h> //标准Windows应用程序头文件
#include <gl/gl.h> //标准OpenGL头文件
#include <gl/glu.h> //OpenGL实用工具库
#include <gl/glut.h>
#include <gl/glaux.h> //OpenGL辅助函数库
#include "vase.h"
//全局变量
GLfloat xrot; // X 旋转量
GLfloat yrot; // Y 旋转量
GLfloat xspeed; // X 旋转增量
GLfloat yspeed; // Y 旋转增量
GLfloat z=-5.0f; // 屏幕深度
HDC g_HDC; //全局设备环境
bool fullScreen = false; //是否全屏幕
GLboolean g_keys[256]; //按键信息 如果按下 对应的值为true
POINTF lpPoints[4]; //bezier控制点
//光源
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; //环境光
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; //散射光
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f }; //光的位置
POINTF point1[100];
POINTF point2[100];
POINTF point[200]; //拼接的Bezier曲线
VPOINT points[80000]; //旋转后生成的点
int sum=0; //点的个数
int row=0;
VPOINT pointbuttom[400];
int buttom=0;
//花瓶数据
void projector1(POINTF lpPoints[], int k)
{
int i=0;
for (double t = 0; t <= 1; t = t + 0.01) //得bezier上的点
{
POINTF p1;
p1.x = (float)(pow((1 - t), 3) * lpPoints[0].x + 3 * t * pow((1 - t), 2) * lpPoints[1].x + 3 * pow(t, 2) * (1 - t) * lpPoints[2].x + pow(t, 3) * lpPoints[3].x);
p1.y = (float)(pow((1 - t), 3) * lpPoints[0].y + 3 * t * pow((1 - t), 2) * lpPoints[1].y + 3 * pow(t, 2) * (1 - t) * lpPoints[2].y + pow(t, 3) * lpPoints[3].y);
if (k==0)
{
point1[i].x=p1.x;
point1[i].y=p1.y;
i=i+1;
}
else
{
point2[i].x=p1.x;
point2[i].y=p1.y;
i=i+1;
}
}
}
//两条曲线拼接成一条曲线
void add()
{
for (int i=0;i<200;i++)
{
if (i<100)
{
point[i].x=point1[i].x;
point[i].y=point1[i].y;
}
else
{
point[i].x=point2[i-100].x;
point[i].y=point2[i-100].y;
}
}
}
void projector2()
{
//VPOINT p;
int i;
for (double angle = 0; angle <= 2 * PAI; angle += 0.05)
{
for (i = 0; i < 200; i++)
{
points[sum].x = (float)(point[i].x * cos(angle)); //绕y旋转
points[sum].z = (float)(point[i].x * sin(angle));
points[sum].y = (float)(point[i].y);
sum = sum + 1;
}
row = row + 1;
}
}
void vase()
{
//************************************************************
//花瓶的第一条Bezier曲线
/*lpPoints[0].x = 18;
lpPoints[0].y = -80;
lpPoints[1].x = -18;
lpPoints[1].y = 40;
lpPoints[2].x = -40;
lpPoints[2].y = 30;
lpPoints[3].x = -55;
lpPoints[3].y = 15;
*/
lpPoints[0].x = 60;
lpPoints[0].y = -40;
lpPoints[1].x = -30;
lpPoints[1].y = 38;
lpPoints[2].x = 0;
lpPoints[2].y = -0;
lpPoints[3].x = 5;
lpPoints[3].y = -120;
projector1(lpPoints, 0);
//***********************************************************
//花瓶的第二条Bezier曲线
lpPoints[0].x = -0;
lpPoints[0].y = 10;
lpPoints[1].x = -0;
lpPoints[1].y = 10;
lpPoints[2].x = 10;
lpPoints[2].y = -0;
lpPoints[3].x =0;
lpPoints[3].y = -0;
projector1(lpPoints, 1);
add();
projector2();
}
void keyfun()
{
if (g_keys[VK_PRIOR]) //前
{
z-=0.02f;
}
if (g_keys[VK_NEXT]) //后
{
z+=0.02f;
}
if (g_keys[VK_UP]) //上
{
xspeed-=0.01f;
}
if (g_keys[VK_DOWN]) //下
{
xspeed+=0.01f;
}
if (g_keys[VK_RIGHT]) //右
{
yspeed+=0.01f;
}
if (g_keys[VK_LEFT]) //左
{
yspeed-=0.01f;
}
}
void DisplaySence()
{
int i , j ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,z);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
for (j = 0; j < row - 1; j++)
{
for (i = 1; i < 200; i++)
{
glBegin(GL_QUAD_STRIP);
glNormal3f(points[j * 200 + i].x / 100, points[j * 200 + i].y / 100, points[j * 200 + i].z / 100);
glVertex3f(points[j * 200 + i].x / 100, points[j * 200 + i].y / 100, points[j * 200 + i].z / 100);
glNormal3f(points[j * 200 + i - 1].x / 100, points[j * 200 + i - 1].y / 100, points[j * 200 + i - 1].z / 100);
glVertex3f(points[j * 200 + i - 1].x / 100, points[j * 200 + i - 1].y / 100, points[j * 200 + i - 1].z / 100);
glNormal3f(points[j * 200 + i + 200].x / 100, points[j * 200 + i + 200].y / 100, points[j * 200 + i + 200].z / 100);
glVertex3f(points[j * 200 + i + 200].x / 100, points[j * 200 + i + 200].y / 100, points[j * 200 + i + 200].z / 100);
glNormal3f(points[j * 200 + i + 200 - 1].x / 100, points[j * 200 + i + 200 - 1].y / 100, points[j * 200 + i + 200 - 1].z / 100);
glVertex3f(points[j * 200 + i + 200 - 1].x / 100, points[j * 200 + i + 200 - 1].y / 100, points[j * 200 + i + 200 - 1].z / 100);
glEnd();
}
}
int row1;
row1=row-1;
//最后的
for (i = 0; i < 199; i++)
{
glBegin(GL_QUADS);
glNormal3f(points[i].x/100,points[i].y/100,points[i].z/100);
glVertex3f(points[i].x/100,points[i].y/100,points[i].z/100);
glNormal3f(points[i+1].x/100,points[i+1].y/100,points[i+1].z/100);
glVertex3f(points[i+1].x/100,points[i+1].y/100,points[i+1].z/100);
glNormal3f(points[row1*200+i+1].x/100,points[row1*200+i+1].y/100,points[row1*200+i+1].z/100);
glVertex3f(points[row1*200+i+1].x/100,points[row1*200+i+1].y/100,points[row1*200+i+1].z/100);
glNormal3f(points[row1*200+i].x/100,points[row1*200+i].y/100,points[row1*200+i].z/100);
glVertex3f(points[row1*200+i].x/100,points[row1*200+i].y/100,points[row1*200+i].z/100);
glEnd();
}
//glEnable(GL_AUTO_NORMAL);
//花瓶 瓶底
glEnable(GL_NORMALIZE);
glBegin(GL_POLYGON);
for (i = 0 ; i < row ; i++ )
glVertex3f(points[i*200+199].x/100,points[i*200+199].y/100,points[i*200+199].z/100);
glEnd();
glFlush();
SwapBuffers(g_HDC);
xrot+=xspeed;
yrot+=yspeed;
}
//Render
//说明:初始化
void InitGL()
{
glEnable(GL_DEPTH_TEST); // 启动深度检测
//在此处绘制
glClearColor(0.6f, 0.6f, 0.6f, 0.6f); // 设置清理色为黑色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清理颜色和深度缓存
glLoadIdentity(); // 复位模型视图矩阵
//光照
/*float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float mat_shiniess[] = { 50.0f };
float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
float white_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float lmodel_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shiniess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
*/
GLfloat ambient[]={2.0,2.0,2.0,1.0};
GLfloat diffuse[]={1.0,1.0,0.0,1.0};
GLfloat specular[]={5.0,2.0,1.1,0.0};
GLfloat position[]={2.0,5.-0,5.0,0.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
GLfloat mat_ambient[]={0.6,0.4,