TOP

ABOUT

PROGRAM

DIALY







���b�V����ǂݍ��ށI�I(1)�@�`OBJ�t�@�C���`

���@1�D�͂��߂Ɂc
��<��1> 2009/03/01�@�ꕔ�L���ƃv���O�����̏C�����s���܂����B�ȑO�̋L��������������{���ł��܂��B
��<��2> 2009/03/04�@�v���O�������C���B�l�p�`�|���S���ɂ��Ή����܂����B

�������ɃT���v�����̂ɁC���‚����Ƃ����ʂ��Ƃւڂ��̂ŁC���b�V���t�@�C����ǂݍ���ł݂܂��B
�g���t�@�C����Alias Wavefront OBJ File(*.obj)�ł��B




���@2�D�t�@�C���t�H�[�}�b�g
�@���낢��ȃt�@�C��������܂����ǂ��C���̂Ȃ��ł�OBJ�t�@�C���̓V���v���ł킩��₷���ق����Ǝv���܂��B
�@�܂��̓t�@�C���̓ǂݍ��݂��‚���O�ɂ����ƃt�@�C���t�H�[�}�b�g�̐��������܂��B
�@��̂悤�ȃ��b�V���t�@�C����\�����邽�߂ɂ�OBJ�t�@�C����MTL�t�@�C����2�‚��g���܂��B
OBJ�t�@�C���̕��͊􉽌`��̃f�[�^����舵���܂��B�v����ɒ��_���W�Ƃ��@���x�N�g���Ƃ��e�N�X�`�����W�Ƃ��C�ʂ��\������̂ɕK�v�ȃf�[�^�Ƃ����������̂�������Ă��܂��B
���MTL�t�@�C���̕��́C�ގ��f�[�^����舵���܂��B�‹��F�Ƃ��g�U���ˌ��Ƃ��e�N�X�`���̃t�@�C�����Ƃ����������̂�������Ă��܂��B
�Ƃ肠�����C�܂�OBJ�t�@�C���̂ق�����������܂��B
�t�@�C���t�H�[�}�b�g�ŐF�X�ƈ�����悤�Ɍ��܂��Ă���̂ł����c
����̓ǂݍ��݃v���O�������C�O�p�`�E�l�p�`���b�V���̂ݎ�舵�����Ƃɂ��܂��B
�g�p����MTL�t�@�C���@���@mtllib name
���_���W�@���@v x y z (w)
�@���x�N�g���@���@vn x y z
�ʏ��@���@�p�^�[���@�@f v0/t0/n0 v1/t1/n1 �c
�@�@�@�@�@�@�p�^�[���A�@f v0//n0 v1//n1 �c
�@�@�@�@�@�@�p�^�[���B�@f v0/t0 v1/t1 �c
�@�@�@�@�@�@�p�^�[���C�@f v0 v1 �c
�g�p����ގ��@���@usemtl name
��ԏ�́u�g�p����MTL�t�@�C���v�ł����COBJ�t�@�C�����ɖʃf�[�^��������MTL���ɂ͖ʃf�[�^������܂���B�‚܂�u�ʂɂ��̐F�‚��������`�I�v���Ă����̂�OBJ�t�@�C���ɏ�����Ă����ł��ˁB�u����C���̐F�͋�̓I�ɂǂ������F�Ȃ́HRGBA�̒l�����‚��炢�Ȃ́H�v�Ǝv���̂ł����CRGBA�̒l�������‚ɂȂ��Ă���̂��Ƃ�����MTL�t�@�C���̕��ɏ�����Ă����ł��ˁB�ŁC�ǂ��Ȃ��Ă��邩�킩��悤�ɂ��邽�߂ɁCOBJ�t�@�C�����ɂ���u���̐F��MTL�t�@�C���̕��ɂ̂��Ă��邩�炻�������Ă���B�v�Ƃ����̂�
mtllib name
�Œ�`����܂��Bmtllib���Ă����̂��u����MTL�t�@�C�����g����I�v�ɂ�����Cname��MTL�t�@���̖������܂��B

�‚Â��āC���_���W�ł��B����́c
v x y z (w)
�Œ�`����܂��Bv�����_���W�̒�`�̊J�n��\���܂��Bx, y, z, w�ɂ�x���W�̒l�Cy���W�̒l�Cz���W�̒l, w���W�̒l���͂���܂��BOBJ�t�@�C���o�͂���\�t�g�ɂ����w���W����`����Ă�����C����Ă��Ȃ������肵�܂��B���Ȃ݂Ƀ��^�Z�R�C�A�͒�`����ĂȂ��ق��ł��B����̓ǂݍ��݂ł́C����w�����͖������邱�Ƃɂ��܂��B

���͖@���x�N�g���ł����C��̒��_���W�Ƃقړ����ł��B
vn x y z
�Œ�`����܂��Bx,y,z�͖@���x�N�g����x����,y����,z�����ł��B

�‚��͖ʃf�[�^�ł��B�ʃf�[�^�́c
�@�@f v0/t0/n0 v1/t1/n1 v2/t2/n2 �c
�A�@f v0//n0 v1//n1 v2//n2�@�c
�B�@f v0/t0 v1/t1 v2/t2�@�c
�C�@f v0 v1 v2 �c
�Ƃ������ɒ�`����܂��B�ʃf�[�^�͒�`�̎d�����኱���G�ŁCface��\��f�Œ�`���J�n���܂��B�‚���v0/t0/n0�Ƃ������Ƀf�[�^�����āC����͎g�p���钸�_�̔ԍ���v0�ɂ͂���܂��B�X���b�V�����͂���Ŏ��ɂ���t0�͎g�p����e�N�X�`�����W�̔ԍ��������C�܂��X���b�V�����͂����n0�Ƃ��āC����n0�͎g�p����@���x�N�g���̔ԍ��������܂��B
�e�N�X�`�����W�̔ԍ��Ɩ@���x�N�g���̔ԍ��͎g�p���Ȃ��ꍇ�C�ȗ����”\�ƂȂ��Ă��邽�߂ɏ�̂悤��4�p�^�[���̒�`�����݂��܂��B�@�͂��ׂĎg�p�C�A�̓e�N�X�`�����W���g��Ȃ��^�C�v�C�B�͖@�����g��Ȃ��^�C�v�C�C�̓e�N�X�`�����W�Ɩ@���x�N�g�����g��Ȃ��^�C�v�ł��B
�‚���v2/t2/n2�Ȃǂ̌�Ɂc�ƂȂ��Ă���͖̂ʍ\������v�f�ɂ���Ă��̒��������܂邽�߂ł��B
���Ƃ��΁C�O�p�`�̏ꍇ�́@f v0/t0/n0 v1/t1/n1 v2/t2/n2 �Ƃ�������3�‚ɂȂ�B
�l�p�`�̏ꍇ�́@f v0/t0/n0 v1/t1/n1 v2/t2/n2 v3/t3/n3�@�ł��B
�܊p�`�̏ꍇ�́@f v0/t0/n0 v1/t1/n1 v2/t2/n2 v3/t3/n3 v4/t4/n4�@�Ƃ�������5�‚ɂȂ�܂��B

�Ō�͖ʂɎg�p����F�ł��B����́c
usemtl name
�Œ�`���܂��Busemtl��use material�̂����炭���ł��傤�Bname�͎g�p����}�e���A���̖��O������܂��B

���ۂ�OBJ�t�@�C�����������ŊJ���Ă݂�Ɖ��̂悤�Ȋ����ŏ����Ă���܂��B

���Ȃ݂ɍs�̐擪�ɏ����Ă���#�̓R�����g�s�������Ă��܂��B


������MTL�t�@�C���ł����C�����悤�Ƀ������ŊJ���Ă݂�Ɖ��̂悤�Ȋ����ł��B

��̉摜���ɐ������Ă����܂��B
�܂��V�����}�e���A���́c
newmtl name
�Œ�`���܂��B
���ɃA���r�G���g�J���[�E�f�B�t���[�Y�J���[�E�X�y�L�����[�J���[�́CKa, Kd, Ks���g���Ē�`���܂��B
Ka r g b
Kd r g b
Ks r g b
Ka, Kd, Ks�̌�ɂ‚Â����l��R, G, B�������Ӗ����܂��B
Ns shininess
Ns�͔��˂̋����ł��B�v���O�����Ō����Ə���GL_SHININESS���w�肵���Ƃ��Ɏg���l�ɂȂ�܂��B
���Ka, Kd, Ks������Ƃ킩��悤�ɐF�̐�����R, G, B��������܂���B�u�A���t�@�l�͂ǂ�����Ďw�肷��񂾂��H�v�Ǝv���邩������܂���B
�A���t�@�l�́C�L�[���[�hd�C���邢��Tr���g���Ē�`���܂��B
d alpha
Tr alpha

����́C�e�N�X�`���̓ǂݍ��݃v���O�����͕t�������Ă��Ȃ��̂ł����C�e�N�X�`�����戵���������ꍇ�́Cmap_Kd�i�f�B�t���[�Y�}�b�v�j�̃L�[���[�h�̌�Ƀe�N�X�`���t�@�C�������i�[�����悤�ɂȂ��Ă��܂��B
map_Kd texturefilename
�Ȃ̂ŁC�e�N�X�`���������炩���ߓǂ݂Ƃ��Ă����C���ƂŎ��O�̃e�N�X�`���ǂݍ��݊֐��ȂǂŁC�ۑ�����Ă���e�N�X�`���t�@�C�������烍�[�h���Ă��΂����Ǝv���܂��B
�����Ƃł����C�t�@�C���t�H�[�}�b�g�͂���Ȋ����ł��B���Ă킩��悤��1�s�ɒ��_���W��F�݂̂Ƃ�������1��ނ̃f�[�^����`����Ă��邽�߁C��r�I�t�@�C���̓ǂݍ��݂͂‚���₷���ł��B


���@3�D�t�@�C���̓ǂݍ���
�@�t�@�C���̓ǂݍ��݂̎d���ł����C�t�@�C�����J���āC1�s���“ǂݎ���ēǂݎ�������e���o�b�t�@�Ɋi�[���܂��B���Ƃ́C�s�̐擪��v���nv���newmtl���̃L�[���[�h������̂ŁC�o�b�t�@�̐擪��1�����Ŕ��ʂ��āCKa, Kd, Ks�̂悤��1�����ڂ������ꍇ�̓o�b�t�@��2�����ڂŔ��ʂ��Ă��܂��B
�O�́Csscanf_s�֐����g���āC��͂��s���Ă�����ł����c
DirectX��Sample Browser���Ă���COBJ�̓ǂݍ��݂̃T���v�����������̂ŁC�����ō������‚��������Ȃ��Ǝv�����̂ŁC�Q�l�ɂ����Ă��炤���Ƃɂ��܂����B
�܂��́Cstrcpy�֐��Ȃǂ��g���̂ŁC�w�b�_�[�̍ŏ���C4996�x�����b�Z�[�W�𖳌��ɂ���悤�ɉ��̂悤��define���Ă����܂��B
00015:  #pragma region Disable Waring C4996
00016:  //
00017:  // Disable Warning C4996
00018:  //
00019:  #ifndef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
00020:  #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
00021:  #endif
00022:  #ifndef _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES
00023:  #define _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES 1
00024:  #endif
00025:  #ifndef _CRT_SECURE_NO_WARNINGS 
00026:  #define _CRT_SECURE_NO_WARNINGS 1
00027:  #endif
00028:  #pragma endregion
00029:  
���define�����Ă����΁C�����I��C++�̃I�[�o�[���[�h�̋@�\���g���āCstrcpy�֐��Ȃǂ�strcpy_s�֐��Ȃǂɒu�������Ă���邻���ł��B�ڂ������Ƃ�MSDN���C�u�����ɍڂ��Ă���̂ŁC�m�F���Ă݂Ă��������BC++�̋@�\���g���̂ŁC������O�ł����CC����ł͎g���Ȃ��̂Œ��ӂ��ĉ������ˁB
���ɁC�e�N�X�`�����W�Ɩ@���x�N�g���C���_���W�C�F�Ȃǂ��i�[���邽�߂̃x�N�g���\���̂��`���Ă����܂��B
00056:  /////////////////////////////////////////////////////////////////////////
00057:  // OBJVEC2
00058:  /////////////////////////////////////////////////////////////////////////
00059:  struct OBJVEC2
00060:  {
00061:  	float x;
00062:  	float y;
00063:  	OBJVEC2( float nx=0.0f, float ny=0.0f );
00064:  	operator float* ();
00065:  	operator const float* () const;
00066:  };
00067:  
00068:  /////////////////////////////////////////////////////////////////////////
00069:  // OBJVEC3
00070:  /////////////////////////////////////////////////////////////////////////
00071:  struct OBJVEC3
00072:  {
00073:  	float x;
00074:  	float y;
00075:  	float z;
00076:  	OBJVEC3( float nx=0.0f, float ny=0.0f, float nz=0.0f );
00077:  	operator float* ();
00078:  	operator const float* () const;
00079:  };
00080:  
glVertex3fv()�֐���glTexCoord2fv()�֐��Ƃ��ł��g����悤�ɁCoperator float*()��operator const float*()���`���Ă���܂��B
����ƁCglBegin()�֐���glEnd()�֐��ł͂���ŕ`�悷��̂́C�p�t�H�[�}���X�I�ɍl���Ă������Ȃ��̂Ȃ̂����`�ƑO�X����v���Ă��āC���_�o�b�t�@���C���f�b�N�X�o�b�t�@�����g����`���ɒ��������Ȃ��`�Ǝv���Ă����̂ŁC����g����`���ɕύX���܂����B����Ȃ킯�ŁC�l�p�`�ƎO�p�`���������Ă���ƁC�戵���ɍ���̂ŁC�`��͎O�p�`���b�V��������舵��Ȃ��d�l�ɂ��܂����BDirectX�Ȃ񂩂͎O�p�`���b�V������������舵�����Ȃ����C�Q�[���@�Ȃ񂩂ł��قƂ�ǎO�p�`�|���S���ł���ˁHglBegin(GL_QUADS)�ŕ`�悵�Ă��C����GPU�����ŎO�p�`�ɂ����񂶂�Ȃ��ł��������H�c�Ƃ��K���Ȍ���������Ă���w
�@�ŁC�O�܂ł̓e�N�X�`�����W�Ƃ��@���x�N�g���Ƃ��ʁX�̔z��ɂ��Ċi�[���Ă�����ł����C���̂悤�ɒ��_�f�[�^�\���ɂ��Ē�`����`�ɂ��܂����B
00081:  /////////////////////////////////////////////////////////////////////////
00082:  // OBJVERTEX
00083:  /////////////////////////////////////////////////////////////////////////
00084:  struct OBJVERTEX
00085:  {
00086:  	OBJVEC2 texcoord;
00087:  	OBJVEC3 normal;
00088:  	OBJVEC3 position;
00089:  	OBJVERTEX(){}
00090:  };
00091:  
�@���ƁC���񂩂�T�u�Z�b�g�p�̍\���̂����܂����B
�}�e���A����؂�ւ���ɂ́C���\�����R�X�g�����������肵�܂��B�ł��邾�������������グ������΁C�}�e���A���̐؂�ւ��̉񐔂͌��炵�����������ł��B�̂́C����Ȓm�������Ėʈ�ˆ�‘S����glColor4fv()���glMaterialfv()���C���������}�e���A���؂�ւ������Ă�����ł����c���̍����ł��ˁB
�ŁC�����ŏo�Ă���̂��T�u�Z�b�g�ł��B


��̐}�̂悤�ɓ����}�e���A����K�p����ʂ̓}�e���A����؂�ւ����ɂ܂Ƃ߂ĕ`�悵�Ă��܂��������ʈ�ˆ�‚ɑ΂��ă}�e���A����؂�ւ�����͌����I�ɂȂ�܂��B���̂悤�ɓ����}�e���A����K�p����ʂ̃Z�b�g���T�u�Z�b�g�ƌĂԂ����ŁC������g���܂��B
�\���͉̂��̂悤�ɒ�`���܂����B
00092:  /////////////////////////////////////////////////////////////////////////
00093:  // OBJSUBSET
00094:  /////////////////////////////////////////////////////////////////////////
00095:  struct OBJSUBSET
00096:  {
00097:  	unsigned int materialIndex;
00098:  	unsigned int faceStart;
00099:  	unsigned int faceCount;
00100:  	OBJSUBSET(){}
00101:  };
00102:  
�̐S�ȁC��͂̕����Ȃ�ł����C�ꉞ�f�ڂ��Ă����܂����c�@�܂��C�܂�܂��Ċ����ł����ǁC���v���ȁH
�܂��COBJMESH�N���X�̒�`�Ȃ�ł����c���̂悤�ɂ��Ă���܂��B
00145:  /////////////////////////////////////////////////////////////////////////
00146:  // OBJMESH
00147:  /////////////////////////////////////////////////////////////////////////
00148:  class OBJMESH
00149:  {
00150:  private:
00151:  	OBJVERTEX* m_Vertices;
00152:  	OBJSUBSET* m_Subsets;
00153:  	OBJMATERIAL* m_Materials;
00154:  	unsigned int* m_Indices;
00155:  	unsigned int m_NumVertices;
00156:  	unsigned int m_NumSubsets;
00157:  	unsigned int m_NumMaterials;
00158:  	unsigned int m_NumIndices;
00159:  	OBJBOUNDINGBOX m_Box;
00160:  	OBJBOUNDINGSPHERE m_Sphere;
00161:  	char m_directoryPath[OBJ_NAME_LENGTH];
00162:  
00163:  	bool LoadMTLFile( const char* filename );
00164:  	bool LoadOBJFile( const char* filename );
00165:  
00166:  public:
00167:  	OBJMESH();
00168:  	~OBJMESH();
00169:  
00170:  	bool LoadFile( const char* filename );
00171:  	void Release();
00172:  	void Draw();
00173:  	unsigned int GetNumVertices();
00174:  	unsigned int GetNumSubsets();
00175:  	unsigned int GetNumMaterials();
00176:  	unsigned int GetNumIndices();
00177:  	unsigned int  GetIndexData( unsigned int index );
00178:  	unsigned int* GetIndices();
00179:  	OBJVERTEX  GetVertex( unsigned int index );
00180:  	OBJVERTEX* GetVertices();
00181:  	OBJSUBSET  GetSubset( unsigned int index );
00182:  	OBJSUBSET* GetSubsets();
00183:  	OBJMATERIAL  GetMaterial( unsigned int index );
00184:  	OBJMATERIAL* GetMaterials();
00185:  	OBJBOUNDINGBOX GetBox();
00186:  	OBJBOUNDINGSPHERE GetSphere();
00187:  };
00188:  
�܂��C�f�[�^�i�[����z��Ɠǂݍ��ݗp�̊֐��ƕ`��֐���錾���Ă��邾���ł��B
�ŁC�j�ƂȂ�OBJ�t�@�C���̓ǂݍ��݂ł����CLoadOBJFile()�֐��ɂčs���܂��B��`�͉��̂悤�ɂȂ��Ƃ�܂��B
00322:  //-----------------------------------------------------------------------
00323:  // Name : LoadOBJFile()
00324:  // Desc : OBJ�t�@�C���̓ǂݍ���
00325:  //-----------------------------------------------------------------------
00326:  bool OBJMESH::LoadOBJFile(const char *filename)
00327:  {
00328:  	ifstream file;
00329:  
00330:  	char mtlFileName[OBJ_NAME_LENGTH] = {0};
00331:  	char buf[OBJ_BUFFER_LENGTH] = {0};
00332:  	vector<OBJVEC3> positions;
00333:  	vector<OBJVEC3> normals;
00334:  	vector<OBJVEC2> texcoords;
00335:  	vector<OBJVERTEX> t_vertices;
00336:  	vector<OBJSUBSET> t_subsets;
00337:  	vector<unsigned int> t_indices;
00338:  	bool initBox = false;
00339:  	int prevSize = 0;
00340:  
00341:  	unsigned long total = 0;
00342:  
00343:  	OBJMATERIAL material;
00344:  	unsigned int dwFaceIndex = 0;
00345:  	unsigned int dwFaceCount = 0;
00346:  	unsigned int dwCurSubset = 0;
00347:  
00348:  	//�@�f�B���N�g����؂���
00349:  	strcpy( m_directoryPath, GetDirectoryPath( filename ) );
00350:  
00351:  	//�@�t�@�C�����J��
00352:  	file.open( filename, ios::in );
00353:  
00354:  	//�@�`�F�b�N
00355:  	if ( !file.is_open() )
00356:  	{
00357:  		cerr << "Error : �t�@�C���I�[�v���Ɏ��s\n";
00358:  		cerr << "File Name : " << filename << endl;
00359:  		return false;
00360:  	}
00361:  
00362:  	//�@���[�v
00363:  	for( ;; )
00364:  	{
00365:  		file >> buf;
00366:  		if ( !file )
00367:  			break;
00368:  
00369:  		//�@�R�����g
00370:  		if ( 0 == strcmp( buf, "#" ) )
00371:  		{
00372:  		}
00373:  
00374:  		//�@���_���W
00375:  		else if ( 0 == strcmp( buf, "v" ) )
00376:  		{
00377:  			float x, y, z;
00378:  			file >> x >> y >> z;
00379:  			OBJVEC3 v( x, y, z );
00380:  			positions.push_back( v );
00381:  
00382:  			//�@�o�E���f�B���O�{�b�N�X�̏�����
00383:  			if ( !initBox )
00384:  			{
00385:  				m_Box = OBJBOUNDINGBOX( v );
00386:  				initBox = true;
00387:  			}
00388:  
00389:  			//�@�o�E���f�B���O�{�b�N�X�̎Z�o
00390:  			m_Box.Merge( v );
00391:  		}
00392:  
00393:  		//�@�e�N�X�`�����W
00394:  		else if ( 0 == strcmp( buf, "vt" ) )
00395:  		{
00396:  			float u, v;
00397:  			file >> u >> v;
00398:  			texcoords.push_back( OBJVEC2( u, v ) );
00399:  		}
00400:  
00401:  		//�@�@���x�N�g��
00402:  		else if ( 0 == strcmp( buf, "vn" ) )
00403:  		{
00404:  			float x, y, z;
00405:  			file >> x >> y >> z;
00406:  			normals.push_back( OBJVEC3( x, y, z) );
00407:  		}
00408:  
00409:  		//�@��
00410:  		else if ( 0 == strcmp( buf, "f" ) )
00411:  		{
00412:  			unsigned int iPosition, iTexCoord, iNormal;
00413:  			unsigned int p[4] = {-1}, t[4] = {-1}, n[4] = {-1};	
00414:  			OBJVERTEX vertex;
00415:  			dwFaceIndex++;
00416:  			dwFaceCount++;
00417:  			int count = 0;
00418:  			unsigned int index = 0;
00419:  		
00420:  			//�@�O�p�`�E�l�p�`�̂ݑΉ�
00421:  			for ( int iFace = 0; iFace < 4; iFace++ )
00422:  			{
00423:  				count++;	//�@���_���𐔂���
00424:  				ZeroMemory( &vertex, sizeof( OBJVERTEX ) );
00425:  
00426:  				file >> iPosition;
00427:  				vertex.position = positions[ iPosition - 1 ];
00428:  				p[iFace] = iPosition -1;
00429:  				
00430:  				if ( '/' == file.peek() )
00431:  				{
00432:  					file.ignore();
00433:  
00434:  					//�@�e�N�X�`�����W�C���f�b�N�X
00435:  					if ( '/' != file.peek() )
00436:  					{
00437:  						file >> iTexCoord;
00438:  						vertex.texcoord = texcoords[ iTexCoord - 1 ];
00439:  						t[iFace] = iTexCoord -1;
00440:  					}
00441:  
00442:  					//�@�@���x�N�g���C���f�b�N�X
00443:  					if ( '/' == file.peek() )
00444:  					{
00445:  						file.ignore();
00446:  
00447:  						file >> iNormal;
00448:  						vertex.normal = normals[ iNormal - 1 ];
00449:  						n[iFace] = iNormal -1;
00450:  					}
00451:  				}
00452:  
00453:  				//�@�J�E���g��3����
00454:  				if ( iFace < 3 )
00455:  				{
00456:  					t_vertices.push_back( vertex );
00457:  					index = t_vertices.size()-1;
00458:  					t_indices.push_back( index );
00459:  				}
00460:  
00461:  				//�@�������s��������I��
00462:  				if ( '\n' == file.peek() )
00463:  				{
00464:  					break;
00465:  				}
00466:  
00467:  			}
00468:  
00469:  			//�@�l�p�`�|���S���̏ꍇ�C�O�p�`��lj�����
00470:  			if ( count > 3 )
00471:  			{
00472:  				//�@�J�E���g
00473:  				dwFaceIndex++;
00474:  				dwFaceCount++;
00475:  
00476:  				//�@���_�ƃC���f�b�N�X��lj�
00477:  				for ( int iFace = 1; iFace < 4; iFace++ )
00478:  				{
00479:  					int j = (iFace+1)%4;
00480:  					ZeroMemory( &vertex, sizeof( OBJVERTEX ) );
00481:  
00482:  					if ( p[j] != -1 ) vertex.position = positions[ p[j] ];
00483:  					if ( t[j] != -1 ) vertex.texcoord = texcoords[ t[j] ];
00484:  					if ( n[j] != -1 ) vertex.normal = normals[ n[j] ];
00485:  
00486:  					t_vertices.push_back( vertex );
00487:  					index = t_vertices.size() - 1;
00488:  					t_indices.push_back( index );
00489:  				}
00490:  
00491:  			}
00492:  		}
00493:  
�����āCMTL�t�@�C���̓ǂݍ��݂ł���LoadMTLFile()�֐��ɂčs���܂��B
00541:  //-----------------------------------------------------------------------
00542:  // Name : LoadMTLFile()
00543:  // Desc : MTL�t�@�C���̓ǂݍ���
00544:  //-----------------------------------------------------------------------
00545:  bool OBJMESH::LoadMTLFile( const char* filename )
00546:  {
00547:  	char buf[OBJ_BUFFER_LENGTH] = {0};
00548:  	int iMtlCount = -1;
00549:  	ifstream file;
00550:  	vector<OBJMATERIAL> t_materials;
00551:  	OBJMATERIAL material;
00552:  	InitMaterial( &material );
00553:  
00554:  	//�@�t�@�C�����J��
00555:  	file.open( filename, ios::in );
00556:  
00557:  	//�@�`�F�b�N
00558:  	if ( !file.is_open() )
00559:  	{
00560:  		cerr << "Error : �t�@�C���I�[�v���Ɏ��s���܂���\n";
00561:  		cerr << "File Name : " << filename << endl;
00562:  		return false;
00563:  	}
00564:  
00565:  	//�@���[�v
00566:  	for( ;; )
00567:  	{
00568:  		file >> buf;
00569:  		if ( !file )
00570:  			break;
00571:  
00572:  		// New Material
00573:  		if ( 0 == strcmp( buf, "newmtl" ) )
00574:  		{
00575:  			iMtlCount++;
00576:  			t_materials.push_back( material );
00577:  			char strName[OBJ_NAME_LENGTH] = {0};
00578:  			file >> strName;
00579:  			strcpy( t_materials[iMtlCount].name, strName );
00580:  		}
00581:  		// Ambient Color
00582:  		else if ( 0 == strcmp( buf, "Ka" ) )
00583:  		{
00584:  			float r, g, b;
00585:  			file >> r >> g >> b;
00586:  			t_materials[iMtlCount].ambient = OBJVEC3( r, g, b );
00587:  		}
00588:  		// Diffuse Color
00589:  		else if ( 0 == strcmp( buf, "Kd" ) )
00590:  		{
00591:  			float r, g, b;
00592:  			file >> r >> g >> b;
00593:  			t_materials[iMtlCount].diffuse = OBJVEC3( r, g, b );
00594:  		}
00595:  		// Specular Color
00596:  		else if ( 0 == strcmp( buf, "Ks" ) )
00597:  		{
00598:  			float r, g, b;
00599:  			file >> r >> g >> b;
00600:  			t_materials[iMtlCount].specular = OBJVEC3( r, g, b );
00601:  		}
00602:  		// Alpha
00603:  		else if ( 0 == strcmp( buf, "d" ) ||
00604:  				  0 == strcmp( buf, "Tr" ) )
00605:  		{
00606:  			file >> t_materials[iMtlCount].alpha;
00607:  		}
00608:  		// Shininess
00609:  		else if ( 0 == strcmp( buf, "Ns" ) )
00610:  		{
00611:  			file >> t_materials[iMtlCount].shininess;
00612:  		}
00613:  		// Ambient Map
00614:  		else if ( 0 == strcmp( buf, "map_Ka" ) )
00615:  		{
00616:  			char mapKaName[OBJ_NAME_LENGTH];
00617:  			file >> mapKaName;
00618:  			SetDirectoryPath( mapKaName, m_directoryPath );
00619:  			strcpy( t_materials[iMtlCount].ambientMapName, mapKaName );
00620:  		}
00621:  		// Diffuse Map
00622:  		else if ( 0 == strcmp( buf, "map_Kd" ) )
00623:  		{
00624:  			char mapKdName[OBJ_NAME_LENGTH];
00625:  			file >> mapKdName;
00626:  			SetDirectoryPath( mapKdName, m_directoryPath );
00627:  			strcpy( t_materials[iMtlCount].diffuseMapName, mapKdName );
00628:  		}
00629:  		// Specular Map
00630:  		else if ( 0 == strcmp( buf, "map_Ks" ) )
00631:  		{
00632:  			char mapKsName[OBJ_NAME_LENGTH];
00633:  			file >> mapKsName;
00634:  			SetDirectoryPath( mapKsName, m_directoryPath );
00635:  			strcpy( t_materials[iMtlCount].specularMapName, mapKsName );
00636:  		}
00637:  		// Bump Map
00638:  		else if ( 0 == strcmp( buf, "map_Bump" ) )
00639:  		{
00640:  			char mapBumpName[OBJ_NAME_LENGTH];
00641:  			file >> mapBumpName;
00642:  			SetDirectoryPath( mapBumpName, m_directoryPath );
00643:  			strcpy( t_materials[iMtlCount].bumpMapName, mapBumpName );
00644:  		}
00645:  
00646:  		file.ignore( OBJ_BUFFER_LENGTH, '\n' );
00647:  	}
00648:  
00649:  	//�@�t�@�C����‚���
00650:  	file.close();
00651:  
00652:  	//�@�}�e���A���f�[�^���R�s�[
00653:  	m_NumMaterials = t_materials.size();
00654:  	m_Materials = new OBJMATERIAL[ m_NumMaterials ];
00655:  	for ( unsigned int i = 0; i<m_NumMaterials; i++ )
00656:  		m_Materials[i] = t_materials[i];
00657:  
00658:  	//�@����I��
00659:  	return true;
00660:  }
00661:  
��Ŏ������C�����֐��ł���LoadOBJFile()�֐���LoadMTLFile()�֐��́Cpublic�Ő錾����Ă���LoadFile()�֐����ŌĂяo�������悤�ɂȂ��Ă��܂��B
00662:  //-----------------------------------------------------------------------
00663:  // Name : LoadFile()
00664:  // Desc : ���b�V���t�@�C���̓ǂݍ���
00665:  //-----------------------------------------------------------------------
00666:  bool OBJMESH::LoadFile( const char* filename )
00667:  {
00668:  	//�@OBJ, MTL�t�@�C����ǂݍ���
00669:  	if ( !LoadOBJFile( filename ) )
00670:  	{
00671:  		cerr << "Error : ���b�V���t�@�C���̓ǂݍ��݂Ɏ��s���܂���\n";
00672:  		return false;
00673:  	}
00674:  
00675:  	//�@����I��
00676:  	return true;
00677:  }
00678:  
00679:  	
���͕`��֐��Ȃ�ł����C�C���f�b�N�X�o�b�t�@���g���ĕ`�悷��悤�ɂȂ��Ă��܂��B�p�t�H�[�}���X���C�ɂȂ���͒��_�o�b�t�@�I�u�W�F�N�g�iVBO�j�Ƃ��g���Ƃ�낵�����Ǝv���܂��B
00680:  //-----------------------------------------------------------------------
00681:  // Name : Draw()
00682:  // Desc : �`�揈��
00683:  //-----------------------------------------------------------------------
00684:  void OBJMESH::Draw()
00685:  {
00686:  	for ( unsigned int i = 0; i<m_NumSubsets; i++ )
00687:  	{
00688:  		//�@�}�e���A��
00689:  		OBJMATERIAL* pMat = &m_Materials[m_Subsets[i].materialIndex];
00690:  		SetMaterial( pMat );	
00691:  
00692:  		//�@�O�p�`�`��
00693:  		glInterleavedArrays( GL_T2F_N3F_V3F, 0, m_Vertices );
00694:  		glDrawElements( GL_TRIANGLES, m_Subsets[i].faceCount, GL_UNSIGNED_INT, &m_Indices[m_Subsets[i].faceStart] );
00695:  	}
00696:  }
00697:  


���@4�D�\�����Ă݂�
�쐬�������[�_�[���g���āC�\�����Ă݂܂��B
�܂��́C�w�b�_�[�t�@�C�����C���N���[�h���Ă����܂��B
00011:  #pragma region Includes
00012:  //
00013:  // Includes
00014:  //
00015:  #include <iostream>
00016:  #include <cmath>
00017:  #include <GL/glut.h>
00018:  #include "Mouse.h"
00019:  #include "OBJLoader.h"
00020:  #pragma endregion
00021:  
�����āCOBJMESH�N���X��錾���Ă����܂��B
00029:  #pragma endregion Global Variables
00030:  //
00031:  // Global Variables
00032:  //

�c�i�ȗ��j�c

00040:  OBJMESH mesh;

LoadFile()�֐����Ăяo���āC�t�@�C���̃��[�h���s���܂��B�����ɂ́C�t�@�C�������w�肵�Ă����܂��B
00109:  //----------------------------------------------------------------------------------------------------
00110:  // Name : Initialize()
00111:  // Desc : ����������
00112:  //----------------------------------------------------------------------------------------------------
00113:  void Initialize()
00114:  {

�c�@�i�ȗ��j�@�c

00127:  	//�@���b�V�������[�h
00128:  	if ( !mesh.LoadFile( "Mesh/test2.obj" ) )
00129:  		Shutdown();
00131:  }
00132:  
�`��́C���ʂ�Display()�֐�����Draw()�֐����Ăяo�������ł��B
00155:  //---------------------------------------------------------------------------------------------------
00156:  // Name : Display()
00157:  // Desc : �E�B���h�E�ւ̕`��
00158:  //---------------------------------------------------------------------------------------------------
00159:  void Display()
00160:  {

�c�@�i�ȗ��j�@�c

00182:  	//�@���b�V����`��
00183:  	mesh.Draw();

�c�@�i�ȗ��j�@�c

00195:  	//�@�_�u���o�b�t�@
00196:  	glutSwapBuffers();
00197:  
00198:  #if defined(DEBUG) || defined(_DEBUG)
00199:  	glutReportErrors();
00200:  #endif
00201:  }
00202:  
�Ƃ肠�����C����Ń��b�V�����\���ł���悤�ɂȂ�܂����B
�\�����x���C�ɂȂ���̓f�B�X�v���C���X�g�Ƃ�VBO(Vertexbuffer Object)�Ƃ��Ō����悭�Ȃ�悤�ɂ������Ă݂Ă��������B


���@Download
�{�\�[�X�R�[�h����уv���O�������g�p�������Ƃɂ��@���Ȃ鑹�Q������҂͐ӔC�𕉂��܂���B
�{�\�[�X�R�[�h����уv���O�����͎��ȐӔC�ł��g�p���������B
�v���O�����̍쐬�ɂ�Visual Studio 2008 Professional��p���Ă��܂��B