2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Alexandre Deckner <alex@zappotek.com>
9 #include "StaticMesh.h"
11 #include <Application.h>
12 #include <Resources.h>
20 StaticMesh::StaticMesh(const char* name
)
25 // TODO : move that in a utility class
27 //fileName << "data/" << name << ".hk3d";
28 //_LoadText(fileName);
31 //_WriteBinary(fileName);
37 StaticMesh::~StaticMesh()
44 StaticMesh::_ReadText(const char* fileName
)
46 FILE* f
= fopen(fileName
, "r");
48 printf("Mesh::_ReadText, error accessing %s\n", fileName
);
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
++) {
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
;
71 printf("Mesh::_ReadText, loaded %s (%" B_PRIu32
" faces)\n",
72 fileName
, fFaceCount
);
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
);
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
);
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
);
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
);
122 StaticMesh::_ReadResource(const char* resourceName
)
124 // TODO: factorize with _ReadBinary
126 be_app
->GetAppInfo(&info
);
127 BFile
file(&info
.ref
, B_READ_ONLY
);
130 if (res
.SetTo(&file
) != B_OK
) {
131 printf("Mesh::_ReadResource, error accessing resources data\n");
136 const void* data
= res
.LoadResource(B_RAW_TYPE
, resourceName
, &size
);
138 printf("Mesh::_ReadResource, can't access resource %s\n", resourceName
);
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
);
158 StaticMesh::GetFace(uint32 index
) const
160 return fFaces
[index
];
165 StaticMesh::FaceCount() const