14/2/1意味着position(14)和texturecoordinates(2)映射在一起.整条线将连接这3个顶点,就像(1,14,13)会建立一个三角形.直到这里我都正确理解了吗? 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)))顶点的定义如下:case class Vertex(position: Vector4,/* normal: Vector4,*/ textureCoordinates: Vector2)我需要在每个面上都做"-1",以避免偏离一和IndexOutOfBoundsExceptions,因为List的索引以0开头,而索引以1开头.最后是索引:在OpenGL中,您可以使用ELEMENT_ARRAY_BUFFER指定要连接的顶点并保存(可能很多)数据.正如我前面说的,每个面f 1/1/1 14/2/1 13/3/1都是像这样v1/t1/n1 v2/t2/n2 v3/t3/n3(对于每一行)构建的,所以我只需要提取v1,v2,v3并将其打包到一个长列表中,然后由OpenGL完成其余工作. /p>然后 v1/v2/v3/v4/v5/v6/.../vn将成为我的索引直到这里我是对还是错?最后但并非最不重要的一点是,我将创建一个新模型:class Model(vertices: Array[Vertex], indices: Array[Int]/*, textures: Array[Texture]*/){ // Create VAO, VBO and a buffer for the indices val vao: Int = glGenVertexArrays val vbo: Int = glGenBuffers val ibo: Int = glGenBuffers setup private def setup(): Unit = { val interleavedBuffer: FloatBuffer = prepareVertexBuffer(vertices) val indicesBuffer: IntBuffer = prepareIntBuffer(indices) // One VAO to bind them all! glBindVertexArray(vao) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) // Fill buffers with data glBufferData(GL_ARRAY_BUFFER, interleavedBuffer, GL_STATIC_DRAW) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW) // Set vertex attribute pointers glVertexAttribPointer(0, 4, GL_FLOAT, false, 4*6, 0) // 0 = Position = Vector4(x,y,z,w) glVertexAttribPointer(1, 2, GL_FLOAT, false, 4*6, 4*4) // 1 = Texture Coordinates = Vector2(x,y) -> 2 (coordinates) * 4 (byte-size of float) => stride = 4 (position) + 2 (texture coordinates) = 6 * 4 (byte-size of float); offset = 4 (position) * 4 (byte-size of float) glBindBuffer(GL_ARRAY_BUFFER, 0) glBindVertexArray(0) } private def prepareIntBuffer(data: Array[Int]): IntBuffer = { val buffer: IntBuffer = BufferUtils.createIntBuffer(data.length) buffer.put(data) buffer.flip // Make the buffer readable buffer } private def prepareVertexBuffer(vertices: Array[Vertex]): FloatBuffer = { val positions: Array[Float] = vertices.map(v => v.position).map(_.toArray).flatten val textureCoordinates: Array[Float] = vertices.map(v => v.textureCoordinates).map(_.toArray).flatten // TODO remove hardcoded sizes for grouping val zipped: Array[Float] = (positions.grouped(4) zip textureCoordinates.grouped(2)).toArray.flatMap {case (x,y) => x ++ y} val buffer: FloatBuffer = BufferUtils.createFloatBuffer(zipped.size) buffer.put(zipped) buffer.flip buffer }问题是,输出看起来像这样:实际上它应该是Blender的Ico-Sphere.因此,我的实现或对.obj-file-format的理解确实有些偏离.我认为它与索引有关,但不知道.我进行了很多调试,手动将不同的列表与实际的.obj文件进行比较,但无济于事.一切看起来都还不错,所以我认为这确实是我的理解. 编辑:我不确定我误会了什么,但我没有直面创建顶点列表(我自己的课程),给模型所有的位置,纹理坐标和贴图,网格外观看起来是正确的(除了错误的纹理化处理).那我做了什么:class Model(positions: Array[Vector3], texcoords: Array[Vector2], indices: Array[Int])private def prepareVertexBuffer(positions: Array[Vector3], texcoords: Array[Vector2]): FloatBuffer = { val pos: Array[Float] = positions.map(_.toArray).flatten val textureCoordinates: Array[Float] = texcoords.map(_.toArray).flatten // TODO remove hardcoded sizes for grouping val zipped: Array[Float] = (pos.grouped(3) zip textureCoordinates.grouped(2)).toArray.flatMap {case (x,y) => x ++ y} println(zipped.toList.grouped(5).mkString("\n")) val buffer: FloatBuffer = BufferUtils.createFloatBuffer(zipped.size) buffer.put(zipped) buffer.flip buffer }(注意:我现在也已切换回Vector3而不是Vector4.以为它可能有问题)val indices: List[Int] = faces.map(f => f._1-1) // Wrong?new Model(vertices.toArray, indices.toArray)其余相同.所以我想我的纹理问题是由此产生的.现在我只有42个顶点,而不是240个(来自面孔),所以我失去了很多组合.当前结果: 修改问题归结为.obj和opengl之间的差异. OpenGL只能使用一个索引缓冲区,而在.obj中,每个属性都有一个.所以我需要以适当的方式手动合并它们.我已经找到了用于C ++的函数,尽管我对C ++相当了解,但是我不确定为什么它在Scala上无法正常工作.我只是找不到任何重复... 解决方案您正在做的是v1-t1 v2-t2, v3-t3等.如果您查看原始的.obj文件,可能就没有发生任何事情.可能是v1-t1, v2-t1, v3-t1.谁知道.因此,您需要做的是找到与顶点匹配的texcoord.这将使您接近所需的位置,但是却不尽然,因为在.OBJ文件中,1个顶点可以具有多个texcoords,而现在您还不能处理.我在这里回答了类似的问题: OpenGL-使用glDrawElements错误地映射了纹理(请注意,您不必摆脱索引缓冲区,但是更容易)I'm working on a small 3D engine to learn more of OpenGL. I'm using Scala and lwjgl for the task.Loading "hand-made" (all vertices, texture coordinates and indices - arrays specified by hand) models works like a charm, so I wanted to move on to loading .obj files. Turns out, it's not that easy, or, well, I'm somehow misunderstanding something.Let's look into my parsing-method:private def parseObj(path: String): Model ={ val objSource: List[String] = Source.fromFile(path).getLines.toList val positions: List[Vector4] = objSource.filter(_.startsWith("v ")).map(_.split(" ")).map(v => new Vector4(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).toDouble.toFloat,v(2).toDouble.toFloat)) 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 indices: List[Int] = faces.map(f => f._1-1) // Wrong? /* println(positions.map{p => s"v ${p.x} ${p.y} ${p.z}"}.mkString("\n")) println(textureCoordinates.map {t => s"vt ${t.x} ${t.y}"}.mkString("\n")) println(indices) */ //println(faces.length) val vertices: List[Vertex] = for(face <- faces) yield(new Vertex(positions(face._1-1), textureCoordinates(face._2-1))) println(vertices.mkString("\n")) new Model(vertices.toArray, indices.toArray)}Now, what am I doing? An .obj-file looks like this:v 0.000000 -1.000000 0.000000v 0.723607 -0.447220 0.525725v -0.276388 -0.447220 0.850649v -0.894426 -0.447216 0.000000v -0.276388 -0.447220 -0.850649v 0.723607 -0.447220 -0.525725v 0.276388 0.447220 0.850649v -0.723607 0.447220 0.525725v -0.723607 0.447220 -0.525725v 0.276388 0.447220 -0.850649v 0.894426 0.447216 0.000000v 0.000000 1.000000 0.000000v -0.162456 -0.850654 0.499995v 0.425323 -0.850654 0.309011v 0.262869 -0.525738 0.809012v 0.850648 -0.525736 0.000000v 0.425323 -0.850654 -0.309011v -0.525730 -0.850652 0.000000v -0.688189 -0.525736 0.499997v -0.162456 -0.850654 -0.499995v -0.688189 -0.525736 -0.499997v 0.262869 -0.525738 -0.809012v 0.951058 0.000000 0.309013v 0.951058 0.000000 -0.309013v 0.000000 0.000000 1.000000v 0.587786 0.000000 0.809017v -0.951058 0.000000 0.309013v -0.587786 0.000000 0.809017v -0.587786 0.000000 -0.809017v -0.951058 0.000000 -0.309013v 0.587786 0.000000 -0.809017v 0.000000 0.000000 -1.000000v 0.688189 0.525736 0.499997v -0.262869 0.525738 0.809012v -0.850648 0.525736 0.000000v -0.262869 0.525738 -0.809012v 0.688189 0.525736 -0.499997v 0.162456 0.850654 0.499995v 0.525730 0.850652 0.000000v -0.425323 0.850654 0.309011v -0.425323 0.850654 -0.309011v 0.162456 0.850654 -0.499995vt 0.534208 0.190162vt 0.439232 0.259166vt 0.570486 0.301814vt 0.995860 0.190574vt 0.999792 0.292763vt 0.903537 0.265179vt 0.651606 0.190162vt 0.570486 0.078511vt 0.439232 0.121159vt 0.903535 0.129002vt 0.092743 0.471488vt 0.212444 0.449586vt 0.158053 0.569043vt 0.737562 0.458199vt 0.632422 0.499661vt 0.728679 0.579260vt 0.408849 0.471488vt 0.316315 0.457312vt 0.342992 0.573818vt 0.632005 0.463365vt 0.528550 0.449586vt 0.605329 0.564268vt 0.999792 0.072423vt 0.026885 0.573818vt 0.847656 0.509327vt 0.474160 0.569043vt 0.793444 0.309553vt 0.688302 0.255514vt 0.784558 0.198935vt 0.251881 0.000208vt 0.343838 0.078511vt 0.226439 0.078511vt 0.028577 0.072765vt 0.131462 0.009506vt 0.095186 0.121159vt 0.939981 0.570899vt 0.847658 0.645504vt 0.943912 0.673088vt 0.539471 0.666598vt 0.419770 0.688500vt 0.517323 0.757552vt 0.793441 0.077875vt 0.688301 0.119336vt 0.747790 0.000208vt 0.307560 0.190162vt 0.226439 0.301814vt 0.190162 0.190162vt 0.343838 0.301814vt 0.251881 0.380116vt 0.095186 0.259166vt 0.131462 0.370818vt 0.028577 0.307559vt 0.000208 0.190162vt 0.737565 0.689877vt 0.824936 0.743021vt 0.103663 0.688500vt 0.223364 0.666598vt 0.201217 0.757552vt 0.880811 0.006181vt 0.632005 0.680774vt 0.316315 0.674721vt 0.000208 0.674721vt 0.289222 0.564268vt 0.315898 0.680774vt 0.943912 0.452748vt 0.824932 0.386505vt 0.632423 0.635839vt 0.000208 0.457312vt 0.315898 0.463365vt 0.475509 0.009506vt 0.372624 0.072765vt 0.430997 0.380533vt 0.687884 0.078511vt 0.595927 0.000208vt 0.691911 0.380533vt 0.687884 0.301814vt 0.595927 0.380116vt 0.880815 0.362696vt 0.344255 0.190162vt 0.114890 0.380533vt 0.475509 0.370818vt 0.372624 0.307559vn 0.102400 -0.943500 0.315100vn 0.700200 -0.661700 0.268000vn -0.268000 -0.943500 0.194700vn -0.268000 -0.943500 -0.194700vn 0.102400 -0.943500 -0.315100vn 0.905000 -0.330400 0.268000vn 0.024700 -0.330400 0.943500vn -0.889700 -0.330400 0.315100vn -0.574600 -0.330400 -0.748800vn 0.534600 -0.330400 -0.777900vn 0.802600 -0.125600 0.583100vn -0.306600 -0.125600 0.943500vn -0.992100 -0.125600 0.000000vn -0.306600 -0.125600 -0.943500vn 0.802600 -0.125600 -0.583100vn 0.408900 0.661700 0.628400vn -0.471300 0.661700 0.583100vn -0.700200 0.661700 -0.268000vn 0.038500 0.661700 -0.748800vn 0.724000 0.661700 -0.194700vn 0.268000 0.943500 -0.194700vn 0.491100 0.794700 -0.356800vn 0.408900 0.661700 -0.628400vn -0.102400 0.943500 -0.315100vn -0.187600 0.794700 -0.577300vn -0.471300 0.661700 -0.583100vn -0.331300 0.943500 0.000000vn -0.607100 0.794700 0.000000vn -0.700200 0.661700 0.268000vn -0.102400 0.943500 0.315100vn -0.187600 0.794700 0.577300vn 0.038500 0.661700 0.748800vn 0.268000 0.943500 0.194700vn 0.491100 0.794700 0.356800vn 0.724000 0.661700 0.194700vn 0.889700 0.330400 -0.315100vn 0.794700 0.187600 -0.577300vn 0.574600 0.330400 -0.748800vn -0.024700 0.330400 -0.943500vn -0.303500 0.187600 -0.934200vn -0.534600 0.330400 -0.777900vn -0.905000 0.330400 -0.268000vn -0.982200 0.187600 0.000000vn -0.905000 0.330400 0.268000vn -0.534600 0.330400 0.777900vn -0.303500 0.187600 0.934200vn -0.024700 0.330400 0.943500vn 0.574600 0.330400 0.748800vn 0.794700 0.187600 0.577300vn 0.889700 0.330400 0.315100vn 0.306600 0.125600 -0.943500vn 0.303500 -0.187600 -0.934200vn 0.024700 -0.330400 -0.943500vn -0.802600 0.125600 -0.583100vn -0.794700 -0.187600 -0.577300vn -0.889700 -0.330400 -0.315100vn -0.802600 0.125600 0.583100vn -0.794700 -0.187600 0.577300vn -0.574600 -0.330400 0.748800vn 0.306600 0.125600 0.943500vn 0.303500 -0.187600 0.934200vn 0.534600 -0.330400 0.777900vn 0.992100 0.125600 0.000000vn 0.982200 -0.187600 0.000000vn 0.905000 -0.330400 -0.268000vn 0.471300 -0.661700 -0.583100vn 0.187600 -0.794700 -0.577300vn -0.038500 -0.661700 -0.748800vn -0.408900 -0.661700 -0.628400vn -0.491100 -0.794700 -0.356800vn -0.724000 -0.661700 -0.194700vn -0.724000 -0.661700 0.194700vn -0.491100 -0.794700 0.356800vn -0.408900 -0.661700 0.628400vn 0.700200 -0.661700 -0.268000vn 0.607100 -0.794700 0.000000vn 0.331300 -0.943500 0.000000vn -0.038500 -0.661700 0.748800vn 0.187600 -0.794700 0.577300vn 0.471300 -0.661700 0.583100usemtl Material.001s offf 1/1/1 14/2/1 13/3/1f 2/4/2 14/5/2 16/6/2f 1/1/3 13/3/3 18/7/3f 1/1/4 18/7/4 20/8/4f 1/1/5 20/8/5 17/9/5f 2/4/6 16/6/6 23/10/6f 3/11/7 15/12/7 25/13/7f 4/14/8 19/15/8 27/16/8f 5/17/9 21/18/9 29/19/9f 6/20/10 22/21/10 31/22/10f 2/4/11 23/10/11 26/23/11f 3/11/12 25/13/12 28/24/12f 4/14/13 27/16/13 30/25/13f 5/17/14 29/19/14 32/26/14f 6/27/15 31/28/15 24/29/15f 7/30/16 33/31/16 38/32/16f 8/33/17 34/34/17 40/35/17f 9/36/18 35/37/18 41/38/18f 10/39/19 36/40/19 42/41/19f 11/42/20 37/43/20 39/44/20f 39/45/21 42/46/21 12/47/21f 39/45/22 37/48/22 42/46/22f 37/48/23 10/49/23 42/46/23f 42/46/24 41/50/24 12/47/24f 42/46/25 36/51/25 41/50/25f 36/51/26 9/52/26 41/50/26f 41/50/27 40/35/27 12/47/27f 41/50/28 35/53/28 40/35/28f 35/37/29 8/54/29 40/55/29f 40/35/30 38/32/30 12/47/30f 40/35/31 34/34/31 38/32/31f 34/56/32 7/57/32 38/58/32f 38/32/33 39/45/33 12/47/33f 38/32/34 33/31/34 39/45/34f 33/59/35 11/42/35 39/44/35f 24/29/36 37/43/36 11/42/36f 24/29/37 31/28/37 37/43/37f 31/22/38 10/39/38 37/60/38f 32/26/39 36/40/39 10/39/39f 32/26/40 29/19/40 36/40/40f 29/19/41 9/61/41 36/40/41f 30/25/42 35/37/42 9/36/42f 30/25/43 27/16/43 35/37/43f 27/16/44 8/54/44 35/37/44f 28/24/45 34/56/45 8/62/45f 28/24/46 25/13/46 34/56/46f 25/13/47 7/57/47 34/56/47f 26/63/48 33/64/48 7/57/48f 26/23/49 23/10/49 33/59/49f 23/10/50 11/42/50 33/59/50f 31/22/51 32/26/51 10/39/51f 31/22/52 22/21/52 32/26/52f 22/21/53 5/17/53 32/26/53f 29/65/54 30/25/54 9/36/54f 29/65/55 21/66/55 30/25/55f 21/66/56 4/14/56 30/25/56f 27/16/57 28/67/57 8/54/57f 27/16/58 19/15/58 28/67/58f 19/68/59 3/11/59 28/24/59f 25/13/60 26/63/60 7/57/60f 25/13/61 15/12/61 26/63/61f 15/12/62 2/69/62 26/63/62f 23/10/63 24/29/63 11/42/63f 23/10/64 16/6/64 24/29/64f 16/6/65 6/27/65 24/29/65f 17/9/66 22/70/66 6/71/66f 17/9/67 20/8/67 22/70/67f 20/72/68 5/17/68 22/21/68f 20/8/69 21/73/69 5/74/69f 20/8/70 18/7/70 21/73/70f 18/75/71 4/14/71 21/66/71f 18/75/72 19/15/72 4/14/72f 18/7/73 13/3/73 19/76/73f 13/3/74 3/77/74 19/76/74f 16/6/75 17/78/75 6/27/75f 16/79/76 14/2/76 17/9/76f 14/2/77 1/1/77 17/9/77f 13/80/78 15/12/78 3/11/78f 13/3/79 14/2/79 15/81/79f 14/2/80 2/82/80 15/81/80v 0.000000 -1.000000 0.000000v 0.723607 -0.447220 0.525725v -0.276388 -0.447220 0.850649v -0.894426 -0.447216 0.000000v -0.276388 -0.447220 -0.850649v 0.723607 -0.447220 -0.525725v 0.276388 0.447220 0.850649v -0.723607 0.447220 0.525725v -0.723607 0.447220 -0.525725v 0.276388 0.447220 -0.850649v 0.894426 0.447216 0.000000v 0.000000 1.000000 0.000000v -0.162456 -0.850654 0.499995v 0.425323 -0.850654 0.309011v 0.262869 -0.525738 0.809012v 0.850648 -0.525736 0.000000v 0.425323 -0.850654 -0.309011v -0.525730 -0.850652 0.000000v -0.688189 -0.525736 0.499997v -0.162456 -0.850654 -0.499995v -0.688189 -0.525736 -0.499997v 0.262869 -0.525738 -0.809012v 0.951058 0.000000 0.309013v 0.951058 0.000000 -0.309013v 0.000000 0.000000 1.000000v 0.587786 0.000000 0.809017v -0.951058 0.000000 0.309013v -0.587786 0.000000 0.809017v -0.587786 0.000000 -0.809017v -0.951058 0.000000 -0.309013v 0.587786 0.000000 -0.809017v 0.000000 0.000000 -1.000000v 0.688189 0.525736 0.499997v -0.262869 0.525738 0.809012v -0.850648 0.525736 0.000000v -0.262869 0.525738 -0.809012v 0.688189 0.525736 -0.499997v 0.162456 0.850654 0.499995v 0.525730 0.850652 0.000000v -0.425323 0.850654 0.309011v -0.425323 0.850654 -0.309011v 0.162456 0.850654 -0.499995vt 0.534208 0.190162vt 0.439232 0.259166vt 0.570486 0.301814vt 0.995860 0.190574vt 0.999792 0.292763vt 0.903537 0.265179vt 0.651606 0.190162vt 0.570486 0.078511vt 0.439232 0.121159vt 0.903535 0.129002vt 0.092743 0.471488vt 0.212444 0.449586vt 0.158053 0.569043vt 0.737562 0.458199vt 0.632422 0.499661vt 0.728679 0.579260vt 0.408849 0.471488vt 0.316315 0.457312vt 0.342992 0.573818vt 0.632005 0.463365vt 0.528550 0.449586vt 0.605329 0.564268vt 0.999792 0.072423vt 0.026885 0.573818vt 0.847656 0.509327vt 0.474160 0.569043vt 0.793444 0.309553vt 0.688302 0.255514vt 0.784558 0.198935vt 0.251881 0.000208vt 0.343838 0.078511vt 0.226439 0.078511vt 0.028577 0.072765vt 0.131462 0.009506vt 0.095186 0.121159vt 0.939981 0.570899vt 0.847658 0.645504vt 0.943912 0.673088vt 0.539471 0.666598vt 0.419770 0.688500vt 0.517323 0.757552vt 0.793441 0.077875vt 0.688301 0.119336vt 0.747790 0.000208vt 0.307560 0.190162vt 0.226439 0.301814vt 0.190162 0.190162vt 0.343838 0.301814vt 0.251881 0.380116vt 0.095186 0.259166vt 0.131462 0.370818vt 0.028577 0.307559vt 0.000208 0.190162vt 0.737565 0.689877vt 0.824936 0.743021vt 0.103663 0.688500vt 0.223364 0.666598vt 0.201217 0.757552vt 0.880811 0.006181vt 0.632005 0.680774vt 0.316315 0.674721vt 0.000208 0.674721vt 0.289222 0.564268vt 0.315898 0.680774vt 0.943912 0.452748vt 0.824932 0.386505vt 0.632423 0.635839vt 0.000208 0.457312vt 0.315898 0.463365vt 0.475509 0.009506vt 0.372624 0.072765vt 0.430997 0.380533vt 0.687884 0.078511vt 0.595927 0.000208vt 0.691911 0.380533vt 0.687884 0.301814vt 0.595927 0.380116vt 0.880815 0.362696vt 0.344255 0.190162vt 0.114890 0.380533vt 0.475509 0.370818vt 0.372624 0.307559vn 0.102400 -0.943500 0.315100vn 0.700200 -0.661700 0.268000vn -0.268000 -0.943500 0.194700vn -0.268000 -0.943500 -0.194700vn 0.102400 -0.943500 -0.315100vn 0.905000 -0.330400 0.268000vn 0.024700 -0.330400 0.943500vn -0.889700 -0.330400 0.315100vn -0.574600 -0.330400 -0.748800vn 0.534600 -0.330400 -0.777900vn 0.802600 -0.125600 0.583100vn -0.306600 -0.125600 0.943500vn -0.992100 -0.125600 0.000000vn -0.306600 -0.125600 -0.943500vn 0.802600 -0.125600 -0.583100vn 0.408900 0.661700 0.628400vn -0.471300 0.661700 0.583100vn -0.700200 0.661700 -0.268000vn 0.038500 0.661700 -0.748800vn 0.724000 0.661700 -0.194700vn 0.268000 0.943500 -0.194700vn 0.491100 0.794700 -0.356800vn 0.408900 0.661700 -0.628400vn -0.102400 0.943500 -0.315100vn -0.187600 0.794700 -0.577300vn -0.471300 0.661700 -0.583100vn -0.331300 0.943500 0.000000vn -0.607100 0.794700 0.000000vn -0.700200 0.661700 0.268000vn -0.102400 0.943500 0.315100vn -0.187600 0.794700 0.577300vn 0.038500 0.661700 0.748800vn 0.268000 0.943500 0.194700vn 0.491100 0.794700 0.356800vn 0.724000 0.661700 0.194700vn 0.889700 0.330400 -0.315100vn 0.794700 0.187600 -0.577300vn 0.574600 0.330400 -0.748800vn -0.024700 0.330400 -0.943500vn -0.303500 0.187600 -0.934200vn -0.534600 0.330400 -0.777900vn -0.905000 0.330400 -0.268000vn -0.982200 0.187600 0.000000vn -0.905000 0.330400 0.268000vn -0.534600 0.330400 0.777900vn -0.303500 0.187600 0.934200vn -0.024700 0.330400 0.943500vn 0.574600 0.330400 0.748800vn 0.794700 0.187600 0.577300vn 0.889700 0.330400 0.315100vn 0.306600 0.125600 -0.943500vn 0.303500 -0.187600 -0.934200vn 0.024700 -0.330400 -0.943500vn -0.802600 0.125600 -0.583100vn -0.794700 -0.187600 -0.577300vn -0.889700 -0.330400 -0.315100vn -0.802600 0.125600 0.583100vn -0.794700 -0.187600 0.577300vn -0.574600 -0.330400 0.748800vn 0.306600 0.125600 0.943500vn 0.303500 -0.187600 0.934200vn 0.534600 -0.330400 0.777900vn 0.992100 0.125600 0.000000vn 0.982200 -0.187600 0.000000vn 0.905000 -0.330400 -0.268000vn 0.471300 -0.661700 -0.583100vn 0.187600 -0.794700 -0.577300vn -0.038500 -0.661700 -0.748800vn -0.408900 -0.661700 -0.628400vn -0.491100 -0.794700 -0.356800vn -0.724000 -0.661700 -0.194700vn -0.724000 -0.661700 0.194700vn -0.491100 -0.794700 0.356800vn -0.408900 -0.661700 0.628400vn 0.700200 -0.661700 -0.268000vn 0.607100 -0.794700 0.000000vn 0.331300 -0.943500 0.000000vn -0.038500 -0.661700 0.748800vn 0.187600 -0.794700 0.577300vn 0.471300 -0.661700 0.583100usemtl Material.001s offf 1/1/1 14/2/1 13/3/1f 2/4/2 14/5/2 16/6/2f 1/1/3 13/3/3 18/7/3f 1/1/4 18/7/4 20/8/4f 1/1/5 20/8/5 17/9/5f 2/4/6 16/6/6 23/10/6f 3/11/7 15/12/7 25/13/7f 4/14/8 19/15/8 27/16/8f 5/17/9 21/18/9 29/19/9f 6/20/10 22/21/10 31/22/10f 2/4/11 23/10/11 26/23/11f 3/11/12 25/13/12 28/24/12f 4/14/13 27/16/13 30/25/13f 5/17/14 29/19/14 32/26/14f 6/27/15 31/28/15 24/29/15f 7/30/16 33/31/16 38/32/16f 8/33/17 34/34/17 40/35/17f 9/36/18 35/37/18 41/38/18f 10/39/19 36/40/19 42/41/19f 11/42/20 37/43/20 39/44/20f 39/45/21 42/46/21 12/47/21f 39/45/22 37/48/22 42/46/22f 37/48/23 10/49/23 42/46/23f 42/46/24 41/50/24 12/47/24f 42/46/25 36/51/25 41/50/25f 36/51/26 9/52/26 41/50/26f 41/50/27 40/35/27 12/47/27f 41/50/28 35/53/28 40/35/28f 35/37/29 8/54/29 40/55/29f 40/35/30 38/32/30 12/47/30f 40/35/31 34/34/31 38/32/31f 34/56/32 7/57/32 38/58/32f 38/32/33 39/45/33 12/47/33f 38/32/34 33/31/34 39/45/34f 33/59/35 11/42/35 39/44/35f 24/29/36 37/43/36 11/42/36f 24/29/37 31/28/37 37/43/37f 31/22/38 10/39/38 37/60/38f 32/26/39 36/40/39 10/39/39f 32/26/40 29/19/40 36/40/40f 29/19/41 9/61/41 36/40/41f 30/25/42 35/37/42 9/36/42f 30/25/43 27/16/43 35/37/43f 27/16/44 8/54/44 35/37/44f 28/24/45 34/56/45 8/62/45f 28/24/46 25/13/46 34/56/46f 25/13/47 7/57/47 34/56/47f 26/63/48 33/64/48 7/57/48f 26/23/49 23/10/49 33/59/49f 23/10/50 11/42/50 33/59/50f 31/22/51 32/26/51 10/39/51f 31/22/52 22/21/52 32/26/52f 22/21/53 5/17/53 32/26/53f 29/65/54 30/25/54 9/36/54f 29/65/55 21/66/55 30/25/55f 21/66/56 4/14/56 30/25/56f 27/16/57 28/67/57 8/54/57f 27/16/58 19/15/58 28/67/58f 19/68/59 3/11/59 28/24/59f 25/13/60 26/63/60 7/57/60f 25/13/61 15/12/61 26/63/61f 15/12/62 2/69/62 26/63/62f 23/10/63 24/29/63 11/42/63f 23/10/64 16/6/64 24/29/64f 16/6/65 6/27/65 24/29/65f 17/9/66 22/70/66 6/71/66f 17/9/67 20/8/67 22/70/67f 20/72/68 5/17/68 22/21/68f 20/8/69 21/73/69 5/74/69f 20/8/70 18/7/70 21/73/70f 18/75/71 4/14/71 21/66/71f 18/75/72 19/15/72 4/14/72f 18/7/73 13/3/73 19/76/73f 13/3/74 3/77/74 19/76/74f 16/6/75 17/78/75 6/27/75f 16/79/76 14/2/76 17/9/76f 14/2/77 1/1/77 17/9/77f 13/80/78 15/12/78 3/11/78f 13/3/79 14/2/79 15/81/79f 14/2/80 2/82/80 15/81/80each line starting with v defines a vertex' position in 3D-space.So I'm reading all these in and create new Vector4 to get homogeneous coordinates.I ignore vn for now, these would be the normals.For vt - specifying texture coordinates - I do the same as for the positions.Now f is prepending the really interesting lines. These are faces, triangles defined as triples of (vertexposition/texturecoordinates/normals). Each of these define which index of positions and texturecoordinates maps together.So, for example in f 1/1/1 14/2/1 13/3/1 1/1/1 would mean that positions(1) has the texture coordinates(1) (and the last 1 in the first triple would stand for the normal in normals(1))14/2/1 would mean that positions(14) and texturecoordinates(2) map together.The whole line would connect these 3 vertices, like (1,14,13) would build up a triangle.Am I understanding this correctly until here?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)))Would give me a List of these triples. For each of the entries I would then create a Vertex:val vertices: List[Vertex] = for(face <- faces) yield(new Vertex(positions(face._1-1), textureCoordinates(face._2-1)))Vertex is defined like so:case class Vertex(position: Vector4,/* normal: Vector4,*/ textureCoordinates: Vector2)I need to do "-1" on each face to avoid off-by-one and IndexOutOfBoundsExceptions, as the index of a List starts with 0 while the indices are starting with 1.The last thing are the indices: In OpenGL you may use an ELEMENT_ARRAY_BUFFER to specify which vertices are connected and to save (possibly a lot) of data.As I said earlier, each face f 1/1/1 14/2/1 13/3/1 is built up like this v1/t1/n1 v2/t2/n2 v3/t3/n3 (for each line) so I only need to extract v1,v2,v3, pack it into one long List and let OpenGL do the rest.v1/v2/v3/v4/v5/v6/.../vn would then be my indicesAm I right or is something wrong until here?Last but not least I would create a new model:class Model(vertices: Array[Vertex], indices: Array[Int]/*, textures: Array[Texture]*/){ // Create VAO, VBO and a buffer for the indices val vao: Int = glGenVertexArrays val vbo: Int = glGenBuffers val ibo: Int = glGenBuffers setup private def setup(): Unit = { val interleavedBuffer: FloatBuffer = prepareVertexBuffer(vertices) val indicesBuffer: IntBuffer = prepareIntBuffer(indices) // One VAO to bind them all! glBindVertexArray(vao) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) // Fill buffers with data glBufferData(GL_ARRAY_BUFFER, interleavedBuffer, GL_STATIC_DRAW) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW) // Set vertex attribute pointers glVertexAttribPointer(0, 4, GL_FLOAT, false, 4*6, 0) // 0 = Position = Vector4(x,y,z,w) glVertexAttribPointer(1, 2, GL_FLOAT, false, 4*6, 4*4) // 1 = Texture Coordinates = Vector2(x,y) -> 2 (coordinates) * 4 (byte-size of float) => stride = 4 (position) + 2 (texture coordinates) = 6 * 4 (byte-size of float); offset = 4 (position) * 4 (byte-size of float) glBindBuffer(GL_ARRAY_BUFFER, 0) glBindVertexArray(0) } private def prepareIntBuffer(data: Array[Int]): IntBuffer = { val buffer: IntBuffer = BufferUtils.createIntBuffer(data.length) buffer.put(data) buffer.flip // Make the buffer readable buffer } private def prepareVertexBuffer(vertices: Array[Vertex]): FloatBuffer = { val positions: Array[Float] = vertices.map(v => v.position).map(_.toArray).flatten val textureCoordinates: Array[Float] = vertices.map(v => v.textureCoordinates).map(_.toArray).flatten // TODO remove hardcoded sizes for grouping val zipped: Array[Float] = (positions.grouped(4) zip textureCoordinates.grouped(2)).toArray.flatMap {case (x,y) => x ++ y} val buffer: FloatBuffer = BufferUtils.createFloatBuffer(zipped.size) buffer.put(zipped) buffer.flip buffer }The problem is, the output looks like this:While actually it should be an Ico-Sphere from Blender.So, something is really off with my implementation or understanding of the .obj-file-format.I assume it has to do with the indices, but no idea. I've debugged a lot, manually compared the different Lists with the actual .obj-file but to no avail. Everything looks okay, so I assume it's really my understanding.edit: I'm not yet sure what exactly I am misunderstanding but, instead of going through the faces to create a List of Vertices (my own class) I gave the model all the positions and texture coordinates and voilà, the meshes appearance looks right (apart from wrong texturing).So what I did:class Model(positions: Array[Vector3], texcoords: Array[Vector2], indices: Array[Int])private def prepareVertexBuffer(positions: Array[Vector3], texcoords: Array[Vector2]): FloatBuffer = { val pos: Array[Float] = positions.map(_.toArray).flatten val textureCoordinates: Array[Float] = texcoords.map(_.toArray).flatten // TODO remove hardcoded sizes for grouping val zipped: Array[Float] = (pos.grouped(3) zip textureCoordinates.grouped(2)).toArray.flatMap {case (x,y) => x ++ y} println(zipped.toList.grouped(5).mkString("\n")) val buffer: FloatBuffer = BufferUtils.createFloatBuffer(zipped.size) buffer.put(zipped) buffer.flip buffer }(Note: I've also switched back to Vector3 instead of Vector4 for now. Thought there might be a problem with it)val indices: List[Int] = faces.map(f => f._1-1) // Wrong?new Model(vertices.toArray, indices.toArray)The rest is the same. So I guess my texturing problem results from this.I now have only 42 vertices instead of 240 (coming from the faces), so I lose a lot of combinations.The current result:editThe problem boils down to the difference between .obj and opengl. OpenGL may only use one index-buffer while in .obj there is one for each attribute.So I need to manually merge these in an appropriate way. I've found C++ functions for that and although I know C++ quite a bit, I'm not sure why it's not working on my end with Scala.I simply do not find any duplicates... 解决方案 What you're doing is v1-t1 v2-t2, v3-t3 etc. If you look at your original .obj file, thats probably not whats happening. It could be v1-t1, v2-t1, v3-t1. Who knows. So what you need to do is find the texcoord matching to the vertex. This will get you close to what you want, but not quite there, because in .OBJ files 1 vertex can have multiple texcoords, and right now you can't handle that yet. I have answered a similar question here:OpenGL - texture are mapped incorrectly using glDrawElements(Note that you don't HAVE to get rid of the index buffer, but its easier) 这篇关于.obj-Loader-输出不完全正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-13 00:21