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");
|
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);
|
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");
|
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");
|
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);
|
xml_vertice->QueryIntAttribute("count", &vertice_count);
|
||||||
|
|
||||||
const char* xml2_vertice = xml_vertice->GetText();
|
const char* xml2_vertice = xml_vertice->GetText();
|
||||||
@@ -110,10 +110,18 @@ ColladaModel::ColladaModel(const char* filename) : positions_(), triangles_() ,n
|
|||||||
ColladaModel::~ColladaModel() {
|
ColladaModel::~ColladaModel() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> ColladaModel::triangle(int idx) {
|
std::vector<Vec3i> ColladaModel::triangle(int idx) {
|
||||||
return triangles_[idx];
|
return triangles_[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3f ColladaModel::position(int i) {
|
Vec3f ColladaModel::position(int i) {
|
||||||
return positions_[i];
|
return positions_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ColladaModel::nposition() {
|
||||||
|
return vertice_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ColladaModel::ntriangle() {
|
||||||
|
return triangle_count;
|
||||||
|
}
|
||||||
@@ -21,13 +21,19 @@ class ColladaModel {
|
|||||||
private:
|
private:
|
||||||
std::vector<Vec3f> positions_;
|
std::vector<Vec3f> positions_;
|
||||||
std::vector<Vec3f> normals_;
|
std::vector<Vec3f> normals_;
|
||||||
std::vector<std::vector<int> > triangles_;
|
std::vector<std::vector<Vec3i> > triangles_;
|
||||||
|
|
||||||
|
int vertice_count;
|
||||||
|
int triangle_count;
|
||||||
public:
|
public:
|
||||||
ColladaModel(const char* filename);
|
ColladaModel(const char* filename);
|
||||||
~ColladaModel();
|
~ColladaModel();
|
||||||
|
|
||||||
Vec3f position(int i);
|
Vec3f position(int i);
|
||||||
std::vector<int> triangle(int idx);
|
std::vector<Vec3i> triangle(int idx);
|
||||||
|
|
||||||
|
int nposition();
|
||||||
|
int ntriangle();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -22,32 +22,32 @@
|
|||||||
<VCProjectVersion>15.0</VCProjectVersion>
|
<VCProjectVersion>15.0</VCProjectVersion>
|
||||||
<ProjectGuid>{42971E68-F861-4D45-9DA6-F5E163705584}</ProjectGuid>
|
<ProjectGuid>{42971E68-F861-4D45-9DA6-F5E163705584}</ProjectGuid>
|
||||||
<RootNamespace>OpenWindow</RootNamespace>
|
<RootNamespace>OpenWindow</RootNamespace>
|
||||||
<WindowsTargetPlatformVersion>10.0.18362.0</WindowsTargetPlatformVersion>
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
<PlatformToolset>v141</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
<PlatformToolset>v141</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
<PlatformToolset>v141</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
<PlatformToolset>v141</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</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 "renderer.h"
|
||||||
#include "util_window.h"
|
#include "util_window.h"
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include "ColladaModel.h"
|
||||||
|
|
||||||
const TGAColor white = TGAColor(255, 255, 255, 255);
|
const TGAColor white = TGAColor(255, 255, 255, 255);
|
||||||
const TGAColor red = TGAColor(255, 0, 0, 255);
|
const TGAColor red = TGAColor(255, 0, 0, 255);
|
||||||
@@ -185,42 +186,79 @@ Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
|
|||||||
return Minv * Tr;
|
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()
|
void render()
|
||||||
{
|
{
|
||||||
Model* model = new Model("african_head.obj");
|
ColladaModel* model = new ColladaModel("african_head.dae");
|
||||||
|
TGAImage image(800, 800, TGAImage::RGB);
|
||||||
Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4);
|
Vec3f light_dir(0, 0, -1);
|
||||||
Matrix Projection = Matrix::identity();
|
for (int i = 0; i < model->ntriangle(); i++) {
|
||||||
Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
|
std::vector<Vec3i> face = model->triangle(i);
|
||||||
|
Vec2i screen_coords[3];
|
||||||
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];
|
Vec3f world_coords[3];
|
||||||
Vec2f diffuse_coords[3];
|
for (int j = 0; j < 3; j++) {
|
||||||
float intensities[3];
|
Vec3f v = face[0];
|
||||||
|
screen_coords[j] = Vec2i((v.x + 1.) * 800 / 2., (v.y + 1.) * 800 / 2.);
|
||||||
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;
|
world_coords[j] = v;
|
||||||
diffuse_coords[j] = model->uv(i, j);
|
}
|
||||||
intensities[j] = model->normal(i, j) * light_dir;
|
my_triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(255,255,255,255));
|
||||||
}
|
|
||||||
|
|
||||||
triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
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