Sign in to follow this  

DirectX .ase loader

This topic is 3842 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

Ive started to make a .ase loader for Direct3D. Please note that a lot of code is reduntand because i didnt finish it / polish it. Well the problem is that doesnt work..Ive tryed to manualy make some buffers, fill the vertices with the data but the same... I made a modification to the file to memorate how many object are in the file. Please dont laugh at the code. What Im doing wrong, what shoud i change / not use etc... ? ase_importer
#ifndef ASE_IMPORTER_H
#define ASE_IMPORTER_H


class ase_importer {
public:


	struct mesh_object {
		string name;
		int vertices;
		int faces;
		LPDIRECT3DINDEXBUFFER9 indice_buffer;
		LPDIRECT3DVERTEXBUFFER9 vertex_buffer;
        vector<int> indices;
		vector<Vertex> vertice;

	};



    vector<mesh_object> mesh;
	int scene_objects; //how many objects are in the file
	string ch; // file browser




	void loadFile(LPCWSTR file_name); //load a file :-)
	void RenderScene();




};




#endif

ase_importer.cpp
#include "stdafx.h"
#include "ase_importer.h"
#include "definitions.h"

void ase_importer::loadFile(LPCWSTR file_name) {



	fstream in(file_name ,std::ios_base::in);
	fstream out_fis(L"C:\\logs\\log.html", std::ios_base::out);



	//walk until scene objects is found
	while(ch!="*SCENE_OBJECTS"){
		in >> ch;
	}

	in >> scene_objects;
	out_fis << "Number of objects detected in the scene is <b>"<<scene_objects <<"</b>" << endl;

	mesh.resize(scene_objects);


	//LOOP WITHIN OBJECTS:::::::::::::::::::::::::::::::
	//
	out_fis << "<br> </br>" <<"STARTING LOOPING THRO OBJECTS";


	for(int obj_c = 0; obj_c < scene_objects; obj_c++) {


		//WALK UNTILL A OBJECT IS FOUND::::::::::::::::::::
		//
		while(ch!="*GEOMOBJECT"){
			in >> ch;
		}

		//after this part *GEOMOBJECT is selected in the browser buffer ch;


		//WALK UNTILL the OBJECT vertices number IS FOUND::::::::::::::::::::
		//

		while(ch!="*MESH_NUMVERTEX"){
			in >> ch;
		}
		int temp_vertex;
		in >> mesh[obj_c].vertices ;

		out_fis <<"<br> </br>";
		out_fis << "<u>" << "mesh[" << obj_c <<"] vertices = " << mesh[obj_c].vertices << "</u>" ;//inpur


		while(ch!="*MESH_NUMFACES"){
			in >> ch;

		}

		int temp_face;
		in >> mesh[obj_c].faces;

		out_fis <<"<br> </br>";
		out_fis <<  "<u>"<< "Object[" << obj_c <<"] faces = " << mesh[obj_c].faces << "</u>" ;//

		mesh[obj_c].vertice.resize(mesh[obj_c].vertices);


		//WALK AND MEMORATE ALL VERTICES FROM THE OBJECT;
		out_fis <<"<br> </br>";
		out_fis <<"<br> </br>";
		out_fis <<"<br> </br>";//





		for(int vert=0; vert < mesh[obj_c].vertices ;vert++) {             //START 

			while (ch!="*MESH_VERTEX")
			{
				in >> ch;

		 }
			in >> ch;
			out_fis << "Object:" << obj_c << "<b> vertex " << vert << "</b>" ;
			out_fis <<"<br> </br>";

			float c;


			in >> c;
			mesh[obj_c].vertice[vert].x = c;
			out_fis << "["<<  mesh[obj_c].vertice[vert].x << "  ][  ";
			c = NULL;

			in >> c;
			mesh[obj_c].vertice[vert].y = c;
			out_fis <<   mesh[obj_c].vertice[vert].y << "  ][  ";
			c = NULL;

			in >> c;
			mesh[obj_c].vertice[vert].z = c;
			out_fis <<   mesh[obj_c].vertice[vert].z << " ]";
			c = NULL;
			out_fis <<"<br> </br>";
		}                                                                   //END



		//WALK AND MEMORATE INDICES


		mesh[obj_c].indices.resize(mesh[obj_c].faces * 3);


		for(int indice=0;indice < mesh[obj_c].faces ;indice++) {


			string checker;
			stringstream ss;
			ss.clear();

			checker = "";
			ss << indice << ":";
			ss >> checker;


			while (ch!=checker) {
				in >> ch;
			}
			in >> ch;


			in >> mesh[obj_c].indices[indice];
			out_fis << "INDICES" << obj_c << " " << mesh[obj_c].indices[indice] << ",";


		}



		for (int buf=0 ; buf < scene_objects ; buf++) {

			///////////////////////////////////////////////////////////////////////////////////////
			//VERTECSESSSDSDSDFSDF
			///////////////////////////////////////////////////////////////////////////////////////

			g_pd3dDevice->CreateVertexBuffer( 24*sizeof(Vertex),
				D3DUSAGE_WRITEONLY,
				D3DFVF_MY_VERTEX,
				D3DPOOL_DEFAULT,
				&mesh[buf].vertex_buffer,
				NULL );


			void *pVertices = NULL; 
			mesh[buf].vertex_buffer->Lock( 0, sizeof(mesh[buf].vertice), (void**)&pVertices, 0 );
			memcpy( pVertices, &mesh[buf].vertice, sizeof(mesh[buf].vertice) );
			mesh[buf].vertex_buffer->Unlock();



			///////////////////////////////////////////////////////////////////////////////////////
			//INDICES
			///////////////////////////////////////////////////////////////////////////////////////




			g_pd3dDevice->CreateIndexBuffer( 24*sizeof(WORD),
				D3DUSAGE_WRITEONLY,
				D3DFMT_INDEX16,
				D3DPOOL_DEFAULT,
				&mesh[buf].indice_buffer,
				NULL);
			WORD *pIndices = NULL;

			mesh[buf].indice_buffer->Lock( 0, sizeof(mesh[buf].indices), (void**)&pIndices, 0 );
			memcpy( pIndices, &mesh[buf].indices, sizeof(mesh[buf].indices) );
			mesh[buf].indice_buffer->Unlock();




		}




		//END OBJECT LOOPING
	}
	//END OBJECT LOOPING

}



void ase_importer::RenderScene() {

	for(int obj_c = 0 ; obj_c < 4 ; obj_c++) {

		g_pd3dDevice->SetIndices(mesh[obj_c].indice_buffer);
		g_pd3dDevice->SetStreamSource(0, mesh[obj_c].vertex_buffer, 0, sizeof(Vertex)*3);
		g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 12,  0, 4 );

	}
}



Thanks..

Share this post


Link to post
Share on other sites

This topic is 3842 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this