mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | texture
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/16 15:00:49 by tomoron #+# #+# */
|
/* Created: 2025/01/16 15:00:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/01/24 18:49:57 by ycontre ### ########.fr */
|
/* Updated: 2025/01/28 18:53:37 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ 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, glm::vec2 vt1, glm::vec2 vt2, glm::vec2 vt3);
|
void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, std::vector<glm::vec2> textureVertices);
|
||||||
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);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */
|
/* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/25 14:10:19 by tomoron ### ########.fr */
|
/* Updated: 2025/01/28 19:05:16 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
|
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/23 19:39:33 by ycontre ### ########.fr */
|
/* Updated: 2025/01/28 19:15:41 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -153,6 +153,9 @@ struct hitInfo
|
|||||||
vec3 position;
|
vec3 position;
|
||||||
int obj_index;
|
int obj_index;
|
||||||
int mat_index;
|
int mat_index;
|
||||||
|
|
||||||
|
float u;
|
||||||
|
float v;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "shaders/random.glsl"
|
#include "shaders/random.glsl"
|
||||||
|
@ -87,10 +87,12 @@ bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
|
|||||||
v >= 0.0 && (u + v) <= 1.0 &&
|
v >= 0.0 && (u + v) <= 1.0 &&
|
||||||
t > 0.0;
|
t > 0.0;
|
||||||
|
|
||||||
|
hit.u = u;
|
||||||
|
hit.v = v;
|
||||||
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 = 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);
|
||||||
}
|
}
|
||||||
|
@ -105,12 +105,15 @@ uniform sampler2D textures[16];
|
|||||||
|
|
||||||
vec3 getTextureColor(int texture_index, hitInfo hit)
|
vec3 getTextureColor(int texture_index, hitInfo hit)
|
||||||
{
|
{
|
||||||
vec2 uv = getSphereUV(hit.normal);
|
GPUTriangle tri = triangles[hit.obj_index];
|
||||||
|
vec2 uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1;
|
||||||
|
return vec3(uv, 0.);
|
||||||
return (texture(textures[texture_index], uv).rgb);
|
return (texture(textures[texture_index], uv).rgb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
|
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
|
||||||
{
|
{
|
||||||
|
color *= mat.texture_index;
|
||||||
if (mat.texture_index != -1)
|
if (mat.texture_index != -1)
|
||||||
color *= getTextureColor(mat.texture_index, hit);
|
color *= getTextureColor(mat.texture_index, hit);
|
||||||
|
|
||||||
|
@ -80,6 +80,8 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
|
|||||||
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.u = temp_hit.u;
|
||||||
|
hit.v = temp_hit.v;
|
||||||
hit.t = temp_hit.t;
|
hit.t = temp_hit.t;
|
||||||
hit.last_t = temp_hit.last_t;
|
hit.last_t = temp_hit.last_t;
|
||||||
hit.obj_index = bvh_data.triangle_start_index + node.index + i;
|
hit.obj_index = bvh_data.triangle_start_index + node.index + i;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
||||||
/* Updated: 2025/01/28 18:34:10 by ycontre ### ########.fr */
|
/* Updated: 2025/01/28 19:01:09 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,9 +20,9 @@ int main(int argc, char **argv)
|
|||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
||||||
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
||||||
|
|
||||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
||||||
|
|
||||||
GLint max_gpu_size;
|
GLint max_gpu_size;
|
||||||
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/16 15:00:33 by tomoron #+# #+# */
|
/* Created: 2025/01/16 15:00:33 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/01/28 18:30:33 by ycontre ### ########.fr */
|
/* Updated: 2025/01/28 19:18:37 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -122,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, glm::vec2(0.), glm::vec2(0.), glm::vec2(0.));
|
addTriangle(v1, v2, v3, std::vector<glm::vec2>(0));
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
return(0);
|
return(0);
|
||||||
@ -153,7 +153,9 @@ void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::vecto
|
|||||||
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())]);
|
|
||||||
|
if (sp.size() > 1 && sp[1].length())
|
||||||
|
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())]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,11 +178,22 @@ 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], textureVertices[0], textureVertices[1], textureVertices[2]);
|
addTriangle(faceVertices[0], faceVertices[1], faceVertices[2], textureVertices);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, glm::vec2 vt1, glm::vec2 vt2, glm::vec2 vt3)
|
void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, std::vector<glm::vec2> texture_vertices)
|
||||||
{
|
{
|
||||||
|
glm::vec2 vt1 = glm::vec2(0.);
|
||||||
|
glm::vec2 vt2 = glm::vec2(0.);
|
||||||
|
glm::vec2 vt3 = glm::vec2(0.);
|
||||||
|
|
||||||
|
if (texture_vertices.size() == 3)
|
||||||
|
{
|
||||||
|
vt1 = texture_vertices[0];
|
||||||
|
vt2 = texture_vertices[1];
|
||||||
|
vt3 = texture_vertices[2];
|
||||||
|
}
|
||||||
|
|
||||||
_triangles.push_back(Triangle(v1, v2, v3, vt1, vt2, vt3, _mat));
|
_triangles.push_back(Triangle(v1, v2, v3, vt1, vt2, vt3, _mat));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/27 18:55:18 by ycontre ### ########.fr */
|
/* Updated: 2025/01/28 19:17:39 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -116,6 +116,10 @@ void Scene::addObject(Object *obj)
|
|||||||
gpu_triangle.vertex2 = triangle->getVertex3();
|
gpu_triangle.vertex2 = triangle->getVertex3();
|
||||||
gpu_triangle.normal = triangle->getNormal();
|
gpu_triangle.normal = triangle->getNormal();
|
||||||
|
|
||||||
|
gpu_triangle.texture_vertex1 = triangle->getTextureVertex1();
|
||||||
|
gpu_triangle.texture_vertex2 = triangle->getTextureVertex1();
|
||||||
|
gpu_triangle.texture_vertex3 = triangle->getTextureVertex1();
|
||||||
|
|
||||||
_gpu_triangles.push_back(gpu_triangle);
|
_gpu_triangles.push_back(gpu_triangle);
|
||||||
|
|
||||||
gpu_obj.vertex1 = triangle->getVertex2();
|
gpu_obj.vertex1 = triangle->getVertex2();
|
||||||
@ -185,7 +189,6 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
|
|||||||
gpu_triangle.texture_vertex2 = triangles[i].getTextureVertex2();
|
gpu_triangle.texture_vertex2 = triangles[i].getTextureVertex2();
|
||||||
gpu_triangle.texture_vertex3 = triangles[i].getTextureVertex3();
|
gpu_triangle.texture_vertex3 = triangles[i].getTextureVertex3();
|
||||||
|
|
||||||
|
|
||||||
_gpu_triangles.push_back(gpu_triangle);
|
_gpu_triangles.push_back(gpu_triangle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user