XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Help with Triangle creation

Last post 05-12-2008 5:02 PM by klawline. 6 replies.
  • 05-12-2008 2:21 PM

    Help with Triangle creation

    Hello all

    I have a problem trying to create a TriangleList by filling a VertexBuffer.  Currently, my xna game is using an extremely basic hlsl effect that doesn't do anything but get the color from the texture.  The triangles i am trying to render are randomly created and then each individual vertex is placed into an array to which i eventually set the vertex buffer data to.  All that works just fine.

    So now i want to try and create quads (to display the entire texture) instead of triangles, so for each triangle i've created a fourth point and reused the shared two points from before and added these additional triangles to the array of vertices. But doing this results in this exception: "InvalidOperationException" with this description: "The size of data being requested is too large for this vertex buffer."

    But i know it's not the actual number of elements in the array because i can place thousands of triangles into the array and it works fine.  But then with just a single quad i get that exception.  So then i thought that for some reason it was because i was reusing two points of every quad so i tried recreating the shared vertices with new instances of objects and i still get the same Exception.

    Any ideas?

    Thank you in advance!

  • 05-12-2008 2:44 PM In reply to

    Re: Help with Triangle creation

    You've made an error somewhere in your code. You'll have to post the code before anyone can guess what that might be.
    XNA Framework Developer - blog - homepage
  • 05-12-2008 3:40 PM In reply to

    Re: Help with Triangle creation

    Here is my code. i took out the portion of the code that creates the values of the Vector3 objects since i figured that stuff didn't matter.

    for (int i = 0; i < numPlanes; i++)
    {

        //p1, p2, p3, p4 are all instances of Vector3

        SHARenderedVertex v1 = new SHARenderedVertex(p1, new Vector2(0, 0));
        SHARenderedVertex v2 = new SHARenderedVertex(p2, new Vector2(1, 0));
        SHARenderedVertex v3 = new SHARenderedVertex(p3, new Vector2(1, 1));
        SHARenderedVertex v4 = new SHARenderedVertex(p4, new Vector2(0, 1));
        SHARenderedTriangle tempTriangle = new SHARenderedTriangle(v1, v2, v3, texture);

        triangles[triangleCounter++] = tempTriangle;
        
        //if i include the following two lines then i get the Exception
        SHARenderedTriangle tempTriangle2 = new SHARenderedTriangle(v1, v3, v4, texture);
        triangles[triangleCounter++] = tempTriangle2;

    }

    counter = 0;
    foreach (SHARenderedTriangle triangle in triangles)
    {
        triangleList[counter++] = triangle.vertices[0];
        triangleList[counter++] = triangle.vertices[1];
        triangleList[counter++] = triangle.vertices[2];
    }

  • 05-12-2008 4:05 PM In reply to

    Re: Help with Triangle creation

    Can I also see where you initialize this stuff, and where you draw it?
    XNA Framework Developer - blog - homepage
  • 05-12-2008 4:19 PM In reply to

    Re: Help with Triangle creation

    Thanks for taking the time out to look at this. Here is the complete method.  The code dealing with the vertex buffer is copied and edited from the XNA tutorials on www.riemers.net i've added various comments to try and explain what is going on or why the code is what it is.

    int numPlanes = 20;
    float size = 20;
    triangles = new SHARenderedTriangle[numPlanes];
    vb = new VertexBuffer(device, SHARenderedVertex.SizeInBytes * numPlanes * 3, ResourceUsage.WriteOnly);
    triangleList = new SHARenderedVertex[numPlanes * 3];
    Vector3 origin = new Vector3(-321.805f, 3.711f, 258.372f);
    Random random = new Random();
    int counter = 0;
    int triangleCounter = 0;
    for (int i = 0; i < numPlanes; i++)
    {
        //this is my attempt at making random right triangles, though i don't think it's actually working, but that currently is not my greatest concern, that's something i should be able to figure out on my own eventually
        Vector3 tempOrigin = origin + new Vector3((float)random.NextDouble() * size, (float)random.NextDouble() * size, (float)random.NextDouble() * size);
        Vector3 p1 = Vector3.Zero;
        Vector3 p2 = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
        p2.Normalize();
        p2 *= size / 3;
        Vector3 direction = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
        Vector3 p3 = Vector3.Cross(p2, direction);
        p3.Normalize();
        p3 *= size / 3;
        Vector3 p4 = p2 + p3;
        p1 += tempOrigin;
        p2 += tempOrigin;
        p3 += tempOrigin;
        p4 += tempOrigin;

        //here are the 4 vertices that i'm trying to use as my quad
        SHARenderedVertex v1 = new SHARenderedVertex(p1, new Vector2(0, 0));
        SHARenderedVertex v2 = new SHARenderedVertex(p2, new Vector2(1, 0));
        SHARenderedVertex v3 = new SHARenderedVertex(p3, new Vector2(1, 1));
        SHARenderedVertex v4 = new SHARenderedVertex(p4, new Vector2(0, 1));
        SHARenderedTriangle tempTriangle = new SHARenderedTriangle(v1, v2, v3, texture);
        triangles[triangleCounter++] = tempTriangle;

         //adding the following two lines will eventually cause the InvalidOperationException at the bottom
         SHARenderedTriangle tempTriangle2 = new SHARenderedTriangle(v1, v3, v4, texture);
         triangles2[triangleCounter++] = tempTriangle2;
    }

    counter = 0;
    foreach (SHARenderedTriangle triangle in triangles)
    {
        triangleList[counter++] = triangle.vertices[0];
        triangleList[counter++] = triangle.vertices[1];
        triangleList[counter++] = triangle.vertices[2];
    }

    //this is where the exception is thrown, so the program never gets to drawing the triangles
    vb.SetData(triangleList);

  • 05-12-2008 4:22 PM In reply to

    Re: Help with Triangle creation

    You are adding two triangles per plane, but you've only allocated memory for 1 triangle per plane in your vertex buffer.

     

  • 05-12-2008 5:02 PM In reply to

    Re: Help with Triangle creation

    ugh... what a stupid mistake.  yeah looking back at my code i had a * 2 for the triangles and trianglelist but not the vertex buffer.  thanks to anyone who took the time to read this.
Page 1 of 1 (7 items) Previous Next