13 #include <osg/ShadeModel>
14 #include <osgUtil/Optimizer>
29 osg::NodeVisitor * )
override
31 uniform->set(osg::Vec3(
camera->getInverseViewMatrix().getTrans()));
42 osg::NodeVisitor * )
override
44 uniform->set(
geode->getBound().center());
57 osg::NodeVisitor *nodeVisitor)
override
59 uniform->set(osg::computeLocalToWorld(nodeVisitor->getNodePath()));
68 osg::NodeVisitor * )
override
70 uniform->set(osg::Matrix(
camera->getViewMatrix()));
81 osg::NodeVisitor * )
override
83 uniform->set(osg::Matrix(
camera->getInverseViewMatrix()));
94 osg::NodeVisitor * )
override
96 uniform->set(osg::Matrix(
camera->getProjectionMatrix()));
107 osg::NodeVisitor *nodeVisitor)
override
109 const auto viewMatrix =
camera->getViewMatrix();
110 const auto modelMatrix =
111 osg::computeLocalToWorld(nodeVisitor->getNodePath());
112 const auto mvpMatrix =
113 modelMatrix * viewMatrix *
camera->getProjectionMatrix();
115 uniform->set(mvpMatrix);
124 auto baseDir = QDir(QApplication::applicationDirPath());
126 #if defined(Q_OS_MAC)
127 if(baseDir.dirName() ==
"MacOS")
135 return baseDir.absolutePath().toStdString();
138 inline osg::ref_ptr< osg::Texture2D >
141 osg::ref_ptr< osg::Image > img =
new osg::Image();
142 img->allocateImage(1, 1, 1, GL_RGB, GL_UNSIGNED_BYTE);
144 const osg::Vec3ub color(value, value, value);
145 const auto imgData =
reinterpret_cast< osg::Vec3ub *
>(img->data());
148 osg::ref_ptr< osg::Texture2D > texture =
new osg::Texture2D();
149 texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
150 texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
151 texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
152 texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
153 texture->setImage(img);
166 const unsigned char *imgdata = osgimg.data();
167 unsigned int channels =
168 osgimg.getTotalDataSize() / (osgimg.s() * osgimg.t() * osgimg.r());
170 std::cout <<
"----------------------------\n";
171 std::cout <<
"OSG image info:\n";
172 std::cout <<
" width " << osgimg.s() <<
'\n';
173 std::cout <<
" height " << osgimg.t() <<
'\n';
174 std::cout <<
" depth " << osgimg.r() <<
'\n';
175 std::cout <<
" channels " << channels <<
'\n';
176 std::cout <<
" contig. " << osgimg.isDataContiguous() <<
'\n';
177 std::cout <<
" dataSz " << osgimg.getTotalDataSize() <<
'\n';
178 std::cout <<
" data() " <<
static_cast< const void*
>(osgimg.data())
180 std::cout <<
" data[] ";
182 for(
unsigned int l = 0; l < 2; l++)
184 const unsigned char *linedata = imgdata + l*osgimg.s()*channels;
186 for(
unsigned int i = 0; i < 2*channels; i++)
188 std::cout <<
' ' << (int)linedata[i];
191 std::cout <<
"...\n" <<
" ";
193 std::cout <<
" ...\n";
194 std::cout <<
"----------------------------\n";
205 const unsigned char *imgdata = cimg.data();
207 std::cout <<
"----------------------------\n";
208 std::cout <<
"CImg image info:\n";
209 std::cout <<
" width " << cimg.width() <<
'\n';
210 std::cout <<
" height " << cimg.height() <<
'\n';
211 std::cout <<
" depth " << cimg.depth() <<
'\n';
212 std::cout <<
" channels " << cimg.spectrum() <<
'\n';
213 std::cout <<
" data() " <<
static_cast< const void*
>(cimg.data())
215 std::cout <<
" data[] ";
218 for(
unsigned int l = 0; l < 2; l++)
220 const unsigned char *linedata = imgdata + l*cimg.width();
222 for(
unsigned int i = 0; i < 3; i++)
224 std::cout <<
' ' << (int)linedata[i];
227 std::cout <<
"...\n" <<
" ";
229 std::cout <<
" ...\n";
230 std::cout <<
"----------------------------\n";
249 if(cimg.depth() != 1)
251 std::cout <<
"WARNING: cimg_to_osgimg: unsupported image depth = "
252 << cimg.depth() <<
"." << std::endl;
255 else if(cimg.spectrum() != 3 && cimg.spectrum() != 4)
257 std::cout <<
"WARNING: cimg_to_osgimg: unsupported image channels = "
258 << cimg.spectrum() <<
"." << std::endl;
263 bool alpha = (cimg.spectrum() == 4);
264 const unsigned char *cimg_r_ptr = cimg.data(0, 0, 0, 0);
265 const unsigned char *cimg_g_ptr = cimg.data(0, 0, 0, 1);
266 const unsigned char *cimg_b_ptr = cimg.data(0, 0, 0, 2);
267 const unsigned char *cimg_a_ptr =
268 (alpha) ? cimg.data(0, 0, 0, 3) :
nullptr;
272 auto osg_pix_format = (alpha) ? GL_RGBA : GL_RGB;
273 osgimg->allocateImage(cimg.width(), cimg.height(), cimg.depth(),
274 osg_pix_format, GL_UNSIGNED_BYTE);
275 unsigned char *osg_data = osgimg->data();
279 unsigned int w = cimg.width();
280 unsigned int h = cimg.height();
281 unsigned int ch = cimg.spectrum();
283 for(
unsigned int l = 0; l < h; l++)
287 unsigned char *osg_ptr = osg_data + w*(h-1-l)*ch;
289 for(
unsigned int c = 0; c < w; c++)
292 *osg_ptr = *cimg_r_ptr;
294 *osg_ptr = *cimg_g_ptr;
296 *osg_ptr = *cimg_b_ptr;
301 *osg_ptr = *cimg_a_ptr;
318 inline osg::ref_ptr< osg::Texture2D >
322 osg::ref_ptr< osg::Texture2D > texture;
329 auto &cimg = material.
images.at(filename);
343 texture =
new osg::Texture2D;
346 texture->setDataVariance(osg::Object::STATIC);
349 texture->setImage(image);
351 texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
352 texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
353 texture->setFilter(osg::Texture2D::MIN_FILTER,
354 osg::Texture2D::LINEAR_MIPMAP_LINEAR);
355 texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
359 std::cerr <<
"-> [MeshLoading] Couldn't find texture file, creating "
372 uint8_t map_index = 0;
374 geometry->getOrCreateStateSet()->addUniform(
375 new osg::Uniform(
"uniMaterial.ambientMap", 0));
376 geometry->getOrCreateStateSet()->addUniform(
377 new osg::Uniform(
"uniMaterial.diffuseMap", 1));
378 geometry->getOrCreateStateSet()->addUniform(
379 new osg::Uniform(
"uniMaterial.specularMap", 2));
380 geometry->getOrCreateStateSet()->addUniform(
381 new osg::Uniform(
"uniMaterial.emissiveMap", 3));
382 geometry->getOrCreateStateSet()->addUniform(
383 new osg::Uniform(
"uniMaterial.transparencyMap", 4));
384 geometry->getOrCreateStateSet()->addUniform(
385 new osg::Uniform(
"uniMaterial.bumpMap", 5));
394 osg::ref_ptr< osg::Texture2D > texture;
396 if(!texture_filename.empty())
403 geometry->getOrCreateStateSet()->setTextureAttribute(
406 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
415 uint8_t map_index = 0;
417 geometry->getOrCreateStateSet()->addUniform(
418 new osg::Uniform(
"uniMaterial.albedoMap", 0));
419 geometry->getOrCreateStateSet()->addUniform(
420 new osg::Uniform(
"uniMaterial.normalMap", 1));
421 geometry->getOrCreateStateSet()->addUniform(
422 new osg::Uniform(
"uniMaterial.metallicMap", 2));
423 geometry->getOrCreateStateSet()->addUniform(
424 new osg::Uniform(
"uniMaterial.roughnessMap", 3));
425 geometry->getOrCreateStateSet()->addUniform(
426 new osg::Uniform(
"uniMaterial.emissiveMap", 4));
427 geometry->getOrCreateStateSet()->addUniform(
428 new osg::Uniform(
"uniMaterial.ambientOcclusionMap", 5));
437 osg::ref_ptr< osg::Texture2D > texture;
439 if(!texture_filename.empty())
446 geometry->getOrCreateStateSet()->setTextureAttribute(
449 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
454 template<
typename VertexNormalMap,
455 typename VertexTangentMap,
456 typename FaceMaterialMap >
457 inline osg::ref_ptr< osg::Program >
459 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries,
460 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
461 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
462 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_tangentsArrays,
463 const std::vector< osg::ref_ptr< osg::Vec2Array > > &_texcoordsArrays,
464 bool _useSmoothShading,
466 VertexTangentMap *_vt_tm,
467 FaceMaterialMap *_m_mm,
471 osg::ref_ptr< osg::Program > program =
new osg::Program;
473 osg::ref_ptr< osg::Shader > vertShader =
new osg::Shader(osg::Shader::VERTEX);
474 if(!vertShader->loadShaderSourceFromFile(shadersDirectory +
"vert.glsl"))
475 std::cerr <<
"[MeshLoading] Could not read VERTEX shader from file"
477 program->addShader(vertShader);
483 _geometries[unit_ii]->setVertexAttribArray(
484 1, _texcoordsArrays[unit_ii], osg::Array::BIND_PER_VERTEX);
487 if(_vt_tm !=
nullptr && _useSmoothShading)
488 _geometries[unit_ii]->setVertexAttribArray(
489 3, _tangentsArrays[unit_ii], osg::Array::BIND_PER_VERTEX);
491 std::string fragShaderLocation =
492 shadersDirectory +
"blinn-phong.glsl";
494 const auto &material =
get(*_m_mm, unit_ii);
495 if(!material.name.empty())
500 fragShaderLocation = shadersDirectory +
"cook-torrance.glsl";
502 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
503 new osg::Uniform(
"uniMaterial.baseColor",
504 osg::Vec3f(material.diffuse_red_component,
505 material.diffuse_green_component,
506 material.diffuse_blue_component)));
507 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
508 new osg::Uniform(
"uniMaterial.metallicFactor",
509 static_cast< float >(material.metallic_factor)));
510 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
511 new osg::Uniform(
"uniMaterial.roughnessFactor",
512 static_cast< float >(material.roughness_factor)));
513 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
514 new osg::Uniform(
"uniMaterial.emissive",
515 osg::Vec3f(material.emissive_red_component,
516 material.emissive_green_component,
517 material.emissive_blue_component)));
523 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
524 new osg::Uniform(
"uniMaterial.ambient",
525 osg::Vec3f(material.ambient_red_component,
526 material.ambient_green_component,
527 material.ambient_blue_component)));
528 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
529 new osg::Uniform(
"uniMaterial.diffuse",
530 osg::Vec3f(material.diffuse_red_component,
531 material.diffuse_green_component,
532 material.diffuse_blue_component)));
533 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
534 new osg::Uniform(
"uniMaterial.specular",
535 osg::Vec3f(material.specular_red_component,
536 material.specular_green_component,
537 material.specular_blue_component)));
538 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
539 new osg::Uniform(
"uniMaterial.emissive",
540 osg::Vec3f(material.emissive_red_component,
541 material.emissive_green_component,
542 material.emissive_blue_component)));
543 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
544 new osg::Uniform(
"uniMaterial.transparency",
545 static_cast< float >(material.transparency)));
550 if(material.has_normal_map && _useSmoothShading)
551 _geometries[unit_ii]->getOrCreateStateSet()->addUniform(
552 new osg::Uniform(
"uniUseNormalMapping",
true));
555 osg::ref_ptr< osg::Shader > fragShader =
556 new osg::Shader(osg::Shader::FRAGMENT);
557 if(!fragShader->loadShaderSourceFromFile(fragShaderLocation))
558 std::cerr <<
"[MeshLoading] Could not read FRAGMENT shader from file"
561 program->addShader(fragShader);
566 template<
typename VertexNormalMap,
typename VertexColorMap >
567 inline osg::ref_ptr< osg::Program >
569 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries,
570 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
571 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
572 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays,
574 VertexColorMap *_vt_cm,
578 osg::ref_ptr< osg::Program > program =
new osg::Program;
580 osg::ref_ptr< osg::Shader > vertShader =
new osg::Shader(osg::Shader::VERTEX);
581 if(!vertShader->loadShaderSourceFromFile(shadersDirectory +
"colorVert.glsl"))
582 std::cerr <<
"[MeshLoading] Could not read VERTEX shader from file"
584 program->addShader(vertShader);
586 osg::ref_ptr< osg::Shader > fragShader =
587 new osg::Shader(osg::Shader::FRAGMENT);
588 if(!fragShader->loadShaderSourceFromFile(shadersDirectory +
"colorFrag.glsl"))
589 std::cerr <<
"[MeshLoading] Could not read FRAGMENT shader from file"
591 program->addShader(fragShader);
597 _geometries[unit_ii]->setVertexAttribArray(
599 _colorsArrays[unit_ii],
600 (_vt_cm !=
nullptr ? osg::Array::BIND_PER_VERTEX
601 : osg::Array::BIND_PER_PRIMITIVE_SET));
606 template<
typename HalfedgeGraph,
607 typename VertexNormalMap,
608 typename VertexTangentMap,
609 typename VertexColorMap,
610 typename FaceColorMap,
611 typename VertexUVMap,
612 typename HalfedgeUVMap,
613 typename FaceMaterialMap >
618 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries,
619 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_edges,
620 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_vertices,
621 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_normals,
622 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_custom_vectors,
623 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays,
624 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_edges,
625 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_vertices,
626 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_normals,
627 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_custom_vectors,
628 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_normalsArrays,
629 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_normalsArrays_edges,
630 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_normalsArrays_vertices,
631 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_tangentsArrays,
632 const std::vector< osg::ref_ptr< osg::Vec2Array > > &_texcoordsArrays,
633 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays,
634 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_edges,
635 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_vertices,
636 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_normals,
637 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_custom_vectors,
638 std::size_t _m_mm_size,
639 VertexNormalMap *_vt_nm,
640 VertexTangentMap *_vt_tm,
641 VertexColorMap *_vt_cm,
643 VertexUVMap *_vt_uv_m,
644 HalfedgeUVMap *_het_uv_m,
645 FaceMaterialMap *_m_mm,
646 bool has_face_color_map)
648 std::cout <<
"[MeshLoading] Loading using shaders." << std::endl;
652 std::vector< osg::ref_ptr< osg::Vec4Array > > colorsArrays2;
657 _geometries[unit_ii]->setStateSet(NULL);
658 _geometries[unit_ii]->setColorArray(NULL);
660 osg::ref_ptr< osg::Program > program;
663 _geometries[unit_ii]->setVertexAttribArray(
664 0, _vertexArrays[unit_ii], osg::Array::BIND_PER_VERTEX);
667 _geometries[unit_ii]->setVertexAttribArray(
669 _normalsArrays[unit_ii],
670 (_vt_nm !=
nullptr ? osg::Array::BIND_PER_VERTEX
671 : osg::Array::BIND_PER_PRIMITIVE_SET));
674 if((_vt_uv_m !=
nullptr || _het_uv_m !=
nullptr) &&
675 !_texcoordsArrays[unit_ii].
get()->empty())
690 colorsArrays2.push_back(
new osg::Vec4Array);
692 const std::vector< osg::ref_ptr< osg::Vec4Array > > *colorsArrays_tmp;
694 VertexColorMap vt_cm2;
695 VertexColorMap *vt_cm_tmp;
697 if(_vt_cm !=
nullptr || _f_cm !=
nullptr)
699 colorsArrays_tmp = &_colorsArrays;
704 colorsArrays2[unit_ii].get()->resize(
705 _vertexArrays[unit_ii].
get()->size(),
708 colorsArrays_tmp = &colorsArrays2;
723 _geometries[unit_ii]->getOrCreateStateSet()->setAttributeAndModes(
724 program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
730 _geode->addDrawable(_geometries[unit_ii]);
738 osg::ref_ptr< osg::Program > superimpProgram =
new osg::Program;
740 osg::ref_ptr< osg::Shader > superimpVertShader =
741 new osg::Shader(osg::Shader::VERTEX);
742 if(!superimpVertShader->loadShaderSourceFromFile(
743 shadersDirectory +
"superimposeVert.glsl"))
744 std::cerr <<
"[MeshLoading] Could not read VERTEX shader from file"
746 superimpProgram->addShader(superimpVertShader);
758 osg::ref_ptr< osg::Shader > superimpFragShader =
759 new osg::Shader(osg::Shader::FRAGMENT);
760 if(!superimpFragShader->loadShaderSourceFromFile(
761 shadersDirectory +
"superimposeFrag.glsl"))
762 std::cerr <<
"Could not read FRAGMENT shader from file" << std::endl;
763 superimpProgram->addShader(superimpFragShader);
770 std::cout <<
"[MeshLoading] Drawing superimposed edges" << std::endl;
775 _geometries_edges[unit_ii]->setVertexAttribArray(
776 0, _vertexArrays_edges[unit_ii], osg::Array::BIND_PER_VERTEX);
777 _geometries_edges[unit_ii]->setVertexAttribArray(
778 1, _colorsArrays_edges[unit_ii], osg::Array::BIND_PER_VERTEX);
779 _geometries_edges[unit_ii]->setVertexAttribArray(
780 2, _normalsArrays_edges[unit_ii], osg::Array::BIND_PER_VERTEX);
781 _geometries_edges[unit_ii]
782 ->getOrCreateStateSet()
783 ->setAttributeAndModes(superimpProgram.get(),
784 osg::StateAttribute::ON |
785 osg::StateAttribute::OVERRIDE);
787 _geode->addDrawable(_geometries_edges[unit_ii]);
794 std::cout <<
"[MeshLoading] Drawing superimposed vertices" << std::endl;
799 _geometries_vertices[unit_ii]->setVertexAttribArray(
800 0, _vertexArrays_vertices[unit_ii], osg::Array::BIND_PER_VERTEX);
801 _geometries_vertices[unit_ii]->setVertexAttribArray(
802 1, _colorsArrays_vertices[unit_ii], osg::Array::BIND_PER_VERTEX);
803 _geometries_vertices[unit_ii]->setVertexAttribArray(
804 2, _normalsArrays_vertices[unit_ii], osg::Array::BIND_PER_VERTEX);
805 _geometries_vertices[unit_ii]
806 ->getOrCreateStateSet()
807 ->setAttributeAndModes(superimpProgram.get(),
808 osg::StateAttribute::ON |
809 osg::StateAttribute::OVERRIDE);
821 _geode->addDrawable(_geometries_vertices[unit_ii]);
827 std::cout <<
"[MeshLoading] Drawing normals" << std::endl;
832 _geometries_normals[unit_ii]->setVertexAttribArray(
833 0, _vertexArrays_normals[unit_ii], osg::Array::BIND_PER_VERTEX);
834 _geometries_normals[unit_ii]->setVertexAttribArray(
835 1, _colorsArrays_normals[unit_ii], osg::Array::BIND_PER_VERTEX);
836 _geometries_normals[unit_ii]
837 ->getOrCreateStateSet()
838 ->setAttributeAndModes(superimpProgram.get(),
839 osg::StateAttribute::ON |
840 osg::StateAttribute::OVERRIDE);
842 _geode->addDrawable(_geometries_normals[unit_ii]);
849 std::cout <<
"[MeshLoading] Drawing custom_vectors" << std::endl;
854 _geometries_custom_vectors[unit_ii]->setVertexAttribArray(
855 0, _vertexArrays_custom_vectors[unit_ii], osg::Array::BIND_PER_VERTEX);
856 _geometries_custom_vectors[unit_ii]->setVertexAttribArray(
857 1, _colorsArrays_custom_vectors[unit_ii], osg::Array::BIND_PER_VERTEX);
858 _geometries_custom_vectors[unit_ii]
859 ->getOrCreateStateSet()
860 ->setAttributeAndModes(superimpProgram.get(),
861 osg::StateAttribute::ON |
862 osg::StateAttribute::OVERRIDE);
864 _geode->addDrawable(_geometries_custom_vectors[unit_ii]);
870 }
while(unit_ii < _m_mm_size);
874 _geode->getOrCreateStateSet()->addUniform(
875 new osg::Uniform(
"uniUseLighting",
true));
877 _geode->getOrCreateStateSet()->addUniform(
878 new osg::Uniform(
"uniUseLighting",
false));
881 _geode->getOrCreateStateSet()->addUniform(
882 new osg::Uniform(
"uniUseSmoothShading",
false));
884 std::cout <<
"--- Setting uniforms callbacks" << std::endl;
887 osgViewer::View *view =
888 dynamic_cast< osgViewer::View *
>(
this);
890 #if(FEVV_USE_QT5) // FOR_QT6
896 view = sav->my_osgQOpenGLWindow->getOsgViewer();
900 osg::Uniform *cameraPos =
901 new osg::Uniform(osg::Uniform::FLOAT_VEC3,
"uniCameraPos");
903 _geode->getOrCreateStateSet()->addUniform(cameraPos);
913 osg::Uniform *modelMatrix =
914 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"uniModelMatrix");
916 _geode->getOrCreateStateSet()->addUniform(modelMatrix);
919 osg::Uniform *viewMatrix =
920 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"uniViewMatrix");
922 _geode->getOrCreateStateSet()->addUniform(viewMatrix);
925 osg::Uniform *invViewMatrix =
926 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"uniInvViewMatrix");
927 invViewMatrix->setUpdateCallback(
929 _geode->getOrCreateStateSet()->addUniform(invViewMatrix);
932 osg::Uniform *mvpMatrix =
933 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"uniMvpMatrix");
935 _geode->getOrCreateStateSet()->addUniform(mvpMatrix);
939 osg::Uniform *g_modelMatrix =
940 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"model");
942 _geode->getOrCreateStateSet()->addUniform(g_modelMatrix);
945 osg::Uniform *g_viewMatrix =
946 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"view");
948 _geode->getOrCreateStateSet()->addUniform(g_viewMatrix);
951 osg::Uniform *g_projectionMatrix =
952 new osg::Uniform(osg::Uniform::FLOAT_MAT4,
"projection");
954 _geode->getOrCreateStateSet()->addUniform(g_projectionMatrix);
982 _geode->getOrCreateStateSet()->addUniform(
new osg::Uniform(
983 "uniLightCount",
static_cast< unsigned int >(
lights.size())));
985 for(std::size_t lightIndex = 0; lightIndex <
lights.size(); ++lightIndex)
987 const std::string locationBase =
988 "uniLights[" + std::to_string(lightIndex) +
"].";
990 const std::string posLocation = locationBase +
"position";
991 const std::string dirLocation = locationBase +
"direction";
992 const std::string colorLocation = locationBase +
"color";
993 const std::string energyLocation = locationBase +
"energy";
995 _geode->getOrCreateStateSet()->addUniform(
new osg::Uniform(
996 posLocation.c_str(),
lights[lightIndex]->getPosition()));
997 _geode->getOrCreateStateSet()->addUniform(
new osg::Uniform(
998 dirLocation.c_str(),
lights[lightIndex]->getDirection()));
999 _geode->getOrCreateStateSet()->addUniform(
new osg::Uniform(
1000 colorLocation.c_str(),
lights[lightIndex]->getDiffuse()));
1001 _geode->getOrCreateStateSet()->addUniform(
new osg::Uniform(
1002 energyLocation.c_str(),
lights[lightIndex]->getLinearAttenuation()));
1005 osgUtil::Optimizer optimizer;
1023 if (has_face_color_map)
1025 #if OSG_MIN_VERSION_REQUIRED(3, 6, 0)
1026 optimizer.optimize(_geode, osgUtil::Optimizer::BUFFER_OBJECT_SETTINGS );
1033 #if OSG_MIN_VERSION_REQUIRED(3, 6, 0)
1034 optimizer.optimize(_geode, osgUtil::Optimizer::BUFFER_OBJECT_SETTINGS | osgUtil::Optimizer::MERGE_GEOMETRY );
1036 optimizer.optimize(_geode, osgUtil::Optimizer::MERGE_GEOMETRY);
1042 template<
typename HalfedgeGraph,
1043 typename VertexNormalMap,
1044 typename VertexColorMap,
1045 typename FaceColorMap,
1046 typename VertexUVMap,
1047 typename HalfedgeUVMap,
1048 typename FaceMaterialMap >
1053 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries,
1054 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_edges,
1055 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_vertices,
1056 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_normals,
1057 const std::vector< osg::ref_ptr< osg::Geometry > > &_geometries_custom_vectors,
1058 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays,
1059 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_edges,
1060 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_vertices,
1061 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_normals,
1062 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_vertexArrays_custom_vectors,
1063 const std::vector< osg::ref_ptr< osg::Vec3Array > > &_normalsArrays,
1064 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
1065 const std::vector< osg::ref_ptr< osg::Vec3Array > > &,
1066 const std::vector< osg::ref_ptr< osg::Vec2Array > > &_texcoordsArrays,
1067 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays,
1068 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_edges,
1069 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_vertices,
1070 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_normals,
1071 const std::vector< osg::ref_ptr< osg::Vec4Array > > &_colorsArrays_custom_vectors,
1072 std::size_t _m_mm_size,
1074 VertexNormalMap *_vt_nm,
1075 VertexColorMap *_vt_cm,
1076 FaceColorMap *_f_cm,
1077 VertexUVMap *_vt_uv_m,
1078 HalfedgeUVMap *_het_uv_m,
1079 FaceMaterialMap *_m_mm,
1080 bool has_face_color_map)
1082 std::cout <<
"[MeshLoading] Loading using legacy rendering." << std::endl;
1087 _geometries[unit_ii]->setStateSet(NULL);
1088 _geometries[unit_ii]->setColorArray(NULL);
1090 _geometries[unit_ii]->setVertexArray(_vertexArrays[unit_ii]);
1091 if(_vt_nm !=
nullptr)
1093 _geometries[unit_ii]->setNormalArray(_normalsArrays[unit_ii],
1094 osg::Array::BIND_PER_VERTEX);
1098 _geometries[unit_ii]->setNormalArray(_normalsArrays[unit_ii],
1099 osg::Array::BIND_PER_PRIMITIVE_SET);
1101 if(_vt_cm !=
nullptr)
1103 _geometries[unit_ii]->setColorArray(_colorsArrays[unit_ii],
1104 osg::Array::BIND_PER_VERTEX);
1106 else if(_f_cm !=
nullptr)
1108 _geometries[unit_ii]->setColorArray(_colorsArrays[unit_ii],
1109 osg::Array::BIND_PER_PRIMITIVE_SET);
1112 else if((_het_uv_m !=
nullptr || _vt_uv_m !=
nullptr) &&
1116 auto material =
get(*_m_mm, unit_ii);
1118 osg::ref_ptr< osg::Material > Kdmaterial =
new osg::Material;
1119 Kdmaterial->setDiffuse(osg::Material::FRONT,
1120 osg::Vec4(material.diffuse_red_component,
1121 material.diffuse_green_component,
1122 material.diffuse_blue_component,
1124 _geometries[unit_ii]->getOrCreateStateSet()->setAttribute(
1127 if(!material.diffuse_texture_filename.empty())
1134 auto &cimg = material.images[material.diffuse_texture_filename];
1146 osg::ref_ptr< osg::Texture2D > texture =
new osg::Texture2D;
1149 texture->setDataVariance(osg::Object::STATIC);
1152 texture->setImage(image);
1154 texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
1155 texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
1157 osg::Texture2D::MIN_FILTER,
1158 osg::Texture2D::LINEAR_MIPMAP_LINEAR);
1160 texture->setFilter(osg::Texture2D::MAG_FILTER,
1161 osg::Texture2D::LINEAR);
1165 _geometries[unit_ii]->getOrCreateStateSet()->setAttribute(
1174 _geometries[unit_ii]
1175 ->getOrCreateStateSet()
1176 ->setTextureAttributeAndModes(
1177 0 , texture, osg::StateAttribute::ON);
1179 _geometries[unit_ii]->setTexCoordArray(0 ,
1180 _texcoordsArrays[unit_ii]);
1184 std::cerr <<
"-> [SimpleViewer] Couldn't find texture file: "
1185 << material.diffuse_texture_filename << std::endl;
1195 osg::ref_ptr< osg::Material > Kdmaterial =
new osg::Material;
1196 Kdmaterial->setDiffuse(osg::Material::FRONT,
1198 _geometries[unit_ii]->getOrCreateStateSet()->setAttribute(
1203 osg::ref_ptr< osg::LightModel > lightModel =
new osg::LightModel;
1211 lightModel->setTwoSided(
true);
1220 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
1222 lightModel->setAmbientIntensity(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));
1228 lightModel->setLocalViewer(
false);
1230 geode->getOrCreateStateSet()->setAttributeAndModes(
1239 _geode->addDrawable(_geometries[unit_ii]);
1243 if(m_RenderSuperimposedEdges &&
1246 _geometries_edges[unit_ii]->setVertexArray(
1247 _vertexArrays_edges[unit_ii]);
1248 _geometries_edges[unit_ii]->setColorArray(_colorsArrays_edges[unit_ii],
1249 osg::Array::BIND_PER_VERTEX);
1250 _geode->addDrawable(_geometries_edges[unit_ii]);
1255 if(m_RenderSuperimposedVertices || m_RenderSuperimposedVertices_Big ||
1258 _geometries_vertices[unit_ii]->setVertexArray(
1259 _vertexArrays_vertices[unit_ii]);
1260 _geometries_vertices[unit_ii]->setColorArray(
1261 _colorsArrays_vertices[unit_ii], osg::Array::BIND_PER_VERTEX);
1262 _geode->addDrawable(_geometries_vertices[unit_ii]);
1267 if(m_Show_Vertex_Normals)
1269 _geometries_normals[unit_ii]->setVertexArray(
1270 _vertexArrays_normals[unit_ii]);
1271 _geometries_normals[unit_ii]->setColorArray(
1272 _colorsArrays_normals[unit_ii], osg::Array::BIND_PER_VERTEX);
1273 _geode->addDrawable(_geometries_normals[unit_ii]);
1279 if(m_Show_CustomVectors_Vertices)
1281 _geometries_custom_vectors[unit_ii]->setVertexArray(
1282 _vertexArrays_custom_vectors[unit_ii]);
1283 _geometries_custom_vectors[unit_ii]->setColorArray(
1284 _colorsArrays_custom_vectors[unit_ii], osg::Array::BIND_PER_VERTEX);
1285 _geode->addDrawable(_geometries_custom_vectors[unit_ii]);
1290 }
while(unit_ii < _m_mm_size);
1292 _geode->getOrCreateStateSet()->setMode(
1293 GL_NORMALIZE, osg::StateAttribute::ON);
1297 _geode->getOrCreateStateSet()->setMode(GL_LIGHTING,
1298 osg::StateAttribute::ON);
1300 _geode->getOrCreateStateSet()->setMode(GL_LIGHTING,
1301 osg::StateAttribute::OFF);
1303 if(m_SmoothFlat_Shading)
1305 osg::ref_ptr< osg::ShadeModel > shadeModel =
1306 new osg::ShadeModel(osg::ShadeModel::SMOOTH);
1307 _geode->getOrCreateStateSet()->setAttribute(shadeModel);
1311 osg::ref_ptr< osg::ShadeModel > shadeModel =
1312 new osg::ShadeModel(osg::ShadeModel::FLAT);
1313 _geode->getOrCreateStateSet()->setAttribute(shadeModel);
1316 osgUtil::Optimizer optimizer;
1318 if (has_face_color_map)
1320 #if OSG_MIN_VERSION_REQUIRED(3, 6, 0)
1321 optimizer.optimize(_geode, osgUtil::Optimizer::BUFFER_OBJECT_SETTINGS );
1328 #if OSG_MIN_VERSION_REQUIRED(3, 6, 0)
1329 optimizer.optimize(_geode, osgUtil::Optimizer::BUFFER_OBJECT_SETTINGS | osgUtil::Optimizer::MERGE_GEOMETRY );
1331 optimizer.optimize(_geode, osgUtil::Optimizer::MERGE_GEOMETRY);