0
\$\begingroup\$

I have recently started writing a game engine in C++ with OpenGL and using some tutorials online. So far from what I have seen, shaders are the way to manipulate an object's position in worldspace. Is this true or is there a way to write a physics engine which can work without shaders in order to "move" and object in worldspace?

\$\endgroup\$
2
\$\begingroup\$

Shaders are just programs that run on different stages of the graphics pipeline. You say:

So far from what I have seen, shaders are the way to manipulate an object's position in worldspace.

This is partially correct, as this is not the only thing shaders can do. Moving an object's position in world space would be the job of a vertex shader.

Vertex shaders work on each vertex passed to it. A simple shader that moves an object would be like:

#version 410 core
uniform mat4 mvp;
layout(location = 0) in vec4 vPosition;
void main() {
  gl_Position = vPosition * mvp;
}

This shader simply does for each vertex multiply by matrix mvp.

The mvp is just a matrix comprised of a Model Matrix(the objects position), View Matrix(the view/"camera" matrix) and a Projection Matrix(usually a perspective projection martix).

There is a great OpenGL tutorial here which should help you further understand OpenGL, shaders and how matrices work to move your objects.

\$\endgroup\$
1
\$\begingroup\$

Physics engine and shaders are unrelated. A game engine stores object's location/orientation somewhere and both shaders and physics engine can use it. Vertex shaders can modify the position to change an object's appearance but it's a visual thing, the rest of the game engine doesn't automatically get the modified position.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.