Adding Camera Support

The two most important considerations when writing a renderer for the first time are the camera and the light source. If either of these is set improperly, you might see nothing on the screen and incorrectly assume that nothing is being rendered. In fact, something is often being rendered even when you don’t see anything, but due to the camera’s orientation or the lighting conditions of the scene, you might not see anything. Remember those two issues while you peruse the code in this chapter—camera and lighting.

Let’s add camera support to the Advanced2D engine. The camera determines what you see on the screen. The camera may be positioned anywhere in 3D space, as well as pointed in any direction in 3D space. The following Camera.h definition provides support for the projection and view matrices for our engine. Because we aren’t truly studying 3D rendering—only using Direct3D as a tool for our upcoming 2D game projects—I will leave the details to another resource. Note that the Camera.h file has already been added to the list of included files in Advanced2D.h. So any new project you create using the Advanced2D engine may simply include Advanced2D.h, rather than all of the individual header files.

Advice

Here are two good reference books that will teach you the ins and outs of Direct3D rendering: 3D Game Engine Programming (Thomson Course Technology PTR, 2004) by Stefan Zerbst and Oliver Duvel and Advanced Visual Effects with Direct3D (Thomson Course Technology PTR, 2005) by Peter Walsh.


#pragma once
#include "Advanced2D.h"

namespace Advanced2D {
class Camera
{
private:
    D3DXMATRIX p_matrixProj;
    D3DXMATRIX p_matrixView;
    D3DXVECTOR3 p_updir;

    D3DXVECTOR3 p_position;
    D3DXVECTOR3 p_target;
    float p_nearRange;
    float p_farRange;
    float p_aspectRatio;
    float p_fov;
public:
    Camera(void);
    ~Camera(void);
    void setPerspective(float fov, float aspectRatio, float nearRange,
float farRange);
    float getNearRange() { return p_nearRange; }
    void setNearRange(float value) { p_nearRange = value; }
    float getFarRange() { return p_farRange; }
    void setFarRange(float value) { p_farRange = value; }
    float getAspectRatio() { return p_aspectRatio; }
    void setAspectRatio(float value) { p_aspectRatio = value; }
    float getFOV() { return p_fov; }
    void setFOV(float value) { p_fov = value; }
    void Update();

    D3DXVECTOR3 getPosition() { return p_position; }
    void setPosition(float x, float y, float z);
    void setPosition(D3DXVECTOR3 position);
    float getX() { return p_position.x; }
    void setX(float value) { p_position.x = value; }
    float getY() { return p_position.y; }
    void setY(float value) { p_position.y = value; }
    float getZ() { return p_position.z; }
    void setZ(float value) { p_position.z = value; }

    D3DXVECTOR3 getTarget() { return p_target; }
    void setTarget(D3DXVECTOR3 value) { p_target = value; }
    void setTarget(float x, float y, float z) {
        p_target.x = x; p_target.y = y; p_target.z = z;
    }
};
};

Now for the Camera.cpp source code. Because our header file included so many accessors and mutators, the implementation file is rather short in comparison.

#include "Camera.h"

namespace Advanced2D {
Camera::Camera(void)

{
    p_position = D3DXVECTOR3(0.0f,0.0f,10.0f);
    p_updir = D3DXVECTOR3(0.0f,1.0f,0.0f);

    //hard coded to 1.3333 by default
    float ratio = 640 / 480;
    setPerspective(3.14159f / 4, ratio, 1.0f, 2000.0f);
}

Camera::~Camera(void) { }

void Camera::setPerspective(float fov, float aspectRatio, float nearRange,
float farRange)
{
    this->setFOV(fov);
    this->setAspectRatio(aspectRatio);
    this->setNearRange(nearRange);
    this->setFarRange(farRange);
}


void Camera::Update()
{
    //set the camera's perspective matrix
    D3DXMatrixPerspectiveFovLH(&this->p_matrixProj, this->p_fov,
this->p_aspectRatio, this->p_nearRange, this->p_farRange);
    g_engine->getDevice()->SetTransform(D3DTS_PROJECTION, &this->p_matrixProj);

    //set the camera's view matrix
    D3DXMatrixLookAtLH(&this->p_matrixView, &this->p_position,
&this->p_target, &this->p_updir);
    g_engine->getDevice()->SetTransform(D3DTS_VIEW, &this->p_matrixView);
}

void Camera::setPosition(float x, float y, float z)
{
    this->p_position.x = x;
    this->p_position.y = y;
    this->p_position.z = z;
}
void Camera::setPosition(D3DXVECTOR3 position)
{
    this->setPosition(position.x, position.y, position.z);
}
};

Be sure to add both files (Camera.h and Camera.cpp) to your engine project. Or you may just open the Engine project located on the CD-ROM in this chapter’s folder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset