Model not showing up on screen...

This is a discussion on Model not showing up on screen... within the Game Programming forums, part of the General Programming Boards category; Okay, so I'm about to draw my first model via scene graph, and strange, weird stuff is a happening.. I ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Model not showing up on screen...

    Okay, so I'm about to draw my first model via scene graph, and strange, weird stuff is a happening..

    I have verified that the model is being loaded, and the draw function is being performed, I have verified that the scene graph IS updating recursively, and the draw function is being called.

    Weirdness..

    Okay, so first we load the model...

    Code:
    	Resource_Observer Request_Resource(const std::string & name)
    	{
    		Resource_Map::iterator  it = mResources.find(name);
    
    		if (it == mResources.end())
    		{
    			Resource_Ptr Raw_Resource(new T_);
    			Raw_Resource->Load(name);
    			mResources.insert(std::make_pair(name, Raw_Resource));
    			Resource_Observer Resource(Raw_Resource);
    			return Resource;
    		}
    		else
    		{
    			return Resource_Observer(it->second);
    		}
    	}
    the resource isn't being found, therefore it is loaded, this is being performed successfully...

    Second, we create the geometry node...

    Code:
    class CGeometryNode : public CSceneNode
    {
    public:
    
    	CGeometryNode(boost::weak_ptr<MS3DModel> Resource_Observer) 
    	{ 
    		Geometry = Resource_Observer;	
    	}
    
    	~CGeometryNode() { }
    
    	void Update()
    	{
    
    	boost::shared_ptr<MS3DModel> geom = Geometry.lock();
    		if(geom)
    		{
    			geom->Draw();
    		}
    		else
    		{
    			// eep! all shared_ptr's deleted!
    			//
    			// Reload Geometry, or take other measures
    		}
    
    		CSceneNode::Update();
    	}
    
    private:
    	boost::weak_ptr<MS3DModel> Geometry;
    
    };
    All the pointers remain valid, we initialize the node like this
    ->CGeometryNode * Geometry = new CGeometryNode(Models.Request_Resource("Data/Model.ms3d"));

    The path is correct, the model is loaded, and a weak pointer is returned, a usable pointer is created out of it, here...

    Code:
    	CGeometryNode(boost::weak_ptr<MS3DModel> Resource_Observer) 
    	{ 
    		Geometry = Resource_Observer;	
    	}
    
    	~CGeometryNode() { }
    
    	void Update()
    	{
    
    	boost::shared_ptr<MS3DModel> geom = Geometry.lock();
    And at this very moment, the simple Scene graph looks like this:

    Root Node->Transformation Node->Geometry Node..

    In the Draw function I do this..

    Code:
    RootNode->Update();
    This starts at the root node and works its way down recursively to draw the geometry, right now I have the transformation function just calling a loadIdentity, to avoid any more errors..

    The model isn't drawing. Simply put. Or maybe it is drawing, the function is being performed, I just can't see it... But why?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Where's your camera? If that also has it's transform set to the identity, then you're sitting ontop of what you're drawing.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I can move the camera with keyboard and mouse input, so that isn't the problem :\
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Have you checked that your model loading and rendering code works (independant of your scence graph)?

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I havn't explicitly done that, but I ran debug and the program did everything exactly as it should..

    It recursively updated all the nodes, just like it should, the pointer was valid all the way through, no boost errors, no silly anything like that...

    But no, I havn't straight up rendered the model, but I have no reason to believe it wouldn't work, debug went fine, everything went as planned.

    I'm using the same loading and drawing method I used before I implemented the scene graph, and it worked then, but then again, I don't think it is the scene graph's fault, there must be something silly I'm doing...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6

    Join Date
    May 2005
    Posts
    1,041
    You need to post the rendering specific code...OpenGL likely isn't setup properly (i.e, the proper states arent' set, textures aren't loaded, the camera isn't in the proper place).
    I'm not immature, I'm refined in the opposite direction.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okie Dokey...

    Loading

    Code:
    bool MS3DModel::Load(const std::string & name)
    {
    	std::ifstream inputFile( name.c_str(), std::ios::in | std::ios::binary);
    	if ( inputFile.fail())
    		return false;	// "Couldn't open the model file."
    
    	inputFile.seekg( 0, std::ios::end );
    	long fileSize = inputFile.tellg();
    	inputFile.seekg( 0, std::ios::beg );
    
    	byte *pBuffer = new byte[fileSize];
    	//inputFile.read( pBuffer, fileSize );
    	inputFile.close();
    
    	const byte *pPtr = pBuffer;
    	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
    	pPtr += sizeof( MS3DHeader );
    
    	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
    		return false; // "Not a valid Milkshape3D model file."
    
    	if ( pHeader->m_version < 3 || pHeader->m_version > 7 )
    		return false; // "Unhandled file version.  Milkshape3D Version 1.3 through 1.7 is supported." :)
    
    	int nVertices = *( word* )pPtr; 
    	NumVertices = nVertices;
    	Vertices = new Vertex[nVertices];
    	pPtr += sizeof( word );
    
    	int i;
    	for ( i = 0; i < nVertices; i++ )
    	{
    		MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
    		Vertices[i].BoneID = pVertex->m_boneID;
    		memcpy( Vertices[i].Location, pVertex->m_vertex, sizeof( float )*3 );
    		pPtr += sizeof( MS3DVertex );
    	}
    
    	int nTriangles = *( word* )pPtr;
    	NumTriangles = nTriangles;
    	Triangles = new Triangle[nTriangles];
    	pPtr += sizeof( word );
    
    	for ( i = 0; i < nTriangles; i++ )
    	{
    		MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
    		int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
    		float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
    		memcpy( Triangles[i].VertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
    		memcpy( Triangles[i].Textures1, pTriangle->m_s, sizeof( float )*3 );
    		memcpy( Triangles[i].Textures2, t, sizeof( float )*3 );
    		memcpy( Triangles[i].VertexIndices, vertexIndices, sizeof( int )*3 );
    		pPtr += sizeof( MS3DTriangle );
    	}
    
    	int nGroups = *( word* )pPtr;
    	NumMeshes = nGroups;
    	Meshes = new Mesh[nGroups];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nGroups; i++ )
    	{
    		pPtr += sizeof( byte );	// flags
    		pPtr += 32;				// name
    
    		word nTriangles = *( word* )pPtr;
    		pPtr += sizeof( word );
    		int *pTriangleIndices = new int[nTriangles];
    		for ( int j = 0; j < nTriangles; j++ )
    		{
    			pTriangleIndices[j] = *( word* )pPtr;
    			pPtr += sizeof( word );
    		}
    
    		char materialIndex = *( char* )pPtr;
    		pPtr += sizeof( char );
    	
    		Meshes[i].MaterialIndex = materialIndex;
    		Meshes[i].NumTriangles = nTriangles;
    		Meshes[i].TriangleIndices = pTriangleIndices;
    	}
    
    	int nMaterials = *( word* )pPtr;
    	NumMaterials = nMaterials;
    	Materials = new Material[nMaterials];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nMaterials; i++ )
    	{
    		MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
    		memcpy( Materials[i].Ambient, pMaterial->m_ambient, sizeof( float )*4 );
    		memcpy( Materials[i].Diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
    		memcpy( Materials[i].Specular, pMaterial->m_specular, sizeof( float )*4 );
    		memcpy( Materials[i].Emissive, pMaterial->m_emissive, sizeof( float )*4 );
    		Materials[i].Shininess = pMaterial->m_shininess;
    		Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    		strcpy( Materials[i].TextureFilename, pMaterial->m_texture );
    		pPtr += sizeof( MS3DMaterial );
    	}
    
    	ReloadTextures();
    
    	delete[] pBuffer;
    
    	return true;
    }
    
    void MS3DModel::ReloadTextures()
    {
    	for ( int i = 0; i < NumMaterials; i++ )
    		if ( strlen( Materials[i].TextureFilename ) > 0 )
    			Materials[i].Texture = LoadGLTexture( Materials[i].TextureFilename );
    		else
    			Materials[i].Texture = 0;
    }
    Drawing

    Code:
    void MS3DModel::Draw()
    {
    
    	GLboolean texEnabled = glIsEnabled( GL_TEXTURE_2D );
     
    	// Draw by group
    	for ( int i = 0; i < NumMeshes; i++ )
    	{
    
    		const int materialIndex = Meshes[i].MaterialIndex;
    		if ( materialIndex >= 0 )
    		{
    			glMaterialfv( GL_FRONT, GL_AMBIENT, Materials[materialIndex].Ambient );
    			glMaterialfv( GL_FRONT, GL_DIFFUSE, Materials[materialIndex].Diffuse );
    			glMaterialfv( GL_FRONT, GL_SPECULAR, Materials[materialIndex].Specular );
    			glMaterialfv( GL_FRONT, GL_EMISSION, Materials[materialIndex].Emissive );
    			glMaterialf( GL_FRONT, GL_SHININESS, Materials[materialIndex].Shininess );
     
    			if ( Materials[materialIndex].Texture > 0 )
    			{
    				glBindTexture( GL_TEXTURE_2D, Materials[materialIndex].Texture );
    				glEnable( GL_TEXTURE_2D );
    			}
    			else
    				glDisable( GL_TEXTURE_2D );
    		}
    		else
    		{
    			// Material properties?
    			glDisable( GL_TEXTURE_2D );
    		}
     
    		glBegin( GL_TRIANGLES );
    		{
    			for ( int j = 0; j < Meshes[i].NumTriangles; j++ )
    			{
    				const int triangleIndex = Meshes[i].TriangleIndices[j];
    				const MS3DModel::Triangle * pTri = &(Triangles[triangleIndex]);
     
    				for ( int k = 0; k < 3; k++ )
    				{
    					const int index = pTri->VertexIndices[k];
     
    					glNormal3fv( pTri->VertexNormals[k] );
    					glTexCoord2f( pTri->Textures1[k], pTri->Textures2[k] );
    					glVertex3fv( Vertices[index].Location );
    				}
    			}
    		}
    		glEnd();
    	}
     
    	if ( texEnabled )
    		glEnable( GL_TEXTURE_2D );
    	else
    		glDisable( GL_TEXTURE_2D );
    }
    Now, I'm 100% positive that both of these functions are being called successfully!

    Here is my Initialization function..

    Code:
    BOOL Initialize (GL_Window* window, Keys* keys)		
    {
    	g_window	= window;
    	g_keys		= keys;
    
    	// Start Of User Initialization
    	angle		= 0.0;	
    
    	RootNode->AddChild(Transformation);
    	Transformation->AddChild(Geometry);
    
    	// Now set up our max values for the camera
    	Cam.m_MaxForwardVelocity = 5.0f;
    	Cam.m_MaxPitchRate = 5.0f;
    	Cam.m_MaxHeadingRate = 5.0f;
    	Cam.m_PitchDegrees = 0.0f;
    	Cam.m_HeadingDegrees = 0.0f;
    
    	glClearColor (0.0f, 0.0f, 0.0f, 0.5f);				
    	glClearDepth (1.0f);					
    	glDepthFunc (GL_LEQUAL);							
    	glEnable (GL_DEPTH_TEST);							
    	glShadeModel (GL_SMOOTH);						
    	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);		
    
    	return TRUE;									
    }
    And the Drawing in the GLDrawing loop thingy..

    Code:
    void DrawGLScene () 
    { 
    
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
    	glLoadIdentity();
    	RootNode->Update();
    	Cam.SetPrespective();
    	CheckKeys();
    	CheckMouse();                                                 
    }
    All pretty straightforward, I'll post if anything comes up..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8

    Join Date
    May 2005
    Posts
    1,041
    RootNode->AddChild(Transformation);
    Transformation->AddChild(Geometry);


    Put these at the end of the Initialize function and tell me what happens (if anything).
    I'm not immature, I'm refined in the opposite direction.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I noticed one commented out line in my loading code..

    Code:
    	inputFile.read( pBuffer, fileSize );
    I un commented it and I get an error, this is probably my root of all evil..

    This is the error...
    Code:
    c:\documents and settings\jonathan\desktop\engine source\ms3d.cpp(160) : error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'byte *' to 'char *'
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>
            ]
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Here is what is happening in that small frame of time

    Code:
    bool MS3DModel::Load(const std::string & name)
    {
    	const char *ptr1 = 0;
    	ptr1= name.data ( );
    
    	ifstream inputFile( ptr1, ios::in | ios::binary);
    	if ( inputFile.fail())
    	{
    		MessageBox(NULL,"Model file not found.","Model Error",MB_OK);
    		return false;	// "Couldn't open the model file."
    	}
    
    	inputFile.seekg( 0, ios::end );
    	long fileSize = inputFile.tellg();
    	inputFile.seekg( 0, ios::beg );
    
    	byte *pBuffer = new byte[fileSize];
    	inputFile.read( pBuffer, fileSize );
    	inputFile.close();
    Any ideas?

    LOL I just realized I wasn't even reading the file and was expecting it to draw... But that read file line is causing an error, what is that?
    Last edited by Shamino; 03-09-2006 at 07:19 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    When reading from binary files, you must read in as a char *. So, just cast it.

    Code:
    inputFile.read(static_cast<char *>(pBuffer), fileSize );
    From the error it would seem you would need a reinterpret cast or a regular old c style cast (as it said), so use the one that works based on the context.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm sorry, I've never really had to cast anything in my entire life...

    How does one do this?

    EDIT: I find it funny how I am doing metahierarchies and can't type cast.

    I tried that code and it errored..

    Code:
    c:\documents and settings\jonathan\desktop\engine source\ms3d.cpp(160) : error C2440: 'static_cast' : cannot convert from 'byte *' to 'char *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Forgive me, I don't have my C++ book with me atm or I'd look it up, it's at school :\
    Last edited by Shamino; 03-09-2006 at 07:43 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    Oops...

    See the changes I made to the above post (I didn't expect you to reply so fast).

    This should work (I think):

    Code:
    inputFile.read((char *)pBuffer, fileSize );
    Edit: We keep missing each other... What I posted here will work, the static cast as an example.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    DINGDINGDING, congratulations, Morgul is the winner!!!

    We have an attack dwarf!!!

    http://img.photobucket.com/albums/v1...ttackDwarf.jpg
    Last edited by Shamino; 03-09-2006 at 07:54 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    Yay... looks pretty good, nice job.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    And for the first time ever..

    Drum roll please, for everyone on this forum who has put up with me for more than 450 posts... You guys all know how monumental this is .

    We have...

    MULTIPLE MODELS!!!!

    http://img.photobucket.com/albums/v1...fied_Dwarf.jpg

    I call it, crucified dwarf!

    EDIT: Wish my textures would work...

    Another pic, different angle, gotta love the camera class!!

    http://img.photobucket.com/albums/v1.../BetterPic.jpg
    Last edited by Shamino; 03-09-2006 at 08:14 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Model Not Showing up on screen...
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 04-05-2006, 10:57 AM
  2. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  5. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21