Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Taralieth

Texture isn't loading

This topic is 5848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm reading the Role playing games /w directx book and I was just testing out the game core. I was rendering a rotating cube (copying some code from the tutorials at Andypike.com) and it worked. But when i tried to add a texture using a class in the gamecore called cTexture, the texture wouldn't appear. So tried testing if the texture was loaded but i found that it wasn't loading. The texture and the game exe file are both in the same folder, and the code compiles fine, but it still doesn't appear. Any idea why it won't be loading?? Heres the code:
  
// a program that draws a cube in direct3D


#include <windows.h>
#include "Core_Global.h"

class cApp : public cApplication
{
private:
	cGraphics m_Graphics;
	cTexture Texture1;

	cVertexBuffer m_Vertex;
	
	// holds the vertex 

	typedef struct {
		FLOAT x,y,z; //3D Coordinates

		D3DCOLOR diffuse; // Diffuse colour

		FLOAT u,v; // texture coord

	} sVertex;

	#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)


public:
	cApp();

	void Rotation();
	void Camera();

	BOOL Init();
	BOOL Shutdown();
	BOOL Frame();
};

cApp::cApp()
{
	m_Style = WS_OVERLAPPEDWINDOW; // the window style

	m_XPos = 100; // X Coordinate of window

	m_YPos = 20;  // Y coordinates of window

	m_Width = 640; //the window width

	m_Height = 480; // Window height


	// Assign a class name and window caption

	strcpy(m_Class, "Class");
	strcpy(m_Caption, "A Cube");
}

void cApp::Rotation()
{
	cWorldPosition ObjectPos;
	
	ObjectPos.Move(0.0f,0.0f,0.0f);
	ObjectPos.Rotate(timeGetTime() % 123456 / 400.0f,timeGetTime() % 123456 / 400.0f,timeGetTime() % 123456 / 400.0f);
	ObjectPos.Update();
	m_Graphics.SetWorldPosition(&ObjectPos);
}

void cApp::Camera()
{
	cCamera Cam;
	Cam.Point(0.0f, 0.0f,-30.0f,0.0f,0.0f,0.0f);
	m_Graphics.SetCamera(&Cam);
	Cam.Update();
}

cApp::Init()
{
	if(Texture1.Load(&m_Graphics,"1.bmp",0)==FALSE)
	{ 
		Error(FALSE, "Cannot load the texture");
	}

	if(Texture1.IsLoaded() == FALSE)
	{
		Error(FALSE,"Texture isn't loaded.");
	}

	sVertex Verts[] =
    { 
        //Top Face

        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0f,1.0f}, //Vertex 0 - Blue 

        {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f,0.0f}, //Vertex 1 - Red 

        {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 2 - Red 

        {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 3 - Green 


        //Face 1

        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f,1.0f}, //Vertex 4 - Red 

        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f,0.0f}, //Vertex 5 - Blue 

        {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,1.0f}, //Vertex 6 - Green 

        {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,0.0f}, //Vertex 7 - Red 


        //Face 2

        {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),1.0f,1.0f}, //Vertex 8 - Blue 

        {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 9 - Green

        
        //Face 3

        {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,1.0f}, //Vertex 10 - Green 

        {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,0.0f}, //Vertex 11 - Red 


        //Face 4

        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 12 - Red 

        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),1.0f,0.0f}, //Vertex 13 - Blue


        //Bottom Face

        {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),0.0f,1.0f}, //Vertex 14 - Green 

        {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f,0.0f}, //Vertex 15 - Blue 

        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 16 - Red 

        {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 17 - Green

    };


	// initializes the graphics core

	m_Graphics.Init();
	// Sets the graphics mode to windowed, 640x480 with default colour bits

	m_Graphics.SetMode(GethWnd(),TRUE,FALSE,640,480,0);

	m_Vertex.Create(&m_Graphics,18,VertexFVF,sizeof(sVertex));
	
	m_Vertex.Set(0,18,(void*)Verts);

	return TRUE;
}

cApp::Shutdown()
{
	m_Vertex.Free();
	m_Graphics.Shutdown();

	return TRUE;
}

cApp::Frame()
{

	// Clears the display to black

	m_Graphics.ClearDisplay(0xFF000000);
	
	//Begins the scene

	if(m_Graphics.BeginScene() == TRUE)
	{

		Rotation();
		Camera();

		m_Graphics.SetPerspective();

		m_Graphics.SetTexture(2,&Texture1);

		m_Vertex.Render(0,2,D3DPT_TRIANGLESTRIP);
		m_Vertex.Render(4,8,D3DPT_TRIANGLESTRIP);
		m_Vertex.Render(14,2,D3DPT_TRIANGLESTRIP);


		//Ends the rendering

		m_Graphics.EndScene();
	}
	
	//Flips the backbuffer and frontbuffer

	m_Graphics.Display();

	return TRUE;
}

//The WINMAIN Function

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
	cApp App;

	return App.Run();
}
   
* Edit: tags - Jim [edited by - Jim Adams on September 15, 2002 1:31:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
There may be something else wrong, but the first thing I wanted to check in problem solving this, was the "Load" method on the cTexture object. I did not see that in the code above.

In the end, i suppose it could be many things outside the scope of the code above - maybe even the size of the texture? I think you can query the error codes returned by the D3D texture loading methods to get a good idea of what went wrong.

Share this post


Link to post
Share on other sites
You''re trying to load the texture before initializing the graphics object. Try this init function:


  
cApp::Init()
{
sVertex Verts[] =
{
//Top Face

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0f,1.0f}, //Vertex 0 - Blue

{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f,0.0f}, //Vertex 1 - Red

{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 2 - Red

{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 3 - Green


//Face 1

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f,1.0f}, //Vertex 4 - Red

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f,0.0f}, //Vertex 5 - Blue

{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,1.0f}, //Vertex 6 - Green

{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,0.0f}, //Vertex 7 - Red


//Face 2

{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),1.0f,1.0f}, //Vertex 8 - Blue

{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 9 - Green


//Face 3

{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,1.0f}, //Vertex 10 - Green

{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,0.0f}, //Vertex 11 - Red


//Face 4

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 12 - Red

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),1.0f,0.0f}, //Vertex 13 - Blue


//Bottom Face

{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),0.0f,1.0f}, //Vertex 14 - Green

{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f,0.0f}, //Vertex 15 - Blue

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f,1.0f}, //Vertex 16 - Red

{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f,0.0f}, //Vertex 17 - Green

};


// initializes the graphics core

m_Graphics.Init();
// Sets the graphics mode to windowed, 640x480 with default colour bits

m_Graphics.SetMode(GethWnd(),TRUE,FALSE,640,480,0);

m_Vertex.Create(&m_Graphics,18,VertexFVF,sizeof(sVertex));

m_Vertex.Set(0,18,(void*)Verts);

if(Texture1.Load(&m_Graphics,"1.bmp",0)==FALSE)
{
Error(FALSE, "Cannot load the texture");
}

if(Texture1.IsLoaded() == FALSE)
{
Error(FALSE,"Texture isn''t loaded.");
}

return TRUE;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!