diff --git a/OpenWindow/ColladaModel.cpp b/OpenWindow/ColladaModel.cpp index ee45904..51db00e 100644 --- a/OpenWindow/ColladaModel.cpp +++ b/OpenWindow/ColladaModel.cpp @@ -1,127 +1,78 @@ #include "ColladaModel.h" -ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,normals_() +ColladaModel::ColladaModel(const char* filename) : faces_(), vertices_(), normals_() { tinyxml2::XMLDocument doc; doc.LoadFile(filename); + /////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////faces////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////////////// + face_count = 0; + tinyxml2::XMLElement* xml_face = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("triangles"); + xml_face->QueryIntAttribute("count", &face_count); //get count + std::stringstream str_triangle(xml_face->FirstChildElement("p")->GetText()); //get values as a string - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - tinyxml2::XMLElement* xml_triangle_count = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("triangles"); - triangle_count = 0; - xml_triangle_count->QueryIntAttribute("count", &triangle_count); - - tinyxml2::XMLElement* xml_triangle = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("triangles")->FirstChildElement("p"); - const char* xml2_triangle = xml_triangle->GetText(); - std::stringstream str_triangle(xml2_triangle); - - std::vector> triangle(triangle_count, std::vector(3, Vec3i(0, 0, 0))); - for (int i = 0; i < triangle_count; i++) + for (int i = 0; i < face_count; i++) { - int x = 0; - - str_triangle >> x; - triangle[i][0].x = x; - - str_triangle >> x; - triangle[i][1].x = x; - - str_triangle >> x; - triangle[i][2].x = x; - - - - str_triangle >> x; - triangle[i][0].y = x; - - str_triangle >> x; - triangle[i][1].y = x; - - str_triangle >> x; - triangle[i][2].y = x; - - - - str_triangle >> x; - triangle[i][0].z = x; - - str_triangle >> x; - triangle[i][1].z = x; - - str_triangle >> x; - triangle[i][2].z = x; + faces_.push_back(std::vector()); + for (int j = 0; j < 3; j++) + { + Vec3i temp; + str_triangle >> temp.x; + str_triangle >> temp.y; + str_triangle >> temp.z; + faces_[i].push_back(temp); + } } + /////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////vertex///////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////////////// + vertex_count = 0; + tinyxml2::XMLElement* xml_vertex = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->FirstChildElement("float_array"); + xml_vertex->QueryIntAttribute("count", &vertex_count); + std::stringstream str_vertex(xml_vertex->GetText()); - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - tinyxml2::XMLElement* xml_vertice = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->FirstChildElement("float_array"); - vertice_count = 0; - xml_vertice->QueryIntAttribute("count", &vertice_count); - - const char* xml2_vertice = xml_vertice->GetText(); - std::stringstream str_vertice(xml2_vertice); - - std::vector vertice(vertice_count / 3); - for (int i = 0; i < vertice_count / 3; i++) + for (int i = 0; i < vertex_count / 3; i++) { - float x = 0; - - str_vertice >> x; - vertice[i].x = x; - - str_vertice >> x; - vertice[i].y = x; - - str_vertice >> x; - vertice[i].z = x; + Vec3f temp; + str_vertex >> temp.x; + str_vertex >> temp.y; + str_vertex >> temp.z; + vertices_.push_back(temp); } - - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - tinyxml2::XMLElement* xml_normal_count = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->NextSiblingElement()->FirstChildElement("float_array"); - int normal_count = 0; - xml_normal_count->QueryIntAttribute("count", &normal_count); + /////////////////////////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////normal//////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////////////// + normal_count = 0; tinyxml2::XMLElement* xml_normal = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->NextSiblingElement()->FirstChildElement("float_array"); - const char* xml2_normal = xml_normal->GetText(); - std::stringstream str_normal(xml2_normal); + xml_normal->QueryIntAttribute("count", &normal_count); + std::stringstream str_normal(xml_normal->GetText()); - std::vector normal(normal_count / 3); for (int i = 0; i < normal_count / 3; i++) { - float x = 0; - - str_normal >> x; - normal[i].x = x; - - str_normal >> x; - normal[i].y = x; - - str_normal >> x; - normal[i].z = x; + Vec3f temp; + str_normal >> temp.x; + str_normal >> temp.y; + str_normal >> temp.z; + normals_.push_back(temp); } - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////// } ColladaModel::~ColladaModel() { } -std::vector ColladaModel::triangle(int idx) { - return triangles_[idx]; +std::vector ColladaModel::face(int idx) { + return faces_[idx]; } -Vec3f ColladaModel::position(int i) { - return positions_[i]; +Vec3f ColladaModel::vertex(int i) { + return vertices_[i]; } -int ColladaModel::nposition() { - return vertice_count; +int ColladaModel::nvertices() { + return vertex_count; } -int ColladaModel::ntriangle() { - return triangle_count; +int ColladaModel::nfaces() { + return face_count; } \ No newline at end of file diff --git a/OpenWindow/ColladaModel.h b/OpenWindow/ColladaModel.h index 0229568..622cfa7 100644 --- a/OpenWindow/ColladaModel.h +++ b/OpenWindow/ColladaModel.h @@ -19,21 +19,28 @@ class ColladaModel { private: - std::vector positions_; - std::vector normals_; - std::vector > triangles_; + int face_count; + int vertex_count; + int normal_count; + int textureco_count; - int vertice_count; - int triangle_count; + std::vector > faces_; + std::vector vertices_; + std::vector normals_; + std::vector textureco_; public: ColladaModel(const char* filename); ~ColladaModel(); - Vec3f position(int i); - std::vector triangle(int idx); + std::vector face(int idx); + Vec3f vertex(int i); + Vec3f normal(int i); + Vec3f textureco(int i); - int nposition(); - int ntriangle(); + int nfaces(); + int nvertices(); + int nnormals(); + int ntextureco(); }; #endif \ No newline at end of file diff --git a/OpenWindow/output.tga b/OpenWindow/output.tga index 2cf62b6..6d683ce 100644 Binary files a/OpenWindow/output.tga and b/OpenWindow/output.tga differ diff --git a/OpenWindow/renderer.cpp b/OpenWindow/renderer.cpp index 30c6b88..4a77694 100644 --- a/OpenWindow/renderer.cpp +++ b/OpenWindow/renderer.cpp @@ -208,25 +208,7 @@ void my_triangle(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage& image, TGAColor color) void render() { - ColladaModel* model = new ColladaModel("african_head.dae"); - TGAImage image(800, 800, TGAImage::RGB); - Vec3f light_dir(0, 0, -1); - for (int i = 0; i < model->ntriangle(); i++) { - std::vector face = model->triangle(i); - Vec2i screen_coords[3]; - Vec3f world_coords[3]; - for (int j = 0; j < 3; j++) { - Vec3f v = face[0]; - screen_coords[j] = Vec2i((v.x + 1.) * 800 / 2., (v.y + 1.) * 800 / 2.); - world_coords[j] = v; - } - my_triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(255,255,255,255)); - } - - image.flip_vertically(); // i want to have the origin at the left bottom corner of the image - image.write_tga_file("output.tga"); - delete model; - + //Model* model = new Model("african_head.obj"); //Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4); //Matrix Projection = Matrix::identity(); //Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0)); @@ -249,7 +231,7 @@ void render() // Vec3f v = model->vert(face[j]); // Vec4f v4(v); // Vec3f coord(z * v4); - // + // screen_coords[j] = coord; // world_coords[j] = v; // diffuse_coords[j] = model->uv(i, j); @@ -259,6 +241,22 @@ void render() // triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5)); //} - //delete model; -} + ColladaModel* model = new ColladaModel("african_head.dae"); + TGAImage image(800, 800, TGAImage::RGB); + Vec3f light_dir(0, 0, -1); + for (int i = 0; i < model->nfaces(); i++) { + std::vector face = model->face(i); + Vec2i screen_coords[3]; + Vec3f world_coords[3]; + for (int j = 0; j < 3; j++) { + Vec3f v = model->vertex(face[j][0]); + screen_coords[j] = Vec2i((v.x + 1.) * 800 / 2., (v.y + 1.) * 800 / 2.); + world_coords[j] = v; + } + my_triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, white); + } + image.flip_vertically(); // i want to have the origin at the left bottom corner of the image + image.write_tga_file("output.tga"); + delete model; +} \ No newline at end of file