xref: /haiku/src/apps/haiku3d/mesh/StaticMesh.cpp (revision 1294543de9ac0eff000eaea1b18368c36435d08e)
1 /*
2  * Copyright 2008, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 		Alexandre Deckner <alex@zappotek.com>
7  */
8 
9 #include "StaticMesh.h"
10 
11 #include <Application.h>
12 #include <Resources.h>
13 #include <Roster.h>
14 #include <File.h>
15 #include <String.h>
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 
20 StaticMesh::StaticMesh(const char* name)
21 	:
22 	fFaces(NULL),
23 	fFaceCount(0)
24 {
25 	// TODO : move that in a utility class
26 	//BString fileName;
27 	//fileName << "data/" << name << ".hk3d";
28 	//_LoadText(fileName);
29 
30 	//fileName << ".bin";
31 	//_WriteBinary(fileName);
32 
33 	_ReadResource(name);
34 }
35 
36 
37 StaticMesh::~StaticMesh()
38 {
39 	delete [] fFaces;
40 }
41 
42 
43 void
44 StaticMesh::_ReadText(const char* fileName)
45 {
46 	FILE* f = fopen(fileName, "r");
47 	if (f == NULL) {
48 		printf("Mesh::_ReadText, error accessing %s\n", fileName);
49 		return;
50 	}
51 
52 	fscanf(f, "%lu", &fFaceCount);
53 	fFaces = new Face[fFaceCount];
54 
55 	for (uint32 i = 0; i < fFaceCount; i++) {
56 
57 		uint32 vertexCount = 0;
58 		fscanf(f, "%lu", &vertexCount);
59 		fFaces[i].vertexCount = vertexCount;
60 
61 		for (uint32 vi = 0; vi < vertexCount; vi++) {
62 			float x, y, z, u, v;
63 			fscanf(f, "%f %f %f %f %f",	&x,	&y,	&z,	&v,	&u);
64 			fFaces[i].v[vi].p.setValue(x, y, z);
65 			fFaces[i].v[vi].u = v;
66 			fFaces[i].v[vi].v = 1.0 - u;
67 		}
68 	}
69 
70 	fclose(f);
71 	printf("Mesh::_ReadText, loaded %s (%lu faces)\n", fileName, fFaceCount);
72 }
73 
74 
75 void
76 StaticMesh::_WriteBinary(const char* fileName)
77 {
78 	BFile file(fileName, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
79 
80 	if (file.InitCheck() != B_OK) {
81 		printf("Mesh::_WriteBinary, error accessing %s\n", fileName);
82 		return;
83 	}
84 
85 	file.Write(&fFaceCount, sizeof(uint32));
86 	for (uint32 i = 0; i < fFaceCount; i++) {
87 		file.Write(&fFaces[i].vertexCount, sizeof(uint16));
88 		for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
89 			file.Write(&fFaces[i].v[vi], sizeof(Vertex));
90 		}
91 	}
92 	printf("Mesh::_WriteBinary, wrote %s (%lu faces)\n", fileName, fFaceCount);
93 }
94 
95 
96 void
97 StaticMesh::_ReadBinary(const char* fileName)
98 {
99 	BFile file(fileName, B_READ_ONLY);
100 
101 	if (file.InitCheck() != B_OK) {
102 		printf("Mesh::_ReadBinary, error accessing %s\n", fileName);
103 		return;
104 	}
105 
106 	file.Read(&fFaceCount, sizeof(uint32));
107 	fFaces = new Face[fFaceCount];
108 	for (uint32 i = 0; i < fFaceCount; i++) {
109 		file.Read(&fFaces[i].vertexCount, sizeof(uint16));
110 		for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
111 			file.Read(&fFaces[i].v[vi], sizeof(Vertex));
112 		}
113 	}
114 	printf("Mesh::_ReadBinary, loaded %s (%lu faces)\n", fileName, fFaceCount);
115 }
116 
117 
118 void
119 StaticMesh::_ReadResource(const char* resourceName)
120 {
121 	// TODO: factorize with _ReadBinary
122 	app_info info;
123 	be_app->GetAppInfo(&info);
124 	BFile file(&info.ref, B_READ_ONLY);
125 
126 	BResources res;
127 	if (res.SetTo(&file) != B_OK) {
128 		printf("Mesh::_ReadResource, error accessing resources data\n");
129 		return;
130 	}
131 
132 	size_t size;
133 	const void* data = res.LoadResource(B_RAW_TYPE, resourceName, &size);
134 	if (data == NULL) {
135 		printf("Mesh::_ReadResource, can't access resource %s\n", resourceName);
136 		return;
137 	}
138 
139 	BMemoryIO memoryIO(data, size);
140 
141 	memoryIO.Read(&fFaceCount, sizeof(uint32));
142 	fFaces = new Face[fFaceCount];
143 	for (uint32 i = 0; i < fFaceCount; i++) {
144 		memoryIO.Read(&fFaces[i].vertexCount, sizeof(uint16));
145 		for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
146 			memoryIO.Read(&fFaces[i].v[vi], sizeof(Vertex));
147 		}
148 	}
149 	printf("Mesh::_ReadResource, loaded %s (%lu faces)\n", resourceName,
150 		fFaceCount);
151 }
152 
153 
154 Face&
155 StaticMesh::GetFace(uint32 index) const
156 {
157 	return fFaces[index];
158 }
159 
160 
161 uint32
162 StaticMesh::FaceCount() const
163 {
164 	return fFaceCount;
165 }
166