assimp fail to find the obj. file - c++

I try to use Assimp::Importer.ReadFile() to load my obj.file but it turns out that assimp fail to find the file correctly.
Here is a simple test
#include<string>
#include<assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
using namespace std;
int main(){
Assimp::Importer importer;
string modelPath = "D:\\素材\\nanosuit\\nanosuit.obj";
const aiScene* scene = importer.ReadFile(modelPath, aiProcess_Triangulate | aiProcess_FlipUVs );
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
cout << "ERROR::ASSIMP::" << importer.GetErrorString() << endl;
}
else cout << scene;
and the output is following text
ERROR::ASSIMP::Unable to open file "D:\素材\nanosuit\nanosuit.obj".
I haved tried to load different obj.file and it doesn't work too

This is a problem with the unicode name. You need to use a workaround to fix this. Use the Wi32-API-Call
SetCurrentDirectory(L"D:\\素材\\nanosuit\\");
to open the folder and import the asset afterwards:
const aiScene* scene = importer.ReadFile(L"nanosuit.obj", aiProcess_Triangulate | aiProcess_FlipUVs );
There is a design error in the Asset-Importer-Lib. The imported name will be used to name the asset and the asset-name is using ASCII. So unicode names will interpreted as ASCII-names and this will cause your error.

Related

How to read text(ASCII) files with Assimp?

I can successfully read binary files of different extensions(fbx, blend, ifc and etc.) with assimp but if file is not binary assimp failed reading.
#include <string>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/Importer.hpp>
int main() {
Assimp::Importer importer;
std::string path = "myFile.ifc";
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenNormals);
if (scene == nullptr
|| scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE
|| scene->mRootNode == nullptr) {
return false;
}
// ProcessNode(scene->mRootNode, scene);
return 0;
}
I did not find any flags in docs for solving this problem. And nowhere is it said about file types.
For example, if .fbx file in ASCII format - aiScene* is just nullptr but if .ifc file in ASCII program throw exception
enter image description here
The Asset-Importer-Library will detect the kind of data automatically. So when loading an IFC-File the loader will take care which kind of import much be used.
So the problem you are dealing with is not a missing ASCII-Import-Option you have forgotten.
My first guess would be that there is an issue created by your IFC-File WOuld it be possible to check it? You can create an issue-report with our issue-tracker

ifstream No such file or directory C++

So I'm trying to write a shader class in C++ similar to this. This is my file structure:
| -- /source
| | -- main.cpp
| | -- /Shaders
| | | -- Shader.h
| | | -- shader.frag
| | | -- shader.vert
In my main.cpp file, I import shaders.h. Shaders.h constains the shader class, which reads in shader code from the shader.frag and shader.vert files (or so it should). The path I pass from main.cpp is Shaders/shader.frag and Shaders/shader.vert, and I am getting the error No such file or directory.
Here is my (or their) relevant shader code:
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
Shader(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath); <------- this is where it is getting caught
fShaderFile.open(fragmentPath); <---------- and i assume it would be here as well
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e)
{
char buffer[256];
strerror_s(buffer, 256, errno);
printf("ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: %s\n", buffer);
}
...
I have tried multiple different path variations. I've tried passing an absolute path as well, and consistently get the same error. I would really appreciate any help with this.
You just need to include the <fstream> library.

Program with SPI_SETDESKWALLPAPER Function Only Changes the Desktop Background to the Color Black when Trying to Change it to an Image using C++

I am trying to change the desktop background/wallpaper to a different image with a .png file. Although when I run the program, the background turns to solid black instead.
I am certain that I typed the file name, "ksa.png", correctly in my code to be the image I want to be on my background. I used an if condition to write out the last error on a file when the error occurred and used an else condition to write out "Success" if no errors occurred; but when I run the program, it writes "Success" to the file. I have thought about using a .jpg file instead, thinking that maybe .png files just don't work. I'll give an update when I tried using that.
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"ksa.png";
std::ofstream log;
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log.open("log.txt");
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
When I run this program, the desktop background is suppose to be set as the image "ksa.png". Instead it's solid black. Any help is appreciated for making this work, thank you.
UPDATE
Okay so I updated the code to where it would run a .jpg file and I'm still getting the same result. Also I moved the line log.open("log.txt") command before the SystemParametersInfo() function like Remy Lebeau suggested and it still writes out "Success" to the file. I'm still having the same problem.
Here is my updated code:
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"3.jpg";
std::ofstream log;
log.open("log.txt");
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
Emmmm,there is a problem with your picture path. I've tried your code. You can't get pictures under relative paths unless you use absolute paths.
Like Cody Gray♦'s judgment .
const wchar_t *filenm = L"C:\\Users\\strives\\Desktop\\timg.bmp";

Assimp md5 scene with animations "has no animations"

I've been following the tutorials at OGLdev and LearnOpenGL to load assimp files. I can load static objects like the crysis nanosuit and the animated doom3 format model "boblampclean". The problem is that I cant get the animations from the md5 file. When I query whether the file has animations it returns 0 and other calls to get animation related things from it crash. I've tried different versions of assimp - 3.0, 3.1.1, and 3.3.3 built from vcpkg.
For example, if I run this it will return 0 although there is for sure an animation in the file. If I use the 32bit visual studio project from OGLdev it returns 1.
int main(int argc, char *argv[])
{
Assimp::Importer myImporter;
const aiScene *m_scene = NULL;
m_scene = myImporter.ReadFile("C:/users/bergj/desktop/obj/boblampclean.md5mesh", aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_CalcTangentSpace);
if (m_scene)
{
cout << "has anim = " << m_scene->HasAnimations() << endl;
}
else
{
cout << "no scene" << endl;
}
return 0;
}
I had to copy the md5anim file into the same folder. Damn.

ASSIMP exporting an imported scene without any changes throws exception

I am working on a project where I import a 3D mesh of an avatar using ASSIMP library, update it and export the updated scene again using the same ASSIMP library. To achieve this, as the first step, I have written a code to import a scene and without making any changes I am passing the reference to the export function. But, the export function throws me an error. The main function is as follows (you can verify that I am not making any changes to the imported scene):
int main(int argc, char** argv)
{
string filename = "../Content/PinocchioMesh/Model1.obj";
Assimp::Importer Importer;
//Importer.
cout << "\tReading file using ASSIMP" << endl;
const aiScene* aiscene = Importer.ReadFile(filename.c_str(), aiProcess_JoinIdenticalVertices | \
aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_SortByPType | aiProcess_Triangulate);
string str = Importer.GetErrorString();
if (!aiscene) {
printf("Error parsing '%s': '%s'\n", filename.c_str(), Importer.GetErrorString());
return false;
}
Assimp::Exporter exporter;
const aiExportFormatDesc* format = exporter.GetExportFormatDescription(0);
int lIndex = filename.find_last_of('/');
//const string path = Filename.substr(0,lIndex+1);
string path = "../Content/PinocchioMesh/";
cout << "\tExport path: " << path << endl;
aiReturn ret = exporter.Export(aiscene, format->id, path, aiscene->mFlags);
cout << exporter.GetErrorString() << endl;
return 0;
}
The error is in the Export() function ans says:
First-chance exception at 0x1052591B (Assimp32.dll) in ImportRigExport.exe: 0xC0000005: Access violation reading location 0x00000000.
If anyone has used assimp to export scenes, please help me.
It seems path doesn't include a file name, only the directory part, so that for export() is unlikely to be able to create an output file. Nevertheless, I agree, Assimp should manage this error case.