37 using std::unique_ptr;
66 void GenerateImposterLOD::generate(
68 const string& pathName,
69 const string& fileName,
71 vector<string>& imposterModelFileNames,
72 vector<Model*>& imposterModels
74 auto osEngine = unique_ptr<Engine>(Engine::createOffScreenInstance(4096, 4096,
true,
true,
false));
76 osEngine->setSceneColor(
Color4(0.0f, 0.0f, 0.0f, 0.0f));
78 auto light = osEngine->getLightAt(0);
79 light->setAmbient(
Color4(1.0f, 1.0f, 1.0f, 1.0f));
80 light->setDiffuse(
Color4(0.5f, 0.5f, 0.5f, 1.0f));
81 light->setSpecular(
Color4(1.0f, 1.0f, 1.0f, 1.0f));
82 light->setPosition(
Vector4(0.0f, 20000.0f, 0.0f, 0.0f));
83 light->setSpotDirection(
Vector3(0.0f, 0.0f, 0.0f).sub(
Vector3(light->getPosition().getX(), light->getPosition().getY(), light->getPosition().getZ())));
84 light->setConstantAttenuation(0.5f);
85 light->setLinearAttenuation(0.0f);
86 light->setQuadraticAttenuation(0.0f);
87 light->setSpotExponent(0.0f);
88 light->setSpotCutOff(180.0f);
89 light->setEnabled(
true);
93 if (maxAxisDimension < Math::EPSILON) maxAxisDimension = 1.0f;
96 auto camera = osEngine->getCamera();
97 camera->setLookAt(boundingBox->getCenter());
98 camera->setLookFrom(boundingBox->getCenter().clone().add(
Vector3(0.0f, 0.0f, boundingBox->getCenter().getZ() + maxAxisDimension * 1.25f)));
101 auto entity =
new Object(
"model", model);
102 entity->addRotation(
Vector3(0.0f, 1.0f, 0.0f), 0.0f);
103 osEngine->addEntity(entity);
106 imposterModels.resize(billboardCount);
107 imposterModelFileNames.resize(billboardCount);
110 for (
auto i = 0; i < billboardCount; i++) {
112 auto yRotation =
static_cast<float>(i) * (360.0f /
static_cast<float>(billboardCount));
113 entity->setRotationAngle(0,
static_cast<float>(yRotation));
120 auto modelFileName = modelId +
".tm";
122 osEngine->makeScreenshot(pathName, textureFileName,
false);
129 auto texture = unique_ptr<
Texture, decltype([](
Texture* texture){ texture->
releaseReference(); })>(TextureReader::read(pathName, textureFileName,
false,
false));
134 if (alpha < 5)
continue;
135 minX = Math::min(minX, x);
136 maxX = Math::max(maxX, x);
137 minY = Math::min(minY, y);
138 maxY = Math::max(maxY, y);
143 auto croppedTextureWidth = maxX - minX;
144 auto croppedTextureHeight = maxY - minY;
145 auto croppedTextureByteBuffer =
ByteBuffer(croppedTextureWidth * croppedTextureHeight * 4);
146 for (
auto y = minY; y < maxY; y++) {
147 for (
auto x = minX; x < maxX; x++) {
149 auto red = textureTextureData.get(pixelOffset + 0);
150 auto green = textureTextureData.get(pixelOffset + 1);
151 auto blue = textureTextureData.get(pixelOffset + 2);
152 auto alpha = textureTextureData.get(pixelOffset + 3);
153 croppedTextureByteBuffer.put(red);
154 croppedTextureByteBuffer.put(green);
155 croppedTextureByteBuffer.put(blue);
156 croppedTextureByteBuffer.put(alpha);
162 auto croppedTexture =
168 "tdme.engine.croppedtexture",
169 Texture::TEXTUREDEPTH_RGBA,
170 Texture::TEXTUREFORMAT_RGBA,
172 croppedTextureHeight,
174 croppedTextureHeight,
175 Texture::TEXTUREFORMAT_RGBA,
176 croppedTextureByteBuffer
179 croppedTexture->acquireReference();
180 auto scaledTexture = unique_ptr<
Texture, decltype([](
Texture* texture){ texture->
releaseReference(); })>(TextureReader::scale(croppedTexture.get(), 1024, 1024));
182 PNGTextureWriter::write(scaledTexture.get(), pathName, textureFileName,
false,
false);
186 auto left = boundingBox->getMin().getX();
187 auto right = boundingBox->getMax().getX();
188 auto top = boundingBox->getMin().getY();
189 auto bottom = boundingBox->getMax().getY();
190 auto depth = boundingBox->getCenter().getZ();
192 auto billboard = make_unique<Model>(modelId, modelId, UpVector::Y_UP, RotationOrder::ZYX,
nullptr);
194 auto billboardMaterial = make_unique<Material>(
"billboard");
195 billboardMaterial->setSpecularMaterialProperties(make_unique<SpecularMaterialProperties>().release());
196 billboardMaterial->getSpecularMaterialProperties()->setSpecularColor(
Color4(0.0f, 0.0f, 0.0f, 1.0f));
197 billboardMaterial->getSpecularMaterialProperties()->setDiffuseTexture(pathName, textureFileName);
198 billboardMaterial->getSpecularMaterialProperties()->setDiffuseTextureMaskedTransparency(
true);
200 auto billboardNode = make_unique<Node>(billboard.get(),
nullptr,
"billboard",
"billboard");
201 vector<Vector3> billboardVertices;
202 billboardVertices.emplace_back(left, top, depth);
203 billboardVertices.emplace_back(left, bottom, depth);
204 billboardVertices.emplace_back(right, bottom, depth);
205 billboardVertices.emplace_back(right, top, depth);
206 vector<Vector3> billboardNormals;
207 billboardNormals.emplace_back(0.0f, 1.0f, 0.0f);
208 vector<Vector2> billboardTextureCoordinates;
209 billboardTextureCoordinates.emplace_back(0.0f, 1.0f);
210 billboardTextureCoordinates.emplace_back(0.0f, 0.0f);
211 billboardTextureCoordinates.emplace_back(1.0f, 0.0f);
212 billboardTextureCoordinates.emplace_back(1.0f, 1.0f);
213 vector<Face> billboardFacesGround;
214 billboardFacesGround.emplace_back(billboardNode.get(), 0, 1, 2, 0, 0, 0, 0, 1, 2);
215 billboardFacesGround.emplace_back(billboardNode.get(), 2, 3, 0, 0, 0, 0, 2, 3, 0);
216 FacesEntity billboardNodeFacesEntity(billboardNode.get(),
"billboard.facesentity");
217 billboardNodeFacesEntity.
setMaterial(billboardMaterial.get());
218 vector<FacesEntity> billboardNodeFacesEntities;
219 billboardNodeFacesEntity.
setFaces(billboardFacesGround);
220 billboardNodeFacesEntities.push_back(billboardNodeFacesEntity);
221 billboardNode->setVertices(billboardVertices);
222 billboardNode->setNormals(billboardNormals);
223 billboardNode->setTextureCoordinates(billboardTextureCoordinates);
224 billboardNode->setFacesEntities(billboardNodeFacesEntities);
225 billboard->getNodes()[
"billboard"] = billboardNode.get();
226 billboard->getSubNodes()[
"billboard"] = billboardNode.get();
227 billboardNode.release();
229 billboard->getMaterials()[billboardMaterial->getId()] = billboardMaterial.get();
230 billboardMaterial.release();
232 ModelTools::prepareForIndexedRendering(billboard.get());
242 imposterModels[i] = billboard.release();
243 imposterModelFileNames[i] = (pathName.empty() ==
false?pathName +
"/":
"") + modelFileName;
Color 4 definition class.
LOD object to be used with engine class.
Object to be used with engine class.
Bogus/Simple partition implementation.
ByteBuffer getRGBTextureData()
uint16_t getTextureHeight() const
uint16_t getTextureWidth() const
PNG texture writer class.
Represents a model face, consisting of vertex, normal, tangent and bitangent vectors,...
Node faces entity A node can have multiple entities containing faces and a applied material.
void setMaterial(Material *material)
Set up the entity's material.
void setFaces(const vector< Face > &faces)
Set up entity's faces.
Representation of a 3D model.
BoundingBox * getBoundingBox()
Represents rotation orders of a model.
Represents specular material properties.
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Vector4 class representing vector4 mathematical structure and operations with x, y,...
Generate imposter LOD utility class.
uint8_t get(int64_t position) const
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
std::exception Exception
Exception base class.