#include<iostream>
#include<fstream>
#include "GL/glut.h"
#include <math.h>
#include<string>
#include<queue>
using namespace std;
int numOfVertex = 0;
int numOfFace = 0;
int numOfLine = 0;
int WIDTH = 600;
int HEIGHT = 600;
float maxx = -1;
float maxy = -1;
float maxz = -1;
float minx = 1;
float miny = 1;
float minz = 1;
GLfloat ShootPosition[] = { 0,0,0 };
GLfloat ShootDirect[] = { 0,0,0 };
float scale = 1.0;
float px;
float py;
float theta1 = 0;
float theta2 = 0;
float radius = 0;
bool light1 = false;
bool light2 = false;
bool light3 = false;
int displaystate = 0;
float PI = 3.1415926;
struct vertex;
struct face;
struct halfedge;
struct he_face;
struct normalVec;
struct nedge;
vertex* vertexs;
face* faces;
he_face** hefaces;
normalVec* normalvectors;
nedge** iedges;
struct halfedge {//半边结构
halfedge* next;
halfedge* opposite;
int end;
bool visit;
he_face* face;
halfedge() {
next = NULL;
opposite = NULL;
end = -1;
face = NULL;
visit = false;
}
};
struct vertex {//顶点
float x;
float y;
float z;
halfedge* edge;
bool visit;
vertex() {
visit = false;
}
vertex(float a, float b, float c) {
x = a;
y = b;
z = c;
edge = NULL;
visit = false;
}
};
struct normalVec {//法向量
float x;
float y;
float z;
normalVec() {
}
normalVec(float a, float b, float c) {
x = a;
y = b;
z = c;
}
};
struct he_face {//半边面
halfedge* edge;
bool visit;
he_face() {
edge = NULL;
visit = false;
}
};
struct face {//面
int numofv;
int* vertexs;
face() {
}
face(int nv) {
numofv = nv;
vertexs = new int[nv];
}
};
struct nedge {
int start;
int middle;
halfedge* he;
nedge* next;
nedge() {
start = -1;
he = NULL;
next = NULL;
middle = -1;
}
};
void readFile() {//读取文件
char data[100];
ifstream infile;
//cout << "输入要读取的文件名 :" << endl;
string filename="m0.off";
//cin >> filename;
infile.open("off/" + filename);
infile >> data;
infile >> numOfVertex;
infile >> numOfFace;
infile >> numOfLine;
vertexs = new vertex[numOfVertex];
faces = new face[numOfFace];
int vnum = 0;
int fnum = 0;
while (vnum < numOfVertex) {//读顶点信息
float x;
float y;
float z;
infile >> x;
infile >> y;
infile >> z;
vertexs[vnum] = vertex(x, y, z);
vnum++;
}
while (fnum<numOfFace)//读面信息
{
int numofv;
infile >> numofv;
face f = face(numofv);
for (int i = 0; i < numofv; i++)
{
int v;
infile >> v;
f.vertexs[i] = v;
}
faces[fnum] = f;
fnum++;
}
infile.close();
}
int getMiddle(int start, int end, nedge** iedges) {
nedge* temp = iedges[start];
while (temp != NULL) {
if (temp->he->end == end) {
break;
}
temp = temp->next;
}
if (temp == NULL) {
return -1;
}
else {
return temp->middle;
}
}
halfedge* getHalfEdge(int start, int end, nedge** iedges) {
nedge* temp = iedges[start];
while (temp != NULL) {
if (temp->he->end == end) {
break;
}
temp = temp->next;
}
if (temp == NULL) {
return NULL;
}
else {
return temp->he;
}
}
void initHalfedge() {//初始化半边结构
hefaces = new he_face*[numOfFace];
int numofhe = 0;
for (int i = 0; i < numOfVertex; i++)
{
iedges[i] = NULL;
}
for (int i = 0; i < numOfFace; i++)
{
int v1 = faces[i].vertexs[0];
int v2 = faces[i].vertexs[1];
int v3 = faces[i].vertexs[2];
he_face* hf = new he_face();
halfedge* he1 = getHalfEdge(v1, v2, iedges);
halfedge* he2 = getHalfEdge(v2, v3, iedges);
halfedge* he3 = getHalfEdge(v3, v1, iedges);
if (he1 == NULL) {
he1 = new halfedge();
he1->end = v2;
halfedge* opposite1 = new halfedge();
opposite1->end = v1;
opposite1->opposite = he1;
he1->opposite = opposite1;
nedge* temp = iedges[v1];
nedge* tempi = new nedge();
tempi->start = v1;
tempi->he = he1;
if (temp == NULL) {
iedges[v1] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
temp = iedges[v2];
tempi = new nedge();
tempi->start = v2;
tempi->he = opposite1;
if (temp == NULL) {
iedges[v2] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
}
if (he2 == NULL) {
he2 = new halfedge();
he2->end = v3;
halfedge* opposite2 = new halfedge();
opposite2->end = v2;
opposite2->opposite = he2;
he2->opposite = opposite2;
nedge* temp = iedges[v2];
nedge* tempi = new nedge();
tempi->start = v2;
tempi->he = he2;
if (temp == NULL) {
iedges[v2] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
temp = iedges[v3];
tempi = new nedge();
tempi->start = v3;
tempi->he = opposite2;
if (temp == NULL) {
iedges[v3] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
}
if (he3 == NULL) {
he3 = new halfedge();
he3->end = v1;
halfedge* opposite3 = new halfedge();
opposite3->end = v3;
opposite3->opposite = he3;
he3->opposite = opposite3;
nedge* temp = iedges[v3];
nedge* tempi = new nedge();
tempi->start = v3;
tempi->he = he3;
if (temp == NULL) {
iedges[v3] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
temp = iedges[v1];
tempi = new nedge();
tempi->start = v1;
tempi->he = opposite3;
if (temp == NULL) {
iedges[v1] = tempi;
}
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = tempi;
}
}
he1->next = he2;
he2->next = he3;
he3->next = he1;
hf->edge = he1;
he1->face = hf;
he2->face = hf;
he3->face = hf;
if (vertexs[v1].edge == NULL)
vertexs[v1].edge = he1;
if (vertexs[v2].edge == NULL)
vertexs[v2].edge = he2;
if (vertexs[v3].edge == NULL)
vertexs[v3].edge = he3;
hefaces[i] = hf;
}
}
void drawModel() {//画出模型
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < numOfFace; i++)
{
halfedge* e = hefaces[i]->edge;
vertex v1 = vertexs[e->end];
vertex v2 = vertexs[e->next->end];
vertex v3 = vertexs[e->next->next->end];
glBegin(GL_POLYGON);
glNormal3f(normalvectors[i].x, normalvectors[i].y, normalvectors[i].z);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glVertex3f(v3.x, v3.y, v3.z);
glEnd();
}
glutSwapBuffers();
glFlush();
}
void insertIedge(nedge** iedges, nedge* ie) {
nedge* it = iedges[ie->start];
if (it == NULL) {
iedges[ie->start] = ie;
}
else {
while (it->next != NULL)
{
it = it->next;
}
it->next = ie;
}
}
void computeNormalVec() {
normalvectors = new normalVec[numOfFace];
for (int i = 0; i < numOfFace; i++)
{
halfedge* hef = hefaces[i]->edge;
int v0 = hef->next->next->end;
int v1 = hef->end;
int v2 = hef->next->end;
float p1[] = { vertexs[v1].x - vertexs[v0].x, vertexs[v1].y - vertexs[v0].y,vertexs[v1].z - vertexs[v0].z };
float p2[] = { vertexs[v2].x - vertexs[v1].x,vertexs[v2].y - vertexs[v1].y,vertexs[v2].z - vertexs[v1].z };
float nx = p1[1] * p2[2] - p1[2] * p2[1];
float ny = p1[2] * p2[0] - p1[0] * p2[2];
float nz = p1[0] * p2[1] - p1[1] * p2[0];
float len = sqrt(pow(nx, 2) + pow(ny, 2) + pow(nz, 2));
nx = nx / len;
ny = ny / len;
nz = nz / len;
normalvectors[i] = normalVec(nx, ny, nz);
}
}
void loopDivision() {//loop细分算法
vertex* newvertexs = new vertex[numOfVertex + 1.5*numOfFace]; //新添加顶点数组
he_face** nfaces = new he_face*[4 * numOfFace]; //新添加面数组
nedge** niedges = new nedge*[num