#include "CubeOperate.h"
CubeVector cubev; //立方体变量
GLuint texture[6];
Image *image[6]; //图片
Plane planeAll[6]; //六个平面
GLfloat ModelViewMatrix[16] = {1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0}; //模型视图变换矩阵,初始化为单位矩阵
Point pointCurrentState[8]; //八个点当前状态状态
Point IntersectionPoint[6]; //观察点和鼠标的连线与六个面的交点
Point eyepoint ; //经过鼠标点,垂直与近裁截面的直线的一个点
Point mousepoint; //用来保存鼠标点击的点的三维坐标
BOOL Tag[6]; //用来表示六个交点是否在面上,在则true,否则false
char *pictures[]={ //图片路径
"pictures/1.bmp",
"pictures/2.bmp",
"pictures/3.bmp",
"pictures/4.bmp",
"pictures/5.bmp",
"pictures/6.bmp"
};
int ImageLoad(char *filename,Image *image) //读取bmp格式的图片到image
{
FILE *fp;
unsigned long size; // 图片长度
unsigned long i; // 计数
unsigned short int planes;
unsigned short int bpp;
char temp; // bgr -rgb 变换
if ((fp = fopen(filename, "rb")) == NULL) //打开文件
{
printf("File Not Found: %s\n", filename);
return 0;
}
fseek(fp, 18, SEEK_CUR); // 跳过文件的头部,准备读取宽度和高度
if ((i = fread(&image->width, 4, 1, fp)) != 1) // 读取宽度
{
printf("Error reading width from %s. \n", filename);
return 0;
}
printf("Width of %s: %lu\n", filename, image->width);
if ((i = fread(&image->length, 4, 1, fp)) != 1) // 读取宽度
{
printf("Error reading height from %s. \n", filename);
return 0;
}
printf("Height of %s: %lu\n", filename, image->length);
size = image->width * image->length * 3; // 计算长度(24bits或3bytes每个像素)
// 读取
if ((fread(&planes, 2, 1, fp)) != 1)
{
printf("Error reading planes from %s. \n", filename);
return 0;
}
if (planes != 1)
{
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
if ((i = fread(&bpp, 2, 1, fp)) != 1)
{
printf("Error reading bpp from %s. \n", filename);
return 0;
}
if (bpp != 24)
{
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
fseek(fp, 24, SEEK_CUR); // 跳过余下的头文件数据
image->data = (char*) malloc (size); // 分配大小
if (image->data == NULL)
{
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, fp)) != 1) //读取数据
{
printf("Error reading image data from %s. \n", filename);
return 0;
}
for (i = 0; i < size; i += 3) // 交换颜色 bgr -> rgb
{
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
return 1;
}
// 读取 bitmaps 并转化成纹理
void LoadGLTextures()
{
for(int i = 0; i < 6; i++)
{
image[i] = (struct Image*)malloc(sizeof(Image));
if (image[i] == NULL)
{
printf("Error allocating space for image");
exit(0);
}
if (!ImageLoad(pictures[i], image[i])) //读取图片到Image中
{
exit(1);
}
glGenTextures(i+1, &texture[i]); // 创建纹理
glBindTexture(GL_TEXTURE_2D, texture[i]); // 绑定2D纹理
// 当图片大于当前纹理时线性缩放
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 当图片小于当前纹理时线性缩放
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[i]->width, image[i]->length,
0, GL_RGB, GL_UNSIGNED_BYTE, image[i]->data);
}
}
void drawcube()//画立方体
{
//左面
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-LENGTH,-LENGTH,LENGTH);
glTexCoord2f(1.0f, 0.0f);glVertex3f(-LENGTH,LENGTH,LENGTH);
glTexCoord2f(1.0f, 1.0f);glVertex3f(-LENGTH,LENGTH,-LENGTH);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-LENGTH,-LENGTH,-LENGTH);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]); //右面
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(LENGTH,-LENGTH,-LENGTH);
glTexCoord2f(1.0f, 0.0f);glVertex3f(LENGTH,LENGTH,-LENGTH);
glTexCoord2f(1.0f, 1.0f);glVertex3f(LENGTH,LENGTH,LENGTH);
glTexCoord2f(0.0f, 1.0f);glVertex3f(LENGTH,-LENGTH,LENGTH);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[2]); //前面
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);glVertex3f(LENGTH,-LENGTH,LENGTH);
glTexCoord2f(0.0f, 1.0f);glVertex3f(LENGTH,LENGTH,LENGTH);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-LENGTH,LENGTH,LENGTH);
glTexCoord2f(1.0f, 0.0f);glVertex3f(-LENGTH,-LENGTH,LENGTH);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[3]); //后面
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-LENGTH,-LENGTH,-LENGTH);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-LENGTH,LENGTH,-LENGTH);
glTexCoord2f(1.0f, 0.0f);glVertex3f(LENGTH,LENGTH,-LENGTH);
glTexCoord2f(1.0f, 1.0f);glVertex3f(LENGTH,-LENGTH,-LENGTH);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[4]); //上面
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);glVertex3f(LENGTH,LENGTH,LENGTH);
glTexCoord2f(0.0f, 1.0f);glVertex3f(LENGTH,LENGTH,-LENGTH);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-LENGTH,LENGTH,-LENGTH);
glTexCoord2f(1.0f, 0.0f);glVertex3f(-LENGTH,LENGTH,LENGTH);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[5]); //下面
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f);glVertex3f(LENGTH,-LENGTH,-LENGTH);
glTexCoord2f(1.0f, 1.0f);glVertex3f(LENGTH,-LENGTH,LENGTH);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-LENGTH,-LENGTH,LENGTH);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-LENGTH,-LENGTH,-LENGTH);
glEnd();
glFlush();
}
void initPoint() //初始化点数组
{
//先读取前面的四个点,按逆时针方向,正对着这个面
pointCurrentState[0].x = LENGTH; pointCurrentState[0].y = -LENGTH; pointCurrentState[0].z = LENGTH;
pointCurrentState[1].x = LENGTH; pointCurrentState[1].y = LENGTH; pointCurrentState[1].z = LENGTH;
pointCurrentState[2].x = -LENGTH; pointCurrentState[2].y = LENGTH; pointCurrentState[2].z = LENGTH;
pointCurrentState[3].x = -LENGTH; pointCurrentState[3].y = -LENGTH; pointCurrentState[3].z = LENGTH;
//再读取后面的四个点,正对着这个面,按逆时针
pointCurrentState[4].x = -LENGTH; pointCurrentState[4].y = -LENGTH; pointCurrentState[4].z = -LENGTH;
pointCurrentState[5].x = -LENGTH; pointCurrentState[5].y = LENGTH; pointCurrentState[5].z = -LENGTH;
pointCurrentState[6].x = LENGTH; pointCurrentState[6].y = LENGTH; pointCurrentState[6].z = -LENGTH;
pointCurrentState[7].x = LENGTH; pointCurrentState[7].y = -LENGTH; pointCurrentState[7].z = -LENGTH;
}
void initPlane() //初始化每个面
{
//初始化顺序为左、右、前、后、上、下
planeAll[0].mask = 0; //左边的那个面初始化
planeAll[0].p[0] = 3;
planeAll[0].p[1] = 2;
planeAll[0].p[2] = 5;
planeAll[0].p[3] = 4;
planeAll[1].mask = 1; //右边的那个面初始化
planeAll[1].p[0] = 7;
planeAll[1].p[1] = 6;
planeAll[1].p[2] = 1;
planeAll[1].p[3] = 0;
planeAll[2].mask = 2; //前边的那个面初始化
planeAll[2].p[0] = 0;
planeAll[2].p[1] = 1;
planeAll[2].p[2] = 2;
planeAll[2].p[3] = 3;
planeAll[3].mask = 3; //后边的那个面初始化
planeAll[3].p[0] = 4;
planeAll[3].p[1] = 5;
planeAll[3].p[2] = 6;
planeAll[3].p[3] = 7;
planeAll[4].mask = 4;//上边的那个面初始化
planeAll[4].p[0] = 1;
planeAll[4].p[1] = 6;
planeAll[4].p[2] = 5;
planeAll[4].p[3] = 2;
planeAll[5].mask = 5;//下边的那个面初始化
planeAll[5].p[0] = 7;
planeAll[5].p[1] = 0;
planeAll[5].p[2] = 3;
planeAll[5].p[3] = 4;
}
void clearCubev() //清空cubev中的内容
{
cubev.x = 0.0f; //初始化立方体中心点位置
cubev.y = 0.0f;
cubev.z = 0.0f;
cubev.xrot = 0.0f; //初始化立方体旋转角度
cubev.yrot = 0.0f;
cubev.zrot = 0.0f;
}
void init(void) //初始化
- 1
- 2
前往页