• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mellkor

Theory: Vertices from file?

4 posts in this topic

Hi,

 

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
{
    [StructLayout(LayoutKind.Sequential)]
    struct Vertex
    {
        public Vector4 Position;
        public SharpDX.Color Color;
    }

    /// <summary>
    ///   SlimDX2 port of SlimDX-MiniTri Direct3D9 Sample
    /// </summary>
    static class Program
    {
        [STAThread]
        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) }
            });
            vertices.Unlock();

            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),
				VertexElement.VertexDeclarationEnd
        	};

            var vertexDecl = new VertexDeclaration(device, vertexElems);

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

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

                device.EndScene();
                device.Present();
            });
        }
    }
}
0

Share this post


Link to post
Share on other sites

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
{
private:
  //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);
  
public:
  SS_MeshData();

  ~SS_MeshData();

  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.

 

[attachment=18011:blenderEntp.jpg]

 

 

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

 

 

 

 

1

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
Share on other sites
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.

Cheers.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0