+ | Multiple textures

This commit is contained in:
RedShip
2025-01-27 19:08:52 +01:00
parent 1e0ff2d88d
commit 6b64d2188e
4 changed files with 75 additions and 27 deletions

View File

@ -101,14 +101,14 @@ vec2 getSphereUV(vec3 surfacePoint)
return vec2(u, v); return vec2(u, v);
} }
uniform sampler2D sphereTexture; uniform sampler2D textures[16];
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) if (objects[hit.obj_index].type == 0)
{ {
vec2 uv = getSphereUV(hit.normal); vec2 uv = getSphereUV(hit.normal);
color *= texture(sphereTexture, uv).rgb; color *= texture(textures[0], uv).rgb;
} }
else else
color *= mat.color; color *= mat.color;

View File

@ -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/24 18:10:03 by ycontre ### ########.fr */ /* Updated: 2025/01/27 19:07:49 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,55 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
struct GPUTexture {
int width;
int height;
int channels;
int object_id; // to map texture to specific objects
};
std::vector<GLuint> loadTextures(const std::vector<std::string>& texture_paths, std::vector<GPUTexture>& gpu_textures) {
std::vector<GLuint> textureIDs;
for (size_t i = 0; i < texture_paths.size(); i++) {
int width, height, channels;
unsigned char* image = stbi_load(texture_paths[i].c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (!image) {
std::cerr << "Failed to load texture: " << texture_paths[i] << std::endl;
continue;
}
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);
// Store texture info for GPU
GPUTexture tex_info = {
width,
height,
channels,
static_cast<int>(i) // or map to specific object ID
};
gpu_textures.push_back(tex_info);
textureIDs.push_back(textureID);
stbi_image_free(image);
}
return textureIDs;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Scene scene; Scene scene;
@ -104,30 +153,24 @@ int main(int argc, char **argv)
glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO); glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO);
shader.attach();
// texture // texture
int width, height, channels;
unsigned char* image = stbi_load("texture.jpg", &width, &height, &channels, STBI_rgb_alpha);
GLuint textureID; std::vector<std::string> texture_paths = {"texture.jpg", "texture2.jpg", "texture.jpg"};
glGenTextures(1, &textureID); std::vector<GPUTexture> gpu_textures;
glBindTexture(GL_TEXTURE_2D, textureID); std::vector<GLuint> textureIDs = loadTextures(texture_paths, gpu_textures);
// Set texture parameters GLuint textureInfoSSBO;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glGenBuffers(1, &textureInfoSSBO);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindBuffer(GL_SHADER_STORAGE_BUFFER, textureInfoSSBO);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_textures.size() * sizeof(GPUTexture), gpu_textures.data(), GL_STATIC_DRAW);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, textureInfoSSBO); // Using binding point 7
// 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);
// //
shader.attach();
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);
@ -196,9 +239,14 @@ int main(int argc, char **argv)
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
//texture //texture
glActiveTexture(GL_TEXTURE0); // In your render loop
glBindTexture(GL_TEXTURE_2D, textureID); for (size_t i = 0; i < textureIDs.size(); i++) {
glUniform1i(glGetUniformLocation(shader.getProgramCompute(), "sphereTexture"), 0); glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textureIDs[i]);
// Set uniform for each texture
std::string uniform_name = "textures[" + std::to_string(i) + "]";
glUniform1i(glGetUniformLocation(shader.getProgramCompute(), uniform_name.c_str()), i);
}
// //
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);

View File

@ -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/23 18:31:52 by ycontre ### ########.fr */ /* Updated: 2025/01/27 18:55:18 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -210,7 +210,7 @@ void Scene::addMaterial(Material *material)
void Scene::addTexture(std::string path) void Scene::addTexture(std::string path)
{ {
(void) path;
} }
void Scene::updateLightAndObjects(int mat_id) void Scene::updateLightAndObjects(int mat_id)

BIN
texture2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB