afTerrainPatch.h
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:7k
源码类别:
其他游戏
开发平台:
Visual C++
- #ifndef AF_TERRAINPATCH
- #define AF_TERRAINPATCH
- #include <D3DX9.h>
- #include "afVec2.h"
- #include "afVec3.h"
- #include "af3DObject.h"
- #define D3DFVF_PATCHVERTEX ( D3DFVF_XYZ | D3DFVF_TEX1 )
- class afTerrain;
- class afTerrainPatch:public af3DObject
- {
- public:
- enum VISFLAGS { VIS_BASE = 1,
- VIS_LAYER0 = 2,
- VIS_LAYER1 = 4,
- VIS_LAYER2 = 8,
- VIS_LAYER3 = 16,
- VIS_LAYER4 = 32,
- VIS_LAYERALL = 63 };
- enum { MAX_SUBDIV = 4,
- SOURCE_WIDTH = 17,
- SOURCE_HEIGHT = 17,
- MAX_VERTICES = SOURCE_WIDTH * SOURCE_HEIGHT,
- MAX_INDICES = MAX_VERTICES*3 };
- enum { LEFT = 1,
- RIGHT = 2,
- TOP = 4,
- BOTTOM = 8 };
- struct PATCHVERTEX
- {
- afVec3 pos;
- afVec2 tex;
- };
- afTerrainPatch(afTerrain* nMaster, int nX, int nY);
- void init();
- void deinit();
- int render();
- bool updateTessellation();
- bool updateTessellation2();
- bool updateTessellation3();
- void deleteDeviceObjects();
- bool restoreDeviceObjects();
- int fillBaseVertices(afTerrainPatch::PATCHVERTEX* nDest);
- void setRendered(bool nSet) { rendered = nSet; }
- bool getRendered() const { return rendered; }
- void setVisFlags(int nFlags) { visFlags = nFlags; }
- int getVisFlags() const { return visFlags; }
- void setMaster(afTerrain* nMaster) { master = nMaster; }
- void setScale(const afVec3& nScale);
- void addToActiveList();
- void removeFromActiveList();
- void setHeightMapPosition(int nX0, int nY0);
- float getExactTessellation() const;
- float getCurrentError() const { return currentError; }
- // returns the current tessellation:
- // 0...maximum details (~2*SOURCE_WIDTH*SOURCE_WIDTH triangles)
- // 4...minimum details (2 triangles)
- int getCurrentTessellation() const { return selfTes; }
- int getNewTessellation() const { return newSelfTes; }
- int getNewTessellationSafe() const;
- int getRealTessellation() const { return realSelfTes; }
- int getNumIndices() const { return numNewIndices; }
- int getNumVertices() const { return numNewVertices; }
- const PATCHVERTEX* getVertices() const { return vertices; }
- const WORD* getIndices() const { return indices; }
- bool checkVisibility(const D3DXMATRIX* nWorldMatrix, const afPlane nPlanes[6]);
- void calcErrors();
- bool isVisible() const { return visible; }
- bool isActive() const { return active; }
- void setVisible(bool nVis);
- void updateProjectedErrors(const D3DXMATRIX* nWorldMatrix);
- float getProjectedError(int nWhich) { return errors[nWhich].diff; }
- void setRealTessellation(int nTes);
- void setTessellation(int nTes) { newSelfTes = nTes; }
- class afTerrainPatch* getNextActive() { return next; }
- int getGridPosX() const { return gridX; }
- int getGridPosY() const { return gridY; }
- void setFlagActive(bool nSet) { flagActive = nSet; }
- bool getFlagActive() const { return flagActive; }
- void setPosition(const afVec3& nPos) { pos = nPos; }
- void setNeighbors(const afTerrainPatch* nLeft, const afTerrainPatch* nRight, const afTerrainPatch *nUp, const afTerrainPatch* nDown);
- static int getAddLayerFlag(int nLayer) { return 1 << nLayer; }
- void createTessellation(int nCenter, int nLeft, int nRight, int nUp, int nDown);
- const afTerrainPatch* getLeft() const { return left; }
- const afTerrainPatch* getRight() const { return right; }
- const afTerrainPatch* getBottom() const { return bottom; }
- const afTerrainPatch* getTop() const { return top; }
- void calcMinMaxY();
- protected:
- static int getPowerTwo(int nVal);
- unsigned short getIndex(int nX, int nY);
- float getHeight(int nX, int nY) const;
- void getVertex(int nX, int nY, afVec3& nPoint) const;
- void getTexCoord(int nX, int nY, afVec2& nTCoord) const;
- void getNormal(int nX, int nY, afVec3& nNormal) const;
- void calcError(int nTes);
- void addIndex(unsigned short nIdx);
- void addLastIndexAgain();
- void reduceShapeTo(int nTes, PATCHVERTEX *nData);
- void makeBordersSimpler(PATCHVERTEX *nData);
- void makeSimpler(int nTes, PATCHVERTEX *nData);
- void calculateInbetweenVertex(int x, int y, int xc, int yc, int pow, PATCHVERTEX *nData);
- int calculateDiagonalVertices(int x, int y, int xc, int yc, int pow, PATCHVERTEX *nData,
- float& nHeightLeftTop, int& idxLT, float& nHeightRightBottom, int& idxRB);
- void addLevel4PatchTriangles(bool nLeft, bool nRight, bool nBottom, bool nTop);
- void addTriangleRow(int nY, int nPow, bool nLeft, bool nRight, bool nBottom, bool nTop);
- void addTriangleBottomRow(int nPow, bool nLeft, bool nRight);
- void addTriangleTopRow(int nPow, bool nLeft, bool nRight);
- struct Error
- {
- Error() { diff = 0.0f; }
- afVec3 correct, real;
- float diff;
- };
- //RENDERMODE renderMode;
- int visFlags;
- afTerrain* master;
- int gridX,gridY;
- bool active;
- bool visible;
- bool forceBufferCreate;
- Error errors[MAX_SUBDIV+2];
- float currentError;
- int newSelfTes, selfTes, oldRealSelfTes, newRealSelfTes, realSelfTes, leftTes,rightTes,topTes,bottomTes;
- afVec3 pos, scale;
- float minY,maxY;
- int hmX0, hmY0;
- int numVertices, numIndices,
- numNewVertices, numNewIndices;
- int frameIdx;
- // for linking in a list of active patches
- //
- afTerrainPatch *next, *prev;
- bool flagActive;
- // neighbors
- //
- const afTerrainPatch *left, *right, *top, *bottom;
- // dynamic data
- //
- unsigned short *indexMap;
- PATCHVERTEX *vertices; //, *verticesLow;
- unsigned short *indices;
- bool rendered;
- bool lowVertMethod2;
- unsigned char useNeightbors;
- PDIRECT3DINDEXBUFFER9 iBuffer;
- PDIRECT3DVERTEXBUFFER9 vBuffer;
- PATCHVERTEX *verticesLow;
- float *yMoveSelf, *yMoveLeft,*yMoveLeft2, *yMoveRight,*yMoveRight2, *yMoveBottom,*yMoveBottom2, *yMoveTop,*yMoveTop2;
- float factorSelf, factorLeft,factorLeft2, factorRight,factorRight2, factorBottom,factorBottom2, factorTop,factorTop2;
- bool newFollowsLeft,followsLeft, newFollowsRight,followsRight, newFollowsBottom,followsBottom, newFollowsTop,followsTop;
- bool recalcBorderLeft, recalcBorderRight, recalcBorderBottom, recalcBorderTop;
- bool forceRetessellation;
- bool fillBuffers();
- bool checkBuffers();
- void fillMorphVertices();
- void checkBorderLeft();
- void checkBorderRight();
- void checkBorderBottom();
- void checkBorderTop();
- };
- inline void afTerrainPatch::addIndex(unsigned short nIdx)
- {
- indices[numNewIndices++] = nIdx;
- }
- inline void afTerrainPatch::addLastIndexAgain()
- {
- if(numNewIndices>0)
- indices[numNewIndices++] = indices[numNewIndices-1];
- }
- typedef afTerrainPatch *afTerrainPatchPtr;
- typedef afTerrainPatchPtr *afTerrainPatchPPtr;
- #endif