+ | Texture working on sphere

This commit is contained in:
TheRedShip
2025-01-26 22:42:37 +01:00
parent 07fd5870bf
commit d93a7342a1
7 changed files with 8061 additions and 13 deletions

View File

@ -3,25 +3,22 @@ Pos=60,60
Size=400,400 Size=400,400
[Window][Camera] [Window][Camera]
Pos=1277,16 Pos=1645,8
Size=259,200 Size=259,200
[Window][Material] [Window][Material]
Pos=927,29 Pos=1646,214
Size=266,299 Size=262,299
Collapsed=1
[Window][Fog settings] [Window][Fog settings]
Pos=927,52 Pos=1647,517
Size=247,130 Size=247,130
Collapsed=1
[Window][Debug] [Window][Debug]
Pos=1642,668 Pos=1642,668
Size=260,143 Size=260,143
[Window][Debug BVH] [Window][Debug BVH]
Pos=927,72 Pos=1644,648
Size=274,205 Size=274,131
Collapsed=1

7988
includes/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,8 +24,8 @@ pl 0 -2 0 0 1 0 2 // floor
qu -1 1.999 -1 2 0 0 0 0 2 6 qu -1 1.999 -1 2 0 0 0 0 2 6
OBJ obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0 OBJ scenes/obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0
OBJ obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0 OBJ scenes/obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0
# OBJ obj/Model.obj # OBJ obj/Model.obj

View File

@ -2,7 +2,7 @@ hitInfo traceRay(Ray ray);
vec3 GetEnvironmentLight(Ray ray) vec3 GetEnvironmentLight(Ray ray)
{ {
return vec3(0.); // return vec3(0.);
vec3 sun_pos = vec3(-0.5, 0.5, 0.5); vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5; float SunFocus = 1.5;
float SunIntensity = 1.; float SunIntensity = 1.;
@ -88,8 +88,29 @@ vec3 sampleLights(vec3 position, inout uint rng_state)
return (light); return (light);
} }
vec2 getSphereUV(vec3 surfacePoint)
{
// Convert 3D point to spherical coordinates
float phi = atan(surfacePoint.z, surfacePoint.x);
float theta = acos(surfacePoint.y);
// Map to [0, 1] UV space
float u = (phi + M_PI) / (2.0 * M_PI);
float v = theta / M_PI;
return vec2(u, v);
}
uniform sampler2D sphereTexture;
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)
{ {
// if (objects[hit.obj_index].type == 0)
// {
// vec2 uv = getSphereUV(hit.normal);
// color *= texture(sphereTexture, uv).rgb;
// }
// else
color *= mat.color; color *= mat.color;
light += mat.emission * mat.color; light += mat.emission * mat.color;
// light += sampleLights(hit.position, rng_state); // light += sampleLights(hit.position, rng_state);

View File

@ -12,6 +12,10 @@
#include "RT.hpp" #include "RT.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Scene scene; Scene scene;
@ -97,6 +101,28 @@ int main(int argc, char **argv)
shader.attach(); shader.attach();
// texture
int width, height, channels;
unsigned char* image = stbi_load("texture.jpg", &width, &height, &channels, STBI_rgb_alpha);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload texture data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
// Free the image data
stbi_image_free(image);
//
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}}; Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
size_t size = sizeof(vertices) / sizeof(Vertex) / 3; size_t size = sizeof(vertices) / sizeof(Vertex) / 3;
shader.setupVertexBuffer(vertices, size); shader.setupVertexBuffer(vertices, size);
@ -164,6 +190,12 @@ int main(int argc, char **argv)
shader.set_float("u_time", (float)(glfwGetTime())); shader.set_float("u_time", (float)(glfwGetTime()));
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
//texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glUniform1i(glGetUniformLocation(shader.getProgramCompute(), "sphereTexture"), 0);
//
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

View File

@ -114,7 +114,10 @@ void Scene::addObject(Object *obj)
gpu_triangle.normal = triangle->getNormal(); gpu_triangle.normal = triangle->getNormal();
_gpu_triangles.push_back(gpu_triangle); _gpu_triangles.push_back(gpu_triangle);
return ;
gpu_obj.vertex1 = triangle->getVertex2();
gpu_obj.vertex2 = triangle->getVertex3();
gpu_obj.normal = triangle->getNormal();
} }
else if (obj->getType() == Object::Type::PORTAL) else if (obj->getType() == Object::Type::PORTAL)
{ {

View File

@ -105,6 +105,13 @@ void Shader::attach(void)
_program = glCreateProgram(); _program = glCreateProgram();
_program_compute = glCreateProgram(); _program_compute = glCreateProgram();
glProgramParameteri(_program_compute, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
glProgramParameteri(_program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
glAttachShader(_program, _vertex); glAttachShader(_program, _vertex);
glAttachShader(_program, _fragment); glAttachShader(_program, _fragment);
glAttachShader(_program_compute, _compute); glAttachShader(_program_compute, _compute);