Jump to content

  • Log In with Google      Sign In   
  • Create Account

Theory: Vertices from file?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1   Members   -  Reputation: 130


Posted 21 September 2013 - 01:35 PM



New to 3d concepts, but have been experimenting with SharpDX (i have been using XNA in a 2d capacity previously) and wondering about loading Vertices from an external source..


Looking at the Sample code from the SharpDX sdk, It hard codes the vertices of a triangle, Now its my understanding that a mesh is just a list of vertices contained within in a file(among other things) So my hypothesis is that i could draw that triangle in blender, and load it from file, then draw it?


IF this is the case, whats a good way to go about doing this? primary thoughts would be to create a class to hold that data...


Here's the SharpDX template for reference:

namespace MiniTri
    struct Vertex
        public Vector4 Position;
        public SharpDX.Color Color;

    /// <summary>
    ///   SlimDX2 port of SlimDX-MiniTri Direct3D9 Sample
    /// </summary>
    static class Program
        static void Main()
            var form = new RenderForm("SlimDX2 - MiniTri Direct3D9 Sample");
            var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width,form.ClientSize.Height));

            var vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
            vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
                new Vertex() { Color = Color.Red, Position = new Vector4(400.0f, 100.0f, 0.5f, 1.0f) },
                new Vertex() { Color = Color.Blue, Position = new Vector4(650.0f, 500.0f, 0.5f, 1.0f) },
                new Vertex() { Color = Color.Green, Position = new Vector4(150.0f, 500.0f, 0.5f, 1.0f) }

            var vertexElems = new[] {
        		new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
        		new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),

            var vertexDecl = new VertexDeclaration(device, vertexElems);

            RenderLoop.Run(form, () =>
                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

                device.SetStreamSource(0, vertices, 0, 20);
                device.VertexDeclaration = vertexDecl;
                device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);


#2   Members   -  Reputation: 296


Posted 22 September 2013 - 04:03 AM

Okay, the bare minimum you need to load is Vertices and Faces. Faces can be defined in a few different ways depending if the file contains normal information etc but for now lets assume all you have is vertices and faces.


Your file will look something like this, you can delete all the extra crap blender puts in. Here we are also assuming all faces in the object file are triangles, they might not be, but I believe blender has a convert to triangles option when you export an object.


v 1.0 0.0 0.0

v 0.0 0.0 0.0

v 1.0 1.0 0.0

f  1 2 3


it is fairly trivial to make a class that has an array of vertices and faces. The approach I have seen most often is to first run through the file and count the number of vertices and faces. Use this information to create arrays to hold the data, then run through the file again reading the data into your arrays. You can store a face, as int faces[3], where each array value is simply an index for the vertex array. I coded something like this a while ago but for loading quads and more than just faces, I will include the header file here, might give you a few ideas.

//Describes a Material
struct Material
  string m_directory; //Directory name
  string m_name;  //Material name
  float m_ns;     //Specular colour coefficient 
  float m_ka[4];  //Ambient colour
  float m_kd[4];  //Diffuse colour
  float m_ks[4];  //Specular colour
  float m_ni;     //Refraction index
  float m_d;      //Transparency
  float m_illum;    //Illumination settings

//Describes a Triangle or Quad
struct Face
  int m_vertices[4];
  int m_texCoords[4];
  int m_vertNorms[4];
  Material* m_material;
  bool m_smooth;

//SS_MeshData Class
class SS_MeshData
  //Bool to record if a mesh is currently loaded
  bool m_loaded;

  //Bools that describe mesh contents
  bool m_vPresent;  //Vertices are available
  bool m_fPresent;  //Faces are available
  bool m_vtPresent; //Vertex texture coordinates
  bool m_vnPresent; //Vertex normals present
  bool m_mPresent;  //Material file present

  //Mesh data
  Vector3D *m_v;          //Verticies
  Vector3D *m_vn;         //Vertex Normals
  Vector3D *m_vt;         //Texture Coordinates
  Face *m_faces;          //Stores faces
  int m_numVertices;      //Stores number of vertices
  int m_numVertexNormals; //Stores number of vertex normals
  int m_numFaces;         //Stores number of faces
  int m_numTextureCoords; //Stores number of texture coordinates
  int m_numMaterials;     //Stores the number of materials

  //Stores a list of materials
  string m_materialLibrary;  //Stores address of materials library
  Material *m_materials;     //Stores list of materials

  //Current draw state info
  Material* m_material;
  bool m_smooth;

  //Functions that should only be called internally
  bool CountData(ifstream &input);

  bool LoadMaterialData(ifstream &input);

  bool LoadVertexData(ifstream &input);

  bool LoadFaceData(ifstream &input);


  bool LoadMesh(string _address);

  //Draw object
  void DrawMesh();

  void ClearMesh();

  //Prints out material data, for materials, then for each face
  void DebugTheFucker();


And just to prove it works, here is something it loaded, not textured but materials from mtl file are applied. I modeled this in blender.





I hope something here has been useful. Good luck with your mesh loader :)





#3   Crossbones+   -  Reputation: 8708


Posted 22 September 2013 - 04:55 AM

Not to encourage laziness but I'd just search around for a library that can load .obj (Wavefront Object, a common Blender file export) in whatever language you're using (C# ?) :D

It will probably give you a list of vertices, normals, and tex coords. Maybe more depending on how complex is the library (and the exported file).



My journals: dustArtemis ECS framework and Making a Terrain Generator

#4   Crossbones+   -  Reputation: 8229


Posted 22 September 2013 - 05:25 AM

SharpDX does come with Assimp integration, which supports several formats including blender (to a certain extent). This is only the first step though. For fast loading it's recommended to use a binary format (after all, index and vertex buffers are "just blobs" you send to the graphics card). I'd recommend playing with Assimp's mesh viewer to get a feeling for the library's capabilities.


#5   Members   -  Reputation: 2390


Posted 22 September 2013 - 10:56 AM

It's worth mentioning that the toolkit loads various model formats as well with its ContentManager.  I know you're not using the toolkit, but it's open source and pretty well-written code, so it's a great reference point.  I'd suggest checking out their ModelContentReader class as a starting point.

Edited by SeraphLance, 22 September 2013 - 10:56 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.