<div dir="ltr"><div><div>HI Florian,<br><br></div>I can't any obvious mistakes, but the use of the SmoothingVisitor looks redudent to me as you are providing your own normals. Try comment it out.<br><br></div><div>Robert.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 3 May 2016 at 21:13, Florian GOLESTIN <span dir="ltr"><<a href="mailto:florian.golestin@gmail.com" target="_blank">florian.golestin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Robert,<br>
<br>
Sorry my mistake, I wanted to put only the interesting part but this is not efficient.<br>
No more censure!<br>
<br>
<br>
Code:<br>
<br>
namespace Soleil<br>
{<br>
<br>
LevelReader::LevelReader()<br>
{<br>
//supportsExtension("level", "Text map format for Donjon");<br>
}<br>
<br>
LevelReader::~LevelReader()<br>
{<br>
}<br>
<br>
<br>
osg::ref_ptr<Level> LevelReader::readFile(const std::string &file) const<br>
{<br>
std::string fileName = osgDB::findDataFile(file);<br>
if (fileName.empty()) return nullptr; // TODO throw e?<br>
<br>
errno = 0;<br>
std::cout << "Stream: " << fileName.c_str() << "\n";<br>
std::ifstream stream;<br>
stream.exceptions(std::ifstream::failbit); // | std::ifstream::badbit<br>
stream.open(fileName.c_str(), std::ios::in);<br>
if (!stream) return nullptr; // TODO throw e<br>
<br>
return readStream(stream);<br>
}<br>
<br>
osg::ref_ptr<Level> LevelReader::readStream(std::istream &stream) const<br>
{<br>
char block; // current reading block<br>
float x = 0; // Current position on x<br>
float y = 0; // Current position on y<br>
int wallCount = 0; // Number of cubes<br>
float maxX = 0; // Max size of the map in X<br>
<br>
<br>
osg::ref_ptr<Soleil::Level> level = new Soleil::Level();<br>
std::string line;<br>
<span class=""> while(std::getline(stream, line))<br>
{<br>
std::stringstream linestream(line);<br>
<br>
while (linestream >> block)<br>
{<br>
</span> /* TODO Configurable size for the blocks */<br>
<span class=""> float posx = 1.0 * x;<br>
float posy = 1.0 * y;<br>
float endx = posx + 1.0;<br>
float endy = posy + 1.0;<br>
<br>
</span> bool blockFound = false;<br>
switch (block)<br>
{<br>
case '.':<br>
break;<br>
case 'D':<br>
level->_startingPosition = osg::Vec3(posx + .5, posy + .5, .3);<br>
blockFound = true;<br>
break;<br>
case 'd':<br>
level->_startingOrientation = osg::Vec3(posx + .5, posy + .5, .3);<br>
blockFound = true;<br>
break;<br>
<span class=""> case 'x':<br>
if (wallCount % 2)<br>
</span> {<br>
std::cout << "1" << "\n";<br>
<span class=""> createCube(level.get(), *level->texcoords, *level->texcoords2, posx, posy, endx, endy);<br>
}<br>
else<br>
</span> {<br>
std::cout << "2" << "\n";<br>
<span class=""> createCube(level.get(), *level->texcoords2, *level->texcoords, posx, posy, endx, endy);<br>
}<br>
</span> blockFound = true;<br>
wallCount++;<br>
break;<br>
// default:<br>
// blockFound = levelConfiguration(block);<br>
// break;<br>
}<br>
if (blockFound == false)<br>
std::cout << "Unexpected character: " << block << std::endl;<br>
<br>
x += 1.0;<br>
maxX = std::max(x, maxX);<br>
}<br>
x = 0;<br>
y -= 1.0; // TODO Cube size<br>
}<br>
// TODO read failed?<br>
<br>
// Floor -------------<br>
level->vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));<br>
level->vertices->push_back(osg::Vec3(maxX, 0.0f, 0.0f));<br>
level->vertices->push_back(osg::Vec3(maxX, y, 0.0f));<br>
level->vertices->push_back(osg::Vec3(0, y, 0.0f));<br>
<span class=""><br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
<br>
</span> level->texcoords->push_back( osg::Vec2(0.0f, 0.0f) ); // 0<br>
level->texcoords->push_back( osg::Vec2(0.0f, 1.0f) ); // 1<br>
level->texcoords->push_back( osg::Vec2(1.0f, 1.0f) ); // 2<br>
level->texcoords->push_back( osg::Vec2(1.0f, 0.0f) ); // 3<br>
<span class=""><br>
<br>
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;<br>
geom->setVertexArray(level->vertices);<br>
geom->setNormalArray(level->normals, osg::Array::Binding::BIND_PER_VERTEX);<br>
</span> geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 24*wallCount+4)); // +4 for the floor texture coordinates<br>
osgUtil::SmoothingVisitor::smooth(*geom);<br>
//<br>
<br>
//<br>
osg::ref_ptr<osg::Geode> geode = new osg::Geode;<br>
geode->addDrawable(geom);<br>
<br>
<br>
/* I added this in the hope to have the second texture appearing */<br>
// osg::Material* material = new osg::Material;<br>
// material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);<br>
// material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));<br>
// material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));<br>
// material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));<br>
// material->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);<br>
// level->getOrCreateStateSet()->setAttribute(material,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);<br>
<br>
<br>
<br>
level->addChild(geode);<br>
<br>
<br>
// Texture 1<br>
<span class=""> geom->setTexCoordArray(0, level->texcoords.get());<br>
{<br>
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;<br>
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("media/textures/stone_3_2048x2048.jpg");<br>
texture->setImage(image);<br>
texture->setUnRefImageDataAfterApply(true);<br>
level->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture);<br>
}<br>
//Texture 2<br>
geom->setTexCoordArray(1, level->texcoords2.get());<br>
{<br>
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;<br>
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("media/textures/Metal_seamless2_ch16.jpg");<br>
texture->setImage(image);<br>
texture->setUnRefImageDataAfterApply(true);<br>
level->getOrCreateStateSet()->setTextureAttributeAndModes(1, texture);<br>
}<br>
<br>
</span> std::cout << "done.\n";<br>
return level;<br>
}<br>
<br>
/*<br>
* @param Level Is the container of the vertices, normals and textures for the walls of our labyrinth<br>
* @param tex Is the Vector that should receive the coordinate for the textures<br>
* @param skip Is the second vector for texture filled with '0' to 'skip' this wall<br>
<div><div class="h5"> */<br>
void LevelReader::createCube(Soleil::Level *level, osg::Vec2Array &tex, osg::Vec2Array &skip,<br>
float posx, float posy, float endx, float endy) const<br>
{<br>
// ------ Front<br>
level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));<br>
<br>
level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
<br>
// ------ top<br>
level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));<br>
<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
<br>
// ------ back<br>
level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));<br>
<br>
level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
<br>
// ------ Bottom<br>
level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));<br>
<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
<br>
// ------ Left<br>
level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));<br>
<br>
level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
<br>
// ------ Right<br>
level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));<br>
level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));<br>
level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));<br>
<br>
level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));<br>
level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));<br>
<br>
tex.push_back( osg::Vec2(0.0f, 0.0f));<br>
tex.push_back( osg::Vec2(0.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 1.0f));<br>
tex.push_back( osg::Vec2(1.0f, 0.0f));<br>
//<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
skip.push_back( osg::Vec2(0.0f, 0.0f));<br>
<br>
}<br>
}<br>
<br>
<br>
<br>
<br>
<br>
</div></div>Here is the original file: <a href="https://github.com/fulezi/soleil/blob/master/LevelReader.cpp" rel="noreferrer" target="_blank">https://github.com/fulezi/soleil/blob/master/LevelReader.cpp</a><br>
<br>
<br>
An idea?<br>
<span class=""><br>
Florian<br>
<br>
------------------<br>
Read this topic online here:<br>
</span><a href="http://forum.openscenegraph.org/viewtopic.php?p=67046#67046" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=67046#67046</a><br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</div></div></blockquote></div><br></div>