Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading TGA - problems

Started by kennez Feb 26, 2013 at 3:57 PM 3 replies 1.5k views
Original Post
kennez
kennez

Hi all,

First post here, so please be gentle with me. I'm sure I'm doing something really stupid, but I can't for the life of me figure it out!

I'm following this tutorial, http://www.rastertek.com/gl40tut05.html , and I'm having major problems - I cannot get the targa file texture to display at all. I've tried copying and pasting the code direct from the website into my project, but to no avail.

The relevant function is from the file graphicsclass.cpp, and the function is Initialize()


bool GraphicsClass::Initialize(OpenGLClass* OpenGL, HWND hwnd)
{
	bool result;


	// Store a pointer to the OpenGL class object.
	m_OpenGL = OpenGL;

	// Create the camera object.
	m_Camera = new CameraClass;
	if(!m_Camera)
	{
		return false;
	}

	// Set the initial position of the camera.
	m_Camera->SetPosition(0.0f, 0.0f, -10.0f);

	// Create the model object.
	m_Model = new ModelClass;
	if(!m_Model)
	{
		return false;
	}

	// Get the full path of current exe file.
	TCHAR FilePath[MAX_PATH] = { 0 };
	GetModuleFileName( 0, FilePath, MAX_PATH );

	// Strip the exe filename from path and get folder name.
	PathRemoveFileSpec( FilePath );    

	// Set the current working directory.
	SetCurrentDirectory( FilePath );

	MessageBox( hwnd, FilePath, L"Error", MB_OK );

	// Initialize the model object.
	result = m_Model->Initialize( m_OpenGL, "data/test.tga", 0, true );
	if( !result ) {
		MessageBox( hwnd, L"Could not initialize the model object.", L"Error", MB_OK );
		return false;
	}
	
	// Create the texture shader object.
	m_TextureShader = new TextureShaderClass;
	if(!m_TextureShader)
	{
		return false;
	}

	// Initialize the texture shader object.
	result = m_TextureShader->Initialize(m_OpenGL, hwnd);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the texture shader object.", L"Error", MB_OK);
		return false;
	}

	return true;
}

I keep hitting the error "Could not initialize the model object."

I've even gone through the other classes, copying and pasting them directly, but it still doesn't work. Any ideas?

Thanks,

Malcolm

gfxgangsta
gfxgangsta


I keep hitting the error "Could not initialize the model object."

Have you tried stepping through the code with a debugger? What happens inside ModelClass::Initialize(...) that would make it return an invalid value? Can you post the code for ModelClass::Initialize(...) ?

blueshogun96
blueshogun96

If you go blindly copying and pasting code from random sources, then they are not guaranteed to work. I highly recommend understanding how the code works before actually throwing it in your project. It will save you much hassle. You sound like you are very new to OpenGL (please correct me if I'm wrong). If that's true, I strongly believe you are better off writing your own code to get a good understanding of what is going on. You cannot rely on someone else's code all the time, and it's best to learn to walk on your own two feet at some point, even if you have to crawl first.

Now, I'm assuming that you're using Microsoft Visual Studio C++ as your compiler/IDE, right? My recommendation to you is to insert a breakpoint before your call to m_Model->Initialize(). Start debugging, then when your breakpoint is hit, step into that function and follow it until you find out what's causing it to fail. If you don't know how to use breakpoints, or don't know what a breakpoint is, read this: http://msdn.microsoft.com/en-us/library/ktf38f66(v=vs.71).aspx

Lastly, I don't mean to sound like a douchebag, but did you even read the code you've copied and pasted? I took a look at the tutorial page you linked to and I'm 99.9% sure that the problem is with your .tga file isn't found. If you're launching your .exe from Visual Studio, then you might need to change your working directory in the project settings. And changing the directory with SetCurrentDirectory doesn't look necessary to me. Reading from the wrong directory is likely your problem.

This is my advice to you. Maybe I'm wrong, but you seem rather new to C++, Win32 programming and Visual Studio .net. If this is true, please tell us so we can point you in the right direction so you can learn more!

Shogun

_paf
_paf

Looking at the Initialize method on that website, there are two ways that the method could fail.

Either failing to create the vertex/index buffer objects, which i don't think there's any problem with, or by failing to load the TGA image.

You could check the return value between those two function calls, to see which is causing the error.

Also, as Shogun said, did you make sure that the files you're trying to load exist in the correct folder (the TGA file, and perhaps also the shader files)?

Ectara
Ectara

I know it is kind of irrelevant, but those conditionals that check whether or not a pointer is null after you assigned a new'ed pointer to it won't ever get called. If it fails to allocate memory, it will throw an exception, which will stop executing the function and work its way up the call stack. I doubt that this is what you intended. The only way to get that behavior is to use new(std::nothrow), but first, learning about the language is more important.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.