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