Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Assimp and Common Dialog Box - Strings - C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 28 June 2014 - 07:55 PM

Hi all,

 

I've been playing with Assimp: I've used it before, hardcoding file paths. However now I'm getting a complete file path from one of these Common Dialog Box things with

 GetOpenFileName()

http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646829(v=vs.85).aspx#open_file

 

I'm then jumping through some (CString-shaped) hoops to format the string for the call below:


Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Fast);
//aiProcessPreset_TargetRealtime_Fast has the configs we'll need// nickthecoder.com

 

The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

- what steps do I need to go through to ensure the filepath is good for assimp?

Hope to hear from you soon :)



Sponsor:

#2 Endurion   Crossbones+   -  Reputation: 4033

Like
2Likes
Like

Posted 28 June 2014 - 10:08 PM

Well, for starters, since you assume that your CString hoops are the culprit, you might show that particular code.

 

And to make it even easier for us, show the path before and after. This should make it trivial to point out any problem.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#3 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 04:11 AM

Sounds reasonable :)

My Hoops are straight out of Stack Overflow results of google searches.

This was called wstrtostr() on SO by the Answerer I beleive - this is so I can turn a 

CString into a std::string (....trying to standardize on std::string type in a multi-byte character set build)

This fn looks reasobable enough

std::string FunkyString(const std::wstring &wstr)
{
    std::string strTo;
    
    char *szTo = new char[wstr.length() + 1];
    szTo[wstr.size()] = '\0';
    
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
    
    strTo = szTo;
    delete[] szTo;


    return strTo;
}

//
// Swap '//' for '\\' in the given string
//
CString SlashFix(CString strBang)
{
    int n = strBang.Replace(_T("//"), _T("\\"));
    return strBang;
}

Then here's my Filename getter: Please excuse the poor variable names etc

std::string App::DoOpenFileDialog()
{
	std::string fName;
	OPENFILENAME ofn;       // common dialog box structure
	char szFile[260];       // buffer for file name
	HANDLE hf ;              // file handle

	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(ofn));
	...

	// Display the Open dialog box. 

	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
                file.Format(L"%s", ofn.lpstrFile);
		file = SlashFix(file);

		fName = FunkyString((LPCWSTR)file);
		OutputDebugString(file);
	}
	else
		fName = ("error!");

	return fName;
}

I also have this which lets me find out if a file exists - more stack overflow:

 

static bool FileExists( std::string path )
{
	std::ifstream i(path.c_str());
	return (i != 0);
}

And this will return false with any path from my dialog and 'CHoops',

However it works for files that are in subdirectories of the project dir - if there are no spaces in the name. So I think I need to escape the space characters?

If need more code please ask, however return value of DoOpenFileDialog() goes pretty much straight to the assimp call

case ID_FILE_OPEN:
{
	// returns full path
	std::string fName = DoOpenFileDialog();
				
	// deosn't work- string not fomratted correctly
        bool bReturnsFalse = Scatterbrain::FileExists(fName);
				
	//works: (this file is here and can be found from this space-less path);
	bool bReturnsTrue = Scatterbrain::FileExists("Models\\Cessna\\Cessna172.obj");

	MeshList meshes;
	GLFactory* f = GLFactory::Create();
				
        // loads with assimp
	if( f->LoadMeshListFromFile(fName, &meshes) )
	{
		OutputDebugString(L"Wow");
		// Make GL Meshes
	}
			OutputDebugString(L"OpenFile: ");
}
break;

Hope this helps :)

 



#4 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 04:24 AM

Before and After Strings copied from Output Window (I'm using VS2012 if it matters):

 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

As I said, I think it's the spaces,
 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

 

Code from DoOpenFileDialog() used to produce

	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
		file.Format(L"%s", ofn.lpstrFile);
		OutputDebugString(file);
		file = SlashFix(file);
		fName = FunkyString((LPCWSTR)file);

		OutputDebugString(file);
	}

On another note - I did try and 'replaces the spaces' with the SlashFix function with 

 n = strBang.Replace("\ ", "\\ ");

to no apparent avail except this warning:

c:\users\nathan\documents\visual studio 2012\darkstar\darkstar\appdialog.cpp(27): warning C4129: ' ' : unrecognized character escape sequence


#5 Sponji   Members   -  Reputation: 1600

Like
0Likes
Like

Posted 29 June 2014 - 04:33 AM


The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?

if(!scene) std::cout << importer.GetErrorString() << std::endl;

 


On another note - I did try and 'replaces the spaces' with the SlashFix function with 
n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".
 
Edit. Damn, I really hate this buggy quotation thingie on this forum. Whatever.

Edited by Sponji, 29 June 2014 - 04:37 AM.

Derp

#6 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 04:41 AM

 


The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?

if(!scene) std::cout << importer.GetErrorString() << std::endl;

 


On another note - I did try and 'replaces the spaces' with the SlashFix function with 
n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".

 

I have tried looking at the error:

 

if(!scene)
{
	const char* pszErr = aiGetErrorString();
	MessageBox(NULL, (LPCWSTR)pszErr, L"Error: Assimp", NULL);
	return false;
}

Yields an empty string and message box.

On spaces replaces:

 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj
C:\Users\Nathan\Documents\Visual\ Studio\ 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

 Is what I get in the output but in the debugger it looks like

C:\\Users\Nathan\\Documents\\Visual Studio\\ 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj

I'm sure, looking at that this, that I don't know what I'm doing preparing this string. My lord I feel a fool!

I've looked back at another project using hardcoded paths with assimp and they're of the form

 

"../Models/Cessna/Cessna172.obj"

I think I've got enough to play with for a while but I may get stuck..



#7 Sponji   Members   -  Reputation: 1600

Like
0Likes
Like

Posted 29 June 2014 - 04:49 AM

You shouldn't need to escape those spaces anyway. Maybe try hard coding the filename to the absolute path and see if that works.

filename = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\Cessna172.obj";

Derp

#8 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 05:06 AM

RIght - I'm close now.

I would of thought so too?

I tried the following and found a file with the hardcoded path and didn't when selecting the same file in the DIalog
 

CString SlashFix(CString strBang)
{
	int n = strBang.Replace(_T("\\"), _T("//"));
		n += strBang.Replace(_T(" "), _T("\\ "));

	return strBang;
}

// With this doing my testing

.....
   bool bDialogOpen = true;
	std::string path;
				
	if(bDialogOpen)			                        
		path = DoOpenFileDialog();                      // SlashFix() broken
	else
		path = "Models//Cessna\ 172//Cessna172.obj";	// works
				
	bool bSanity = Scatterbrain::FileExists(path);
				
	MeshList meshes;
	GLFactory* f = GLFactory::Create();

	if( f->LoadMeshListFromFile(path, &meshes) )
		OutputDebugString(L"// Make GL Meshes");
...

I'll try now with the full path hardcoded..

Meanwhile, as an aside pondering, if file.cpp opens writes to and saves a file, it would be created in the directory of that file, right?

Okay so the path hardcoded like this passes the FileExists test - also with slashes going the other way:

path = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj";

So what's in the CString that I'm creating that I can't see, that I'm messing up when I SlashFix() ?


Edited by mynameisnafe, 29 June 2014 - 05:07 AM.


#9 Buckeye   GDNet+   -  Reputation: 9555

Like
1Likes
Like

Posted 29 June 2014 - 06:40 AM

It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"

    char fileName[256], fileTitle[256];
    fileName[0] = 0;
    fileTitle[0] = 0;
    std::string userFilePath;

    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.lpstrFilter = "All files (*.*)\0*.*\0\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = 256;
    ofn.Flags = OFN_FILEMUSTEXIST;
    BOOL response = GetOpenFileNameA(&ofn);
    if (!response)
    {
        return false;
    }
    userFilePath = fileName;
    scene = importer.ReadFile(userFilePath, ... [your flags]);

Edited by Buckeye, 29 June 2014 - 06:42 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


#10 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 10:43 AM


It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"


- precisely ..

So, what you're saying is that this line in DoFileOpenDialog()
hf = CreateFileW (ofn.lpstrFile, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
 

is preventing me from then opening the file with ifstream later (Scatterbrain::FileExists(..) ) and Assimp after that ( .ReadFile(...) )

Why did I not think of that, given it says "LPSECURITY_ATTRIBUTES" in capital flippin' letters?

?

See, now that is something I hadn't thought of - I don't know that much about Win32 / MFC, just the basics like "Give me something I can draw on without using glut or GLFW"; this project is me trying to do some sort of editor.
 



#11 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 11:00 AM

Buckeye - I declare you the Victor!

Thank you sir. I very much should of spotted that, and I get to delete some code - which is always a bonus.



#12 Buckeye   GDNet+   -  Reputation: 9555

Like
0Likes
Like

Posted 29 June 2014 - 11:01 AM


So, what you're saying is ...

 

Thanks for giving me credit for astute observations, but, actually, I wasn't trying to say anything more than I did wink.png

 

I just tried to get to the basics, and didn't understand what you were trying to do with the string manipulations and CreateFile call. My suggestion was simply how I use Assimp! biggrin.png

 

So.. did that all help? EDIT: cross-post - it appears it did.


Edited by Buckeye, 29 June 2014 - 11:02 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


#13 mynameisnafe   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2014 - 12:32 PM

Indeed!

It's a long story - basically having gotten fed up of MFC while trying to create an anything-to-myformat application, I'm rolling Win32 basics and like building a framework on that. I just copied that thing from MSDN and didn't touch it, just adding stuff (......below the line of the code that was causing the problem......) to try and make it work. Face-to-the-palm!

I had a cube a minute ago, I've lost it now, but I'm now back in my comfort zone at least.

Thanks again :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS