bool Water::Init() { if (FAILED(m_device->CreateVertexBuffer(4 * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, TerrainVertex::FVF, D3DPOOL_MANAGED, &m_vb, 0))) return false; int width = 1024; TerrainVertex *vertex = 0; m_vb->Lock(0, 0, (void **)&vertex, 0); vertex[0] = TerrainVertex(0, 2, 0, 0, 1); vertex[1] = TerrainVertex(256, 2, 0, 1,1); vertex[2] = TerrainVertex(256, 2, 256, 1, 0); vertex[3] = TerrainVertex(0, 2, 256, 0, 0); m_vb->Unlock(); //´´½¨Ë÷Òý if (FAILED(m_device->CreateIndexBuffer(6 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ib, 0))) return false; WORD *index = 0; m_ib->Lock(0, 0, (void **)&index, 0); index[0] = 0; index[1] = 1; index[2] = 2; index[3] = 0; index[4] = 2; index[5] = 3; m_ib->Unlock(); }
void TerrainRenderable::MakeVertexBuffer() { auto device = Globals::GetDevice()->GetDeviceD3D9(); int n=GetVertexNum(); HRESULT res = device->CreateVertexBuffer(n * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_pVB, 0); if (res != S_OK) ReportErr("Terrain CreateVB Failed"); TerrainVertex* v = 0; int idx=0; m_pVB->Lock(0, 0, (void**)&v, 0); for (int y = 0; y < terrain_h; ++y) { for (int x = 0; x < terrain_w; ++x) { float posx=x; float posz=y; ConvertPos(posx,posz); m_VertexData.push_back(TerrainVertex(posx, m_pHeightField->GetHeight(x,y), posz, (float)x / terrain_w, (float)y / terrain_h)); //v[idx++]=TerrainVertex(x*vertex_stride - orgX_Offset, GetTerrainHeight(x,y), y*vertex_stride - orgY_Offset, (float)x / terrain_w, (float)y / terrain_h); } } TerrainVertex* first=&(m_VertexData[0]); memcpy_s(v,n*sizeof(TerrainVertex),first,n*sizeof(TerrainVertex)); m_pVB->Unlock(); }
void OnInitial(DEVICEINSTANCE *device) { XInputEnable(true); D3DXMATRIX projection; D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 4, 800 / 600.0f, 1.0f, 10000.0f); device->d3dDevice->SetTransform(D3DTS_PROJECTION, &projection); D3DXLoadMeshFromX(L"skybox.x", 0, device->d3dDevice, nullptr, nullptr, nullptr, nullptr, &skybox); D3DXCreateTextureFromFile(device->d3dDevice, L"sky copy.png", &texture); D3DXCreateTextureFromFile(device->d3dDevice, L"terrain_01.jpg", &heightMap); D3DXCreateTextureFromFile(device->d3dDevice, L"terrain_02.jpg", &heightMapTexture); D3DXIMAGE_INFO heightMapInfo = { 0, }; D3DXGetImageInfoFromFile(L"terrain_01.jpg", &heightMapInfo); terrainWidth = heightMapInfo.Width; terrainHeight = heightMapInfo.Height; D3DCOLOR * colors; D3DLOCKED_RECT lockedRect = { 0, }; RECT rect = { 0, 0, terrainWidth, terrainHeight }; terrainVertices = new TerrainVertex[numOfVertices = terrainWidth * terrainHeight]; heightMap->LockRect(0, &lockedRect, &rect, 0); colors = (D3DCOLOR*)lockedRect.pBits; TerrainVertex * tempVertices = terrainVertices; for (int x = 0; x < terrainHeight; x++) { for (int z = 0; z < terrainWidth; z++) { int location = x * terrainWidth + z; *tempVertices = TerrainVertex( (x - terrainWidth / 2) * 5.0f, (colors[location] & 0xff) * 5.0f / 2, (z - terrainWidth / 2) * 5.0f, z / (float)terrainWidth, x / (float)terrainHeight ); tempVertices++; } } heightMap->UnlockRect(0); heightMap->Release(); terrainIndices = new int[numOfIndices = (terrainWidth - 1) * (terrainHeight - 1) * 2 * 3]; struct Polygon { int index[3]; } *tempIndices = (Polygon*)terrainIndices; for (int z = 0; z < terrainHeight - 1; z++) { for (int x = 0; x < terrainWidth - 1; x++) { (*tempIndices).index[0] = z * terrainWidth + x; (*tempIndices).index[1] = z * terrainWidth + (x + 1); (*tempIndices).index[2] = (z + 1) * terrainWidth + x; tempIndices++; (*tempIndices).index[0] = (z + 1) * terrainWidth + x; (*tempIndices).index[1] = z * terrainWidth + (x + 1); (*tempIndices).index[2] = (z + 1) * terrainWidth + (x + 1); tempIndices++; } } }
bool Terrain::computeVertices() { HRESULT hr = 0; hr = _device->CreateVertexBuffer( _numVertices * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, TerrainVertex::FVF, D3DPOOL_MANAGED, &_vb, 0); if(FAILED(hr)) return false; // coordinates to start generating vertices at int startX = -_width / 2; int startZ = _depth / 2; // coordinates to end generating vertices at int endX = _width / 2; int endZ = -_depth / 2; // compute the increment size of the texture coordinates // from one vertex to the next. float uCoordIncrementSize = 1.0f / (float)_numCellsPerRow; float vCoordIncrementSize = 1.0f / (float)_numCellsPerCol; TerrainVertex* v = 0; _vb->Lock(0, 0, (void**)&v, 0); int i = 0; for(int z = startZ; z >= endZ; z -= _cellSpacing) { int j = 0; for(int x = startX; x <= endX; x += _cellSpacing) { // compute the correct index into the vertex buffer and heightmap // based on where we are in the nested loop. int index = i * _numVertsPerRow + j; v[index] = TerrainVertex( (float)x, (float)_heightmap[index], (float)z, (float)j * uCoordIncrementSize, (float)i * vCoordIncrementSize); j++; // next column } i++; // next row } _vb->Unlock(); return true; }
void TerrainRenderablePlane::MakeVertexBuffer() { auto device = Globals::GetDevice()->GetDeviceD3D9(); int n = GetVertexNum(); HRESULT res = device->CreateVertexBuffer(n * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_pVB, 0); if (res != S_OK) ReportErr("Terrain CreateVB Failed"); TerrainVertex* v = 0; int idx = 0; m_pVB->Lock(0, 0, (void**)&v, 0); m_VertexData.push_back(TerrainVertex(0 - w / 2, 0 , 0 - h / 2, 0, 0)); m_VertexData.push_back(TerrainVertex(0 - w / 2, 0 , h - h / 2, 0, 1)); m_VertexData.push_back(TerrainVertex(w - w / 2, 0 , h - h / 2, 1, 1)); m_VertexData.push_back(TerrainVertex(w - w / 2, 0 , 0 - h / 2, 1, 0)); TerrainVertex* first = &(m_VertexData[0]); memcpy_s(v, n*sizeof(TerrainVertex), first, n*sizeof(TerrainVertex)); m_pVB->Unlock(); }
void TerrainBorderRenderable::MakeVertexBuffer(float left, float top, float right, float bottom) { auto device = Globals::GetDevice()->GetDeviceD3D9(); IDirect3DVertexBuffer9* pVb = 0; HRESULT res = device->CreateVertexBuffer(16 * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &pVb, 0); if (res != S_OK) ReportErr("SkyBox CreateVB Failed"); m_pVB.reset(pVb, [&](IDirect3DVertexBuffer9* p){p->Release(); }); TerrainVertex* v = 0; float scale = 500.0f; m_pVB->Lock(0, 0, (void**)&v, 0); float height = (top - bottom)*0.5f; float width = (right - left)*0.5f; // positive x v[0] = TerrainVertex(1.0f*right, -1.0f*height, 1.0f*width, 0.0f, 1.0f); v[1] = TerrainVertex(1.0f*right, 1.0f*height, 1.0f*width, 0.0f, 0.0f); v[2] = TerrainVertex(1.0f*right, 1.0f*height, -1.0f*width, 1.0f, 0.0f); v[3] = TerrainVertex(1.0f*right, -1.0f*height, -1.0f*width, 1.0f, 1.0f); D3DXPlaneFromPoints(&m_border_planes[POSITIVE_X], &v[0].m_pos, &v[1].m_pos, &v[2].m_pos); // negative x v[4] = TerrainVertex(1.0f*left, -1.0f*height, -1.0f*width, 0.0f, 1.0f); v[5] = TerrainVertex(1.0f*left, 1.0f*height, -1.0f*width, 0.0f, 0.0f); v[6] = TerrainVertex(1.0f*left, 1.0f*height, 1.0f*width, 1.0f, 0.0f); v[7] = TerrainVertex(1.0f*left, -1.0f*height, 1.0f*width, 1.0f, 1.0f); D3DXPlaneFromPoints(&m_border_planes[NEGATIVE_X], &v[4].m_pos, &v[5].m_pos, &v[6].m_pos); // positive z v[8] = TerrainVertex(-1.0f*width, -1.0f*height, 1.0f*top, 0.0f, 1.0f); v[9] = TerrainVertex(-1.0f*width, 1.0f*height, 1.0f*top, 0.0f, 0.0f); v[10] = TerrainVertex(1.0f*width, 1.0f*height, 1.0f*top, 1.0f, 0.0f); v[11] = TerrainVertex(1.0f*width, -1.0f*height, 1.0f*top, 1.0f, 1.0f); D3DXPlaneFromPoints(&m_border_planes[POSITIVE_Y], &v[8].m_pos, &v[9].m_pos, &v[10].m_pos); // negative z v[12] = TerrainVertex(1.0f*width, -1.0f*height, 1.0f*bottom, 0.0f, 1.0f); v[13] = TerrainVertex(1.0f*width, 1.0f*height, 1.0f*bottom, 0.0f, 0.0f); v[14] = TerrainVertex(-1.0f*width, 1.0f*height, 1.0f*bottom, 1.0f, 0.0f); v[15] = TerrainVertex(-1.0f*width, -1.0f*height, 1.0f*bottom, 1.0f, 1.0f); D3DXPlaneFromPoints(&m_border_planes[NEGATIVE_Y], &v[12].m_pos, &v[13].m_pos, &v[14].m_pos); m_pVB->Unlock(); }
void GeoMipMapping::RenderFan(float cX, float cZ, float fSize, SGEOMM_NEIGHBOR neighbor) { float fHalfSize = fSize / 2.0f; IDirect3DVertexBuffer9 *vb = 0; if (FAILED(m_device->CreateVertexBuffer(10 * sizeof(TerrainVertex), D3DUSAGE_WRITEONLY, TerrainVertex::FVF, D3DPOOL_MANAGED, &vb, 0))) return; int index = 0; //锁住顶点 TerrainVertex *vertex = 0; if (FAILED(vb->Lock(0, 0, (void **)&vertex, 0))) return; int num = 0; vertex[index++] = TerrainVertex(cX, cZ, GetHeight(cX, cZ)); vertex[index++] = TerrainVertex(cX - fHalfSize, cZ - fHalfSize, GetHeight(cX - fHalfSize, cZ - fHalfSize)); if (neighbor.m_bLeft) { vertex[index++] = TerrainVertex(cX - fHalfSize, cZ, GetHeight(cX - fHalfSize, cZ)); num++; } vertex[index++] = TerrainVertex(cX - fHalfSize, cZ + fHalfSize, GetHeight(cX - fHalfSize, cZ + fHalfSize)); if (neighbor.m_bUp) { vertex[index++] = TerrainVertex(cX, cZ + fHalfSize, GetHeight(cX, cZ + fHalfSize)); num++; } vertex[index++] = TerrainVertex(cX + fHalfSize, cZ + fHalfSize, GetHeight(cX + fHalfSize, cZ + fHalfSize)); if (neighbor.m_bRight) { vertex[index++] = TerrainVertex(cX + fHalfSize, cZ, GetHeight(cX + fHalfSize, cZ)); num++; } vertex[index++] = TerrainVertex(cX + fHalfSize, cZ - fHalfSize, GetHeight(cX + fHalfSize, cZ - fHalfSize)); if (neighbor.m_bBottom) { vertex[index++] = TerrainVertex(cX, cZ - fHalfSize, GetHeight(cX, cZ - fHalfSize)); num++; } vertex[index++] = TerrainVertex(cX - fHalfSize, cZ - fHalfSize, GetHeight(cX - fHalfSize, cZ - fHalfSize)); vb->Unlock(); if (m_device) { m_device->SetStreamSource(0, vb, 0, sizeof(TerrainVertex)); m_device->SetFVF(TerrainVertex::FVF); if (m_isWireFrame) { m_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); m_device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 4 + num); m_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } else { m_device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 4 + num); } } vb->Release(); }