repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haiku3d / mesh / StaticMesh.cpp
blob45e5c29937860c038ff87316d17c438127984459
1 /*
2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Alexandre Deckner <alex@zappotek.com>
7 */
9 #include "StaticMesh.h"
11 #include <Application.h>
12 #include <Resources.h>
13 #include <Roster.h>
14 #include <File.h>
15 #include <String.h>
17 #include <stdlib.h>
18 #include <stdio.h>
20 StaticMesh::StaticMesh(const char* name)
22 fFaces(NULL),
23 fFaceCount(0)
25 // TODO : move that in a utility class
26 //BString fileName;
27 //fileName << "data/" << name << ".hk3d";
28 //_LoadText(fileName);
30 //fileName << ".bin";
31 //_WriteBinary(fileName);
33 _ReadResource(name);
37 StaticMesh::~StaticMesh()
39 delete [] fFaces;
43 void
44 StaticMesh::_ReadText(const char* fileName)
46 FILE* f = fopen(fileName, "r");
47 if (f == NULL) {
48 printf("Mesh::_ReadText, error accessing %s\n", fileName);
49 return;
52 fscanf(f, "%" B_PRIu32, &fFaceCount);
53 fFaces = new Face[fFaceCount];
55 for (uint32 i = 0; i < fFaceCount; i++) {
57 uint32 vertexCount = 0;
58 fscanf(f, "%" B_PRIu32, &vertexCount);
59 fFaces[i].vertexCount = vertexCount;
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;
70 fclose(f);
71 printf("Mesh::_ReadText, loaded %s (%" B_PRIu32 " faces)\n",
72 fileName, fFaceCount);
76 void
77 StaticMesh::_WriteBinary(const char* fileName)
79 BFile file(fileName, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
81 if (file.InitCheck() != B_OK) {
82 printf("Mesh::_WriteBinary, error accessing %s\n", fileName);
83 return;
86 file.Write(&fFaceCount, sizeof(uint32));
87 for (uint32 i = 0; i < fFaceCount; i++) {
88 file.Write(&fFaces[i].vertexCount, sizeof(uint16));
89 for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
90 file.Write(&fFaces[i].v[vi], sizeof(Vertex));
93 printf("Mesh::_WriteBinary, wrote %s (%" B_PRIu32 " faces)\n",
94 fileName, fFaceCount);
98 void
99 StaticMesh::_ReadBinary(const char* fileName)
101 BFile file(fileName, B_READ_ONLY);
103 if (file.InitCheck() != B_OK) {
104 printf("Mesh::_ReadBinary, error accessing %s\n", fileName);
105 return;
108 file.Read(&fFaceCount, sizeof(uint32));
109 fFaces = new Face[fFaceCount];
110 for (uint32 i = 0; i < fFaceCount; i++) {
111 file.Read(&fFaces[i].vertexCount, sizeof(uint16));
112 for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
113 file.Read(&fFaces[i].v[vi], sizeof(Vertex));
116 printf("Mesh::_ReadBinary, loaded %s (%" B_PRIu32 " faces)\n",
117 fileName, fFaceCount);
121 void
122 StaticMesh::_ReadResource(const char* resourceName)
124 // TODO: factorize with _ReadBinary
125 app_info info;
126 be_app->GetAppInfo(&info);
127 BFile file(&info.ref, B_READ_ONLY);
129 BResources res;
130 if (res.SetTo(&file) != B_OK) {
131 printf("Mesh::_ReadResource, error accessing resources data\n");
132 return;
135 size_t size;
136 const void* data = res.LoadResource(B_RAW_TYPE, resourceName, &size);
137 if (data == NULL) {
138 printf("Mesh::_ReadResource, can't access resource %s\n", resourceName);
139 return;
142 BMemoryIO memoryIO(data, size);
144 memoryIO.Read(&fFaceCount, sizeof(uint32));
145 fFaces = new Face[fFaceCount];
146 for (uint32 i = 0; i < fFaceCount; i++) {
147 memoryIO.Read(&fFaces[i].vertexCount, sizeof(uint16));
148 for (uint32 vi = 0; vi < fFaces[i].vertexCount; vi++) {
149 memoryIO.Read(&fFaces[i].v[vi], sizeof(Vertex));
152 printf("Mesh::_ReadResource, loaded %s (%" B_PRIu32 " faces)\n",
153 resourceName, fFaceCount);
157 Face&
158 StaticMesh::GetFace(uint32 index) const
160 return fFaces[index];
164 uint32
165 StaticMesh::FaceCount() const
167 return fFaceCount;