+ | Texture parsing on obj

This commit is contained in:
TheRedShip
2025-01-28 12:20:09 +01:00
parent e9c77cfdd4
commit 7b81995724
14 changed files with 2940 additions and 1195 deletions

View File

@ -30,11 +30,11 @@ class ObjParser
long int checkVertexIndex(int index, size_t size); long int checkVertexIndex(int index, size_t size);
void parseMtl(std::stringstream &line, Scene &scene); void parseMtl(std::stringstream &line, Scene &scene);
bool addTriangleFromPolygon(std::vector<glm::vec3> &vertices, int inv); bool addTriangleFromPolygon(std::vector<glm::vec3> &vertices, int inv);
void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3); void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, glm::vec2 vt1, glm::vec2 vt2, glm::vec2 vt3);
std::string getFilePath(std::string &file); std::string getFilePath(std::string &file);
int pointInTriangle(glm::vec3 pts[3], std::vector<glm::vec3> vertices, size_t cur); int pointInTriangle(glm::vec3 pts[3], std::vector<glm::vec3> vertices, size_t cur);
std::vector<std::string> objSplit(std::string str, std::string delim); std::vector<std::string> objSplit(std::string str, std::string delim);
void getFaceVertices(std::vector<glm::vec3> &faceVertices, std::stringstream &line); void getFaceVertices(std::vector<glm::vec3> &faceVertices, std::vector<glm::vec2> &textureVertices, std::stringstream &line);
std::string _filename; std::string _filename;
std::ifstream _file; std::ifstream _file;

View File

@ -40,6 +40,11 @@ struct GPUTriangle
alignas(16) glm::vec3 vertex2; alignas(16) glm::vec3 vertex2;
alignas(16) glm::vec3 normal; alignas(16) glm::vec3 normal;
glm::vec2 texture_vertex1;
glm::vec2 texture_vertex2;
glm::vec2 texture_vertex3;
int mat_index; int mat_index;
}; };
@ -121,6 +126,7 @@ class Scene
std::vector<GPUMaterial> &getMaterialData(); std::vector<GPUMaterial> &getMaterialData();
std::vector<GLuint> &getTextureIDs(); std::vector<GLuint> &getTextureIDs();
std::vector<std::string> &getTextures();
std::vector<GPUBvhData> &getBvhData(); std::vector<GPUBvhData> &getBvhData();
std::vector<GPUBvh> &getBvh(); std::vector<GPUBvh> &getBvh();

View File

@ -51,16 +51,22 @@ class Triangle : public Object
_mat_index = mat_index; _mat_index = mat_index;
} }
Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index) Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3,
: Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) { const glm::vec2& vt1, const glm::vec2& vt2, const glm::vec2& vt3, const int mat_index)
: Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3),
_texture_vertex1(vt1), _texture_vertex2(vt2), _texture_vertex3(vt3)
{
_normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization _normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization
_centroid = (_position + _vertex2 + _vertex3) / 3.0f; _centroid = (_position + _vertex2 + _vertex3) / 3.0f;
// _vertex2 -= _position; //optimization // _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization // _vertex3 -= _position; //optimization
} }
const glm::vec2 &getTextureVertex1() const {return (_texture_vertex1); }
const glm::vec2 &getTextureVertex2() const {return (_texture_vertex2); }
const glm::vec2 &getTextureVertex3() const {return (_texture_vertex3); }
const glm::vec3 &getVertex2() const { return (_vertex2); } const glm::vec3 &getVertex2() const { return (_vertex2); }
const glm::vec3 &getVertex3() const { return (_vertex3); } const glm::vec3 &getVertex3() const { return (_vertex3); }
const glm::vec3 &getNormal() const { return (_normal); } const glm::vec3 &getNormal() const { return (_normal); }
@ -73,6 +79,11 @@ class Triangle : public Object
glm::vec3 _vertex2; glm::vec3 _vertex2;
glm::vec3 _vertex3; glm::vec3 _vertex3;
glm::vec2 _texture_vertex1;
glm::vec2 _texture_vertex2;
glm::vec2 _texture_vertex3;
glm::vec3 _normal; glm::vec3 _normal;
glm::vec3 _centroid; glm::vec3 _centroid;

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 B

184
scenes/obj/tails.mtl Normal file
View File

@ -0,0 +1,184 @@
# Aspose.3D Wavefront OBJ Exporter
# Copyright 2004-2024 Aspose Pty Ltd.
# File created: 01/28/2025 10:29:08
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png
newmtl Material
illum 2
Kd 1 1 1
Ke 0 0 0
Pr 0.80404085
Pm 0
d 1
Tr 0
map_Kd Material_baseColor.png

1567
scenes/obj/tails.obj Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,10 @@ struct GPUTriangle
vec3 vertex2; vec3 vertex2;
vec3 normal; vec3 normal;
vec2 texture_vertex1;
vec2 texture_vertex2;
vec2 texture_vertex3;
int mat_index; int mat_index;
}; };

View File

@ -87,14 +87,10 @@ struct GPUBvh
vec3 min; vec3 min;
vec3 max; vec3 max;
int left_index; int index;
int right_index;
int is_leaf;
int first_primitive;
int primitive_count; int primitive_count;
}; };
layout(std430, binding = 4) buffer BvhBuffer layout(std430, binding = 4) buffer BvhBuffer
{ {
GPUBvh Bvh[]; GPUBvh Bvh[];
@ -165,26 +161,27 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
int current_index = stack[stack_ptr--]; int current_index = stack[stack_ptr--];
GPUBvh node = Bvh[bvh_data.bvh_start_index + current_index]; GPUBvh node = Bvh[bvh_data.bvh_start_index + current_index];
if (node.is_leaf != 0) if (node.primitive_count != 0)
{ {
for (int i = 0; i < node.primitive_count; i++) for (int i = 0; i < node.primitive_count; i++)
{ {
GPUTriangle obj = triangles[bvh_data.triangle_start_index + node.first_primitive + i]; GPUTriangle obj = triangles[bvh_data.triangle_start_index + node.index + i];
stats.triangle_count += 1;
hitInfo temp_hit; hitInfo temp_hit;
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t) if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t)
{ {
hit.t = temp_hit.t; hit.t = temp_hit.t;
hit.obj_index = bvh_data.triangle_start_index + node.index + i;
hit.normal = temp_hit.normal; hit.normal = temp_hit.normal;
hit.obj_index = bvh_data.triangle_start_index + node.first_primitive + i;
} }
stats.triangle_count++;
} }
} }
else else
{ {
GPUBvh left_node = Bvh[bvh_data.bvh_start_index + node.left_index]; GPUBvh left_node = Bvh[bvh_data.bvh_start_index + current_index + 1];
GPUBvh right_node = Bvh[bvh_data.bvh_start_index + node.right_index]; GPUBvh right_node = Bvh[bvh_data.bvh_start_index + node.index];
hitInfo left_hit; hitInfo left_hit;
hitInfo right_hit; hitInfo right_hit;
@ -192,20 +189,20 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
left_hit.t = 1e30; left_hit.t = 1e30;
right_hit.t = 1e30; right_hit.t = 1e30;
stats.box_count += 2;
bool left_bool = intersectRayBVH(ray, left_node, left_hit); bool left_bool = intersectRayBVH(ray, left_node, left_hit);
bool right_bool = intersectRayBVH(ray, right_node, right_hit); bool right_bool = intersectRayBVH(ray, right_node, right_hit);
stats.box_count += 2;
if (left_hit.t > right_hit.t) if (left_hit.t > right_hit.t)
{ {
if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = current_index + 1;
if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.index;
} }
else else
{ {
if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.index;
if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = current_index + 1;
} }
} }
} }
@ -213,44 +210,6 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
return (hit); return (hit);
} }
mat3 rotateX(float angleRadians) {
float c = cos(angleRadians);
float s = sin(angleRadians);
return mat3(
1.0, 0.0, 0.0,
0.0, c, -s,
0.0, s, c
);
}
mat3 rotateY(float angleRadians) {
float c = cos(angleRadians);
float s = sin(angleRadians);
return mat3(
c, 0.0, s,
0.0, 1.0, 0.0,
-s, 0.0, c
);
}
mat3 rotateZ(float angleRadians) {
float c = cos(angleRadians);
float s = sin(angleRadians);
return mat3(
c, -s, 0.0,
s, c, 0.0,
0.0, 0.0, 1.0
);
}
mat3 createTransformMatrix(vec3 rotationAngles, float scale) {
mat3 rotMatrix = rotateZ(rotationAngles.z) *
rotateY(rotationAngles.y) *
rotateX(rotationAngles.x);
return rotMatrix * scale;
}
hitInfo traverseBVHs(Ray ray, inout Stats stats) hitInfo traverseBVHs(Ray ray, inout Stats stats)
{ {
hitInfo hit; hitInfo hit;
@ -273,7 +232,7 @@ hitInfo traverseBVHs(Ray ray, inout Stats stats)
hitInfo temp_hit = traceBVH(transformedRay, BvhData[i], stats); hitInfo temp_hit = traceBVH(transformedRay, BvhData[i], stats);
temp_hit.t = temp_hit.t / bvh_data.scale; temp_hit.t = temp_hit.t / bvh_data.scale;
if (temp_hit.t < hit.t) if (temp_hit.t < hit.t)
{ {
hit.t = temp_hit.t; hit.t = temp_hit.t;

View File

@ -89,6 +89,7 @@ bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
hit.t = t; hit.t = t;
hit.position = ray.origin + ray.direction * t; hit.position = ray.origin + ray.direction * t;
// hit.normal = vec3(u, v, 1 - (u + v)); //texture mapping
hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal)); hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
return (valid); return (valid);

View File

@ -55,6 +55,7 @@ glm::vec2 ObjParser::getUV(std::stringstream &line)
if(!(line >> res.x) || (!(line >> res.y) && !line.eof())) if(!(line >> res.x) || (!(line >> res.y) && !line.eof()))
throw std::runtime_error("syntax error in obj file while parsing texture vertex"); throw std::runtime_error("syntax error in obj file while parsing texture vertex");
// res = res - glm::floor(res);
return(res); return(res);
} }
@ -121,7 +122,7 @@ bool ObjParser::addTriangleFromPolygon(std::vector<glm::vec3> &vertices, int inv
if(pointInTriangle(triangleVertices, vertices, i)) if(pointInTriangle(triangleVertices, vertices, i))
continue; continue;
vertices.erase(vertices.begin() + i); vertices.erase(vertices.begin() + i);
addTriangle(v1, v2 ,v3); addTriangle(v1, v2, v3, glm::vec2(0.), glm::vec2(0.), glm::vec2(0.));
return(1); return(1);
} }
return(0); return(0);
@ -140,7 +141,7 @@ std::vector<std::string> ObjParser::objSplit(std::string str, std::string delim)
return(res); return(res);
} }
void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::stringstream &line) void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::vector<glm::vec2> &textureVertices, std::stringstream &line)
{ {
std::string el; std::string el;
std::vector<std::string> sp; std::vector<std::string> sp;
@ -152,6 +153,7 @@ void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::strin
std::runtime_error("OBJ : too many values in an element of a face"); std::runtime_error("OBJ : too many values in an element of a face");
if(sp.size() == 0) if(sp.size() == 0)
std::runtime_error("OBJ : wtf ?"); std::runtime_error("OBJ : wtf ?");
textureVertices.push_back(_textureVertices[checkVertexIndex(std::stoi(sp[1]), _textureVertices.size())]);
faceVertices.push_back(_vertices[checkVertexIndex(std::stoi(sp[0]), _vertices.size())]); faceVertices.push_back(_vertices[checkVertexIndex(std::stoi(sp[0]), _vertices.size())]);
} }
} }
@ -159,8 +161,9 @@ void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::strin
void ObjParser::addFace(std::stringstream &line) void ObjParser::addFace(std::stringstream &line)
{ {
std::vector<glm::vec3> faceVertices; std::vector<glm::vec3> faceVertices;
std::vector<glm::vec2> textureVertices;
getFaceVertices(faceVertices, line);
getFaceVertices(faceVertices, textureVertices, line);
if(!line.eof()) if(!line.eof())
throw std::runtime_error("OBJ : an error occured while paring face"); throw std::runtime_error("OBJ : an error occured while paring face");
if(faceVertices.size() < 3) if(faceVertices.size() < 3)
@ -173,12 +176,12 @@ void ObjParser::addFace(std::stringstream &line)
if(!line.eof()) if(!line.eof())
throw std::runtime_error("OBJ: an error occured while parsing face"); throw std::runtime_error("OBJ: an error occured while parsing face");
addTriangle(faceVertices[0], faceVertices[1], faceVertices[2]); addTriangle(faceVertices[0], faceVertices[1], faceVertices[2], textureVertices[0], textureVertices[1], textureVertices[2]);
} }
void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, glm::vec2 vt1, glm::vec2 vt2, glm::vec2 vt3)
{ {
_triangles.push_back(Triangle(v1, v2, v3, _mat)); _triangles.push_back(Triangle(v1, v2, v3, vt1, vt2, vt3, _mat));
} }
void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
@ -214,6 +217,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
throw std::runtime_error("OBJ: syntax error in material file, missing material name"); throw std::runtime_error("OBJ: syntax error in material file, missing material name");
mat = new Material; mat = new Material;
memset(mat, 0, sizeof(Material)); memset(mat, 0, sizeof(Material));
mat->texture_index = -1;
mat->refraction = 1.0f; mat->refraction = 1.0f;
mat->roughness = 1.0f; mat->roughness = 1.0f;
continue; continue;
@ -248,7 +252,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
float prob; float prob;
if(!(lineStream >> prob)) if(!(lineStream >> prob))
throw std::runtime_error("OBJ : syntax error wile getting material transparency"); throw std::runtime_error("OBJ : syntax error while getting material transparency");
if(prob == 0) if(prob == 0)
continue; continue;
mat->metallic = 1 - prob; mat->metallic = 1 - prob;
@ -259,18 +263,27 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
float prob; float prob;
if(!(lineStream >> prob)) if(!(lineStream >> prob))
throw std::runtime_error("OBJ : syntax error wile getting material transparency"); throw std::runtime_error("OBJ : syntax error while getting material transparency");
if(prob == 1) if(prob == 1)
continue; continue;
mat->metallic = prob; mat->metallic = prob;
mat->type = 2; mat->type = 2;
} }
else if (identifier == "map_Kd")
{
std::string path;
if (!(lineStream >> path))
throw std::runtime_error("OBJ: syntax error while getting material texture");
mat->texture_index = scene.getTextures().size();
scene.addTexture("scenes/obj/" + path);
}
else else
std::cerr << "unsupported material setting : " << identifier << std::endl; std::cerr << "unsupported material setting : " << identifier << std::endl;
} }
if(mat) if(mat)
{ {
mat->texture_index = -1;
scene.addMaterial(mat); scene.addMaterial(mat);
_matNames[matName] = scene.getMaterialData().size() - 1; _matNames[matName] = scene.getMaterialData().size() - 1;
} }

View File

@ -181,6 +181,11 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
gpu_triangle.vertex2 = triangles[i].getVertex3(); gpu_triangle.vertex2 = triangles[i].getVertex3();
gpu_triangle.normal = triangles[i].getNormal(); gpu_triangle.normal = triangles[i].getNormal();
gpu_triangle.texture_vertex1 = triangles[i].getTextureVertex1();
gpu_triangle.texture_vertex2 = triangles[i].getTextureVertex2();
gpu_triangle.texture_vertex3 = triangles[i].getTextureVertex3();
_gpu_triangles.push_back(gpu_triangle); _gpu_triangles.push_back(gpu_triangle);
} }
@ -287,6 +292,11 @@ std::vector<GLuint> &Scene::getTextureIDs()
return (_gpu_textures); return (_gpu_textures);
} }
std::vector<std::string> &Scene::getTextures()
{
return (_textures);
}
GPUVolume &Scene::getVolume() GPUVolume &Scene::getVolume()
{ {
return (_gpu_volume); return (_gpu_volume);

View File

@ -62,7 +62,6 @@ void SceneParser::parseMaterial(std::stringstream &line)
mat->type = 0; mat->type = 0;
mat->texture_index = texture_index; mat->texture_index = texture_index;
std::cout << "Texture index: " << texture_index << std::endl;
_scene->addMaterial(mat); _scene->addMaterial(mat);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 82 KiB