1
\$\begingroup\$

I have been following the 3d game engine tutorial series made by theBennyBox for a few months now, and have finally decided to create a game with it. My problem is that it is a 3d only engine which does not have an easy way to draw in 2d.

The code for it is here (github)

I've figured that I need to change the camera class to take in a generic matrix, so The old camera class was this:

    public class Camera extends GameComponent
    {
    private Matrix4f projection;

    public Camera(float fov, float aspect, float zNear, float zFar)
{
    this.projection = new Matrix4f().initPerspective(fov, aspect, zNear, zFar);
}

public Matrix4f getViewProjection()
{
    Matrix4f cameraRotation = getTransform().getTransformedRot().conjugate().toRotationMatrix();
    Vector3f cameraPos = getTransform().getTransformedPos().mul(-1);

    Matrix4f cameraTranslation = new Matrix4f().initTranslation(cameraPos.getX(), cameraPos.getY(), cameraPos.getZ());

    return projection.mul(cameraRotation.mul(cameraTranslation));
}

@Override
public void addToEngine(CoreEngine engine)
{
    engine.getRenderingEngine().addCamera(this);
}

}

To this:

public class Camera extends GameComponent
{
    private Matrix4f projection;

public Camera(Matrix4f projection)
{
    this.projection = projection; 
}

public Matrix4f getViewProjection()
{
    Matrix4f cameraRotation = getTransform().getTransformedRot().conjugate().toRotationMatrix();
    Vector3f cameraPos = getTransform().getTransformedPos().mul(-1);

    Matrix4f cameraTranslation = new Matrix4f().initTranslation(cameraPos.getX(), cameraPos.getY(), cameraPos.getZ());

    System.out.println("Location: " + cameraPos.getX() + " " + cameraPos.getY() + " " + cameraPos.getZ());
    return projection.mul(cameraRotation.mul(cameraTranslation));
}


@Override
public void addToEngine(CoreEngine engine)
{
    engine.getRenderingEngine().setMainCamera(this);
}

}

But now I don't know what to do. I can't work out how to switch between 2 cameras and draw in 2d.

How would I achieve this? Thanks!!

\$\endgroup\$
  • \$\begingroup\$ If the core is OpenGL, and you have direct access to the OpenGL objects. You can just use OpenGL 2D rendering and bypass the engine itself. \$\endgroup\$ – Lex Webb Apr 29 '14 at 16:26
  • \$\begingroup\$ 3D rendering is a generalisation of 2D rendering; OpenGL is perfectly 2D-capable. Use an appropriate transformation matrix and ignore the z-coordinate. \$\endgroup\$ – Anko Apr 29 '14 at 16:28
  • 1
    \$\begingroup\$ To expand on what @Anko said, just make use of a Othographic projection matrix, and maybe a viewport with different dimensions to simplify exact placing of objects in screen space. As for switching, it will be best to do all 3D rendering first, then switch to the new matrix/viewport at the end - ensuring that the 2D elements are rendered on top (you can also disable z-buffering unless you want it for 2D). \$\endgroup\$ – free3dom Apr 30 '14 at 8:15

Your Answer

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

Browse other questions tagged or ask your own question.