]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/asset.c
1d6ddb0998432e6f2da4cfacfe390c7401ab194e
[shadowclad.git] / src / engine / asset.c
1 #include <stdlib.h>
2 #include <assimp/cimport.h>
3 #include <assimp/postprocess.h>
4
5 #include "asset.h"
6 #include "assimp_types.h"
7 #include "logger.h"
8 #include "tga.h"
9
10 static const AiScene* importScene(const char* path);
11 static Vector3D convertAiVector3D(AiVector3D vect);
12 static const char* replaceFileExtension(const AiString path, const char* ext);
13
14
15
16 const Solid* importSolid(const char* path) {
17         const AiScene* scene = importScene(path);
18         if (scene == NULL) {
19                 return NULL;
20         }
21         
22         const unsigned int numMeshes = scene->mNumMeshes;
23         const unsigned int numMaterials = scene->mNumMaterials;
24
25         // TODO Consider assets with some arrays empty, and prevent zero mallocs
26         
27         Solid* solid = malloc(sizeof(Solid));
28         solid->numMeshes = numMeshes;
29         solid->meshes = malloc(numMeshes * sizeof(Mesh));
30         solid->numMaterials = numMaterials;
31         solid->materials = malloc(numMaterials * sizeof(Material));
32         
33         for (unsigned int meshIndex = 0; meshIndex < numMeshes; ++meshIndex) {
34                 const AiMesh* aiMesh = scene->mMeshes[meshIndex];
35                 const unsigned int numVertices = aiMesh->mNumVertices;
36                 const unsigned int numFaces = aiMesh->mNumFaces;
37                 
38                 Mesh mesh = { .numVertices = numVertices,
39                               .vertices = malloc(numVertices * sizeof(Vector3D)),
40                               .normals = NULL,
41                               .textureCoords = NULL,
42                               .numFaces = numFaces,
43                               .faces = malloc(numFaces * sizeof(Face)),
44                               .materialIndex = aiMesh->mMaterialIndex };
45                 
46                 for (unsigned int vertIndex = 0; vertIndex < numVertices; ++vertIndex) {
47                         mesh.vertices[vertIndex] = convertAiVector3D(
48                                         aiMesh->mVertices[vertIndex]);
49                 }
50                 
51                 if (aiMesh->mNormals != NULL) {
52                         mesh.normals = malloc(numVertices * sizeof(Vector3D));
53                         for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) {
54                                 mesh.normals[normIndex] = convertAiVector3D(
55                                                 aiMesh->mNormals[normIndex]);
56                         }
57                 }
58                 
59                 mesh.textureCoords = malloc(numVertices * sizeof(Vector3D));
60                 for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) {
61                         mesh.textureCoords[texcIndex] = convertAiVector3D(
62                                         aiMesh->mTextureCoords[0][texcIndex]);
63                 }
64                 
65                 for (unsigned int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
66                         const AiFace aiFace = aiMesh->mFaces[faceIndex];
67                         const unsigned int numIndices = aiFace.mNumIndices;
68                         
69                         Face face = { .numIndices = numIndices,
70                                       .indices = malloc(numIndices * sizeof(size_t)) };
71                         
72                         for (unsigned int i = 0; i < numIndices; ++i) {
73                                 face.indices[i] = aiFace.mIndices[i];
74                         }
75                         
76                         mesh.faces[faceIndex] = face;
77                 }
78                 
79                 solid->meshes[meshIndex] = mesh;
80         }
81         
82         GLuint* textureIds = malloc(numMaterials * sizeof(GLuint));
83         glGenTextures(numMaterials, textureIds);
84         
85         for (unsigned int matIndex = 0; matIndex < numMaterials; ++matIndex) {
86                 Material material = { .textureId = textureIds[matIndex] };
87                 
88                 glBindTexture(GL_TEXTURE_2D, material.textureId);
89                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90                 
91                 AiString originalTexturePath;
92                 if (aiGetMaterialTexture(scene->mMaterials[matIndex],
93                                          aiTextureType_DIFFUSE,
94                                          0,
95                                          &originalTexturePath,
96                                          NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
97                         const char* textureFilename = replaceFileExtension(originalTexturePath, ".tga");
98                         const size_t textureFilenameLength = strlen(textureFilename);
99                         char* texturePath = malloc(strlen("assets/") + textureFilenameLength + 1);
100                         strcpy(texturePath, "assets/");
101                         strncat(texturePath, textureFilename, textureFilenameLength);
102                         
103                         TgaImage* textureImage = readTga(texturePath);
104                         if (textureImage == NULL) {
105                                 logError("Asset texture file not found: %s", texturePath);
106                         }
107                         else {
108                                 glTexImage2D(GL_TEXTURE_2D,
109                                              0,
110                                              textureImage->imageComponents,
111                                              textureImage->header.imageWidth,
112                                              textureImage->header.imageHeight,
113                                              0,
114                                              textureImage->imageFormat,
115                                              GL_UNSIGNED_BYTE,
116                                              textureImage->bytes);
117                                 free(textureImage->bytes);
118                                 free(textureImage);
119                         }
120                 }
121                 
122                 solid->materials[matIndex] = material;
123         }
124         glBindTexture(GL_TEXTURE_2D, 0);
125         
126         aiReleaseImport(scene);
127         return solid;
128 }
129
130 static const AiScene* importScene(const char* path) {
131         const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
132         if (scene == NULL) {
133                 logError("Failed to import asset from %s", path);
134         }
135         else if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) {
136                 logError("Incomplete scene imported from %s", path);
137                 aiReleaseImport(scene);
138                 scene = NULL;
139         }
140         return scene;
141 }
142
143 static Vector3D convertAiVector3D(AiVector3D vect) {
144         return (Vector3D) { .x = vect.x,
145                             .y = vect.y,
146                             .z = vect.z };
147 }
148
149 /**
150  * BUGS
151  * The following function will not work properly with texture
152  * file names (excluding directory part) beginning with '.'
153  */
154 static const char* replaceFileExtension(const AiString path, const char* ext) {
155                 size_t lengthToCopy = path.length;
156                 
157                 char* lastDotSubstr = strrchr(path.data, '.');
158                 if (lastDotSubstr != NULL) {
159                         if (strpbrk(lastDotSubstr, "\\/") == NULL) {
160                                 lengthToCopy = lastDotSubstr - path.data;
161                         }
162                 }
163                 
164                 size_t extLength = strlen(ext) + 1;
165                 char* newPath = malloc(lengthToCopy + extLength);
166                 strncpy(newPath, path.data, lengthToCopy);
167                 strncpy(newPath + lengthToCopy, ext, extLength);
168                 
169                 return newPath;
170 }