10

.obj-File を読み込んで、 を使用して描画しようとしていますglDrawElements

さて、glDrawArraysすべてが完璧に機能しますが、もちろん非効率的です。

私が今抱えている問題は、.obj ファイルが (属性ごとに) 複数のインデックス バッファーを使用しているのに対し、OpenGL は 1 つしか使用できないことです。したがって、それに応じてそれらをマッピングする必要があります。

そこには多くの疑似アルゴリズムがあり、C++ の実装も見つけました。私はかなりの C++ を知っていますが、奇妙なことに、どちらも Scala での実装には役立ちませんでした。

どれどれ:

private def parseObj(path: String): Model =
{
    val objSource: List[String] = Source.fromFile(path).getLines.toList

    val positions: List[Vector3] = objSource.filter(_.startsWith("v ")).map(_.split(" ")).map(v => new Vector3(v(1).toFloat,v(2).toFloat,v(3).toFloat))//, 1.0f))
    val normals: List[Vector4] = objSource.filter(_.startsWith("vn ")).map(_.split(" ")).map(v => new Vector4(v(1)toFloat,v(2).toFloat, v(3).toFloat, 0.0f))
    val textureCoordinates: List[Vector2] = objSource.filter(_.startsWith("vt ")).map(_.split(" ")).map(v => new Vector2(v(1).toFloat, 1-v(2).toFloat)) // TODO 1-y because of blender
    val faces: List[(Int, Int, Int)] = objSource.filter(_.startsWith("f ")).map(_.split(" ")).flatten.filterNot(_ == "f").map(_.split("/")).map(a => ((a(0).toInt, a(1).toInt, a(2).toInt)))

    val vertices: List[Vertex] =  for(face <- faces) yield(new Vertex(positions(face._1-1), textureCoordinates(face._2-1)))

    val f: List[(Vector3, Vector2, Vector4)] = for(face <- faces) yield((positions(face._1-1), textureCoordinates(face._2-1), normals(face._3-1)))
    println(f.mkString("\n"))

    val indices: List[Int] = faces.map(f => f._1-1) // Wrong!

    new Model(vertices.toArray, indices.toArray)
}

これval indices: List[Int]は私の最初の単純なアプローチであり、もちろん間違っています。しかし、一番上から始めましょう:

ファイルを読み込んで調べます。(.objファイルがどのように構成されているか知っていると思います)

頂点、テクスチャ座標、法線を読み込みました。それから私は顔に来ます。

さて、私の例の各面には、 をv_x, t_y, n_z定義する3 つの値がありvertexAtIndexX, textureCoordAtIndexY, normalAtIndexZます。したがって、これらのそれぞれが 1 つの頂点を定義し、これらの 3 つ (またはファイル内の 1 行) が面/ポリゴン/三角形を定義します。

私は実際にval vertices: List[Vertex] = for(face <- faces) yield(new Vertex(positions(face._1-1), textureCoordinates(face._2-1)))頂点を作成しようとしています (現在、位置とテクスチャ座標のみを保持し、法線を無視するケースクラス)

本当の問題は次の行です。

val indices: List[Int] = faces.map(f => f._1-1) // Wrong!

実際のインデックスを取得するには、基本的に代わりにこれを行う必要があります

val vertices: List[Vertex] = for(face <- faces) yield(new Vertex(positions(face._1-1), textureCoordinates(face._2-1)))val indices: List[Int] = faces.map(f => f._1-1) // Wrong!

擬似コード:

Iterate over all faces
    Iterate over all vertices in a face
       Check if we already have that combination of(position, texturecoordinate, normal) in our newVertices

       if(true)
          indices.put(indexOfCurrentVertex)
       else
          create a new Vertex from the face
          store the new vertex in the vertex list
          indices.put(indexOfNewVertex)

それでも私は完全に立ち往生しています。私はさまざまなことを試しましたが、実際に機能する素晴らしくクリーンなソリューションを思い付くことができません。

次のようなもの:

val f: List[(Vector3, Vector2, Vector4)] = for(face <- faces) yield((positions(face._1-1), textureCoordinates(face._2-1), normals(face._3-1)))

区別するものは何f.distinctもないため、そこにあるすべてのエントリは一意であるため、機能していません。ファイルを見ると完全に理にかなっていますが、疑似コードが確認するように指示しています。

もちろん、それに応じてインデックスを埋める必要があります(できればワンライナーで、多くの機能的な美しさで)

しかし、重複を見つけようとする必要があるので...ちょっと困惑しています。すべての参照で、さまざまな「頂点」と「位置」を混同しすぎていると思います。

では、私の考えは間違っているのでしょうか、それともアルゴリズム/考え方が正しいのでしょうか?

教えてください!

コメントに従って、少し更新しました。

var index: Int = 0
val map: mutable.HashMap[(Int, Int, Int), Int] = new mutable.HashMap[(Int, Int, Int), Int].empty

val combinedIndices: ListBuffer[Int] = new ListBuffer[Int]

for(face <- faces)
{
    val vID: Int = face._1-1
    val nID: Int = face._2-1
    val tID: Int = face._3-1

    var combinedIndex: Int = -1

    if(map.contains((vID, nID, tID)))
    {
        println("We have a duplicate, wow!")
        combinedIndex = map.get((vID, nID, tID)).get
    }
    else
    {
        combinedIndex = index
        map.put((vID, nID, tID), combinedIndex)
        index += 1
    }

    combinedIndices += combinedIndex
}

顔がまだある場所:

val faces: List[(Int, Int, Int)] = objSource.filter(_.startsWith("f ")).map(_.split(" ")).flatten.filterNot(_ == "f").map(_.split("/")).map(a => ((a(0).toInt, a(1).toInt, a(2).toInt)))

楽しい事実、私はまだそれを明らかに理解していません。

combinedIndices最後に次のような自然数を保持することを意味します。

ListBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...)
4

1 に答える 1

1

これは javascript (スカラでなくてすみません) ですが、コメントが付けられており、かなり簡単に変換できるはずです。

// bow-tie
var objString = "v 0 0 0\nv 1 1 0\nv 1 -1 0\nv -1 1 0\nv -1 -1 0\n" +
    "vt 0 .5\nvt 1 1\nvt 1 0\n" +
    "vn 0 0 1\n" +
    "f 1/1/1 2/2/1 3/3/1\nf 1/1/1 4/2/1 5/3/1";
// output indices should be [0, 1, 2, 0, 3, 4]
// parse the file
var lines = objString.split("\n");
var data = lines.map(function(line) { return line.split(" "); });
var v = [];
var t = [];
var n = [];
var f = [];
var indexMap = new Map(); // HashMap<face:string, index:integer>
var nextIndex = 0;
var vertices = [];
var indices = [];
// fill vertex, texture and normal arrays
data.filter(function(d) { return d[0] == "v"; }).forEach(function(d) { v.push([parseFloat(d[1]), parseFloat(d[2]), parseFloat(d[3])]); });
data.filter(function(d) { return d[0] == "vt"; }).forEach(function(d) { t.push([parseFloat(d[1]), parseFloat(d[2])]); });
data.filter(function(d) { return d[0] == "vn"; }).forEach(function(d) { n.push([parseFloat(d[1]), parseFloat(d[2]), parseFloat(d[3])]); });
//
console.log("V", v.toString());
console.log("T", t.toString());
console.log("N", n.toString());
// create vertices and indices arrays by parsing faces
data.filter(function(d) { return d[0] == "f"; }).forEach(function(d) { 
    var f1 = d[1].split("/").map(function(d) { return parseInt(d)-1; });
    var f2 = d[2].split("/").map(function(d) { return parseInt(d)-1; });
    var f3 = d[3].split("/").map(function(d) { return parseInt(d)-1; });
    // 1
    if(indexMap.has(d[1].toString())) {
        indices.push(indexMap.get(d[1].toString()));
    } else {
        vertices = vertices.concat(v[f1[0]]).concat(t[f1[1]]).concat(n[f1[2]]);
        indexMap.set(d[1].toString(), nextIndex);
        indices.push(nextIndex++);
    }
    // 2
    if(indexMap.has(d[2].toString())) {
        indices.push(indexMap.get(d[2].toString()));
    } else {
        vertices = vertices.concat(v[f2[0]]).concat(t[f2[1]]).concat(n[f2[2]]);
        indexMap.set(d[2].toString(), nextIndex);
        indices.push(nextIndex++);
    }
    // 3
    if(indexMap.has(d[3].toString())) {
        indices.push(indexMap.get(d[3].toString()));
    } else {
        vertices = vertices.concat(v[f3[0]]).concat(t[f3[1]]).concat(n[f3[2]]);
        indexMap.set(d[3].toString(), nextIndex);
        indices.push(nextIndex++);
    }
});
//
console.log("Vertices", vertices.toString());
console.log("Indices", indices.toString());

出力

V 0,0,0,1,1,0,1,-1,0,-1,1,0,-1,-1,0
T 0,0.5,1,1,1,0
N 0,0,1
Vertices 0,0,0,0,0.5,0,0,1,1,1,0,1,1,0,0,1,1,-1,0,1,0,0,0,1,-1,1,0,1,1,0,0,1,-1,-1,0,1,0,0,0,1
Indices 0,1,2,0,3,4

JSFiddle http://jsfiddle.net/8q7jLvsq/2

私が行っている唯一のこと ~ 別の方法は、顔の一部を表す文字列帽子を indexMap のキーとして使用することです (例: "25/32/5")。

EDIT JSFiddle http://jsfiddle.net/8q7jLvsq/2/ このバージョンでは、頂点、テクスチャ、法線の繰り返し値を組み合わせています。これにより、同じ値を繰り返す OBJ ファイルが最適化され、すべての面が一意になります。

// bow-tie
var objString = "v 0 0 0\nv 1 1 0\nv 1 -1 0\nv 0 0 0\nv -1 1 0\nv -1 -1 0\n" +
    "vt 0 .5\nvt 1 1\nvt 1 0\nvt 0 .5\nvt 1 1\nvt 1 0\n" +
    "vn 0 0 1\nvn 0 0 1\nvn 0 0 1\nvn 0 0 1\nvn 0 0 1\nvn 0 0 1\n" +
    "f 1/1/1 2/2/2 3/3/3\nf 4/4/4 5/5/5 6/6/6";
// output indices should be [0, 1, 2, 0, 3, 4]
// parse the file
var lines = objString.split("\n");
var data = lines.map(function(line) { return line.split(" "); });
var v = [];
var t = [];
var n = [];
var f = [];
var vIndexMap = new Map(); // map to earliest index in the list
var vtIndexMap = new Map();
var vnIndexMap = new Map();
var indexMap = new Map(); // HashMap<face:string, index:integer>
var nextIndex = 0;
var vertices = [];
var indices = [];
// fill vertex, texture and normal arrays
data.filter(function(d) { return d[0] == "v"; }).forEach(function(d, i) { 
    v[i] = [parseFloat(d[1]), parseFloat(d[2]), parseFloat(d[3])]; 
    var key = [d[1], d[2], d[3]].toString();
    if(!vIndexMap.has(key)) {
        vIndexMap.set(key, i);
    }
});
data.filter(function(d) { return d[0] == "vt"; }).forEach(function(d, i) { 
    t[i] = [parseFloat(d[1]), parseFloat(d[2])]; 
    var key = [d[1], d[2]].toString();
    if(!vtIndexMap.has(key)) {
        vtIndexMap.set(key, i);
    }
});
data.filter(function(d) { return d[0] == "vn"; }).forEach(function(d, i) { 
    n[i] = [parseFloat(d[1]), parseFloat(d[2]), parseFloat(d[3])]; 
    var key = [d[1], d[2], d[3]].toString();
    if(!vnIndexMap.has(key)) {
        vnIndexMap.set(key, i);
    }
});
//
console.log("V", v.toString());
console.log("T", t.toString());
console.log("N", n.toString());
// create vertices and indices arrays by parsing faces
data.filter(function(d) { return d[0] == "f"; }).forEach(function(d) { 
    var f1 = d[1].split("/").map(function(d, i) {
        var index = parseInt(d)-1;
        if(i == 0) index = vIndexMap.get(v[index].toString());
        else if(i == 1) index = vtIndexMap.get(t[index].toString());
        else if(i == 2) index = vnIndexMap.get(n[index].toString());
        return index;
    });
    var f2 = d[2].split("/").map(function(d, i) { 
        var index = parseInt(d)-1;
        if(i == 0) index = vIndexMap.get(v[index].toString());
        else if(i == 1) index = vtIndexMap.get(t[index].toString());
        else if(i == 2) index = vnIndexMap.get(n[index].toString());
        return index;
    });
    var f3 = d[3].split("/").map(function(d, i) { 
        var index = parseInt(d)-1;
        if(i == 0) index = vIndexMap.get(v[index].toString());
        else if(i == 1) index = vtIndexMap.get(t[index].toString());
        else if(i == 2) index = vnIndexMap.get(n[index].toString());
        return index; 
    });
    // 1
    if(indexMap.has(f1.toString())) {
        indices.push(indexMap.get(f1.toString()));
    } else {
        vertices = vertices.concat(v[f1[0]]).concat(t[f1[1]]).concat(n[f1[2]]);
        indexMap.set(f1.toString(), nextIndex);
        indices.push(nextIndex++);
    }
    // 2
    if(indexMap.has(f2.toString())) {
        indices.push(indexMap.get(f2.toString()));
    } else {
        vertices = vertices.concat(v[f2[0]]).concat(t[f2[1]]).concat(n[f2[2]]);
        indexMap.set(f2.toString(), nextIndex);
        indices.push(nextIndex++);
    }
    // 3
    if(indexMap.has(f3.toString())) {
        indices.push(indexMap.get(f3.toString()));
    } else {
        vertices = vertices.concat(v[f3[0]]).concat(t[f3[1]]).concat(n[f3[2]]);
        indexMap.set(f3.toString(), nextIndex);
        indices.push(nextIndex++);
    }
});
//
console.log("Vertices", vertices.toString());
console.log("Indices", indices.toString());
于 2015-09-15T13:10:50.573 に答える