0

I'm working on some directX, started from tutorial code and have gradually been converting it into their own class systems and cpp files etc.

Everything has been going well until now, but I just started modifying the "Camera.h" and "Camera.cpp". I've got the class function In the header, and when I place it in its class file it comes up with an error. I've got header blockers and so on but I cant see the flaw.

camera.h before editing

https://i.stack.imgur.com/Evxof.png

camera.h after editing

#ifndef CAMERA_H_
#define CAMERA_H_
#include "Player.h"
#include "RandomVariables.h"


class Camera {
    public:
        void    UpdateCamera();
        float   camPitch;
        float   camYaw;
        float   Zoom_Amount; //zoom amount from Model_1.Player
        float   charCamDist; //distance from Model_1.Player

        XMVECTOR camPosition;
        XMVECTOR camTarget;
        XMVECTOR camUp;
        XMVECTOR camRight;
        XMVECTOR camForward;

        XMMATRIX camRotationMatrix;
        XMMATRIX Rotationx;
        XMMATRIX Rotationy;
        XMMATRIX Rotationz;
        XMMATRIX camView;
        XMMATRIX camProjection;
};

Camera MainCamera;

#endif

I remove void Camera::UpdateCamera() and put it in the camera.cpp and it comes up with undefined errors and so on which don't happen if void Camera::UpdateCamera() is in the header file so I'm baffled :S

error list

https://i.stack.imgur.com/7G21V.png

Camera.cpp after moving updatecamera

#include "Camera.h"

void Camera::UpdateCamera(){
    MainCamera.charCamDist = 135.0f;

    MainCamera.camTarget = mainPlayer.playerPos;

    MainCamera.camTarget = XMVectorSetY(MainCamera.camTarget, XMVectorGetY(MainCamera.camTarget)+5.0f); 
    MainCamera.camTarget = XMVectorSetX(MainCamera.camTarget, XMVectorGetX(MainCamera.camTarget) -MainCamera.Zoom_Amount);


    MainCamera.camRotationMatrix = XMMatrixRotationRollPitchYaw(-MainCamera.camPitch, MainCamera.camYaw, 0);
    MainCamera.camPosition = XMVector3TransformNormal(DefaultForward, MainCamera.camRotationMatrix );
    MainCamera.camPosition = XMVector3Normalize(MainCamera.camPosition);

    MainCamera.camPosition = (MainCamera.camPosition * MainCamera.charCamDist) + MainCamera.camTarget;

    MainCamera.camForward = XMVector3Normalize(MainCamera.camTarget - MainCamera.camPosition);  // Get forward vector based on target
    MainCamera.camForward = XMVectorSetY(MainCamera.camForward, 0.0f);  // set forwards y component to 0 so it lays only on
    // the xz plane
    MainCamera.camForward = XMVector3Normalize(MainCamera.camForward);
    MainCamera.camRight = XMVectorSet(-XMVectorGetZ(MainCamera.camForward), 0.0f, XMVectorGetX(MainCamera.camForward), 0.0f);

    MainCamera.camUp =XMVector3Normalize(XMVector3Cross(XMVector3Normalize(MainCamera.camPosition - MainCamera.camTarget), MainCamera.camRight));

    MainCamera.camView = XMMatrixLookAtLH( MainCamera.camPosition, MainCamera.camTarget, MainCamera.camUp );


};

RandomVariables.cpp

//Global Declarations - Interfaces//

#ifndef RAND_H
#define RAND_H
#include "Models.h"
#include "Player.h"
#include "Camera.h"

///start
My_Player   mainPlayer;

XMMATRIX WVP;

XMVECTOR DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
XMVECTOR DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);

float tilt_X = 0.0f;

int NumSphereVertices;
int NumSphereFaces;

XMMATRIX sphereWorld;


XMMATRIX Scale;
XMMATRIX Translation;

///end



IDXGISwapChain* SwapChain;
ID3D11Device* d3d11Device;
ID3D11DeviceContext* d3d11DevCon;
ID3D11RenderTargetView* renderTargetView;
ID3D11DepthStencilView* depthStencilView;
ID3D11Texture2D* depthStencilBuffer;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D11PixelShader* D2D_PS;
ID3D10Blob* D2D_PS_Buffer;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11InputLayout* vertLayout;
ID3D11Buffer* cbPerObjectBuffer;
ID3D11BlendState* d2dTransparency;
ID3D11RasterizerState* CCWcullMode;
ID3D11RasterizerState* CWcullMode;
ID3D11SamplerState* CubesTexSamplerState;
ID3D11Buffer* cbPerFrameBuffer;

ID3D10Device1 *d3d101Device;    
IDXGIKeyedMutex *keyedMutex11;
IDXGIKeyedMutex *keyedMutex10;  
ID2D1RenderTarget *D2DRenderTarget; 
ID2D1SolidColorBrush *Brush;
ID3D11Texture2D *BackBuffer11;
ID3D11Texture2D *sharedTex11;   
ID3D11Buffer *d2dVertBuffer;
ID3D11Buffer *d2dIndexBuffer;
ID3D11ShaderResourceView *d2dTexture;
IDWriteFactory *DWriteFactory;
IDWriteTextFormat *TextFormat;

IDirectInputDevice8* DIKeyboard;
IDirectInputDevice8* DIMouse;

ID3D11Buffer* sphereIndexBuffer;
ID3D11Buffer* sphereVertBuffer;

ID3D11VertexShader* SKYMAP_VS;
ID3D11PixelShader* SKYMAP_PS;
ID3D10Blob* SKYMAP_VS_Buffer;
ID3D10Blob* SKYMAP_PS_Buffer;

ID3D11ShaderResourceView* smrv;

ID3D11DepthStencilState* DSLessEqual;
ID3D11RasterizerState* RSCullNone;

ID3D11BlendState* Transparency;

#endif
eddie
  • 1,252
  • 3
  • 15
  • 20
Invictus
  • 11
  • 3
  • `Camera MainCamera;` Putting this in the header makes you susceptible to the "multiple definition" linker error. If you include `camera.h` in multiple source files, there will be multiple `MainCamera`'s declared, which will lead to such an error. As a matter of fact, that is what *will* happen, even if you address the compilation issues. – PaulMcKenzie Mar 31 '15 at 15:26
  • given its got #ifndef it shouldnt be able to redefine the MainCamera? it is included in multiple files prior to moving the cameraupdate function – Invictus Mar 31 '15 at 15:32
  • No. That is not what will happen. You have 2 separate modules, RandomVariables.cpp and Camera.cpp. They will be compiled separately, no? So when the linker now sees that the `MainCamera` variable appears in both the RandomVariables and Camera object code, the linker will emit the error. Either stop using global variables, or learn how to use them in a multi-module program. http://stackoverflow.com/questions/9702053/how-to-declare-a-global-variable-in-c – PaulMcKenzie Mar 31 '15 at 15:35
  • my mistake RandomVariables is a header not a cpp. As i mentioned I'm breaking down the tutorial code and building it into class systems as before it was all within a single file and everything global, i don't know enough DX to build it from scratch. but yes that would be the error given i only call camera.h in a second cpp when it opens up the error. would you be able to provide an available solution? obviously removing all global variables is the current plan of action but i call MainCamera object in multiple files? potentially an extern will work? – Invictus Mar 31 '15 at 15:45
  • If you stay with globals, then the definition in the header should be `extern`, and one and only one source (.CPP) module will have the "non-extern" definition. – PaulMcKenzie Mar 31 '15 at 15:51
  • ok thanks, thought that was a potential fix after you explained the flaw, will re-code it now to make sure. much appreciated! – Invictus Mar 31 '15 at 15:53
  • Just as a note, using ``XMVECTOR`` and ``XMMATRIX`` in a class only works easily with x64 native. The alignment requirements for these are not met by default on x86 or ARM. You might consider using [SimpleMath](http://blogs.msdn.com/b/shawnhar/archive/2013/01/08/simplemath-a-simplified-wrapper-for-directxmath.aspx) in [DirectX Tool Kit](http://go.microsoft.com/fwlink/?LinkId=248929) instead. – Chuck Walbourn Mar 31 '15 at 19:02

1 Answers1

0

Using externs and just declaring the class object within the relative cpp files solved the issue.

Invictus
  • 11
  • 3