File: integration.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (115 lines) | stat: -rw-r--r-- 3,166 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "integration.h"

#include <k3dsdk/algebra.h>
#include <k3dsdk/polyhedron.h>

#include <lib3ds/file.h>
#include <lib3ds/camera.h>
#include <lib3ds/mesh.h>
#include <lib3ds/node.h>
#include <lib3ds/material.h>
#include <lib3ds/matrix.h>
#include <lib3ds/vector.h>
#include <lib3ds/light.h>

#include <boost/scoped_ptr.hpp>

namespace module
{

namespace f3ds
{

namespace io
{

static void add_nodes(k3d::mesh& Mesh, Lib3dsFile* file, Lib3dsNode* node, k3d::uint_t &offset, k3d::imaterial* const Material, k3d::polyhedron::primitive& polyhedron)
{
	if(node->type == LIB3DS_OBJECT_NODE)
	{
		for(Lib3dsNode* p = node->childs; p != 0; p = p->next)
			add_nodes(Mesh, file, p, offset, Material, polyhedron);

		if(strcmp(node->name,"$$$DUMMY") == 0)
			return;
		
		Lib3dsMesh* mesh = lib3ds_file_mesh_by_name(file, node->data.object.morph);
		if(!mesh)
			mesh = lib3ds_file_mesh_by_name(file, node->name);

		if(!mesh)
			return;

		for(int p = 0; p < mesh->faces; ++p)
		{
			Lib3dsFace* const f = &mesh->faceL[p];

			polyhedron.face_shells.push_back(0);
			polyhedron.face_first_loops.push_back(polyhedron.loop_first_edges.size());
			polyhedron.face_loop_counts.push_back(1);
			polyhedron.face_selections.push_back(0);
			polyhedron.face_materials.push_back(Material);
			polyhedron.loop_first_edges.push_back(polyhedron.clockwise_edges.size());

			for(int j = 0; j != 3; ++j)
			{
				polyhedron.clockwise_edges.push_back(polyhedron.clockwise_edges.size() + 1);
				polyhedron.edge_selections.push_back(0);
				polyhedron.vertex_points.push_back(f->points[j] + offset);
				polyhedron.vertex_selections.push_back(0);
			}
			polyhedron.clockwise_edges.back() = polyhedron.loop_first_edges.back();
		}

		k3d::mesh::points_t& points = Mesh.points.writable();
		k3d::mesh::selection_t& point_selection = Mesh.point_selection.writable();
		for(int i = 0; i < mesh->points; ++i)
		{
			points.push_back(k3d::point3(mesh->pointL[i].pos[0], mesh->pointL[i].pos[1], mesh->pointL[i].pos[2]));
			point_selection.push_back(0);
		}

		offset += mesh->points;
	}
}

f3dsParser::f3dsParser(const char* filename, k3d::imaterial* const Material, k3d::mesh& Mesh)
{
	Lib3dsFile* const file = lib3ds_file_load(filename);
	if(!file) 
	{
		k3d::log() << error << "Not a 3DS file: " << filename << std::endl;
		return;
	}

	Mesh.points.create();
	Mesh.point_selection.create();

	boost::scoped_ptr<k3d::polyhedron::primitive> polyhedron(k3d::polyhedron::create(Mesh));
	polyhedron->shell_types.push_back(k3d::polyhedron::POLYGONS);

	/* No nodes?  Fabricate nodes to display all the meshes. */
	if(!file->nodes)
	{
		k3d::log() << warning << "3DS file doesn't contain any nodes, creating virtual nodes instead." << std::endl;
	
		for(Lib3dsMesh* mesh = file->meshes; mesh; mesh = mesh->next)
		{
			Lib3dsNode* const node = lib3ds_node_new_object();
			strcpy(node->name, mesh->name);
			node->parent_id = LIB3DS_NO_PARENT;
			lib3ds_file_insert_node(file, node);
		}
	}

	k3d::uint_t offset = 0;
	for(Lib3dsNode* p = file->nodes; p; p = p->next)
		add_nodes(Mesh, file, p, offset, Material, *polyhedron);
}

} // namespace io

} // namespace f3ds

} // namespace module