#pragma once #include #include "Parameters.h" #include "Vector.hpp" #include "half.h" #define RAY_TABLE_WIDTH 512 #define RAY_TABLE_HEIGHT 512 #define RAY_ORIGIN_COLOR_FORMAT D3DFMT_A16B16G16R16F #define RAY_DIR_COLOR_FORMAT D3DFMT_A16B16G16R16F #define DEPTH_FORMAT D3DFMT_D24S8 class HREEffect { //! pointer to global user-adjustable application parameters object static Parameters* parameters; //! pointer to main DX device LPDIRECT3DDEVICE9 device; //! pointer to main DX effect LPD3DXEFFECT effect; //! frame color buffer surface //! saved before render-to-texture, and restored as the render target for the final rendering to the screen LPDIRECT3DSURFACE9 frameColorBuffer; //! frame depth buffer surface //! saved before render-to-texture, and restored as the render target for the final rendering to the screen LPDIRECT3DSURFACE9 frameDepthStencilBuffer; LPDIRECT3DTEXTURE9 rayOriginTableTexture; LPDIRECT3DSURFACE9 rayOriginTableSurface; LPDIRECT3DTEXTURE9 rayDirTableTexture; LPDIRECT3DSURFACE9 rayDirTableSurface; LPDIRECT3DTEXTURE9 rayOriginTableTexture2; LPDIRECT3DSURFACE9 rayOriginTableSurface2; LPDIRECT3DTEXTURE9 rayDirTableTexture2; LPDIRECT3DSURFACE9 rayDirTableSurface2; LPDIRECT3DTEXTURE9 conePeakTexture; LPDIRECT3DSURFACE9 conePeakSurface; LPDIRECT3DTEXTURE9 coneDirTexture; LPDIRECT3DSURFACE9 coneDirSurface; LPDIRECT3DTEXTURE9 conePeakTextureMem; LPDIRECT3DSURFACE9 conePeakSurfaceMem; LPDIRECT3DTEXTURE9 coneDirTextureMem; LPDIRECT3DSURFACE9 coneDirSurfaceMem; LPDIRECT3DCUBETEXTURE9 environmentCubeTexture; LPDIRECT3DSURFACE9 depthSurface; LPDIRECT3DSURFACE9 coneStencil; LPDIRECT3DVERTEXDECLARATION9 trianglePrimitiveDeclaration; //! camera CFirstPersonCamera* camera; //! primary light source (can be moved just like the real camera, and can be used as the camera) CFirstPersonCamera* lightCamera; //! struct containing all mesh related data. Will be filled from X files in HREEffect constructor class Mesh { public: HREEffect* owner; unsigned int nSubsets; //!< number of submeshes (with possible different material) LPD3DXMESH mesh; //!< D3D mesh LPD3DXBUFFER materialBuffer; //!< D3D material buffer D3DXMATERIAL* materials; //!< D3D material array LPDIRECT3DTEXTURE9* textures; //!< D3D textures for the materials LPDIRECT3DVERTEXBUFFER9 primitiveVertexBuffer; //!< ray engine representation of the mesh (list of points with a triangle encoded in each) HRESULT setVertexFormat(DWORD fvf, LPDIRECT3DDEVICE9 device); //!< rebuild D3D to have differnet vertex format }; //! private method that loads a mesh and its textures void loadMesh(LPCWSTR fileName); //! private method to load a texture LPDIRECT3DTEXTURE9 loadTexture(LPCWSTR fileName); //! release material texture resources void releaseTextures(); //! release mesh resources void releaseMeshes(); //! release entities void releaseEntities(); //! struct that represents a virtual world object: a mesh and a model-world transformation matrix friend struct Entity; struct Entity { HREEffect* owner; //!< enclosing class instance Mesh* mesh; //!< mesh D3DXMATRIX modelWorldTransform; //!< modeling transform D3DXMATRIX inverseTransposedModelWorldTransform; //!< IT modeling transform for surface normal transformation }; //! vector of loaded meshes std::vector meshes; //! vector of loaded textures (textures specified in mesh's material will be loaded here) std::vector materialTextures; //! vector of virtual world objects std::vector entities; //! for materials with no texture we will render with this texture LPDIRECT3DTEXTURE9 emptyTexture; //! render a full-screen quad static void drawFullScreenQuad(LPDIRECT3DDEVICE9 device, float depth = 0.0f, float fLeftU=0.0f, float fTopV=0.0f, float fRightU=1.0f, float fBottomV=1.0f); //! clever enum for supported final rendering methods class Method{ unsigned int mid; Method(unsigned int mid) {this->mid = mid;} static const wchar_t * methodNames[10]; public: Method(const Method& o) {mid = o.mid;} const Method& operator=(const Method& o) {mid = o.mid; return *this;} bool operator==(const Method& o) const {return mid == o.mid;} bool operator!=(const Method& o) const {return mid != o.mid;} const static Method RAYENGINE; const static Method LAST; const Method& next() {mid = (mid + 1)%LAST.mid; return *this;} const Method& prev() {mid = (mid + LAST.mid - 1)%LAST.mid; return *this;} const wchar_t* getName() {return methodNames[mid];} }method; public: void nextMethod() {method.next();} void prevMethod() {method.prev();} const wchar_t* getCurrentMethodName() {return method.getName();} //! constructor: allocates all resources HREEffect(LPDIRECT3DDEVICE9 device); //! destructor: releases all resources ~HREEffect(void); //! renders the scene using the currently selected method void render(); //! moves the virtual world objects (camera, light) void move(float fElapsedTime); LRESULT handleMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); //! adds controls for the user-adjustable application parameters static void addUiParameters(Parameters* parameters); LPDIRECT3DDEVICE9 getDevice() {return device;} };