Vertex position sent to shader changing when moving camera?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Vertex position sent to shader changing when moving camera?

Post by Locien »

Hello. I'm very new to shaders and graphics programming in general but I've been experimenting with simple shaders to understand how they work. One thing I don't understand is why the position of the vertices seems to change in my vertex shader when I move around the camera position and change the angle. I tried to display the coordinates of two triangles as output color but the color changes depending on how far away the camera is from the triangles and what angle it has. Why does it do this? Am I doing something wrong and not accessing the real in world coordinates?

Here's what my Irrlicht side shader code looks like:

Code: Select all

virtual void OnSetConstants(video::IMaterialRendererServices* Services, s32 UserData)
    {
 
        IVideoDriver* IrrDriver = Services->getVideoDriver();
 
 
        matrix4 ModelMatrix = IrrDriver->getTransform(video::ETS_WORLD);
        Services->setVertexShaderConstant("M", ModelMatrix.pointer(), 16);
 
        matrix4 ViewMatrix = IrrDriver->getTransform(video::ETS_VIEW);
        Services->setVertexShaderConstant("V", ViewMatrix.pointer(), 16);
 
        matrix4 ProjectionMatrix = IrrDriver->getTransform(video::ETS_PROJECTION);
        Services->setVertexShaderConstant("P", ProjectionMatrix.pointer(), 16);
 
    }
And here are the shaders:

Code: Select all

out vec4 ColorTest;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
 
void main()
{
    
    mat4 MV = V * M;
    mat4 MVP = P * MV;
    gl_Position = MVP * gl_Vertex;
    
    ColorTest = vec4(gl_Position.x, gl_Position.y, gl_Position.z, 255);
    
}

Code: Select all

in vec4 ColorTest;
 
void main()
{
 
    gl_FragColor = ColorTest;
    
}
Here's what it looks like:
Image

I'm thankful for any help as shaders are very confusing, hard to debug and the tutorials out there aren't too fantastic :(
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Vertex position sent to shader changing when moving came

Post by Foaly »

You are multiplying the coordinates with the matrices, which transforms them into camera space, and you create the coler after it.
So your colors will represent the coords in camera space.
If you only multiply with the model matrix, you'll get the coords in world space, afaik.
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Re: Vertex position sent to shader changing when moving came

Post by Locien »

That fixed it. Thank you for your help :)

This stuff is very confusing since I don't know anything about low level graphics.
Post Reply