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