Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
idontknow
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
void Terrain::RenderPatch(GraphicsDevice* device, Camera *camera, float offsetHeightX, float offsetHeightY, float scaleHeight, float offsetPositionX, float offsetPositionY, float scalePosition, int level) { float centerX = offsetPositionX + _patchSize * scalePosition * 0.5f; float centerZ = offsetPositionY - _patchSize * scalePosition * 0.5f; XMFLOAT3 patchCenter(centerX, 0.0f, centerZ); float triangleSize = scalePosition; float distance; XMStoreFloat(&distance, XMVector3Length(XMLoadFloat3(&patchCenter) - XMLoadFloat3(&camera->GetPosition()))); float distanceToSizeQuotient = distance / triangleSize; if (distanceToSizeQuotient > _lodParameter || level < 1) { _terrainEffect->SetTerrainPatchData(XMFLOAT2(offsetHeightX, offsetHeightY), scaleHeight, XMFLOAT2(offsetPositionX, offsetPositionY), scalePosition); _terrainEffect->Apply(device->GetImmediateContext()); device->DrawIndexed(_indexCount); } else { float halfHeightMapSize = scaleHeight * 0.5f; float halfScalePosition = scalePosition * 0.5f; RenderPatch(device, camera, offsetHeightX, offsetHeightY, halfHeightMapSize, offsetPositionX, offsetPositionY, halfScalePosition, level-1); RenderPatch(device, camera, offsetHeightX+halfHeightMapSize, offsetHeightY, halfHeightMapSize, centerX, offsetPositionY, halfScalePosition, level-1); RenderPatch(device, camera, offsetHeightX, offsetHeightY+halfHeightMapSize, halfHeightMapSize, offsetPositionX, centerZ, halfScalePosition, level-1); RenderPatch(device, camera, offsetHeightX+halfHeightMapSize, offsetHeightY+halfHeightMapSize, halfHeightMapSize, centerX, centerZ, halfScalePosition, level-1); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
void Terrain::CustomDraw(GraphicsDevice* device, SceneNode *sceneNode, Camera *camera) { //Vorbereitung zum Rendern etc. .... weggelasen float scalePosition = _terrainSize / _patchSize; float scaleHeightMap = 1.0f; //Parameter 3,4 und 5 sind die Texturkoordinaten für die heightmap, also 0,0,1 RenderPatch(device, camera, 0,0, scaleHeightMap, -_terrainSize * 0.5f, _terrainSize * 0.5f, scalePosition, _maxLevels); } |
HLSL-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
cbuffer cbPerScene { float3 LightDir = float3(1.0f,-3,1.0f); float3 CameraPosition; float HeightMapScale = 200.0f; float2 TileBase = float2(10.0f, 10.0f); } cbuffer cbPerObject { float4x4 WorldViewProjection; float2 PosOffset = 0; float PosScale = 1; float2 HeightOffset = 0.0; float HeightScale = 1.0; } Texture2D HeightMap; SamplerState HeightMapSampler { Filter = MIN_MAG_MIP_LINEAR; AddressU = CLAMP; AddressV = CLAMP; }; struct VertexInput { float3 Position : POSITION0; float2 Texcoord : TEXCOORD0; }; struct VertexOutput { float4 Position : SV_POSITION; float2 Texcoord : TEXCOORD0; }; float3 GetNormalFromHeightMap(Texture2D tex, SamplerState samp, float2 texCoord, float offsetToNextPixel) { float2 offsets[4]; offsets[0] = texCoord + float2(-offsetToNextPixel, 0); offsets[1] = texCoord + float2(offsetToNextPixel, 0); offsets[2] = texCoord + float2(0, -offsetToNextPixel); offsets[3] = texCoord + float2(0, offsetToNextPixel); float hts[4]; for(int i = 0; i < 4; i++) { hts[i] = tex.SampleLevel(samp, offsets[i], 0).x * HeightMapScale; } const float cellSpace = 1.0f; float3 tangent = normalize(float3(2.0f * cellSpace, hts[1] - hts[0], 0.0f)); float3 bitangent = normalize(float3(0.0f, hts[3] - hts[2], -2.0f * cellSpace)); return normalize(cross(tangent, bitangent)).xyz; } float4 PSMain(VertexOutput input) : SV_TARGET { //relevante Zeile. Ermittelt die Normale. float3 normal = normalize(GetNormalFromHeightMap(HeightMap, HeightMapSampler, HeightOffset + input.Texcoord * HeightScale, 1.0f / 64.0f)); float diffuseFactor = saturate(dot(normal, normalize(-LightDir))); float4 materialTextureColor = DiffuseTexture.Sample(DiffuseMapSampler, input.Texcoord * TileBase); return materialTextureColor * diffuseFactor; } |
Werbeanzeige