Skip to main content
gamedev.net
🔒 Locked

Cool mesh Method...

Started by devronious Dec 15, 2005 at 4:19 PM 5 replies 750+ views
Original Post
devronious
devronious
Ok, I've been playing around with this to understand how my lighting is working and I decided to post it cause when you set bump to true and give it a magnitude it's kinda cool. Anyways thought someone might enjoy it. It could be the start of a cool terrain mesh. Give it a rainbow texture and it's kinda cool. typical call is: CreatePlatform(200,50,0,true,10);
[source lang=c#]

        public Mesh CreatePlatform(int HalfWidth, int NumberOfSegments, float BaseElevation, bool Bump, float BumpMag)
        {
            int numverts = (NumberOfSegments + 1) * (NumberOfSegments + 1);//21x by 21y two dimensional array represented by a one dimensional array
            float elev = BaseElevation;
            float step = (HalfWidth * 2) / NumberOfSegments;
            Random rnd = new Random();

            //create one long array for 2 dimensional array representation
            CustomVertex.PositionNormalTextured[] verts = new CustomVertex.PositionNormalTextured[numverts];
            for (float i = -HalfWidth; i <= HalfWidth; i += step)
                for (float j = -HalfWidth; j <= HalfWidth; j += step)
                {
                    if (Bump) elev = BaseElevation + (float)(rnd.NextDouble() * BumpMag);
                    int ind = Convert.ToUInt16((((i + HalfWidth) / step) * (NumberOfSegments + 1)) + ((j + HalfWidth) / step));
                    verts[ind] =
                        new CustomVertex.PositionNormalTextured((float)i, (float)elev, (float)j, 0f, 1f, 0f,
                        (float)((j + HalfWidth) / (HalfWidth * 2)), (float)((i + HalfWidth) / (HalfWidth * 2)));
                }


            short[] indices = new short[NumberOfSegments * NumberOfSegments * 2 * 3];//20x20 grid with 2 triangles per grid square and 3 vertices per triangle.
            int c = 0;
            for (int i = 0; i < NumberOfSegments; i++)
                for (int j = 0; j < NumberOfSegments; j++)
                {
                    //first triangle
                    indices[c] = (short)((i * (NumberOfSegments + 1)) + j);//first this row
                    indices[c + 1] = (short)(((i + 1) * (NumberOfSegments + 1)) + j);//first next row 
                    indices[c + 2] = (short)(((i + 1) * (NumberOfSegments + 1)) + j + 1);//second next row 

                    //second triangle
                    indices[c + 3] = (short)((i * (NumberOfSegments + 1)) + j);//first this row 
                    indices[c + 4] = (short)(((i + 1) * (NumberOfSegments + 1)) + j + 1);//second next row 
                    indices[c + 5] = (short)((i * (NumberOfSegments + 1)) + j + 1);//second first row 

                    //advance c
                    c += 6;
                }

            Mesh platform = new Mesh((NumberOfSegments * NumberOfSegments * 2), numverts, MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, device);

            using (VertexBuffer vb = platform.VertexBuffer)
            {
                GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
                data.Write(verts);
                vb.Unlock();
            }


            using (IndexBuffer ib = platform.IndexBuffer)
            {
                ib.SetData(indices, 0, LockFlags.None);
            }

            platform.ComputeNormals();
            return platform;


        }

jollyjeffers
jollyjeffers
Any chance of a screenshot(s)?

I unfortunately don't have the time to go and run random fragments of code from the forums [smile]

Cheers,
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
devronious
devronious
Let me work on that, I just added a couple args to allow for hilltop effect. Not much use prob but for what I'm doing.
devronious
devronious
OK here's a quick image without any effects or mesh normal tangency added. Which I need help on btw...

Platform mesh with HillTop enabled

Link to Image
jollyjeffers
jollyjeffers
Quote:
Original post by devronious
OK here's a quick image without any effects or mesh normal tangency added. Which I need help on btw...

Feel free to ask - people round here are usually happy to help out [smile]

Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
devronious
devronious
Finally got the Sprite thing down....


Emitter

ImageLink


I did a blending between subdivisions for a 9x9 subdivided texture on the clouds. It kinda blends from one texture to the next. Makes for a cool atmospheric look.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.