2 #include <assimp/cimport.h>
3 #include <assimp/postprocess.h>
9 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
12 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
16 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
17 &blockEmpty, &blockEmpty, &blockEmpty,
18 &blockWall01, &blockEmpty, &blockWall01 };
20 BlockGrid levelGrid = { .width = 3,
22 .blocks = testBlocks };
24 //static TgaImage* levelImage = NULL;
26 static const char* replaceFileExtension(const AiString path, const char* ext);
29 const AiScene* sceneData = importScene("out/assets/wall01.3ds");
30 blockWall01.sceneData = sceneData;
31 if (sceneData != NULL) {
32 const unsigned int numTextures = sceneData->mNumMeshes;
34 blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
35 glGenTextures(numTextures, blockWall01.textureIds);
37 for (unsigned int i = 0; i < numTextures; ++i) {
38 glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 AiString originalTexturePath;
42 if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
43 aiTextureType_DIFFUSE,
46 NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
47 const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
48 size_t textureFileLength = strlen(textureFile);
49 char* texturePath = malloc(strlen("out/assets/") + textureFileLength + 1);
50 strcpy(texturePath, "out/assets/");
51 strncat(texturePath, textureFile, textureFileLength);
52 TgaImage* textureImage = readTga(texturePath);
53 if (textureImage == NULL) {
54 logError("Asset texture file not found: %s", texturePath);
57 glTexImage2D(GL_TEXTURE_2D,
59 textureImage->imageComponents,
60 textureImage->header.imageWidth,
61 textureImage->header.imageHeight,
63 textureImage->imageFormat,
66 free(textureImage->bytes);
71 glBindTexture(GL_TEXTURE_2D, 0);
75 void buildLevelFromImage(TgaImage* image) {
76 if (image->header.imageBpp != 32) {
77 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
82 const AiScene* importScene(const char* path) {
83 const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
85 logError("Failed to import asset from %s", path);
87 else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
88 logError("Incomplete scene imported from %s", path);
89 aiReleaseImport(scene);
93 // TODO aiReleaseImport(scene);
97 * The following function will not work properly with texture
98 * file names (excluding directory part) beginning with '.'
100 static const char* replaceFileExtension(const AiString path, const char* ext) {
101 size_t lengthToCopy = path.length;
103 char* lastDotSubstr = strrchr(path.data, '.');
104 if (lastDotSubstr != NULL) {
105 if (strpbrk(lastDotSubstr, "\\/") == NULL) {
106 lengthToCopy = lastDotSubstr - path.data;
110 size_t extLength = strlen(ext) + 1;
111 char* newPath = malloc(lengthToCopy + extLength);
112 strncpy(newPath, path.data, lengthToCopy);
113 strncpy(newPath + lengthToCopy, ext, extLength);