[GLSL] Simplify this vertex passthrough

In a shader I’m making for a simple little side-project, I ended up needing to do this to make the shader be more flexible:

if (useViewMatrix == 1) {
	gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
} else {
	gl_Position = projectionMatrix * modelMatrix * vec4(position, 1.0);
}

I was wondering, however, if there was a way to simplify this down to 1 line.

You could look into using subroutines, which wont make this into one line but will remove the if statement. (probably overkill)

If you are using the same ViewMatrix across the board then you could mutliply the Projection and View matrix on the CPU side and send a ViewProjectionMatrix. If you are not using the view matrix just send the Projection Matrix as is for the ViewProjectionMatrix

edit:

if (useViewMatrix == 1) {gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);} else {gl_Position = projectionMatrix * modelMatrix * vec4(position, 1.0);}

1 line, problem solved :slight_smile:

Pre multiply this

projectionMatrix * viewMatrix * modelMatrix

at cpu. Cpu can then decide per draw call is view matrix used or not. You can’t get simpler than that.

Yah I just figured that :slight_smile:
Makes everything much faster.