Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

ModelViewProjection

edited September 2016 in Shaders Posts: 82

Just a simple question, but confusing to ke anyway... I have this:

Lua

self.mesh.shader.mModel = modelMatrix()
self.mesh.shader.mView = viewMatrix()
self.mesh.shader.mProjection = projectionMatrix()

Vertexshader

uniform mat4 mModel;
uniform mat4 mView;
uniform mat4 mProjection;
...
void main()
{
    ...
    mat4 mvp = mProjection * mView * mModel;
    gl_Position = mvp * position;
    // gl_Position = modelViewProjection * position;
}

I'm confused, because I thought my expression for mvp should be identical to the predefined modelViewProjection, but it isn't working; my mesh isn't showing at all.

Anyone knows how to accomplish this? Eventually I wanna scale the mModel*position coordinates.

Comments

  • Posts: 2,020

    You wrote:

    Eventually I wanna scale the mModel*position coordinates.

    So, presumably what you eventually want then is a viewProjectionMatrix.

    Here's the easiest way to do this:


    function Entity:draw() -- grab the modelMatrix pushMatrix() translate(self.pos.x, self.pos.y, self.pos.z) rotate(self.euler.x, 1,0,0) -- rest of rotations self.mesh.shader.mModel = modelMatrix() -- now for the clever part: reset to the identity matrix before you draw resetMatrix() self.mesh:draw() popMatrix() end

    If you reset the model matrix before you draw, then it means the model matrix passed into the shaders MVPM is an identity matrix:

    1,0,0,0
    0,1,0,0
    0,0,1,0
    0,0,0,1
    

    And this means that the shaders modelViewProjectionMatrix is in fact just a viewProjectionMatrix. You've effectively removed the modelMatrix out of the equation by resetting it to the identity.

    The model's position then comes not from the automatic MVPM, but from the mMatrix you passed in.

    So in the shader, this will be correct:

      gl_Position = modelViewProjection * mModel * position;
    

    This will also be much more perfomant than recalculating the MVP in each run through the vertex shader.

    Sometimes I wish the Codea forum would allow answers to be up/ down-voted :p B)

  • Posts: 82

    Turned out the problem was that the shader variables for each matrix weren't updated every step.

    Only problem now is that the rendering order is reversed somehow. Everything in the back is rendered in in front and vice versa. So I'm still a little bit confused...

  • Posts: 82

    @yojimbo2000 Your solution worked well! Thank you so much.

  • Posts: 2,020

    I didn't come up with this method btw. I think it might've been one of @Ignatz 's?

  • Posts: 2,020

    Ok, I found the thread where we discussed this before, it was @Ignatz who came up with this technique:

    https://codea.io/talk/discussion/comment/62198/#Comment_62198

Sign In or Register to comment.