00001 #include "MeshObject.h"
00002
00003 MeshObject::MeshObject()
00004 {
00005 }
00006
00007 MeshObject MeshObject::operator=(MeshObject mesh_obj2)
00008 {
00009 }
00010
00011 void MeshObject::AddVertex(float x, float y, float z)
00012 {
00013 vertex_list.push_back(glVector(x, y, z));
00014 }
00015
00016 void MeshObject::AddTriangle(int &p1, int &p2, int &p3)
00017 {
00018 triangle_list.push_back(Triangle(p3, p2, p1));
00019
00020 Vertex pt1 = vertex_list[p1];
00021 Vertex pt2 = vertex_list[p2];
00022 Vertex pt3 = vertex_list[p3];
00023
00024 glVector v1 = glVector(pt2 - pt1);
00025 glVector v2 = glVector(pt1 - pt3);
00026
00027 glVector *norm = new glVector(!glVector(v2 % v1));
00028 triangle_list.back().Normal = norm;
00029 }
00030
00031 bool MeshObject::Load(string FileName)
00032 {
00033 ifstream v_in(string(FileName + ".vrt").c_str(),ios_base::in);
00034 if(!v_in.is_open())
00035 return false;
00036
00037 int v_count = 0;
00038 float x, y, z;
00039 v_in >> v_count;
00040 for(int i = 0; i < v_count; i++)
00041 {
00042 v_in >> x >> y >> z;
00043 AddVertex(x, y, z);
00044 }
00045
00046 ifstream t_in(string(FileName + ".tri").c_str(),ios_base::in);
00047 if(!t_in.is_open())
00048 return false;
00049
00050
00051 int p1, p2, p3;
00052
00053 t_in >> total_color_sections;
00054 color_table = new Color[total_color_sections];
00055 triangle_count = new int[total_color_sections];
00056
00057 for(int j = 0; j < total_color_sections; j++)
00058 {
00059 t_in >> color_table[j].color[0] >> color_table[j].color[1] >> color_table[j].color[2] >> color_table[j].color[3];
00060 t_in >> triangle_count[j];
00061 for(int i = 0; i < triangle_count[j]; i++)
00062 {
00063 t_in >> p1 >> p2 >> p3;
00064 AddTriangle(p1, p2, p3);
00065 }
00066 }
00067
00068 return true;
00069 }
00070
00071 void MeshObject::Draw_Triangle(Triangle &tri)
00072 {
00073 glNormal3fv(tri.Normal->data);
00074 for(int i = 0; i < 3; i++)
00075 glVertex3f(vertex_list[tri.points[i]].data[0],
00076 vertex_list[tri.points[i]].data[1],
00077 vertex_list[tri.points[i]].data[2]);
00078 }
00079
00080 void MeshObject::Draw()
00081 {
00082 int total_prev = 0;
00083 int current_count = 0;
00084 glBegin(GL_TRIANGLES);
00085 for(int j = 0; j < total_color_sections; j++)
00086 {
00087 glMaterialfv(GL_FRONT, GL_DIFFUSE, color_table[j].color);
00088 for(current_count = 0; current_count < triangle_count[j]; current_count++)
00089 Draw_Triangle(triangle_list[total_prev + current_count]);
00090
00091 total_prev += current_count;
00092 }
00093 glEnd();
00094 }