private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Load the vertex array and index array with data. for (int i = 0; i < VertexCount; i++) { vertices[i].position = new Vector3(Model[i].x, Model[i].y, Model[i].z); indices[i] = i; } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private DVertexType[] GetVertexBuffer(float r = 1f, float g = 0.2f, float b = 0.1f) { VertexCount = Resolution * Resolution * 8; IndexCount = VertexCount; int[] indices = new int[IndexCount]; DVertexType[] vertices = new DVertexType[VertexCount]; int index = 0; for (int j = 0; j < Resolution; j++) { for (int i = 0; i < Resolution; i++) { float positionX = (float)i; float positionZ = (float)j; if (i == 0 || i == 1 || j == 0 || j == 1 || j == Resolution - 1 || i == Resolution - 1 || j == Resolution - 2 || i == Resolution - 2) { vertices[index].position = new Vector3(positionX, -12, positionZ); vertices[index].color = new Vector4(0, 0, 0, 0); } else { vertices[index].position = new Vector3(positionX, _surface.Grid[i][j].Height / 100, positionZ); //vertices[index].color = new Vector4(_surface.Grid[i][j].Height / 1000, _surface.Grid[i][j].Height / 100, 1, 0.51f); vertices[index].color = new Vector4(r, g, b, 1f); } index++; } } return(vertices); }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Calculate the number of vertices in the terrain mesh. VertexCount = (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 6; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Load the vertex and index arrays with the terrain data. for (var i = 0; i < VertexCount; i++) { vertices[i] = new DVertexType() { position = new Vector3(TerrainModel[i].x, TerrainModel[i].y, TerrainModel[i].z), texture = new Vector2(TerrainModel[i].tu, TerrainModel[i].tv), normal = new Vector3(TerrainModel[i].nx, TerrainModel[i].ny, TerrainModel[i].nz), tangent = new Vector3(TerrainModel[i].tx, TerrainModel[i].ty, TerrainModel[i].tz), binormal = new Vector3(TerrainModel[i].bx, TerrainModel[i].by, TerrainModel[i].bz), color = new Vector3(TerrainModel[i].r, TerrainModel[i].g, TerrainModel[i].b) }; indices[i] = i; } // Yo AVOID OutOfMemory Exceptions being trown first, release the height map since it is no longer needed in memory once the 3D terrain model has been built. ShutdownHeightMap(); // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; } catch (Exception ex) { return(false); } return(true); }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Set the number of the vertices and indices in the vertex and index array, accordingly. VertexCount = 6; IndexCount = 6; // Create the vertex array. var vertices = new DVertexType[VertexCount]; // Create the index array. var indices = new int[IndexCount]; // Load the index array with data. for (var i = 0; i < IndexCount; i++) { indices[i] = i; } // Set up the description of the static vertex buffer. var vertexBuffer = new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = Utilities.SizeOf <DVertexType>() * VertexCount, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }; // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, vertices, vertexBuffer); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Load the vertex and index array with data from the terrain model. for (int i = 0; i < VertexCount; i++) { vertices[i].position = new Vector3(TerrainModel[i].x, TerrainModel[i].y, TerrainModel[i].z); vertices[i].texture = new Vector2(TerrainModel[i].tu, TerrainModel[i].tv); vertices[i].normal = new Vector3(TerrainModel[i].nx, TerrainModel[i].ny, TerrainModel[i].nz); vertices[i].tangent = new Vector3(TerrainModel[i].tx, TerrainModel[i].ty, TerrainModel[i].tz); vertices[i].binormal = new Vector3(TerrainModel[i].bx, TerrainModel[i].by, TerrainModel[i].bz); vertices[i].color = new Vector4(TerrainModel[i].r, TerrainModel[i].g, TerrainModel[i].b, 1.0f); indices[i] = i; } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Calculate the number of vertices in the terrain mesh. VertexCount = (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 8; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int j = 0; j < (m_TerrainHeight - 1); j++) { for (int i = 0; i < (m_TerrainWidth - 1); i++) { // LINE 1 // Upper left. float positionX = (float)i; float positionZ = (float)(j + 1); vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper right. positionX = (float)(i + 1); positionZ = (float)(j + 1); vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(i + 1); positionZ = (float)(j + 1); vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom right. positionX = (float)(i + 1); positionZ = (float)j; vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(i + 1); positionZ = (float)j; vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom left. positionX = (float)i; positionZ = (float)j; vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)i; positionZ = (float)j; vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper left. positionX = (float)i; positionZ = (float)(j + 1); vertices[index].position = new Vector3(positionX, 0.0f, positionZ); vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device, int skyPlaneResolution) { // Calculate the number of vertices in the sky plane mesh. VertexCount = (skyPlaneResolution + 1) * (skyPlaneResolution + 1) * 6; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index into the vertex array. int index = 0; // Load the vertex and index array with the sky plane array data. for (int j = 0; j < skyPlaneResolution; j++) { for (int i = 0; i < skyPlaneResolution; i++) { int index1 = j * (skyPlaneResolution + 1) + i; int index2 = j * (skyPlaneResolution + 1) + (i + 1); int index3 = (j + 1) * (skyPlaneResolution + 1) + i; int index4 = (j + 1) * (skyPlaneResolution + 1) + (i + 1); // Triangle 1 - Upper Left vertices[index].position = new Vector3(SkyPlane[index1].x, SkyPlane[index1].y, SkyPlane[index1].z); vertices[index].texture = new Vector2(SkyPlane[index1].tu, SkyPlane[index1].tv); indices[index] = index; index++; // Triangle 1 - Upper Right vertices[index].position = new Vector3(SkyPlane[index2].x, SkyPlane[index2].y, SkyPlane[index2].z); vertices[index].texture = new Vector2(SkyPlane[index2].tu, SkyPlane[index2].tv); indices[index] = index; index++; // Triangle 1 - Bottom Left vertices[index].position = new Vector3(SkyPlane[index3].x, SkyPlane[index3].y, SkyPlane[index3].z); vertices[index].texture = new Vector2(SkyPlane[index3].tu, SkyPlane[index3].tv); indices[index] = index; index++; // Triangle 2 - Bottom Left vertices[index].position = new Vector3(SkyPlane[index3].x, SkyPlane[index3].y, SkyPlane[index3].z); vertices[index].texture = new Vector2(SkyPlane[index3].tu, SkyPlane[index3].tv); indices[index] = index; index++; // Triangle 2 - Upper Right vertices[index].position = new Vector3(SkyPlane[index2].x, SkyPlane[index2].y, SkyPlane[index2].z); vertices[index].texture = new Vector2(SkyPlane[index2].tu, SkyPlane[index2].tv); indices[index] = index; index++; // Triangle 2 - Bottom Right vertices[index].position = new Vector3(SkyPlane[index4].x, SkyPlane[index4].y, SkyPlane[index4].z); vertices[index].texture = new Vector2(SkyPlane[index4].tu, SkyPlane[index4].tv); indices[index] = index; index++; } } // Set up the description of the vertex buffer. // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { /// Calculate the number of vertices in the terrain mesh. VertexCount = (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 6; // Create the vertex array. Verticies = new DVertexType[VertexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int j = 0; j < (m_TerrainHeight - 1); j++) { for (int i = 0; i < (m_TerrainWidth - 1); i++) { int indexBottomLeft1 = (m_TerrainHeight * j) + i; // Bottom left. int indexBottomRight2 = (m_TerrainHeight * j) + (i + 1); // Bottom right. int indexUpperLeft3 = (m_TerrainHeight * (j + 1)) + i; // Upper left. int indexUpperRight4 = (m_TerrainHeight * (j + 1)) + (i + 1); // Upper right. #region First Triangle // Upper left. float tv = HeightMap[indexUpperLeft3].tv; // Modify the texture coordinates to cover the top edge. if (tv == 1.0f) { tv = 0.0f; } Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperLeft3].x, HeightMap[indexUpperLeft3].y, HeightMap[indexUpperLeft3].z), texture = new Vector2(HeightMap[indexUpperLeft3].tu, tv), normal = new Vector3(HeightMap[indexUpperLeft3].nx, HeightMap[indexUpperLeft3].ny, HeightMap[indexUpperLeft3].nz) }; index++; // Upper right. float tu = HeightMap[indexUpperRight4].tu; tv = HeightMap[indexUpperRight4].tv; // Modify the texture coordinates to cover the top and right edge. if (tu == 0.0f) { tu = 1.0f; } if (tv == 1.0f) { tv = 0.0f; } Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), texture = new Vector2(tu, tv), normal = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz) }; index++; // Bottom left. Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), texture = new Vector2(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv), normal = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz) }; // indices[index] = index++; index++; #endregion #region Second Triangle // Bottom left. Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), texture = new Vector2(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv), normal = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz) }; index++; // Upper right. tu = HeightMap[indexUpperRight4].tu; tv = HeightMap[indexUpperRight4].tv; // Modify the texture coordinates to cover the top and right edge. if (tu == 0.0f) { tu = 1.0f; } if (tv == 1.0f) { tv = 0.0f; } Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), texture = new Vector2(tu, tv), normal = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz) }; index++; // Bottom right. tu = HeightMap[indexBottomRight2].tu; // Modify the texture coordinates to cover the right edge. if (tu == 0.0f) { tu = 1.0f; } Verticies[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomRight2].x, HeightMap[indexBottomRight2].y, HeightMap[indexBottomRight2].z), texture = new Vector2(tu, HeightMap[indexBottomRight2].tv), normal = new Vector3(HeightMap[indexBottomRight2].nx, HeightMap[indexBottomRight2].ny, HeightMap[indexBottomRight2].nz) }; index++; #endregion } } return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Calculate the number of vertices in the terrain mesh. 2 verticies per line, 3 lines per triangle and 2 trangles per quad is 12 verticies per quad. VertexCount = (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 12; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int j = 0; j < (m_TerrainHeight - 1); j++) { for (int i = 0; i < (m_TerrainWidth - 1); i++) { int indexBottomLeft1 = (m_TerrainHeight * j) + i; // Bottom left. int indexBottomRight2 = (m_TerrainHeight * j) + (i + 1); // Bottom right. int indexUpperLeft3 = (m_TerrainHeight * (j + 1)) + i; // Upper left. int indexUpperRight4 = (m_TerrainHeight * (j + 1)) + (i + 1); // Upper right. #region First Triangle // Upper left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperLeft3].x, HeightMap[indexUpperLeft3].y, HeightMap[indexUpperLeft3].z), color = Vector4.One }; indices[index] = index++; // Upper right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), color = Vector4.One }; indices[index] = index++; // Upper right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), color = Vector4.One }; indices[index] = index++; // Bottom left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), color = Vector4.One }; indices[index] = index++; // Bottom left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), color = Vector4.One }; indices[index] = index++; // Upper left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperLeft3].x, HeightMap[indexUpperLeft3].y, HeightMap[indexUpperLeft3].z), color = Vector4.One }; indices[index] = index++; #endregion #region Second Triangle // Bottom Left vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), color = Vector4.One }; indices[index] = index++; // Upper right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), color = Vector4.One }; indices[index] = index++; // Upper right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), color = Vector4.One }; indices[index] = index++; // Bottom right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomRight2].x, HeightMap[indexBottomRight2].y, HeightMap[indexBottomRight2].z), color = Vector4.One }; indices[index] = index++; // Bottom right. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomRight2].x, HeightMap[indexBottomRight2].y, HeightMap[indexBottomRight2].z), color = Vector4.One }; indices[index] = index++; // Bottom left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), color = Vector4.One }; indices[index] = index++; #endregion } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { // Calculate the number of vertices in the terrain mesh. VertexCount = 2 * 8; //(m_TerrainWidth - 1) * (m_TerrainHeight - 1)* (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 8 // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int x = 0; x < (m_TerrainHeight); x++) { for (int y = 0; y < (m_TerrainWidth); y++) { if (_faceBottom) { float positionX = (float)(x); float positionZ = (float)(y); vertices[index].position = new Vector3(positionX, 0, positionZ) * _tileSize; vertices[index].color = new Vector4(0, 1.0f, 0, 1.0f); indices[index] = index; index++; positionX = (float)(x); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX, positionZ, 0) * _tileSize; vertices[index].color = new Vector4(0, 1.0f, 0, 1.0f); indices[index] = index; index++; /*// LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX+_offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; */ /*// LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(0, 0, 1.0f, 1.0f); * indices[index] = index; * index++;*/ } /*else if (_faceTop) * { * // LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX+_offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, _offsetY, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * } * else if (_faceLeft) * { * // LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * } * else if (_faceRight) * { * // LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(positionX + _offsetX, positionZ + _offsetZ, _offsetY) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * } * else if (_faceFront) * { * // LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3( _offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY,positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * } * else if (_faceBack) * { * // LINE 1 * // Upper left. * float positionX = (float)i; * float positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(i + 1); * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(i + 1); * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)i; * positionZ = (float)j; * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * // Upper left. * positionX = (float)i; * positionZ = (float)(j + 1); * vertices[index].position = new Vector3(_offsetY, positionX + _offsetX, positionZ + _offsetZ) * _tileSize; * vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); * indices[index] = index; * index++; * }*/ } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SCCoreSystems.sc_console.SC_console_directx D3D, float depthScreen, float offsetDepth, float maxwidthMul, float maxheightMul) { try { /*float oriWidth = (float)(D3D.SurfaceWidth * 0.5f); * float oriHeight = (float)(D3D.SurfaceWidth * 0.5f); * * oriWidth /= 10; * oriHeight /= 10; * * oriWidth *= 0.0006f; * oriHeight *= 0.0006f;*/ int sizeWidther = (int)(m_TerrainWidth * 0.5f); int sizeHeighter = (int)(m_TerrainHeight * 0.5f); sizeWidther /= 10; sizeHeighter /= 10; // Calculate the number of vertices in the terrain mesh. //VertexCount = (_divX - 1) * (_divY - 1)* (_divX - 1) * (_divY - 1) * 8; VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; //VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. int remains = (int)Math.Round((m_TerrainWidth - m_TerrainHeight) * 0.5f); float offsetZ = (depthScreen * 2) + (1 * offsetDepth); for (int x = (int)(-_divX * maxwidthMul); x < (int)(_divX * maxwidthMul); x++) { //float xx = x - oriWidth; for (int y = (int)(-_divY * maxheightMul); y < (int)(_divY * maxheightMul); y++) { // LINE 1 // Upper left. float positionX = (float)x; float positionZ = (float)(y + 1); //position = new Vector3(-sizeWidther*totalSize, -sizeHeighter*totalSize, 0), vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper left. positionX = (float)x; positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, (positionZ * sizeHeighter) + remains, offsetZ) * _size_screen; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; /* * //float yy = y - oriHeight; * // LINE 1 * // Upper left. * float positionX = (float)x; * float positionZ = (float)(y + 1); * * * float xi = positionX * sizeWidther; * float yi = positionZ * sizeHeighter; * * * * * * * //position = new Vector3(-sizeWidther*totalSize, -sizeHeighter*totalSize, 0), * * vertices[index].position = new Vector3(xi,yi, 0.0f)* _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * // Upper right. * positionX = (float)(x + 1); * positionZ = (float)(y + 1); * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * * // LINE 2 * // Upper right. * positionX = (float)(x + 1); * positionZ = (float)(y + 1); * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * // Bottom right. * positionX = (float)(x + 1); * positionZ = (float)y; * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * * // LINE 3 * // Bottom right. * positionX = (float)(x + 1); * positionZ = (float)y; * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * // Bottom left. * positionX = (float)x; * positionZ = (float)y; * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * * // LINE 4 * // Bottom left. * positionX = (float)x; * positionZ = (float)y; * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; * // Upper left. * positionX = (float)x; * positionZ = (float)(y + 1); * vertices[index].position = new Vector3(xi, yi, 0.0f) * _size_screen; * vertices[index].color = new Vector4(_r, _g, _b, _a); * indices[index] = index; * index++; */ } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(D3D.Device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(D3D.Device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { /// Calculate the number of vertices in the terrain mesh. VertexCount = (m_TerrainWidth - 1) * (m_TerrainHeight - 1) * 6; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int j = 0; j < (m_TerrainHeight - 1); j++) { for (int i = 0; i < (m_TerrainWidth - 1); i++) { int indexBottomLeft1 = (m_TerrainHeight * j) + i; // Bottom left. int indexBottomRight2 = (m_TerrainHeight * j) + (i + 1); // Bottom right. int indexUpperLeft3 = (m_TerrainHeight * (j + 1)) + i; // Upper left. int indexUpperRight4 = (m_TerrainHeight * (j + 1)) + (i + 1); // Upper right. #region First Triangle // Upper left. float tv = HeightMap[indexUpperLeft3].tv; // Modify the texture coordinates to cover the top edge. if (tv == 1.0f) { tv = 0.0f; } vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperLeft3].x, HeightMap[indexUpperLeft3].y, HeightMap[indexUpperLeft3].z), texture = new Vector4(HeightMap[indexUpperLeft3].tu, tv, 0.0f, 0.0f), normal = new Vector3(HeightMap[indexUpperLeft3].nx, HeightMap[indexUpperLeft3].ny, HeightMap[indexUpperLeft3].nz), color = new Vector4(HeightMap[indexUpperLeft3].r, HeightMap[indexUpperLeft3].g, HeightMap[indexUpperLeft3].b, 1.0f) }; indices[index] = index++; // Upper right. float tu = HeightMap[indexUpperRight4].tu; /// May not need tv = HeightMap[indexUpperRight4].tv; // Modify the texture coordinates to cover the top and right edge. if (tu == 0.0f) { tu = 1.0f; } if (tv == 1.0f) { tv = 0.0f; } vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), texture = new Vector4(tu, tv, 1.0f, 0.0f), normal = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz), color = new Vector4(HeightMap[indexUpperRight4].r, HeightMap[indexUpperRight4].g, HeightMap[indexUpperRight4].b, 1.0f) }; indices[index] = index++; // Bottom left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), texture = new Vector4(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv, 0.0f, 1.0f), normal = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz), color = new Vector4(HeightMap[indexBottomLeft1].r, HeightMap[indexBottomLeft1].g, HeightMap[indexBottomLeft1].b, 1.0f) }; indices[index] = index++; #endregion #region Second Triangle // Bottom left. vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z), texture = new Vector4(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv, 0.0f, 1.0f), normal = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz), color = new Vector4(HeightMap[indexBottomLeft1].r, HeightMap[indexBottomLeft1].g, HeightMap[indexBottomLeft1].b, 1.0f) }; indices[index] = index++; // Upper right. tu = HeightMap[indexUpperRight4].tu; tv = HeightMap[indexUpperRight4].tv; // Modify the texture coordinates to cover the top and right edge. if (tu == 0.0f) { tu = 1.0f; } if (tv == 1.0f) { tv = 0.0f; } vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z), texture = new Vector4(tu, tv, 1.0f, 0.0f), normal = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz), color = new Vector4(HeightMap[indexUpperRight4].r, HeightMap[indexUpperRight4].g, HeightMap[indexUpperRight4].b, 1.0f) }; indices[index] = index++; // Bottom right. tu = HeightMap[indexBottomRight2].tu; // Modify the texture coordinates to cover the right edge. if (tu == 0.0f) { tu = 1.0f; } vertices[index] = new DVertexType() { position = new Vector3(HeightMap[indexBottomRight2].x, HeightMap[indexBottomRight2].y, HeightMap[indexBottomRight2].z), texture = new Vector4(tu, HeightMap[indexBottomRight2].tv, 1.0f, 1.0f), normal = new Vector3(HeightMap[indexBottomRight2].nx, HeightMap[indexBottomRight2].ny, HeightMap[indexBottomRight2].nz), color = new Vector4(HeightMap[indexBottomRight2].r, HeightMap[indexBottomRight2].g, HeightMap[indexBottomRight2].b, 1.0f) }; indices[index] = index++; #endregion } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device, Matrix dcontainPos) { try { // Calculate the number of vertices in the terrain mesh. VertexCount = (m_TerrainWidth) * (m_TerrainHeight) * (m_TerrainDepth) * (m_TerrainWidth) * (m_TerrainHeight) * (m_TerrainDepth) * 10 + (2 * m_TerrainWidth * m_TerrainDepth); // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int x = -(m_TerrainWidth); x < (m_TerrainWidth) + 1; x++) { for (int y = -(m_TerrainHeight); y < (m_TerrainHeight) + 1; y++) { for (int z = -(m_TerrainDepth); z < (m_TerrainDepth) + 1; z++) { if (x < m_TerrainWidth && z < m_TerrainDepth) { // LINE 1 // Upper left. float positionX = (float)z; float positionY = (float)(y); float positionZ = (float)(x + 1); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper right. positionX = (float)(z + 1); positionZ = (float)(x + 1); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(z + 1); positionZ = (float)(x + 1); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom right. positionX = (float)(z + 1); positionZ = (float)x; vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(z + 1); positionZ = (float)x; vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom left. positionX = (float)z; positionZ = (float)x; vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)z; positionZ = (float)x; vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper left. positionX = (float)z; positionZ = (float)(x + 1); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; } if (y < m_TerrainHeight) { float positionX = (float)(z); float positionZ = (float)(x); float positionY = (float)(y); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1, 1.0f, 1, 1.0f); indices[index] = index; index++; positionX = (float)(z); positionZ = (float)(x); positionY = (float)(y + 1); vertices[index].position = new Vector3(positionX, positionY, positionZ) * _tileSize; vertices[index].color = new Vector4(1, 1.0f, 1, 1.0f); indices[index] = index; index++; } } } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SCCoreSystems.sc_console.SC_console_directx D3D, float depthScreen, float offsetDepth, float maxwidthMul, float maxheightMul) { try { int sizeWidther = (int)(m_TerrainWidth * 0.5f); int sizeHeighter = (int)(m_TerrainHeight * 0.5f); sizeWidther /= 10; sizeHeighter /= 10; // Calculate the number of vertices in the terrain mesh. //VertexCount = (_divX - 1) * (_divY - 1)* (_divX - 1) * (_divY - 1) * 8; VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; //VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; float offsetZ = (depthScreen * 2) + (1 * offsetDepth); for (int x = (int)(-_divX * maxwidthMul); x < (int)(_divX * maxwidthMul); x++) { //float xx = x - oriWidth; for (int y = (int)(-_divY * maxheightMul); y < (int)(_divY * maxheightMul); y++) { // LINE 1 // Upper left. float positionX = (float)x; float positionZ = (float)(y + 1); //position = new Vector3(-sizeWidther*totalSize, -sizeHeighter*totalSize, 0), vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper left. positionX = (float)x; positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, offsetZ) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(D3D.Device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(D3D.Device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { try { int sizeWidther = (int)(m_TerrainWidth * 0.5f); int sizeHeighter = (int)(m_TerrainHeight * 0.5f); //sizeWidther /= 10; //sizeHeighter /= 10; // Calculate the number of vertices in the terrain mesh. //VertexCount = (_divX - 1) * (_divY - 1)* (_divX - 1) * (_divY - 1) * 8; VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; //VertexCount = (_divX) * (_divY) * (_divX) * (_divY) * 8; // Set the index count to the same as the vertex count. IndexCount = VertexCount; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Initialize the index to the vertex array. int index = 0; // Load the vertex and index arrays with the terrain data. for (int x = 0; x < (_divX); x++) { for (int y = 0; y < (_divY); y++) { // LINE 1 // Upper left. float positionX = (float)x; float positionZ = (float)(y + 1); //position = new Vector3(-sizeWidther*totalSize, -sizeHeighter*totalSize, 0), vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(x + 1); positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(x + 1); positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)x; positionZ = (float)y; vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; // Upper left. positionX = (float)x; positionZ = (float)(y + 1); vertices[index].position = new Vector3(positionX * sizeWidther, positionZ * sizeHeighter, 0.0f) * _tileSize; vertices[index].color = new Vector4(_r, _g, _b, _a); indices[index] = index; index++; } } // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; return(true); } catch { return(false); } }
private bool InitializeBuffers(Device device) { // Set the number of vertices in the vertex array. VertexCount = 6; // Create the vertex array. DVertexType[] vertices = new DVertexType[VertexCount]; // Load the vertex array with data. vertices[0].position = new Vector3(0.0f, 0.0f, 0.0f); // Bottom left. vertices[0].texture = new Vector2(0.0f, 1.0f); vertices[1].position = new Vector3(0.0f, 1.0f, 0.0f); // Top left. vertices[1].texture = new Vector2(0.0f, 0.0f); vertices[2].position = new Vector3(1.0f, 0.0f, 0.0f); // Bottom right. vertices[2].texture = new Vector2(1.0f, 1.0f); vertices[3].position = new Vector3(1.0f, 0.0f, 0.0f); // Bottom right. vertices[3].texture = new Vector2(1.0f, 1.0f); vertices[4].position = new Vector3(0.0f, 1.0f, 0.0f); // Top left. vertices[4].texture = new Vector2(0.0f, 0.0f); vertices[5].position = new Vector3(1.0f, 1.0f, 0.0f); // Top right. vertices[5].texture = new Vector2(1.0f, 0.0f); // Set up the description of the vertex buffer. // Now finally create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Release the array now that the vertex buffer has been created and loaded. vertices = null; // Set the number of instances in the array. InstanceCount = FoliageCount; // Create the instance array. Instances = new DInstanceType[InstanceCount]; // Setup an initial matrix. Matrix matrix = Matrix.Identity; // Load the instance array with data. for (int i = 0; i < InstanceCount; i++) { Instances[i].matrix = matrix; Instances[i].color = new Vector3(FoliageArray[i].r, FoliageArray[i].g, FoliageArray[i].b); } // Set up the description of the Dynamic instance buffer. BufferDescription instanceBufferDesc = new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = Utilities.SizeOf <DInstanceType>() * InstanceCount, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }; // Create the instance buffer. InstanceBuffer = SharpDX.Direct3D11.Buffer.Create(device, Instances, instanceBufferDesc); /// C+ Way return(true); }