35 using std::make_unique;
37 using std::unique_ptr;
65 Model* GenerateBillboardLOD::generate(
67 const string& pathName,
68 const string& fileName
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);
92 if (maxAxisDimension < Math::EPSILON) maxAxisDimension = 1.0f;
94 auto camera = osEngine->getCamera();
95 camera->setLookAt(boundingBox->getCenter());
96 camera->setLookFrom(boundingBox->getCenter().clone().add(
Vector3(0.0f, 0.0f, boundingBox->getCenter().getZ() + maxAxisDimension * 1.25f)));
98 osEngine->addEntity(
new Object(
"model", model));
102 osEngine->makeScreenshot(pathName, textureFileName,
false);
112 auto texture = TextureReader::read(pathName, textureFileName,
false,
false);
113 auto textureTextureData = texture->getRGBTextureData();
114 for (
auto y = 0; y < texture->getTextureHeight(); y++) {
115 for (
auto x = 0; x < texture->getTextureWidth(); x++) {
116 auto alpha = textureTextureData.get(y * texture->getTextureWidth() * 4 + x * 4 + 3);
117 if (alpha < 5)
continue;
118 minX = Math::min(minX, x);
119 maxX = Math::max(maxX, x);
120 minY = Math::min(minY, y);
121 maxY = Math::max(maxY, y);
126 auto croppedTextureWidth = maxX - minX;
127 auto croppedTextureHeight = maxY - minY;
128 auto croppedTextureByteBuffer =
ByteBuffer(croppedTextureWidth * croppedTextureHeight * 4);
129 for (
auto y = minY; y < maxY; y++) {
130 for (
auto x = minX; x < maxX; x++) {
131 auto pixelOffset = y * texture->getTextureWidth() * 4 + x * 4;
132 auto red = textureTextureData.get(pixelOffset + 0);
133 auto green = textureTextureData.get(pixelOffset + 1);
134 auto blue = textureTextureData.get(pixelOffset + 2);
135 auto alpha = textureTextureData.get(pixelOffset + 3);
136 croppedTextureByteBuffer.put(red);
137 croppedTextureByteBuffer.put(green);
138 croppedTextureByteBuffer.put(blue);
139 croppedTextureByteBuffer.put(alpha);
143 texture->releaseReference();
147 auto croppedTexture =
153 "tdme.engine.croppedtexture",
154 Texture::TEXTUREDEPTH_RGBA,
155 Texture::TEXTUREFORMAT_RGBA,
157 croppedTextureHeight,
159 croppedTextureHeight,
160 Texture::TEXTUREFORMAT_RGBA,
161 croppedTextureByteBuffer
164 croppedTexture->acquireReference();
165 auto scaledTexture = unique_ptr<
Texture, decltype([](
Texture* texture){ texture->
releaseReference(); })>(TextureReader::scale(croppedTexture.get(), 1024, 1024));
167 PNGTextureWriter::write(scaledTexture.get(), pathName, textureFileName,
false,
false);
171 auto left = boundingBox->getMin().getX();
172 auto right = boundingBox->getMax().getX();
173 auto top = boundingBox->getMin().getY();
174 auto bottom = boundingBox->getMax().getY();
175 auto depth = boundingBox->getCenter().getZ();
177 auto billboard = make_unique<Model>(modelId, modelId, UpVector::Y_UP, RotationOrder::ZYX,
nullptr);
179 auto billboardMaterial = make_unique<Material>(
"billboard");
180 billboardMaterial->setSpecularMaterialProperties(make_unique<SpecularMaterialProperties>().release());
181 billboardMaterial->getSpecularMaterialProperties()->setSpecularColor(
Color4(0.0f, 0.0f, 0.0f, 1.0f));
182 billboardMaterial->getSpecularMaterialProperties()->setDiffuseTexture(pathName, textureFileName);
183 billboardMaterial->getSpecularMaterialProperties()->setDiffuseTextureMaskedTransparency(
true);
185 auto billboardNode = make_unique<Node>(billboard.get(),
nullptr,
"billboard",
"billboard");
186 vector<Vector3> billboardVertices;
187 billboardVertices.emplace_back(left, top, depth);
188 billboardVertices.emplace_back(left, bottom, depth);
189 billboardVertices.emplace_back(right, bottom, depth);
190 billboardVertices.emplace_back(right, top, depth);
191 vector<Vector3> billboardNormals;
192 billboardNormals.emplace_back(0.0f, 1.0f, 0.0f);
193 vector<Vector2> billboardTextureCoordinates;
194 billboardTextureCoordinates.emplace_back(0.0f, 1.0f);
195 billboardTextureCoordinates.emplace_back(0.0f, 0.0f);
196 billboardTextureCoordinates.emplace_back(1.0f, 0.0f);
197 billboardTextureCoordinates.emplace_back(1.0f, 1.0f);
198 vector<Face> billboardFacesGround;
199 billboardFacesGround.emplace_back(billboardNode.get(), 0, 1, 2, 0, 0, 0, 0, 1, 2);
200 billboardFacesGround.emplace_back(billboardNode.get(), 2, 3, 0, 0, 0, 0, 2, 3, 0);
201 FacesEntity billboardNodeFacesEntity(billboardNode.get(),
"billboard.facesentity");
202 billboardNodeFacesEntity.
setMaterial(billboardMaterial.get());
203 vector<FacesEntity> billboardNodeFacesEntities;
204 billboardNodeFacesEntity.
setFaces(billboardFacesGround);
205 billboardNodeFacesEntities.push_back(billboardNodeFacesEntity);
206 billboardNode->setVertices(billboardVertices);
207 billboardNode->setNormals(billboardNormals);
208 billboardNode->setTextureCoordinates(billboardTextureCoordinates);
209 billboardNode->setFacesEntities(billboardNodeFacesEntities);
211 billboard->getNodes()[
"billboard"] = billboardNode.get();
212 billboard->getSubNodes()[
"billboard"] = billboardNode.get();
213 billboardNode.release();
215 billboard->getMaterials()[billboardMaterial->getId()] = billboardMaterial.get();
216 billboardMaterial.release();
218 ModelTools::prepareForIndexedRendering(billboard.get());
228 return billboard.release();
Color 4 definition class.
LOD object to be used with engine class.
Object to be used with engine class.
Bogus/Simple partition implementation.
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,...
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
std::exception Exception
Exception base class.