Basics of Generated Geometry

rated by 0 users
This post has 11 Replies | 3 Followers

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee
Malkindy Posted: 09-29-2010 7:40 AM

Hello, I've been searching through the forums and experimenting with various aspects of Sunburn. I've read a few posts on using generated geometry for terrains.  Unfortunately, I have failed miserably at rendering generated geometry.   I figured I would start out with the basics and render 3d primitives using sunburn so I endeavored to modify the XNA 3d primitives sample from the creators website.  I was trying to make a MeshData in order to then create a SceneObject.  Some guidance would be greatly appreciated as I've been wrestling with this for some time.  in fact I would really appreciate if someone could show me how to render in Sunburn using a vertexbuffer and indexbuffer.

Top 25 Contributor
Posts 201
SunBurn_Community_Licensee
SunBurn_Contributor
SunBurn_Pro_Licensee

One of the constructors for SceneObject will allow you to just pass in your vertex buffer and index buffer...

Construct your vertex buffer using Sunburn's VertexPositionNormalTextureBump vertex type - to get started you can miss out the Tangent element (although if i remember right there's a utility function to calculate it for you somewhere).

Then create the sceneobject using this constructor

public SceneObject(
	string name,
	Effect effect,
	BoundingSphere objectspaceboundingsphere,
	Matrix objectspace,
	IndexBuffer indexbuffer,
	VertexBuffer vertexbuffer,
	VertexDeclaration vertexdeclaration,
	int indexstart,
	PrimitiveType primitivetype,
	int primitivecount,
	int vertexbase,
	int vertexcount,
	int vertexstreamoffset,
	int vertexstride
)

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

Thanks bamyazi,

I managed to get some of it to work.  Using the code from the primitives example on the cc website, I created a cube.  I then fed the vertexbuffer, indexbuffer as well as other required variables to the sceneobject constructor, and submitted it to the ObjectManager

BoundingSphere bs = new BoundingSphere();

SceneObject so = new SceneObject("cube",basicEffect, bs, mat, cube.vertexBuffer, cube.vertexDeclaration, PrimitiveType.TriangleList, cube.indices.Count / 3, 0, 0, VertexPositionNormalTextureBump.SizeInBytes);

so.CalculateBounds();

sceneInterface.ObjectManager.Submit(so);

The following problem occurs.  Considering that each face of the cube is made up of two triangles.  Only one triangle is rendered per surface.  (So for six sides I'm only getting six triangles). 

Is this a problem related to the winding order?  Or have I missed something somewhere.

Thanks

Top 25 Contributor
Posts 201
SunBurn_Community_Licensee
SunBurn_Contributor
SunBurn_Pro_Licensee

Sounds like a winding order issue...set RenderState.CullMode to none - if you then see both triangles then thats your problem - just reorder the vertices.

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

bamyazi:

Sounds like a winding order issue...set RenderState.CullMode to none - if you then see both triangles then thats your problem - just reorder the vertices.

I tried what you suggested bamyazi.  Unfortunately, my result wasn't any different.

 

This is the code i'm using to build the cube:

 

        private void BuildCube(float size)

        {

            // A cube has six faces, each one pointing in a different direction.

            Vector3[] normals =

            {

                new Vector3(0, 0, 1),

                new Vector3(0, 0, -1),

                new Vector3(1, 0, 0),

                new Vector3(-1, 0, 0),

                new Vector3(0, 1, 0),

                new Vector3(0, -1, 0),

            };

 

            // Create each face in turn.

            foreach (Vector3 normal in normals)

            {

                // Get two vectors perpendicular to the face normal and to each other.

                Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);

                Vector3 side2 = Vector3.Cross(normal, side1);              

                // Six indices (two triangles) per face.

                AddIndex(CurrentVertexCount + 0);

                AddIndex(CurrentVertexCount + 1);

                AddIndex(CurrentVertexCount + 2);                

                AddIndex(CurrentVertexCount + 0);

                AddIndex(CurrentVertexCount + 2);

                AddIndex(CurrentVertexCount + 3);          

 

                // Four vertices per face.

                AddVertex((normal - side1 - side2) * size / 2, normal);                

                AddVertex((normal - side1 + side2) * size / 2, normal);               

                AddVertex((normal + side1 + side2) * size / 2, normal);                

                AddVertex((normal + side1 - side2) * size / 2, normal);

            }   

        }

I have a feeling I've missed something silly but I can't quite put my finger on it.

Thanks again.  

 

Top 10 Contributor
Posts 337
SunBurn_Community_Licensee
SunBurn_Pro_Licensee

a cube has 8 vectors :)

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

@dug  the above listed code generates a cube just fine for a DrawIndexedPrimitives call.  

Top 10 Contributor
Posts 4,854
Employee
SunBurn_Studio_Licensee

Hi Malkindy,

Malkindy:
SceneObject so = new SceneObject("cube",basicEffect, bs, mat, cube.vertexBuffer, cube.vertexDeclaration, PrimitiveType.TriangleList, cube.indices.Count / 3, 0, 0, VertexPositionNormalTextureBump.SizeInBytes);

It doesn't look like the overload you're using passes in the index buffer.  Without it the device will try to render the vertices as if they are packed six to a side (with every set of three defining a triangle - where as with the indices your code packs only four per side).

Let me know if this helps!

Follow me on Twitter – development and personal tweets
Awesome XNA Videos – Lighting, Rendering, and game videos

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

@bob  Thanks that fixed it.  I hand't realized that I didn't put the index buffer in.  A few more tests then I can move on to bigger better things.

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

So much for bigger better things :(.  I can't get my cube to light up.  When it's rendered with vanilla XNA it lights fine.  I am doing the following for Sunburn:

  • Fill a VertexPositionNormalTextureBump array withe new VertexPositionNormalTextureBump vertices.
    • I populate the vertices
      • vertexPosNormTextBump.Normal = normal;
      • vertexPosNormTextBump.Position = vertex;
      • vertexPosNormTextBump.TextureCoordinate = texCoord;
  • After the array has been build I use VertexPositionNormalTextureBump.BuildTangentSpaceDataForTriangleList(indices, vertices); to generate the Tangent and Binormal data.

Questions:  Do I need to assign a material or texture to actually get it to light?  Because I'm currently not assigning anything.

Is there something I must enable somewhere in order to switch on the lighting for custom geometry?  

I am using the SimpleIntegration template.

Thanks ... I'm just trying to evolve from noobness.

Top 10 Contributor
Posts 1,214
SunBurn_Community_Licensee
SunBurn_Contributor
SunBurn_Pro_Licensee

As soon as you create your RenderableMesh with your custom geometry, make sure to assign a LightingEffect or DeferredObjectEffect (for deferred rendering) so that it gets proper lighting.

Your custom geometry requires a shader to be rendered and those above are the SunBurn built in ones ;)

Top 150 Contributor
Posts 15
SunBurn_Community_Licensee

@Da Silva thanks, lighting is now enabled.  I was using a Basic Effect before.

I really appreciate everyones help .... great community.

Page 1 of 1 (12 items) | RSS