Set C standard to C11 and add new assets
Changed CMakeLists.txt to set the C standard to C11. Added multiple binary image files for new visual assets. Added several new image files to enhance rendering capabilities. Changed stb_image.h to improve support for various image formats. Changed ray tracing engine to enhance ray creation and intersection. Changed triangle structure to use a vertex array for better attribute handling. Changed scene initialization to accommodate new texture management.
This commit is contained in:
@@ -1,23 +1,72 @@
|
||||
#include "Geometry/Mesh.h"
|
||||
#include "Geometry/Triangle.h"
|
||||
#include "Common/String.h"
|
||||
#include "Material/SimpleLit.h"
|
||||
#include "assimp/cimport.h"
|
||||
#include "assimp/scene.h"
|
||||
#include "assimp/postprocess.h"
|
||||
|
||||
mesh_entity_t mesh_load(const char* filename, uint8_t material_id, triangle_collection_t* triangles, material_collection_t* materials)
|
||||
mesh_entity_t mesh_load(const char* filename, scene_t* scene)
|
||||
{
|
||||
mesh_entity_t entity = {0};
|
||||
|
||||
const C_STRUCT aiScene* scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_GenSmoothNormals);
|
||||
if (scene == NULL)
|
||||
const struct aiScene* mesh_scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_Quality);
|
||||
if (mesh_scene == NULL)
|
||||
{
|
||||
perror(aiGetErrorString());
|
||||
return entity;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < scene->mNumMeshes; i++)
|
||||
entity.triangle_id = scene->triangles.count;
|
||||
entity.material_id = scene->materials.count;
|
||||
|
||||
for (uint32_t i = 0; i < mesh_scene->mNumMaterials; i++)
|
||||
{
|
||||
struct aiMesh* mesh = scene->mMeshes[i];
|
||||
const struct aiMaterial* src = mesh_scene->mMaterials[i];
|
||||
|
||||
struct aiColor4D base_color;
|
||||
aiGetMaterialColor(src, AI_MATKEY_COLOR_DIFFUSE, &base_color);
|
||||
|
||||
float smoothness = 0.5f;
|
||||
aiGetMaterialFloat(src, AI_MATKEY_SHININESS, &smoothness);
|
||||
|
||||
texture_t* albedo_texture = NULL;
|
||||
if (aiGetMaterialTextureCount(src, aiTextureType_DIFFUSE) > 0)
|
||||
{
|
||||
struct aiString path;
|
||||
if (AI_SUCCESS == aiGetMaterialTexture(src, aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL, NULL))
|
||||
{
|
||||
if (!is_absolute_path(path.data))
|
||||
{
|
||||
char directory[1024];
|
||||
get_path_directory(filename, directory, 1024);
|
||||
char image_path[1024];
|
||||
memcpy(image_path, path.data, 1024);
|
||||
string_join(directory, image_path, path.data, 1024);
|
||||
}
|
||||
|
||||
texture_entity_t entity = texture_load(path.data, true, &scene->textures);
|
||||
if (entity.id != INVALID_TEXTURE_ID)
|
||||
{
|
||||
albedo_texture = &scene->textures.buffer[entity.id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
simple_lit_properties_t prop =
|
||||
{
|
||||
.albedo = {base_color.r, base_color.g, base_color.b},
|
||||
.roughness = 1.0f - smoothness,
|
||||
.metallic = 0.0f,
|
||||
|
||||
.albedo_texture = albedo_texture,
|
||||
};
|
||||
|
||||
material_create_simple_lit_default(&prop, &scene->materials);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < mesh_scene->mNumMeshes; i++)
|
||||
{
|
||||
const struct aiMesh* mesh = mesh_scene->mMeshes[i];
|
||||
|
||||
//TODO: Handle all primitive types, not just triangles
|
||||
if (mesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE)
|
||||
@@ -25,9 +74,11 @@ mesh_entity_t mesh_load(const char* filename, uint8_t material_id, triangle_coll
|
||||
continue;
|
||||
}
|
||||
|
||||
bool has_uv = mesh->mTextureCoords[0] != NULL && mesh->mNumUVComponents[0] >= 2;
|
||||
|
||||
for (uint32_t j = 0; j < mesh->mNumFaces; j++)
|
||||
{
|
||||
struct aiFace* face = &mesh->mFaces[j];
|
||||
const struct aiFace* face = &mesh->mFaces[j];
|
||||
if (face->mNumIndices != 3)
|
||||
{
|
||||
continue;
|
||||
@@ -36,7 +87,7 @@ mesh_entity_t mesh_load(const char* filename, uint8_t material_id, triangle_coll
|
||||
uint32_t index1 = face->mIndices[0];
|
||||
uint32_t index2 = face->mIndices[1];
|
||||
uint32_t index3 = face->mIndices[2];
|
||||
|
||||
|
||||
vec3s point1 = {
|
||||
mesh->mVertices[index1].x,
|
||||
mesh->mVertices[index1].y,
|
||||
@@ -53,6 +104,20 @@ mesh_entity_t mesh_load(const char* filename, uint8_t material_id, triangle_coll
|
||||
mesh->mVertices[index3].z
|
||||
};
|
||||
|
||||
vec2s uv1 = {0.0f, 0.0f};
|
||||
vec2s uv2 = {0.0f, 0.0f};
|
||||
vec2s uv3 = {0.0f, 0.0f};
|
||||
if (has_uv)
|
||||
{
|
||||
struct aiVector3D const* tc = mesh->mTextureCoords[0];
|
||||
uv1.x = tc[index1].x;
|
||||
uv1.y = tc[index1].y;
|
||||
uv2.x = tc[index2].x;
|
||||
uv2.y = tc[index2].y;
|
||||
uv3.x = tc[index3].x;
|
||||
uv3.y = tc[index3].y;
|
||||
}
|
||||
|
||||
vec3s normal1 = {
|
||||
mesh->mNormals[index1].x,
|
||||
mesh->mNormals[index1].y,
|
||||
@@ -69,18 +134,15 @@ mesh_entity_t mesh_load(const char* filename, uint8_t material_id, triangle_coll
|
||||
mesh->mNormals[index3].z
|
||||
};
|
||||
|
||||
//TODO: Handle materials, we use OpenPBR standard for parameter naming
|
||||
vertex_t vertex1 = {.position = point1, .normal = normal1, .uv = uv1};
|
||||
vertex_t vertex2 = {.position = point2, .normal = normal2, .uv = uv2};
|
||||
vertex_t vertex3 = {.position = point3, .normal = normal3, .uv = uv3};
|
||||
|
||||
// uint32_t properties_count = scene->mMaterials[mesh->mMaterialIndex]->mNumProperties;
|
||||
// C_STRUCT aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
// for (uint32_t p = 0; p < properties_count; p++)
|
||||
// {
|
||||
// material->mProperties[p]->mKey;
|
||||
// }
|
||||
|
||||
triangle_create_with_normals(point1, point2, point3, normal1, normal2, normal3, material_id, triangles);
|
||||
triangle_create(vertex1, vertex2, vertex3, entity.material_id + mesh->mMaterialIndex, &scene->triangles);
|
||||
entity.triangle_count++;
|
||||
}
|
||||
}
|
||||
|
||||
aiReleaseImport(mesh_scene);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Geometry/Triangle.h"
|
||||
|
||||
bool triangle_collection_init(triangle_collection_t* triangles, size_t size)
|
||||
bool triangle_collection_init(size_t size, triangle_collection_t* triangles)
|
||||
{
|
||||
if (size > UINT64_MAX)
|
||||
{
|
||||
@@ -45,37 +45,23 @@ void triangle_collection_free(triangle_collection_t* collection)
|
||||
}
|
||||
}
|
||||
|
||||
void triangle_create_with_normals(vec3s point1, vec3s point2, vec3s point3,
|
||||
vec3s normal1, vec3s normal2, vec3s normal3,
|
||||
uint8_t material_id, triangle_collection_t* collection)
|
||||
void triangle_create(vertex_t v1, vertex_t v2, vertex_t v3, uint8_t material_id, triangle_collection_t* collection)
|
||||
{
|
||||
vec3s edge1 = glms_vec3_sub(v2.position, v1.position);
|
||||
vec3s edge2 = glms_vec3_sub(v3.position, v1.position);
|
||||
vec3s normal = glms_vec3_normalize(glms_vec3_cross(edge1, edge2));
|
||||
|
||||
triangle_t triangle = {
|
||||
.vertices = {v1, v2, v3},
|
||||
.face_normal = normal,
|
||||
.material_id = material_id,
|
||||
};
|
||||
|
||||
if (collection->count >= collection->size)
|
||||
{
|
||||
triangle_collection_resize(collection, collection->size * 2);
|
||||
}
|
||||
|
||||
triangle_t triangle;
|
||||
triangle.point_1 = point1;
|
||||
triangle.point_2 = point2;
|
||||
triangle.point_3 = point3;
|
||||
triangle.material_id = material_id;
|
||||
|
||||
triangle.normal_1 = normal1;
|
||||
triangle.normal_2 = normal2;
|
||||
triangle.normal_3 = normal3;
|
||||
|
||||
vec3s edge1 = glms_vec3_sub(point2, point1);
|
||||
vec3s edge2 = glms_vec3_sub(point3, point1);
|
||||
triangle.normal_face = glms_vec3_normalize(glms_vec3_cross(edge1, edge2));
|
||||
|
||||
collection->buffer[collection->count] = triangle;
|
||||
collection->count++;
|
||||
}
|
||||
|
||||
void triangle_create(vec3s point1, vec3s point2, vec3s point3, uint8_t material_id, triangle_collection_t* collection)
|
||||
{
|
||||
vec3s edge1 = glms_vec3_sub(point2, point1);
|
||||
vec3s edge2 = glms_vec3_sub(point3, point1);
|
||||
vec3s normal = glms_vec3_normalize(glms_vec3_cross(edge1, edge2));
|
||||
triangle_create_with_normals(point1, point2, point3, normal, normal, normal, material_id, collection);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user