try fixing .dae
This commit is contained in:
@@ -9,7 +9,7 @@ ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,n
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
tinyxml2::XMLElement* xml_triangle_count = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("triangles");
|
||||
int triangle_count = 0;
|
||||
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");
|
||||
@@ -57,7 +57,7 @@ ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,n
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
tinyxml2::XMLElement* xml_vertice = doc.FirstChildElement("COLLADA")->FirstChildElement("library_geometries")->FirstChildElement("geometry")->FirstChildElement("mesh")->FirstChildElement("source")->FirstChildElement("float_array");
|
||||
int vertice_count = 0;
|
||||
vertice_count = 0;
|
||||
xml_vertice->QueryIntAttribute("count", &vertice_count);
|
||||
|
||||
const char* xml2_vertice = xml_vertice->GetText();
|
||||
@@ -110,10 +110,18 @@ ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,n
|
||||
ColladaModel::~ColladaModel() {
|
||||
}
|
||||
|
||||
std::vector<int> ColladaModel::triangle(int idx) {
|
||||
std::vector<Vec3i> ColladaModel::triangle(int idx) {
|
||||
return triangles_[idx];
|
||||
}
|
||||
|
||||
Vec3f ColladaModel::position(int i) {
|
||||
return positions_[i];
|
||||
}
|
||||
|
||||
int ColladaModel::nposition() {
|
||||
return vertice_count;
|
||||
}
|
||||
|
||||
int ColladaModel::ntriangle() {
|
||||
return triangle_count;
|
||||
}
|
||||
@@ -21,13 +21,19 @@ class ColladaModel {
|
||||
private:
|
||||
std::vector<Vec3f> positions_;
|
||||
std::vector<Vec3f> normals_;
|
||||
std::vector<std::vector<int> > triangles_;
|
||||
std::vector<std::vector<Vec3i> > triangles_;
|
||||
|
||||
int vertice_count;
|
||||
int triangle_count;
|
||||
public:
|
||||
ColladaModel(const char* filename);
|
||||
~ColladaModel();
|
||||
|
||||
Vec3f position(int i);
|
||||
std::vector<int> triangle(int idx);
|
||||
std::vector<Vec3i> triangle(int idx);
|
||||
|
||||
int nposition();
|
||||
int ntriangle();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -22,32 +22,32 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{42971E68-F861-4D45-9DA6-F5E163705584}</ProjectGuid>
|
||||
<RootNamespace>OpenWindow</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.18362.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
+68
-30
@@ -8,6 +8,7 @@
|
||||
#include "renderer.h"
|
||||
#include "util_window.h"
|
||||
#include <ctime>
|
||||
#include "ColladaModel.h"
|
||||
|
||||
const TGAColor white = TGAColor(255, 255, 255, 255);
|
||||
const TGAColor red = TGAColor(255, 0, 0, 255);
|
||||
@@ -185,42 +186,79 @@ Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
|
||||
return Minv * Tr;
|
||||
}
|
||||
|
||||
void my_triangle(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage& image, TGAColor color) {
|
||||
if (t0.y == t1.y && t0.y == t2.y) return; // i dont care about degenerate triangles
|
||||
if (t0.y > t1.y) std::swap(t0, t1);
|
||||
if (t0.y > t2.y) std::swap(t0, t2);
|
||||
if (t1.y > t2.y) std::swap(t1, t2);
|
||||
int total_height = t2.y - t0.y;
|
||||
for (int i = 0; i < total_height; i++) {
|
||||
bool second_half = i > t1.y - t0.y || t1.y == t0.y;
|
||||
int segment_height = second_half ? t2.y - t1.y : t1.y - t0.y;
|
||||
float alpha = (float)i / total_height;
|
||||
float beta = (float)(i - (second_half ? t1.y - t0.y : 0)) / segment_height; // be careful: with above conditions no division by zero here
|
||||
Vec2i A = t0 + (t2 - t0) * alpha;
|
||||
Vec2i B = second_half ? t1 + (t2 - t1) * beta : t0 + (t1 - t0) * beta;
|
||||
if (A.x > B.x) std::swap(A, B);
|
||||
for (int j = A.x; j <= B.x; j++) {
|
||||
image.set(j, t0.y + i, color); // attention, due to int casts t0.y+i != A.y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render()
|
||||
{
|
||||
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));
|
||||
|
||||
Projection[3][2] = -1.f / (eye - center).norm();
|
||||
|
||||
Matrix z = ViewPort * Projection * ModelView * model->Transform;
|
||||
|
||||
init_zbuffer();
|
||||
for (int i = 0; i < model->nfaces(); i++)
|
||||
{
|
||||
std::vector<int> face = model->face(i);
|
||||
Vec3f screen_coords[3];
|
||||
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<Vec3i> face = model->triangle(i);
|
||||
Vec2i screen_coords[3];
|
||||
Vec3f world_coords[3];
|
||||
Vec2f diffuse_coords[3];
|
||||
float intensities[3];
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
Vec3f v = model->vert(face[j]);
|
||||
Vec4f v4(v);
|
||||
Vec3f coord(z * v4);
|
||||
|
||||
screen_coords[j] = coord;
|
||||
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;
|
||||
diffuse_coords[j] = model->uv(i, j);
|
||||
intensities[j] = model->normal(i, j) * light_dir;
|
||||
}
|
||||
|
||||
triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5));
|
||||
}
|
||||
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 Projection = Matrix::identity();
|
||||
//Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
|
||||
|
||||
//Projection[3][2] = -1.f / (eye - center).norm();
|
||||
|
||||
//Matrix z = ViewPort * Projection * ModelView * model->Transform;
|
||||
|
||||
//init_zbuffer();
|
||||
//for (int i = 0; i < model->nfaces(); i++)
|
||||
//{
|
||||
// std::vector<int> face = model->face(i);
|
||||
// Vec3f screen_coords[3];
|
||||
// Vec3f world_coords[3];
|
||||
// Vec2f diffuse_coords[3];
|
||||
// float intensities[3];
|
||||
|
||||
// for (int j = 0; j < 3; j++)
|
||||
// {
|
||||
// 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);
|
||||
// intensities[j] = model->normal(i, j) * light_dir;
|
||||
// }
|
||||
|
||||
// triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5));
|
||||
//}
|
||||
|
||||
//delete model;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user