go-gl / mathgl

A pure Go 3D math library.
BSD 3-Clause "New" or "Revised" License
554 stars 65 forks source link

how to convert glm to mgl32? #53

Closed godispy closed 8 years ago

godispy commented 8 years ago
//------------------cpp glm code--------------------------------
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" );

// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");

// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates

// Camera matrix
glm::mat4 View       = glm::lookAt(
                            glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
                            glm::vec3(0,0,0), // and looks at the origin
                            glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                       );
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model      = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around
dmitshur commented 8 years ago

Translating C++ glm code to Go mathgl is quite doable, since the API is modeled to be similar.

For example, a C++ vec3:

glm::vec3(4,3,3)

Would look like this in Go:

mgl64.Vec3{4, 3, 3}

A bigger example:

glm::mat4 View       = glm::lookAt(
                            glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
                            glm::vec3(0,0,0), // and looks at the origin
                            glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                       );

Would be:

view := mgl64.LookAtV(
    mgl64.Vec3{4, 3, 3},
    mgl64.Vec3{0, 0, 0},
    mgl64.Vec3{0, 1, 0},
}

I recommend the following resources:

godispy commented 8 years ago

It doesn't work!! //---------------------------------- programID := LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" ) gl.BindFragDataLocation(programID, 0, gl.Str("MVP\x00"))

projection:=mgl32.Perspective(45.0, 4.0 / 3.0, 0.1, 100.0)

camera:=mgl32.LookAtV(mgl32.Vec3{4,3,3},mgl32.Vec3{0,0,0},mgl32.Vec3{0,1,0})

model := mgl32.Ident4()

var MVP mgl32.Mat4
MVP=MVP.Mul4(projection)
MVP=MVP.Mul4(camera)
MVP=MVP.Mul4(model)
modelUniform := gl.GetUniformLocation(programID, gl.Str("MVP\x00"))
gl.UniformMatrix4fv(modelUniform, 1, false, &MVP[0])
godispy commented 8 years ago

cpp code:https://github.com/opengl-tutorials/ogl/tree/master/tutorial03_matrices

godispy commented 8 years ago

The golang code contains "github.com/veandco/go-sdl2/sdl" "github.com/go-gl/gl/v3.3-core/gl" "github.com/go-gl/mathgl/mgl32"

dmitshur commented 8 years ago

It doesn't work!!

What's the error message?

godispy commented 8 years ago

Thx shurcooL!! I solved this problem!! go-gl/mathgl is a Great Gopackage!

dmitshur commented 8 years ago

Good to hear.