doudao3170 2016-03-03 14:15
浏览 45

OpenGL中的错误绘图模型

So i'm trying to a basic opengl program that reads in an .obj file and then renders it (the obj file contains triangle vertices in the format v 1.000000 2.000000, 3.00000.

So here's my code to do that (im using GLFW and a Golang binding library for it) (also im currently ignoring normals).

gl.Begin(gl.TRIANGLES)
for scanner.Scan() {
    var v0, v1, v2 float32
    t := strings.Split(scanner.Text(), " ")
    line := scanner.Text()
    fmt.Sscanf(line, "v %f %f %f", &v0, &v1, &v2)
    if t[0] == "v" {
        gl.Vertex3f(v0, v1, v2)
    }
}
gl.End()

However when I go to run this the result isnt quite right

enter image description here

I tried it on a few other files I found around the internet and I get similar results. It almost seems like the triangles vertices are offset or something.

  • 写回答

2条回答 默认 最新

  • duanhua5523 2016-03-03 14:26
    关注

    You should read all vertices data into array. Obj file contains information about triangles, and how vertices should be indexed. Lines starting with f contains 3 numbers which describes triangles. So your code should look like this (I don't know go):

    var vertices [1000*3] float32
    for scanner.Scan() {
        var i=0
        var v0, v1, v2 float32
        var vv byte
        t := strings.Split(scanner.Text(), " ")
        line := scanner.Text()
        fmt.Sscanf(line, "%c %f %f %f", &vv, &v0, &v1, &v2)
        if vv == "v" {
            vertices[i] = v0
            vertices[i+1] = v1
            vertices[i+2] = v2
        }
        i = i+3
    }
    gl.Begin(gl.TRIANGLES)
    for scanner.Scan() {
        var v0, v1, v2 int
        var vv byte
        t := strings.Split(scanner.Text(), " ")
        line := scanner.Text()
        fmt.Sscanf(line, "%c %d %d %d", &vv, &v0, &v1, &v2)
        if vv == "f" {
            gl.Vertex3f(vertices[v0], vertices[v1], vertices[v2])
        }
    }
    gl.End()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值