Point cloud not displaying

Jerdak

18-06-2012 17:10:57

The code at the end of my post is used to load a point cloud from an OBJ file. The code itself works on several of my development machines but I have 4 laptops (identical dell pavilions) on which I can't seem to get the point cloud to display.

I'm positive the data I am loading is valid, it's the test cloud I used on my development machines without problem. It's starting to look like this is more of a driver issue than a M/ogre problem but I would appreciate any help.

class PointCloud
{
protected Mogre.HardwareVertexBufferSharedPtr vertex_buffer_;
protected Mogre.HardwareVertexBufferSharedPtr color_buffer_;

public List<Vector3> Vertices = null;
public Vector3 Min { get; set; }
public Vector3 Max { get; set; }
public Vector3 Centroid { get; set; }
public SceneNode OgreSceneNode { get; set; }
public SceneNode OgreViewNode { get; set; }
public String UniqueIdentifier { get; set; }
public List<Mogre.ColourValue> Colors = null;
public Mesh OgreMesh { get; set; }
public SceneManager Manager { get; set; }
public string MaterialName { get; set; }
public string FileName { get; set;}
public string Name { get; set; }

public PointCloud()
{

}

public Boolean BuildOgreMesh(String name)
{
Name = name;

Console.WriteLine("Vertices: " + Vertices.Count.ToString());

MeshPtr mesh = null;
SubMesh submesh = null;

try
{
mesh = MeshManager.Singleton.CreateManual(name + "_manual_mesh", Mogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
submesh = mesh.CreateSubMesh(name + "_manual_sub");
}
catch (Exception ex)
{
Console.WriteLine("Error building obj: " + ex.Message + "," + ex.StackTrace);
}


//Create OgreMesh
try
{
submesh.useSharedVertices = false;
submesh.vertexData = new VertexData();
submesh.vertexData.vertexStart = 0;
submesh.vertexData.vertexCount = (uint)Vertices.Count + 1;
VertexDeclaration dec = submesh.vertexData.vertexDeclaration;
ushort source = 0;
ushort csource = 1;
uint offset = 0;

offset += dec.AddElement(source, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION).Size;

vertex_buffer_ = HardwareBufferManager.Singleton.CreateVertexBuffer(dec.GetVertexSize(source),
submesh.vertexData.vertexCount,
HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);
///Not sure how to do this w/o using the unsafe catch.
unsafe
{
float* vdata = (float*)vertex_buffer_.Lock(HardwareBuffer.LockOptions.HBL_DISCARD);

AxisAlignedBox aabox = new AxisAlignedBox();

Min = Max = Vertices[0];
Centroid = Vector3.ZERO;


for (int i = 0; i < Vertices.Count; i++)
{

Vector3 pos = Vertices;
Vector3 tmpMax = Max;
Vector3 tmpMin = Min;

if (Max.x < Vertices.x) tmpMax.x = Vertices.x;
if (Min.x > Vertices.x) tmpMin.x = Vertices.x;

if (Max.y < Vertices.y) tmpMax.y = Vertices.y;
if (Min.y > Vertices.y) tmpMin.y = Vertices.y;

if (Max.z < Vertices.z) tmpMax.z = Vertices.z;
if (Min.z > Vertices.z) tmpMin.z = Vertices.z;

Max = tmpMax;
Min = tmpMin;
Centroid += pos;
}
Centroid /= Vertices.Count;
Console.WriteLine("Min: " + Min.ToString());
Console.WriteLine("Max: " + Max.ToString());

//Fill vertex array
for (int i = 0; i < Vertices.Count; i++)
{
Vector3 pos = Vertices;
if (i <= 10)
{
Console.WriteLine("Vertex: " + Vertices.ToString());
}
*vdata++ = pos.x;
*vdata++ = pos.y;
*vdata++ = pos.z;
aabox.Merge(pos);
}

vertex_buffer_.Unlock();


submesh.vertexData.vertexBufferBinding.SetBinding(source, vertex_buffer_);
submesh.operationType = RenderOperation.OperationTypes.OT_POINT_LIST;

/*
//Color, disabled for now.
if(_perFaceColor != NULL){
printf(" - Assigning Color buffer...");
offset = 0;
offset += dec.AddElement(csource,offset,VertexElementType.VET_COLOUR,VertexElementSemantic.VES_DIFFUSE).Size;
vbuffer = HardwareBufferManager.Singleton.CreateVertexBuffer( offset,
submesh.vertexData.vertexCount,
HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);
printf("1...");

RenderSystem ^rs = Root.Singleton.RenderSystem;
unsigned int *cdata = static_cast<unsigned int*>(vbuffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD));
for(int i = 0; i < Faces.Count; i++){
int *idx = face.verts;
rs.ConvertColourValue(ColourValue(_perFaceColor.color.r/255.0,_perFaceColor.color.g/255.0,_perFaceColor.color.b/255.0),cdata[idx[0]]);
rs.ConvertColourValue(ColourValue(_perFaceColor.color.r/255.0,_perFaceColor.color.g/255.0,_perFaceColor.color.b/255.0),cdata[idx[1]]);
rs.ConvertColourValue(ColourValue(_perFaceColor.color.r/255.0,_perFaceColor.color.g/255.0,_perFaceColor.color.b/255.0),cdata[idx[2]]);
}
vbuffer.Unlock();
submesh.vertexData.vertexBufferBinding.SetBinding(csource,vbuffer);
printf("Complete.\n");
Mogre.LogManager.Singleton.LogMessage(" - Color buffer loaded");
} else {
Mogre.LogManager.Singleton.LogMessage(" - No color buffer to load");
}

printf(" - Create mesh boundaries...");*/
//aabox = new Mogre.AxisAlignedBox(new Mogre.Vector3(-100000,-100000,-100000),new Mogre.Vector3(+100000,+100000,+100000));
mesh._setBounds(aabox);
mesh._setBoundingSphereRadius((float)(aabox.Maximum - aabox.Minimum).Length / 2.0f);
}


submesh.SetMaterialName(MaterialName);
mesh.Load();
// Mogre.MeshSerializer s = new MeshSerializer();
// s.ExportMesh(mesh, "test.mesh");
{ //Create new entity and attach it to our object's node.
Entity ent = null;

try
{
ent = Manager.GetEntity(name + "_manual_ent");
}
catch (Exception ex)
{
ent = Manager.CreateEntity(name + "_manual_ent", name + "_manual_mesh");
Console.WriteLine("Building obj1: " + ex.Message + "," + ex.StackTrace);
}

try
{
ent.CastShadows = true;
OgreViewNode.AttachObject(ent);
}
catch (Exception ex)
{
Console.WriteLine("Error attaching obj to node: " + ex.Message + "," + ex.StackTrace);
throw;
}
}
UniqueIdentifier = name;
OgreMesh = mesh;
}
catch (Exception ex)
{
Console.WriteLine("Error building obj3: " + ex.Message + "," + ex.StackTrace);
return false;
}
return true;
}

public void UpdateVertexPositions(List<Vector3> new_vertices){
unsafe {
float* vdata = (float*)vertex_buffer_.Lock(HardwareBuffer.LockOptions.HBL_DISCARD);
//Fill vertex array
for (int i = 0; i < new_vertices.Count; i++)
{
Vector3 pos = new_vertices;

*vdata++ = pos.x;
*vdata++ = pos.y;
*vdata++ = pos.z;

}
vertex_buffer_.Unlock();
}
}
};

zarfius

19-06-2012 00:04:39

It's starting to look like this is more of a driver issue than a M/ogre problem but I would appreciate any help.
I suspect your right.

I'm curious though, why do you need to do this with hardware buffers? Have you considered using a ManualObject instead? You would not need to use the unsafe code block and the code would be simpler and easier to read. I have't studied your code in detail though, so fogive me if I've missed something.

Jerdak

19-06-2012 23:13:12

I'm curious though, why do you need to do this with hardware buffers?

The first tutorial I ever found for loading manual data used hardware buffers and I didn't know, until just yesterday, that manual objects afforded me the same capability for 1/100th of the difficulty. :D

So it turns out my problem was related to the material I'm using. I don't really know what the problem is, just the cause.

Here is my original material:

material PointCloud/Red
{
technique
{
pass
{
diffuse 0.5 0.0 0.0
specular 0.5 0.0 0.0
ambient 0.5 0.0 0.0
point_size 0.05
point_sprites on
point_size_attenuation on
}
}
}


I removed all the calls to point_#####. I'm not sure which call was the offender, I pulled them all just to see what would happen if I applied only a color. So my final material was this:

material PointCloud/Red
{
technique
{
pass
{
diffuse 0.5 0.0 0.0
specular 0.5 0.0 0.0
ambient 0.5 0.0 0.0
}
}
}


And that fixed the problems on my laptops.