c# - reusing xna primitives via class -


is there way make primitive , use on , on again? ex: if make 1 cube, can create 100 , make 10x10 grid? i've tried using loop , updating x , z coords each loop thru, moves 1 cube thats created in beginning. class created using example book. know how move cube around area changing coords in positioncube method. can in main game class allow me create simple 10x10 grid?

using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics;  namespace cube_chaser {     class cube     {         private graphicsdevice device;         private texture2d texture;          public vector3 location;          private vector3 position;          private vertexbuffer cubevertexbuffer;         private list<vertexpositiontexture> vertices = new list<vertexpositiontexture>();          public cube(graphicsdevice graphicsdevice, vector3 playerlocation, float mindistance, texture2d texture)         {             device = graphicsdevice;             this.texture = texture;              positioncube(playerlocation, mindistance);              buildface(new vector3(0, 0, 0), new vector3(0, 1, 1));             buildface(new vector3(0, 0, 1), new vector3(1, 1, 1));             buildface(new vector3(1, 0, 1), new vector3(1, 1, 0));             buildface(new vector3(1, 0, 0), new vector3(0, 1, 0));              buildfacehorizontal(new vector3(0, 1, 0), new vector3(1, 1, 1));             buildfacehorizontal(new vector3(0, 0, 1), new vector3(1, 0, 0));              cubevertexbuffer = new vertexbuffer(device, vertexpositiontexture.vertexdeclaration, vertices.count, bufferusage.writeonly);              cubevertexbuffer.setdata<vertexpositiontexture>(vertices.toarray());              this.position = position;         }          private void buildface(vector3 p1, vector3 p2)         {             vertices.add(buildvertex(p1.x, p1.y, p1.z, 1, 0));             vertices.add(buildvertex(p1.x, p2.y, p1.z, 1, 1));             vertices.add(buildvertex(p2.x, p2.y, p2.z, 0, 1));             vertices.add(buildvertex(p2.x, p2.y, p2.z, 0, 1));             vertices.add(buildvertex(p2.x, p1.y, p2.z, 0, 0));             vertices.add(buildvertex(p1.x, p1.y, p1.z, 1, 0));         }          private void buildfacehorizontal(vector3 p1, vector3 p2)         {             vertices.add(buildvertex(p1.x, p1.y, p1.z, 0, 1));             vertices.add(buildvertex(p2.x, p1.y, p1.z, 1, 1));             vertices.add(buildvertex(p2.x, p2.y, p2.z, 1, 0));             vertices.add(buildvertex(p1.x, p1.y, p1.z, 0, 1));             vertices.add(buildvertex(p2.x, p2.y, p2.z, 1, 0));             vertices.add(buildvertex(p1.x, p1.y, p2.z, 0, 0));         }          private vertexpositiontexture buildvertex(float x, float y, float z, float u, float v)         {             return new vertexpositiontexture(new vector3(x, y, z), new vector2(u, v));         }          public void positioncube(vector3 playerlocation, float mindistance)         {             location = new vector3(.5f, .5f, .5f);         }          public void draw(camera camera, basiceffect effect)         {             effect.vertexcolorenabled = false;             effect.textureenabled = true;             effect.texture = texture;              matrix center = matrix.createtranslation(new vector3(-0.5f, -0.5f, -0.5f));             matrix scale = matrix.createscale(0.05f);             matrix translate = matrix.createtranslation(location);              effect.world = center * scale * translate;             effect.view = camera.view;             effect.projection = camera.projection;              foreach (effectpass pass in effect.currenttechnique.passes)             {                 pass.apply();                 device.setvertexbuffer(cubevertexbuffer);                 device.drawprimitives(primitivetype.trianglelist, 0, cubevertexbuffer.vertexcount / 3);             }         }     } } 

i took @nico-schetler 's answer , created classes you.

cube.cs

class cube {     private graphicsdevice device;     private vertexbuffer cubevertexbuffer;      public cube(graphicsdevice graphicsdevice)     {         device = graphicsdevice;          var vertices = new list<vertexpositiontexture>();          buildface(vertices, new vector3(0, 0, 0), new vector3(0, 1, 1));         buildface(vertices, new vector3(0, 0, 1), new vector3(1, 1, 1));         buildface(vertices, new vector3(1, 0, 1), new vector3(1, 1, 0));         buildface(vertices, new vector3(1, 0, 0), new vector3(0, 1, 0));          buildfacehorizontal(vertices, new vector3(0, 1, 0), new vector3(1, 1, 1));         buildfacehorizontal(vertices, new vector3(0, 0, 1), new vector3(1, 0, 0));          cubevertexbuffer = new vertexbuffer(device, vertexpositiontexture.vertexdeclaration, vertices.count, bufferusage.writeonly);          cubevertexbuffer.setdata<vertexpositiontexture>(vertices.toarray());     }      private void buildface(list<vertexpositiontexture> vertices, vector3 p1, vector3 p2)     {         vertices.add(buildvertex(p1.x, p1.y, p1.z, 1, 0));         vertices.add(buildvertex(p1.x, p2.y, p1.z, 1, 1));         vertices.add(buildvertex(p2.x, p2.y, p2.z, 0, 1));         vertices.add(buildvertex(p2.x, p2.y, p2.z, 0, 1));         vertices.add(buildvertex(p2.x, p1.y, p2.z, 0, 0));         vertices.add(buildvertex(p1.x, p1.y, p1.z, 1, 0));     }      private void buildfacehorizontal(list<vertexpositiontexture> vertices, vector3 p1, vector3 p2)     {         vertices.add(buildvertex(p1.x, p1.y, p1.z, 0, 1));         vertices.add(buildvertex(p2.x, p1.y, p1.z, 1, 1));         vertices.add(buildvertex(p2.x, p2.y, p2.z, 1, 0));         vertices.add(buildvertex(p1.x, p1.y, p1.z, 0, 1));         vertices.add(buildvertex(p2.x, p2.y, p2.z, 1, 0));         vertices.add(buildvertex(p1.x, p1.y, p2.z, 0, 0));     }      private vertexpositiontexture buildvertex(float x, float y, float z, float u, float v)     {         return new vertexpositiontexture(new vector3(x, y, z), new vector2(u, v));     }      public void draw( basiceffect effect)     {         foreach (effectpass pass in effect.currenttechnique.passes)         {             pass.apply();             device.setvertexbuffer(cubevertexbuffer);             device.drawprimitives(primitivetype.trianglelist, 0, cubevertexbuffer.vertexcount / 3);         }     } } 

cubedrawable.cs

public class drawablelist<t> : drawablegamecomponent {     private basiceffect effect;     private camera camera;     private class entity     {         public vector3 position { get; set; }         public matrix orientation { get; set; }         public texture2d texture { get; set; }     }      private cube cube;     private list<entity> entities = new list<entity>();      public drawablelist (game game, camera camera, basiceffect effect)          : base( game )      {         this.effect = effect;         cube = new cube (game.graphicsdevice);         this.camera = camera;     }      public void add( vector3 position, matrix orientation, texture2d texture )     {         entities.add (new entity() {              position = position,             orientation = orientation,             texture = texture         });     }      public override void draw (gametime gametime )     {         base.draw (gametime);          foreach (var item in entities) {              effect.vertexcolorenabled = false;             effect.textureenabled = true;             effect.texture = item.texture;              matrix center = matrix.createtranslation(new vector3(-0.5f, -0.5f, -0.5f));             matrix scale = matrix.createscale(0.05f);             matrix translate = matrix.createtranslation(item.position);              effect.world = center * scale * translate;             effect.view = camera.view;             effect.projection = camera.projection;              cube.draw (effect);         }     } } 

usage

        camera = new camera (graphics.graphicsdevice);         effect = new basiceffect (graphics.graphicsdevice);         cubes = new drawablelist<cube> (this, camera, effect);          components.add (cubes);          (int i=0 ; < 50; i++)         {             cubes.add (new vector3( i*0.5f, 50.0f, 50.0f), matrix.identity, logotexture);         } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -