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