int objLoad(char* filePath) { FILE* file = fopen(filePath, "r"); char* line = (char*)calloc(LINE_SIZE, sizeof(char)); char* token = NULL, * delim = " \n\t"; int vertexCount = 0, textureCount = 0, normalCount = 0, faceCount = 0; while(fgets(line, LINE_SIZE, file) != NULL) { if(prefix("vt", line)) ++textureCount; else if(prefix("vn", line)) ++normalCount; else if(prefix("v", line)) ++vertexCount; else if(prefix("f", line)) ++faceCount; } rewind(file); obj* object = (obj*)calloc(1, sizeof(obj)); object->faceCount = faceCount; object->vertexCount = vertexCount; object->vertices = (vec3*)calloc(vertexCount, sizeof(vec3)); object->shared = (int*)calloc(vertexCount, sizeof(int)); object->normals = (vec3*)calloc(normalCount, sizeof(vec3)); object->tangents = (vec3*)calloc(vertexCount, sizeof(vec3)); object->bitangents = (vec3*)calloc(vertexCount, sizeof(vec3)); object->textures = (vec2*)calloc(textureCount, sizeof(vec2)); object->faces = (face*)calloc(faceCount, sizeof(face)); int vertexTally = 0, normalTally = 0, textureTally = 0, faceTally = 0; while(fgets(line, LINE_SIZE, file) != NULL) { if(prefix("vt", line)) { parseVec2(line, &object->textures[textureTally]); object->textures[textureTally].y = 1.0 - object->textures[textureTally].y; ++textureTally; } else if(prefix("vn", line)) parseVec3(line, &object->normals[normalTally++]); else if(prefix("v", line)) parseVec3(line, &object->vertices[vertexTally++]); else if(prefix("f", line)) parseFace(line, object->faces[faceTally++]); } uniqueMap(object); free(line); fclose(file); int index = nextIndex(); objects[index] = object; return index; }
void TestGame2::initScene() { lua = std::make_unique<LuaInterface>(); scene = std::make_unique<Scene>(); // scene->backgroundColor = glm::vec3(0.5f, 0.15f, 0.25f); scene->backgroundColor = parseVec3(0x06070A); // scene->sceneAmbient = glm::vec3(0.05, 0.1, 0.2); scene->sceneAmbient = parseVec3(0x172B38); scene->sceneSpecular = glm::vec3(0.6, 0.5, 0.2f); // scene->sceneSpecular = parseVec3(0xFFFDEB)*0.5f; // scene->sceneDiffuse = glm::vec3(1.0f, 1.0f, 1.0f); scene->sceneDiffuse = parseVec3(0x9EC2FF); // scene->backgroundColor = glm::vec3(0.0f, 0.1f, 0.25f); const auto gridSize = 10; const auto gridSpacing = 2.f; for (int i = 0; i < gridSize; i++) { for (int j = 0; j < gridSize; j++) { float x = (i - gridSize/2 + 0.5f) * gridSpacing; float z = (j + 1 + 0.5f) * gridSpacing; auto obj = std::make_shared<Object>(glm::translate(glm::vec3(x, -10, z-10)), brickGeo, brickMat); scene->objects.push_back(std::move(obj)); }} ship = std::make_unique<Object>(glm::mat4(), shipGeo, shipMat); scene->objects.push_back(ship); sunRotation = 0.0f; player = std::make_unique<ShipController>(); player->objWk = ship; auto img = AssetManager::get().getImage("bullet2.png"); auto bulletSystem = std::make_unique<BulletSystem>( std::move(img), glm::vec2{0.25f, 4.0f} ); scene->bulletSystems.emplace_back(std::move(bulletSystem)); auto img2 = AssetManager::get().getImage("bullet1.png"); auto bulletSystem2 = std::make_unique<BulletSystem>( std::move(img2), glm::vec2{1.0f, 1.0f} ); scene->bulletSystems.emplace_back(std::move(bulletSystem2)); enemies = std::make_unique<EnemyList>(); enemies->game = this; }
bool Properties::getVec3(const char* name, Vec3* out) const { return parseVec3(getString(name), out); }
void OBJFileReader::parseLine(const char*& current, const char* endOfLine) { if(matchCommand(current, "v")) positions.push_back(parseVec3(current)); else if(matchCommand(current, "vn")) normals.push_back(parseVec3(current)); else if(matchCommand(current, "vt")) texcoords.push_back(parseVec2(current)); else if(matchCommand(current, "f")) // face { int count = 0; while(!errThisLine && *current) { VertInfo nfo; nfo.pos = parseIndex(current, positions.size()); if(*current == '/') nfo.tex = parseIndex(++current, texcoords.size()); else nfo.tex = -1; if(*current == '/') nfo.norm = parseIndex(++current, normals.size()); else nfo.norm = -1; if(*current && !isspace(*current)) diagnostic(false, current, "unexpected character after vertex indices"); vertices.push_back(nfo); count++; } if(count < 3) diagnostic(false, current, "need at least 3 vertices for a face"); else if(count > 4) diagnostic(true, current, "non-tri/quad face"); FaceInfo nfo; nfo.start = vertices.size() - count; nfo.count = count; nfo.mtrl = currentMaterial; nfo.smoothing_group = currentSmoothingGroup; faces.push_back(nfo); } else if(matchCommand(current, "g")) // group { current = endOfLine; } else if(matchCommand(current, "o")) // object { current = endOfLine; } else if(matchCommand(current, "s")) // smooth shading { if(matchCommand(current, "off")) currentSmoothingGroup = -1; else currentSmoothingGroup = parseInt(current) - 1; } else if(matchCommand(current, "usemtl")) { currentMaterial = findMaterial(current); current = endOfLine; } else if(matchCommand(current, "mtllib")) { MTLFileReader mtlreader; if(!mtlreader.read((basepath + current).c_str())) diagnostic(false, current, "error reading material library"); else { materials.swap(mtlreader.materials); current = endOfLine; defaultMaterial = -1; defaultMaterial = findMaterial("default"); sVERIFY(defaultMaterial != -1); currentMaterial = defaultMaterial; } } else diagnostic(false, current, "unknown command"); }