fixes
This commit is contained in:
+51
-100
@@ -1,127 +1,78 @@
|
|||||||
#include "ColladaModel.h"
|
#include "ColladaModel.h"
|
||||||
|
|
||||||
ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,normals_()
|
ColladaModel::ColladaModel(const char* filename) : faces_(), vertices_(), normals_()
|
||||||
{
|
{
|
||||||
tinyxml2::XMLDocument doc;
|
tinyxml2::XMLDocument doc;
|
||||||
doc.LoadFile(filename);
|
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
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
for (int i = 0; i < face_count; i++)
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
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<std::vector<Vec3i>> triangle(triangle_count, std::vector<Vec3i>(3, Vec3i(0, 0, 0)));
|
|
||||||
for (int i = 0; i < triangle_count; i++)
|
|
||||||
{
|
{
|
||||||
int x = 0;
|
faces_.push_back(std::vector<Vec3i>());
|
||||||
|
for (int j = 0; j < 3; j++)
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
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<Vec3f> vertice(vertice_count / 3);
|
|
||||||
for (int i = 0; i < vertice_count / 3; i++)
|
|
||||||
{
|
{
|
||||||
float x = 0;
|
Vec3i temp;
|
||||||
|
str_triangle >> temp.x;
|
||||||
str_vertice >> x;
|
str_triangle >> temp.y;
|
||||||
vertice[i].x = x;
|
str_triangle >> temp.z;
|
||||||
|
faces_[i].push_back(temp);
|
||||||
str_vertice >> x;
|
|
||||||
vertice[i].y = x;
|
|
||||||
|
|
||||||
str_vertice >> x;
|
|
||||||
vertice[i].z = x;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
////////////////////////////////////////////////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());
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
for (int i = 0; i < vertex_count / 3; i++)
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
{
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
Vec3f temp;
|
||||||
tinyxml2::XMLElement* xml_normal_count = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->NextSiblingElement()->FirstChildElement("float_array");
|
str_vertex >> temp.x;
|
||||||
int normal_count = 0;
|
str_vertex >> temp.y;
|
||||||
xml_normal_count->QueryIntAttribute("count", &normal_count);
|
str_vertex >> temp.z;
|
||||||
|
vertices_.push_back(temp);
|
||||||
|
}
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/////////////////////////////////////////////normal////////////////////////////////////////////////////
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
normal_count = 0;
|
||||||
tinyxml2::XMLElement* xml_normal = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->NextSiblingElement()->FirstChildElement("float_array");
|
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();
|
xml_normal->QueryIntAttribute("count", &normal_count);
|
||||||
std::stringstream str_normal(xml2_normal);
|
std::stringstream str_normal(xml_normal->GetText());
|
||||||
|
|
||||||
std::vector<Vec3f> normal(normal_count / 3);
|
|
||||||
for (int i = 0; i < normal_count / 3; i++)
|
for (int i = 0; i < normal_count / 3; i++)
|
||||||
{
|
{
|
||||||
float x = 0;
|
Vec3f temp;
|
||||||
|
str_normal >> temp.x;
|
||||||
str_normal >> x;
|
str_normal >> temp.y;
|
||||||
normal[i].x = x;
|
str_normal >> temp.z;
|
||||||
|
normals_.push_back(temp);
|
||||||
str_normal >> x;
|
|
||||||
normal[i].y = x;
|
|
||||||
|
|
||||||
str_normal >> x;
|
|
||||||
normal[i].z = x;
|
|
||||||
}
|
}
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ColladaModel::~ColladaModel() {
|
ColladaModel::~ColladaModel() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Vec3i> ColladaModel::triangle(int idx) {
|
std::vector<Vec3i> ColladaModel::face(int idx) {
|
||||||
return triangles_[idx];
|
return faces_[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3f ColladaModel::position(int i) {
|
Vec3f ColladaModel::vertex(int i) {
|
||||||
return positions_[i];
|
return vertices_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColladaModel::nposition() {
|
int ColladaModel::nvertices() {
|
||||||
return vertice_count;
|
return vertex_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColladaModel::ntriangle() {
|
int ColladaModel::nfaces() {
|
||||||
return triangle_count;
|
return face_count;
|
||||||
}
|
}
|
||||||
@@ -19,21 +19,28 @@
|
|||||||
|
|
||||||
class ColladaModel {
|
class ColladaModel {
|
||||||
private:
|
private:
|
||||||
std::vector<Vec3f> positions_;
|
int face_count;
|
||||||
std::vector<Vec3f> normals_;
|
int vertex_count;
|
||||||
std::vector<std::vector<Vec3i> > triangles_;
|
int normal_count;
|
||||||
|
int textureco_count;
|
||||||
|
|
||||||
int vertice_count;
|
std::vector<std::vector<Vec3i> > faces_;
|
||||||
int triangle_count;
|
std::vector<Vec3f> vertices_;
|
||||||
|
std::vector<Vec3f> normals_;
|
||||||
|
std::vector<Vec3f> textureco_;
|
||||||
public:
|
public:
|
||||||
ColladaModel(const char* filename);
|
ColladaModel(const char* filename);
|
||||||
~ColladaModel();
|
~ColladaModel();
|
||||||
|
|
||||||
Vec3f position(int i);
|
std::vector<Vec3i> face(int idx);
|
||||||
std::vector<Vec3i> triangle(int idx);
|
Vec3f vertex(int i);
|
||||||
|
Vec3f normal(int i);
|
||||||
|
Vec3f textureco(int i);
|
||||||
|
|
||||||
int nposition();
|
int nfaces();
|
||||||
int ntriangle();
|
int nvertices();
|
||||||
|
int nnormals();
|
||||||
|
int ntextureco();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Binary file not shown.
+19
-21
@@ -208,25 +208,7 @@ void my_triangle(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage& image, TGAColor color)
|
|||||||
|
|
||||||
void render()
|
void render()
|
||||||
{
|
{
|
||||||
ColladaModel* model = new ColladaModel("african_head.dae");
|
//Model* model = new Model("african_head.obj");
|
||||||
TGAImage image(800, 800, TGAImage::RGB);
|
|
||||||
Vec3f light_dir(0, 0, -1);
|
|
||||||
for (int i = 0; i < model->ntriangle(); i++) {
|
|
||||||
std::vector<Vec3i> 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;
|
|
||||||
|
|
||||||
//Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4);
|
//Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4);
|
||||||
//Matrix Projection = Matrix::identity();
|
//Matrix Projection = Matrix::identity();
|
||||||
//Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
|
//Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
|
||||||
@@ -249,7 +231,7 @@ void render()
|
|||||||
// Vec3f v = model->vert(face[j]);
|
// Vec3f v = model->vert(face[j]);
|
||||||
// Vec4f v4(v);
|
// Vec4f v4(v);
|
||||||
// Vec3f coord(z * v4);
|
// Vec3f coord(z * v4);
|
||||||
//
|
|
||||||
// screen_coords[j] = coord;
|
// screen_coords[j] = coord;
|
||||||
// world_coords[j] = v;
|
// world_coords[j] = v;
|
||||||
// diffuse_coords[j] = model->uv(i, j);
|
// diffuse_coords[j] = model->uv(i, j);
|
||||||
@@ -259,6 +241,22 @@ void render()
|
|||||||
// triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5));
|
// 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<Vec3i> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user