www.pudn.com > AndreasHalm-src.zip > minimesh.h


#pragma once 
 
#include "windows.h" 
#include  
 
#include  
#include  
#include  
#include  
 
// simple mesh structure for triangle strips 
// one vertex should be in  
// also needed:  
//  - operator >> (for loading meshes) 
//  - void setup() const  
//    (called for the first item to set up  
//    the arrays) 
//  - void unsetup() const 
//    (should disable all used arrays) 
template 
class MiniMesh  
{ 
   bool ok; 
   std::vector data; 
   std::vector sizes; 
 
public: 
   MiniMesh() :ok(false) {} 
   virtual ~MiniMesh() {} 
 
   bool isOK() const { return ok; } 
   operator bool () const { return ok; } 
 
   bool load( std::istream& ); 
	bool load(WORD identifier,LPCTSTR lpType="MESH",HMODULE hModule=NULL); 
 
   void draw() const; 
 
}; 
 
 
template < typename item > 
inline 
bool MiniMesh::load( std::istream& is ) 
{ 
   // the file is thought to be line-based - one vertex per line 
   // an empty line means the end of a tri strip 
   std::string ln; 
   int items = 0; 
   while (is) { 
      std::getline(is,ln); 
      if (ln.size()>1) { 
			if (ln[0] == '#') continue; // comment char 
         std::istringstream isln(ln); 
         item curitem; 
         isln >> curitem; 
         data.push_back(curitem); 
         ++items; 
      } 
      else 
      { 
         if (items) { 
            sizes.push_back(items); 
            items = 0; 
         } 
      } 
   } 
   return ok = true; 
} 
 
 
template 
inline 
bool MiniMesh::load(WORD identifier,LPCTSTR lpType,HMODULE hModule) 
{ 
	HRSRC hResInfo=FindResource(hModule,MAKEINTRESOURCE(identifier),lpType); 
	if (!hResInfo) return false; 
 
	DWORD size=SizeofResource(hModule,hResInfo); 
	HGLOBAL hResData=LoadResource(hModule,hResInfo); 
	if (!size||!hResData) return false; 
 
	const char *data=(const char*)LockResource(hResData); 
	if (!data) return false; 
 
	return load(std::istrstream(data,size)); 
} 
 
 
template 
inline 
void MiniMesh::draw() const 
{ 
   data.front().setup(); 
   vector::const_iterator index; 
   int total_offset = 0; 
   for (index=sizes.begin();index!=sizes.end();++index) 
   { 
      glDrawArrays(GL_TRIANGLE_STRIP,total_offset,*index); 
      total_offset += *index; 
   } 
   data.front().unsetup(); 
}