]> git.lizzy.rs Git - shadowclad.git/blob - src/asset.c
Overhaul Makefile and directory structure
[shadowclad.git] / src / asset.c
1 #include <stdlib.h>
2 #include <assimp/cimport.h>
3 #include <assimp/postprocess.h>
4
5 #include "asset.h"
6 #include "logger.h"
7 #include "tga.h"
8
9 static const AiScene* importScene(const char* path);
10 static Vector3D convertAiVector3D(AiVector3D vect);
11 static const char* replaceFileExtension(const AiString path, const char* ext);
12
13
14
15 const Asset3D* importAsset(const char* path) {
16         const AiScene* scene = importScene(path);
17         if (scene == NULL) {
18                 return NULL;
19         }
20         
21         const unsigned int numMeshes = scene->mNumMeshes;
22         const unsigned int numMaterials = scene->mNumMaterials;
23         
24         Asset3D* asset = malloc(sizeof(Asset3D));
25         asset->numMeshes = numMeshes;
26         asset->meshes = malloc(numMeshes * sizeof(Mesh));
27         asset->numMaterials = numMaterials;
28         asset->materials = malloc(numMaterials * sizeof(Material));
29         
30         for (unsigned int meshIndex = 0; meshIndex < numMeshes; ++meshIndex) {
31                 const AiMesh* aiMesh = scene->mMeshes[meshIndex];
32                 const unsigned int numVertices = aiMesh->mNumVertices;
33                 const unsigned int numFaces = aiMesh->mNumFaces;
34                 
35                 Mesh mesh = { .numVertices = numVertices,
36                               .vertices = malloc(numVertices * sizeof(Vector3D)),
37                               .normals = NULL,
38                               .textureCoords = NULL,
39                               .numFaces = numFaces,
40                               .faces = malloc(numFaces * sizeof(Face)),
41                               .materialIndex = aiMesh->mMaterialIndex };
42                 
43                 for (unsigned int vertIndex = 0; vertIndex < numVertices; ++vertIndex) {
44                         mesh.vertices[vertIndex] = convertAiVector3D(
45                                         aiMesh->mVertices[vertIndex]);
46                 }
47                 
48                 if (aiMesh->mNormals != NULL) {
49                         mesh.normals = malloc(numVertices * sizeof(Vector3D));
50                         for (unsigned int normIndex = 0; normIndex < numVertices; ++normIndex) {
51                                 mesh.normals[normIndex] = convertAiVector3D(
52                                                 aiMesh->mNormals[normIndex]);
53                         }
54                 }
55                 
56                 if (aiMesh->mTextureCoords != NULL) {
57                         mesh.textureCoords = malloc(numVertices * sizeof(Vector3D));
58                         for (unsigned int texcIndex = 0; texcIndex < numVertices; ++texcIndex) {
59                                 mesh.textureCoords[texcIndex] = convertAiVector3D(
60                                                 aiMesh->mTextureCoords[0][texcIndex]);
61                         }
62                 }
63                 
64                 for (unsigned int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
65                         const AiFace aiFace = aiMesh->mFaces[faceIndex];
66                         const unsigned int numIndices = aiFace.mNumIndices;
67                         
68                         Face face = { .numIndices = numIndices,
69                                       .indices = malloc(numIndices
70                                                         * sizeof(unsigned int)) };
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                 asset->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* textureFile = replaceFileExtension(originalTexturePath, ".tga");
98                         const size_t textureFileLength = strlen(textureFile);
99                         char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
100                         strcpy(texturePath, "assets/");
101                         strncat(texturePath, textureFile, textureFileLength);
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                 asset->materials[matIndex] = material;
123         }
124         glBindTexture(GL_TEXTURE_2D, 0);
125         
126         aiReleaseImport(scene);
127         return asset;
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) == 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 /** BUGS
150  * The following function will not work properly with texture
151  * file names (excluding directory part) beginning with '.'
152  */
153 static const char* replaceFileExtension(const AiString path, const char* ext) {
154                 size_t lengthToCopy = path.length;
155                 
156                 char* lastDotSubstr = strrchr(path.data, '.');
157                 if (lastDotSubstr != NULL) {
158                         if (strpbrk(lastDotSubstr, "\\/") == NULL) {
159                                 lengthToCopy = lastDotSubstr - path.data;
160                         }
161                 }
162                 
163                 size_t extLength = strlen(ext) + 1;
164                 char* newPath = malloc(lengthToCopy + extLength);
165                 strncpy(newPath, path.data, lengthToCopy);
166                 strncpy(newPath + lengthToCopy, ext, extLength);
167                 
168                 return newPath;
169 }