douju5062 2018-03-23 23:34
浏览 130
已采纳

Opengl模型矩阵拉伸顶点

I am working on a simple opengl game to learn more about it. But for some reason when I try to rotate my cube over time it becomes stretched. You can see it in the photo:

enter image description here

I think it has to do with my model matrix but I'm not sure. Here is a portion of my code:

    model := mgl32.Ident4()                                                               
    model = model.Mul4(mgl32.HomogRotate3D(float32(glfw.GetTime()) * 
    mgl32.DegToRad(50.0), mgl32.Vec3{0.5, 1.0, 0.0}))

    view := mgl32.Ident4()
    view = view.Mul4(mgl32.Translate3D(0.0, 0.0, -3.0))

    projection := mgl32.Ident4()
    projection = mgl32.Perspective(mgl32.DegToRad(45.0), 800/ float32(600), 0.1, 100)

    shader.setMat4("model", model)
    shader.setMat4("view", view)
    shader.setMat4("projection", projection)

Here is my Minimal, Complete, and Verifiable example. I've removed the texture as it's not necessary to see the problem. I am also using wireframe so that you can see the stretching more clearly.

package main

import(
     "github.com/go-gl/glfw/v3.2/glfw"
     "github.com/go-gl/gl/v4.5-core/gl"
     "log"
     "strings"
     "fmt"
     "github.com/go-gl/mathgl/mgl32"
 )

 const(
     vertexShaderSource = `
         #version 450 core
         layout (location = 0) in vec3 aPos;

        uniform mat4 model;
        uniform mat4 view;
        uniform mat4 projection;

        void main()
        {
            gl_Position = projection * view * model * vec4(aPos, 1.0);
        }` + "\x00"

    fragmentShaderSource =  `
        #version 450 core
        out vec4 FragColor;

        void main()
        {
            FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
        }` + "\x00"
)

var(
    vertices = []float32 {
        -0.5,0.5,-0.5,
        -0.5,-0.5,-0.5,
        0.5,-0.5,-0.5,
        0.5,0.5,-0.5,

        -0.5,0.5,0.5,
        -0.5,-0.5,0.5,
        0.5,-0.5,0.5,
        0.5,0.5,0.5,

        0.5,0.5,-0.5,
        0.5,-0.5,-0.5,
        -0.5,-0.5,0.5,
        -0.5,0.5,0.5,

        -0.5,0.5,-0.5,
        -0.5,-0.5,0.5,
        0.5,-0.5,0.5,
    }
    indices = []int {
        0,1,3, // -z
        3,1,2,
        5,4,7, // +z
        7,6,5,
        9,8,7, // +x
        7,6,9,
        0,1,11, // -x
        11,1,10,
        12,4,7, // +y
        7,3,12,
        13,1,14,  // -y
        14,1,2,
    }
)


func main() {
    if err := glfw.Init(); err != nil {
        log.Fatalln("failed to initialize glfw:", err)
    }
    defer glfw.Terminate()

    glfw.WindowHint(glfw.ContextVersionMajor, 4)
    glfw.WindowHint(glfw.ContextVersionMinor, 5)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

    window, err := glfw.CreateWindow(800, 600, "LearnOpenGL", nil, nil)
    if err != nil {
        panic(err)
    }
    defer window.Destroy()

    window.MakeContextCurrent()

    // Initialize Glow
    if err := gl.Init(); err != nil {
        panic(err)
    }

    gl.Viewport(0, 0, 800, 600)

    //SHADERS
    vertexID, err := loadShader(vertexShaderSource, gl.VERTEX_SHADER)
    if err != nil {
        panic(err)
    }
    fragmentID, err := loadShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
    if err != nil {
        panic(err)
    }

    programID := gl.CreateProgram()
    gl.AttachShader(programID, vertexID)
    gl.AttachShader(programID, fragmentID)

    gl.DeleteShader(vertexID)
    gl.DeleteShader(fragmentID)

    gl.LinkProgram(programID)

    // VBO / VAO / EBO data
    var vbo, vao, ebo uint32
    gl.GenVertexArrays(1, &vao)
    gl.GenBuffers(1, &vbo)
    gl.GenBuffers(1, &ebo)
    gl.BindVertexArray(vao)

    gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
    gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

    gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
    gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indices) * 4, gl.Ptr(indices), gl.STATIC_DRAW)

    gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, gl.PtrOffset(0))
    gl.EnableVertexAttribArray(0)

    gl.BindBuffer(gl.ARRAY_BUFFER, 0)
    gl.BindVertexArray(0)

    gl.Enable(gl.DEPTH_TEST)
    gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)

    for !window.ShouldClose() {
        gl.ClearColor(0.2, 0.3, 0.3, 1.0)
        gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

        gl.UseProgram(programID)

        model := mgl32.Ident4()
        view := mgl32.Ident4()
        projection := mgl32.Ident4()
        model = model.Mul4(mgl32.HomogRotate3D(float32(glfw.GetTime()) * mgl32.DegToRad(50), mgl32.Vec3{0.5, 1, 0}))
        view = view.Mul4(mgl32.Translate3D(0, 0, -3))
        projection = projection.Mul4(mgl32.Perspective(mgl32.DegToRad(45), 800/float32(600), 0.1, 100))

        gl.UniformMatrix4fv(gl.GetUniformLocation(programID, gl.Str("model\x00")), 1, false, &model[0])
        gl.UniformMatrix4fv(gl.GetUniformLocation(programID, gl.Str("view\x00")), 1, false, &view[0])
        gl.UniformMatrix4fv(gl.GetUniformLocation(programID, gl.Str("projection\x00")), 1, false, &projection[0])

        gl.BindVertexArray(vao)
        gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, gl.PtrOffset(0))

        window.SwapBuffers()
        glfw.PollEvents()
        }
    }

func loadShader(file string, shaderType uint32) (uint32, error) {
    shader := gl.CreateShader(shaderType)

    csources, free := gl.Strs(string(file))
    gl.ShaderSource(shader, 1, csources, nil)
    free()
    gl.CompileShader(shader)

    var status int32
    gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
    if status == gl.FALSE {
        var logLength int32
        gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

        log := strings.Repeat("\x00", int(logLength+1))
        gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))

        return 0,fmt.Errorf("failed to compile %v: %v", file, log)
    }
    return shader, nil
}
  • 写回答

1条回答 默认 最新

  • doubian6241 2018-03-26 20:51
    关注

    HomogRotate3D creates a 3D rotation Matrix that rotates by (radian) angle about some arbitrary axis given by a Normalized Vector.

    mgl32.Vec3{1, 1, 0}.Normalize()
    

    Is apparently all you needed is normalize the axis of rotation vector and it stops the distortion. So your model rotation line:

    model = model.Mul4(mgl32.HomogRotate3D(float32(glfw.GetTime()) * 
        mgl32.DegToRad(50.0), mgl32.Vec3{1.0, 1.0, 0.0}.Normalize()))
    

    Have fun!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置