3#include "ClipmapManager.h"
4#include "ClipmapQueryHandler.h"
6#include "Raster/BridgeRasterSource.h"
8#include "Rendering/IGraphicsDevice.h"
10#include "Foundation/Logging/Logger.h"
12#include <unordered_map>
24 std::unordered_map<int64_t, RasterSourcePtr> activeSources;
33 bool postInitialized =
false;
37void terrainInitialize()
39 LOG_DEBUG(logger,
"Initialized terrain.");
44 LOG_DEBUG(logger,
"Creating terrain...");
53 LOG_DEBUG(logger,
"Destroying terrain...");
55 terrain->manager.cleanupLevels();
59 LOG_DEBUG(logger,
"Terrain destroyed.");
66 context->manager.initialize(graphicsDevice);
67 context->queryHandler.initialize(&context->manager.terrainLevels, &context->manager.imagery);
72 context->manager.update();
75void terrainInvalidateTile(int64_t rasterSourceId,
int level,
int x,
int y,
int flags)
77 auto sIt = activeSources.find(rasterSourceId);
79 if (sIt == activeSources.end()) {
80 LOG_ERROR(logger,
"Cannot invalidate tiles from unregistered raster source (unused or disposed).");
84 WriteLock lock(*sIt->second);
86 sIt->second->invalidateTile({ level, x, y }, (InvalidationFlags::EInvalidationFlags)flags);
93 context->maxLevel = context->manager.preRender(*renderContext);
100 terrain->manager.renderDepth(*renderContext, terrain->maxLevel);
107 terrain->manager.render(*renderContext, terrain->maxLevel);
114 terrain->manager.renderOcean(*renderContext, terrain->maxLevel);
119 terrain->manager.oceanRenderer.setOptions(options);
126 context->manager.postRender(*renderContext);
128 context->manager.previousContext = *renderContext;
130 if (!context->postInitialized) {
131 context->queryHandler.postInitialize();
133 context->postInitialized =
true;
141 const size_t numImagery)
143 if (activeSources.find(terrainParameters->id) == activeSources.end()) {
144 auto terrainSource = std::make_shared<Cogs::BridgeRasterSource>(
nullptr, terrainParameters);
145 activeSources[terrainParameters->id] = std::move(terrainSource);
148 std::vector<RasterSourcePtr> imagery(numImagery);
150 for (
size_t i = 0; i < numImagery; ++i) {
151 auto id = imageryParameters[i].id;
153 if (activeSources.find(
id) == activeSources.end()) {
155 activeSources[id] = std::make_shared<Cogs::BridgeRasterSource>(
nullptr, &imageryParameters[i]);
158 imagery[i] = activeSources[id];
161 context->manager.setRasterSources(activeSources[terrainParameters->id], imagery);
164void terrainDisposeRasterSource(int64_t
id)
166 activeSources.erase(
id);
171 context->manager.setClipmapParameters(parameters);
182 for (
size_t i = 0; i < 8; ++i) {
189 if (!renderContext.wireframe) {
198 context->manager.debugOptions = *debugOptions;
201void terrainInvalidateRasterSource(
Cogs::TerrainContext * context,
const int64_t rasterSourceId,
const bool clearContents)
203 context->manager.invalidateRasterSource(activeSources[rasterSourceId].get(), clearContents);
208 return context->manager.getNearestSample();
211void terrainSetCustomParameters(
Cogs::TerrainContext * context,
const unsigned char * data,
int count)
213 context->manager.clipmapRenderer.setCustomParameters(data, count);
218 for (
int i = 0; i < count; ++i) {
219 context->manager.imageryEnabled[i] = data[i];
225 context->manager.getTerrainMetrics(metrics);
229 const float * screenPos,
230 const float * inverseViewProjection,
231 float * pickedPosition,
235 bool picked = terrain->manager.depthQuery.getPositions((
const Cogs::Vector2 *)screenPos,
236 *(Cogs::Matrix *)inverseViewProjection,
238 (Cogs::Vector4 *)pickedPosition, &valid, rayPickId == -1 ? 0 : rayPickId);
240 return picked && valid;
245 if (!terrain->manager.isInitialized() || !terrain->manager.terrainSource.isReady())
return;
247 if (!terrain->manager.terrainLevels.size())
return;
249 auto & levelZero = terrain->manager.terrainLevels[0];
250 auto & geoExtent = levelZero.rasterLevel->getGeoExtent();
251 auto & worldScale = terrain->manager.worldOptions.worldScale;
253 auto bbox =
reinterpret_cast<Cogs::Vector3d *
>(bounds);
255 bbox[0] = Cogs::Vector3d(geoExtent.getWest() * worldScale.x, geoExtent.getSouth() * worldScale.y, levelZero.minZ * terrain->manager.worldOptions.heightExaggeration);
256 bbox[1] = Cogs::Vector3d(geoExtent.getEast() * worldScale.x, geoExtent.getNorth() * worldScale.y, levelZero.maxZ * terrain->manager.worldOptions.heightExaggeration);
261 terrain->manager.worldOptions = *worldOptions;
263 terrain->manager.updateOptions.invalidateNormals =
true;
268 terrain->manager.updateOptions = *updateOptions;
273 terrain->manager.renderOptions = *renderOptions;
278 return terrain->manager.updateRequired();
283 terrain->queryHandler.postRequest(query, callback, terrain->manager.worldOptions);
288 terrain->queryHandler.processQueries();
293 effectParameters->changed = terrain->manager.effectsNeedUpdate;
294 effectParameters->numTextures = NUM_TEXTURES;
295 effectParameters->numActiveImagery = (int)terrain->manager.imagerySources.size();
296 effectParameters->precomputeNormals = terrain->manager.parameters.precomputeNormals;
298 terrain->manager.effectsNeedUpdate =
false;
Represents a graphics device used to manage graphics resources and issue drawing commands.
Log implementation class.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
static const Handle_t NoHandle
Represents a handle to nothing.
Represents a graphics device context which can receive rendering commands.
virtual void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle)=0
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setRasterizerState(const RasterizerStateHandle handle)=0
Set the current rasterizer state.
virtual void setInputLayout(const InputLayoutHandle inputLayoutHandle)=0
Sets the current input layout.
virtual void reset()
Resets all state changes made to the GPU since the last call to beginFrame.
virtual void setSamplerState(const StringView &name, unsigned int unit, SamplerStateHandle samplerStateHandle)=0
Sets the sampler slot given by unit with the given name to contain the given sampler state.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.