Hi I'm trying to learn how to load a .obj file using OpenTK in windows Forms. I have seen a lot of examples out there, but I do see almost everyone uses List<Tri>, and Point[]. Code examples show these highlighted like there IDE know what these are; for example

List<Tri> tris = new List<Tri>();  

but mine just returns "The type or namespace name 'Tri' could not be found" is there an include I need to add or a using I am missing. Currently have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

EDIT: I have been trying to piece it together primarily from using code samples found here:


As well as from here Missing triangles in model

I have been trying to piece it together primarily from using code samples found here


As well as from here Missing triangles in model


2 Answers 2


List is System.Collections.Generic.List, which is the standard resizing-array-backed vector class you'd see in Java ArrayList or STL vector.

Tri is part of the other guy's code.

OpenTK provides Vector3. If you're representing a model, you're going to need a few more definitions, including your definition of what a "triangle" is. Here's a rough guideline of some classes you'd need to define:

A Vertex defines a single point in space, along with additional data required for rendering the mesh, such as texture coordinates, normal vector, and a vertex color.

struct Vertex { 
    Vector3 Position;
    Vector3 Normal;
    Color4 Color;
    Vector2 TexCoord;

A Face defines a collection of vertices that form a single polygon in space. Most of the time, the only polygon type you'd want to use is a Triangle.

class Triangle { 
    Vertex[] Vertices = new Vertex[3];

A Mesh or a Model defines what you'd think of as a full 3d object; it is a collection of faces. Instead of keeping a List of Triangles around, typically you keep an array of vertices, since in modern OpenGL, you would draw the mesh data by passing OpenGL a pointer to a vertex array.

Note that if you use a Vertex Buffer to render your mesh, after you've registered and populated the buffer with GL.BufferData or GL.BufferSubData you don't need to keep the array of vertices around.

class Mesh { 
    // for educational purposes, I'm including this definition here
    // A mesh is really just a collection of triangles / vertices
    // but you don't need to keep the triangles in memory if you
    // have buffered the object data with OpenGL.
    Vertex[] AllVertices; 

    // if you buffer the data, this is the buffer ID
    int elementArrayID;
    // if you buffer the data and use indexed rendering, 
    // this is the index buffer id
    int indexArrayID;

Judging from the code samples you provided, Tri is simply a class that holds 3 Points. For example, the Triangulate method in the question you linked to would take an array of points and attempt to turn them into an array of Triangles by iterating through the list of points an "connecting" them via a Triangle object.

The constructor in this case takes 3 points and creates a Triangle object out of those 3 points. This would make per-triangle processing and rendering easier to manage. You would need to make sure though that you only use the same point once i.e. you should only have one Point object that refers to point 12,14 - not doing so would be a waste of memory.

So when you process the model, I would keep a list of all points you create ensuring that there are no duplicates and then process each triangle set by using the points from that list. I am not familiar with the OpenTK format - but the OBJ model format makes this easy as it lists all Vertices and then instead of listing Faces as vertices it lists indexes to the list of vertices so you'll never have the same Point in memory more than once.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .