Я пытаюсь загрузить и отображать 3D -модели, используя C ++, современный OpenGL (GLFW 3.4.0, Glad, GLM) и новейший Assimp. Мой модели загружатель в основном работает, но я сталкиваюсь с проблемой, в которой некоторые сетки неправильно текстурируются. Текстура навеса нанесена неправильной. Вместо использования правильного УФ -отображения, кажется, что вся текстура для модели применяется неправильно. Я отключил смешивание прямо сейчас, но проблема сохраняется при смешивании или без него. Кроме того, я сейчас загружаю диффузные текстуры.#include "model.h"
Model::Model(const std::string& file_path)
{
load_model(file_path);
}
void Model::load_model(const std::string& file_path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(file_path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
if (!scene || (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) || !scene->mRootNode) {
std::cerr mMeshes[node->mMeshes];
meshes.push_back(process_mesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
process_node(node->mChildren, scene);
}
}
Mesh Model::process_mesh(aiMesh* mesh, const aiScene* scene)
{
std::vector vertices;
std::vector indices;
std::vector textures;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex;
vertex.position = glm::vec3(mesh->mVertices.x, mesh->mVertices.y, mesh->mVertices.z);
if (mesh->HasNormals()) {
vertex.normal = glm::vec3(mesh->mNormals.x, mesh->mNormals.y, mesh->mNormals.z);
}
if (mesh->mTextureCoords[0]) {
vertex.tex_coords = glm::vec2(mesh->mTextureCoords[0].x, mesh->mTextureCoords[0].y);
} else {
vertex.tex_coords = glm::vec2(0.0f, 0.0f);
}
vertices.push_back(vertex);
}
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
std::vector diffuse_maps = load_material_textures(material, aiTextureType_DIFFUSE, "diffuse");
textures.insert(textures.end(), diffuse_maps.begin(), diffuse_maps.end());
return Mesh(vertices, indices, textures);
}
std::vector Model::load_material_textures(aiMaterial* material, aiTextureType type, const std::string& type_name)
{
std::vector textures;
for (unsigned int i = 0; i < material->GetTextureCount(type); i++) {
aiString str;
material->GetTexture(type, i, &str);
bool skip = false;
for (const auto& loaded_texture : textures_loaded) {
if (std::strcmp(loaded_texture.path.data(), str.C_Str()) == 0) {
textures.push_back(loaded_texture);
skip = true;
break;
}
}
if (!skip) {
Texture texture;
texture.id = load_texture_from_file(str.C_Str(), directory);
texture.type = type_name;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
void Model::render(Shader& shader, Camera& camera) {
for (auto& mesh : meshes) {
mesh.render(shader, camera);
}
}
GLuint load_texture_from_file(const char* file_path, const std::string& directory)
{
std::string file_name = directory + "/" + file_path;
GLuint texture_id;
glGenTextures(1, &texture_id);
int width, height, number_of_color_channels;
unsigned char* data = stbi_load(file_name.c_str(), &width, &height, &number_of_color_channels, 0);
if (data) {
GLenum format = (number_of_color_channels == 1) ? GL_RED : (number_of_color_channels == 3) ? GL_RGB : GL_RGBA;
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
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);
stbi_image_free(data);
} else {
std::cerr
Подробнее здесь: https://stackoverflow.com/questions/794 ... ect-uv-map
Проблемы картирования текстур с Assimp и OpenGL: некоторые сетки имеют неверное ультрафиоматическое отображение ⇐ C++
Программы на C++. Форум разработчиков
1738661956
Гость
Я пытаюсь загрузить и отображать 3D -модели, используя C ++, современный OpenGL (GLFW 3.4.0, Glad, GLM) и новейший Assimp. Мой модели загружатель в основном работает, но я сталкиваюсь с проблемой, в которой некоторые сетки неправильно текстурируются. Текстура навеса нанесена неправильной. Вместо использования правильного УФ -отображения, кажется, что вся текстура для модели применяется неправильно. Я отключил смешивание прямо сейчас, но проблема сохраняется при смешивании или без него. Кроме того, я сейчас загружаю диффузные текстуры.#include "model.h"
Model::Model(const std::string& file_path)
{
load_model(file_path);
}
void Model::load_model(const std::string& file_path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(file_path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
if (!scene || (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) || !scene->mRootNode) {
std::cerr mMeshes[node->mMeshes[i]];
meshes.push_back(process_mesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
process_node(node->mChildren[i], scene);
}
}
Mesh Model::process_mesh(aiMesh* mesh, const aiScene* scene)
{
std::vector vertices;
std::vector indices;
std::vector textures;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex;
vertex.position = glm::vec3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
if (mesh->HasNormals()) {
vertex.normal = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
}
if (mesh->mTextureCoords[0]) {
vertex.tex_coords = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
} else {
vertex.tex_coords = glm::vec2(0.0f, 0.0f);
}
vertices.push_back(vertex);
}
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
std::vector diffuse_maps = load_material_textures(material, aiTextureType_DIFFUSE, "diffuse");
textures.insert(textures.end(), diffuse_maps.begin(), diffuse_maps.end());
return Mesh(vertices, indices, textures);
}
std::vector Model::load_material_textures(aiMaterial* material, aiTextureType type, const std::string& type_name)
{
std::vector textures;
for (unsigned int i = 0; i < material->GetTextureCount(type); i++) {
aiString str;
material->GetTexture(type, i, &str);
bool skip = false;
for (const auto& loaded_texture : textures_loaded) {
if (std::strcmp(loaded_texture.path.data(), str.C_Str()) == 0) {
textures.push_back(loaded_texture);
skip = true;
break;
}
}
if (!skip) {
Texture texture;
texture.id = load_texture_from_file(str.C_Str(), directory);
texture.type = type_name;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
void Model::render(Shader& shader, Camera& camera) {
for (auto& mesh : meshes) {
mesh.render(shader, camera);
}
}
GLuint load_texture_from_file(const char* file_path, const std::string& directory)
{
std::string file_name = directory + "/" + file_path;
GLuint texture_id;
glGenTextures(1, &texture_id);
int width, height, number_of_color_channels;
unsigned char* data = stbi_load(file_name.c_str(), &width, &height, &number_of_color_channels, 0);
if (data) {
GLenum format = (number_of_color_channels == 1) ? GL_RED : (number_of_color_channels == 3) ? GL_RGB : GL_RGBA;
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
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);
stbi_image_free(data);
} else {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79408696/texture-mapping-issues-with-assimp-and-opengl-some-meshes-have-incorrect-uv-map[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия