Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert glm to mgl32? #53

Closed
godispy opened this issue Jan 22, 2016 · 7 comments
Closed

how to convert glm to mgl32? #53

godispy opened this issue Jan 22, 2016 · 7 comments
Labels

Comments

@godispy
Copy link

godispy commented Jan 22, 2016

//------------------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
Copy link
Member

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
Copy link
Author

godispy commented Jan 22, 2016

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
Copy link
Author

godispy commented Jan 22, 2016

@godispy
Copy link
Author

godispy commented Jan 22, 2016

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
Copy link
Member

It doesn't work!!

What's the error message?

@godispy
Copy link
Author

godispy commented Jan 22, 2016

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

@dmitshur
Copy link
Member

Good to hear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants