#include #include #include "Models.h" #include "misc.h" void Model::CalculateNormals() { for (int i = 0; i < TriangleNum; ++i) normals[i] = normalize(crossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]])); for (int i = 0; i < TriangleNum; ++i) { vArray[i*27+0]=vertex[Triangles[i].vertex[0]].x; vArray[i*27+1]=vertex[Triangles[i].vertex[0]].y; vArray[i*27+2]=vertex[Triangles[i].vertex[0]].z; vArray[i*27+3]=normals[i].x; vArray[i*27+4]=normals[i].y; vArray[i*27+5]=normals[i].z; vArray[i*27+6]=Triangles[i].r; vArray[i*27+7]=Triangles[i].g; vArray[i*27+8]=Triangles[i].b; vArray[i*27+9]=vertex[Triangles[i].vertex[1]].x; vArray[i*27+10]=vertex[Triangles[i].vertex[1]].y; vArray[i*27+11]=vertex[Triangles[i].vertex[1]].z; vArray[i*27+12]=normals[i].x; vArray[i*27+13]=normals[i].y; vArray[i*27+14]=normals[i].z; vArray[i*27+15]=Triangles[i].r; vArray[i*27+16]=Triangles[i].g; vArray[i*27+17]=Triangles[i].b; vArray[i*27+18]=vertex[Triangles[i].vertex[2]].x; vArray[i*27+19]=vertex[Triangles[i].vertex[2]].y; vArray[i*27+20]=vertex[Triangles[i].vertex[2]].z; vArray[i*27+21]=normals[i].x; vArray[i*27+22]=normals[i].y; vArray[i*27+23]=normals[i].z; vArray[i*27+24]=Triangles[i].r; vArray[i*27+25]=Triangles[i].g; vArray[i*27+26]=Triangles[i].b; } boundingspherecenter = {}; for (int i = 0; i < vertexNum; ++i) boundingspherecenter += vertex[i]; boundingspherecenter /= vertexNum; boundingsphereradius = 0; for (int i = 0; i < vertexNum; ++i) boundingsphereradius = std::max(boundingsphereradius, sqrlen(boundingspherecenter - vertex[i])); boundingsphereradius = sqrt(boundingsphereradius); } void Model::load(const char* path) { auto model = loadModel(path); vertexNum = model.vertices.len; for (short i = 0; i < vertexNum; ++i) { vertex[i].x = model.vertices.ptr[i].x; vertex[i].y = model.vertices.ptr[i].y; vertex[i].z = model.vertices.ptr[i].z; } free(model.vertices.ptr); TriangleNum = model.faces.len; for (short i = 0; i < TriangleNum; ++i) { Triangles[i].vertex[0] = model.faces.ptr[i].v[0]; Triangles[i].vertex[1] = model.faces.ptr[i].v[1]; Triangles[i].vertex[2] = model.faces.ptr[i].v[2]; Triangles[i].r = model.faces.ptr[i].r; Triangles[i].g = model.faces.ptr[i].g; Triangles[i].b = model.faces.ptr[i].b; } free(model.faces.ptr); CalculateNormals(); } extern int nocolors; void Model::draw() { if(!nocolors){ glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]); glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]); glColorPointer(3,GL_FLOAT, 9*sizeof(GLfloat),&vArray[6]); glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3); } if(nocolors){ glColor4f(0,0,0,1); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]); glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]); glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3); } } void Model::draw(float r, float g, float b) { if(!nocolors)glColor4f(r,g,b,1); if(nocolors==1)glColor4f(0,0,0,1); if(nocolors==2)glColor4f(1,0,0,1); if(nocolors==3)glColor4f(0,0,1,1); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]); glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]); glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3); } int Model::LineCheck(XYZ p1, XYZ p2, XYZ *p) { int result = -1; if (segCrossSphere(p1, p2, boundingspherecenter, boundingsphereradius)) { float olddistance = 9999999.0; for (int j = 0; j < TriangleNum; ++j) { XYZ point; if (!segCrossTrigon(p1, p2, vertex + Triangles[j].vertex[0], vertex + Triangles[j].vertex[1], vertex + Triangles[j].vertex[2], normals + j, &point)) continue; float distance = sqrlen(point - p1); if (distance < olddistance || result == -1) { olddistance = distance; result = j; *p = point; } } } return result; } int Model::LineCheck2(XYZ p1, XYZ p2, XYZ *p, XYZ move, float deg_y) { int result = this->LineCheck(rotate(p1 - move, 0, -deg_y, 0), rotate(p2 - move, 0, -deg_y, 0), p); *p = rotate(*p, 0, deg_y, 0) + move; return result; }