
Loading .obj models
Posted Wednesday, 28 July, 2010 - 12:02 by Darwin226 inI have this code that parses an .obj file and is supposed to load it into OpenGL. The problem is that it doesn't draw so if anyone could please check to see if anything is wrong I would appreciate that.
The parsing works as it should and the vertices array is fine, I'm probably doing something wrong with the way I load that array into OpenGL.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using GridDistorsion; namespace GridDistorsion { struct Model { public int Id; public int Vertices; } struct Vertex { public Vertex ( float X, float Y, float Z, float R, float G, float B, float A, float UVX, float UVY ) { this.X = X; this.Y = Y; this.Z = Z; this.R = R; this.G = G; this.B = B; this.A = A; this.UVX = UVX; this.UVY = UVY; } float X; float Y; float Z; float R; float G; float B; float A; float UVX; float UVY; } class ModelViewer { public static Model LoadModel ( string filename ) { StreamReader file = new StreamReader( filename ); List<Vector3> points = new List<Vector3>(); List<Vector3> normals = new List<Vector3>(); List<Vector2> texcoords = new List<Vector2>(); List<Vertex[]> triangles = new List<Vertex[]>(); float x, y, z; int v, t, n; string keyword; while ( file.ReadWord( out keyword ) ) { if ( keyword == "v" ) { file.ReadWord( out x ); file.ReadWord( out y ); file.ReadWord( out z ); points.Add( new Vector3( x, y, z ) ); } else if ( keyword == "vt" ) { file.ReadWord( out x ); file.ReadWord( out y ); texcoords.Add( new Vector2( x, y ) ); Console.WriteLine( texcoords.Count ); } else if ( keyword == "vn" ) { file.ReadWord( out x ); file.ReadWord( out y ); file.ReadWord( out z ); normals.Add( new Vector3( x, y, z ) ); } else if ( keyword == "f" ) { Vector3 point; Vector2 uv = new Vector2(); Vector3 normal = new Vector3(); Vertex[] tr = new Vertex[ 3 ]; for ( int face = 0 ; face < 3 ; face++ ) { string tmp; file.ReadWord( out tmp ); v = int.Parse( tmp ); point = points[ v - 1 ]; if ( texcoords.Count > 0 ) { //string temp; //file.ReadWord( out temp ); //c = temp[ 0 ]; file.ReadWord( out t ); uv = texcoords[ t - 1 ]; } if ( normals.Count > 0 ) { //string temp; //file.ReadWord( out temp ); //c = temp[ 0 ]; file.ReadWord( out n ); normal = normals[ n - 1 ]; } tr[ face ] = new Vertex( point.X, point.Y, point.Z, 1, 1, 1, 1, uv.X, uv.Y ); } triangles.Add( tr ); } } file.Close(); int vertexcount = triangles.Count * 3; Vertex[] vertices = new Vertex[ vertexcount ]; for ( int i = 0 ; i < triangles.Count() ; i++ ) { vertices[ i * 3 ] = triangles[ i ][ 0 ]; vertices[ i * 3 + 1 ] = triangles[ i ][ 1 ]; vertices[ i * 3 + 2 ] = triangles[ i ][ 2 ]; } Model model = new Model(); model = CreateModel( vertices, vertexcount ); return model; } static Model CreateModel ( Vertex[] points, int count ) { if ( count % 3 != 0 ) return new Model(); Model model; model.Vertices = count; GL.GenBuffers( 1, out model.Id ); GL.BindBuffer( BufferTarget.ArrayBuffer, model.Id ); GL.BufferData( BufferTarget.ArrayBuffer, new IntPtr( sizeof( float ) * 9 * count ), points, BufferUsageHint.StaticDraw ); return model; } public static void DrawModel ( Model model ) { GL.BindBuffer( BufferTarget.ArrayBuffer, model.Id ); GL.VertexPointer( 3, VertexPointerType.Float, sizeof( float ) * 9, 0 ); GL.ColorPointer( 4, ColorPointerType.Float, sizeof( float ) * 9, sizeof( float ) * 3 ); GL.TexCoordPointer( 2, TexCoordPointerType.Float, sizeof( float ) * 9, sizeof( float ) * 7 ); GL.DrawArrays( BeginMode.Triangles, 0, model.Vertices ); } } }


Comments
Re: Loading .obj models
I don't know if this is the real problem, but I know you should add this line:
[StructLayout(LayoutKind.Sequential)]before
struct Vertex {Becouse .Net/Mono structurs size(and where is every field placed) are different in any console, unless you use attributes.
I hope this is the problem and it will be fixed.
By the way- thanks for this code(importing .obj models).
Re: Loading .obj models
Unfortunately that didn't work. (Btw, I included InteropServices)
As for the code you can only thank me for the translation to C#, the original code was given to my by Overv on Facepunch forums in C++.
Re: Loading .obj models
VertexArray not enabled?
Re: Loading .obj models
Did you find an issue?
Re: Loading .obj models
Hello,
May be, the source code below could interests someone, so I post it.
Here are 3 examples of how to import .obj in your project (two are coded in c# and one in C with a C++ object).
I post the C code because it's very fast... I don't know why it is so fast compared to C# (I speak about 50ms to load compared to 10 sec!!!). If somebody can explain me, it would be great.
The source code are not from me. I've modified it because there where not working!
In C
csharp1
csharp2
Re: Loading .obj models
Peter, have you ever heard about Vala? I think it would be really great for you!
I personally would move to Vala, but it doesn't have a good and stable IDE for now.
OpenTK and Mono are very comfort for now, but if you really want speed go ahead and try it!
http://live.gnome.org/Vala