Ray-triangle intersection

Started by
31 comments, last by _Sauce_ 14 years, 1 month ago
Post code!
Advertisement
Quote:Original post by szecs
Post code!

tomorrow! :)
Okay so I've just realised I've got some important uni work that needs doing, so I won't have time to rewrite the ply code at all. It's a beast and would take me all day, so here's the current ply loading code. If there's any unexplained functions in that code, then you'll find them in ply.h and ply.c from http://www.cc.gatech.edu/projects/large_models/ply.html

bool RTMesh::loadPlyFile(const std::tstring &path){	///////////////////////////////////////////////////////////////////	//Example source for loading .ply files:	//http://www.cc.gatech.edu/projects/large_models/ply.html	//	//Documentation:	//http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/plydocs.txt	///////////////////////////////////////////////////////////////////	FILE *file = fopen(path.c_str(), _T("r"));	if(file)	{		int nverts, nfaces;		Vertex **vlist;		Face **flist;		PlyOtherProp *vert_other, *face_other;		/* are normals in PLY file? */		int has_nx = 0;		int has_ny = 0;		int has_nz = 0;		int i,j;		int elem_count;		char *elem_name;		PlyFile *in_ply = read_ply(file);		/* examine each element type that is in the file (vertex, face) */		for (i = 0; i < in_ply->num_elem_types; i++)		{			/* prepare to read the i'th list of elements */			elem_name = setup_element_read_ply (in_ply, i, &elem_count);			if (equal_strings ("vertex", elem_name))			{				/* create a vertex list to hold all the vertices */				vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);				nverts = elem_count;				/* set up for getting vertex elements */				/* (we want x,y,z) */				setup_property_ply (in_ply, &vert_props[0]);				setup_property_ply (in_ply, &vert_props[1]);				setup_property_ply (in_ply, &vert_props[2]);				/* we also want normal information if it is there (nx,ny,nz) */				for (j = 0; j < in_ply->elems->nprops; j++)				{					PlyProperty *prop;					prop = in_ply->elems->props[j];					if (equal_strings ("nx", prop->name))					{						setup_property_ply (in_ply, &vert_props[3]);						has_nx = 1;					}					if (equal_strings ("ny", prop->name))					{						setup_property_ply (in_ply, &vert_props[4]);						has_ny = 1;					}					if (equal_strings ("nz", prop->name))					{						setup_property_ply (in_ply, &vert_props[5]);						has_nz = 1;					}				}				/* also grab anything else that we don't need to know about */				vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props));				/* grab the vertex elements and store them in our list */				for (j = 0; j < elem_count; j++)				{					vlist[j] = (Vertex *) malloc (sizeof (Vertex));					get_element_ply (in_ply, (void *) vlist[j]);				}			}			else if (equal_strings ("face", elem_name))			{				/* create a list to hold all the face elements */				flist = (Face **) malloc (sizeof (Face *) * elem_count);				nfaces = elem_count;				/* set up for getting face elements */				/* (all we need are vertex indices) */				setup_property_ply (in_ply, &face_props[0]);				face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props));				/* grab all the face elements and place them in our list */				for (j = 0; j < elem_count; j++)				{					flist[j] = (Face *) malloc (sizeof (Face));					get_element_ply (in_ply, (void *) flist[j]);				}			}			else  /* all non-vertex and non-face elements are grabbed here */			{				get_other_element_ply (in_ply);			}		}		close_ply(in_ply);		//This is where my code begins.		/////////////////////////////////////		//Take raw data and add tris to mesh.		/////////////////////////////////////		for(unsigned int i = 0; i < nfaces; ++i)		{			Face *face = flist;			int numVerts = face->nverts;			Vector3f vec[3];			//Fill three vertices with data from vlist.			//Only use the first 3 verts of a polygon - models MUST consist of triangles only for this to work.			for(unsigned int j = 0; j < 3 /*numVerts*/; ++j)			{				Vertex *vertex = vlist[face->verts[j]];				vec[j] = Vector3f(vertex->x, vertex->y, vertex->z);			}			//add the face to the mesh, reversing the winding order.			mTris.push_back(RTTriangle(vec[0], vec[2], vec[1], mMaterial));		}		return true;	}	return false;}

This topic is closed to new replies.

Advertisement