Hi all,
The problem is something like this when rotating the scene (using trackball)
the partial cylinder i created appears and disappears randomly.
I think the problem might be with the stateset also.
...
Thank you!
Cheers,
nikhil
Code:
PositionAttitudeTransform* Objects::makeObject(float length,float height,const
float angle,Vec3 Position,std::string texname)
{
const float k=8;
float i=0;
Vec3Array *vertexArray = new Vec3Array();
DrawElementsUInt *faceArray = new DrawElementsUInt(PrimitiveSet::QUADS,
0);
vertexArray->push_back(Vec3(0,0,0));
vertexArray->push_back(Vec3(0,0,height));
while(i < angle)
{
vertexArray->push_back(Vec3(length*cos(DegreesToRadians(i)),
length*sin(DegreesToRadians(i)), 0));
vertexArray->push_back(Vec3(length*cos(DegreesToRadians(i)),
length*sin(DegreesToRadians(i)), height));
faceArray->push_back((i/k));
faceArray->push_back((i/k)+1);
faceArray->push_back((i/k)-1);
faceArray->push_back((i/k)-2);
i=i+(2*k);
}
Geometry *geometry = new Geometry();
geometry->setVertexArray(vertexArray);
geometry->addPrimitiveSet(faceArray);
Geode *objectGeode = new Geode();
objectGeode->addDrawable(geometry);
objectGeode->setCullingActive(false);
objectGeode->setStateSet(setState(texname,1,0));
PositionAttitudeTransform *objectTransform = new
PositionAttitudeTransform();
objectTransform->addChild(objectGeode);
objectTransform->setPosition(Position);
return objectTransform;
}
complete code
Code:
#include "objects.h"
using namespace osg;
Objects::Objects(){
clearMaterial();
x_pivot=0;
y_pivot=0;
z_pivot=0;
shaderVertFile="gouraud.vert";
shaderFragFile="gouraud.frag";
FogColour=Vec4d(.5,0.5,.85,0.45);
}
void Objects::SetPivot(float x,float y,float z){
x_pivot=x;
y_pivot=y;
z_pivot=z;
}
bool Objects::loadShaderSource(osg::Shader* obj, const std::string& fileName )
{
std::string fqFileName = osgDB::findDataFile(fileName);
if( fqFileName.length() == 0 )
{
std::cout << "File \"" << fileName << "\" not found." <<
std::endl;
return false;
}
bool success = obj->loadShaderSourceFromFile( fqFileName.c_str());
if ( !success )
{
std::cout << "Couldn't load file: " << fileName << std::endl;
return false;
}
else
{
return true;
}
}
Texture2D *Objects::createTexture(std::string texName, bool texRepeat)
{
// load texture file
Image *image = osgDB::readImageFile(texName);
if (!image) {
throw "Couldn't load texture.";
}
// create texture
Texture2D *texture = new Texture2D;
texture->setDataVariance(Object::DYNAMIC);
texture->setFilter(Texture::MIN_FILTER, Texture::LINEAR_MIPMAP_LINEAR);
texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR);
// handle repeat
if (texRepeat) {
texture->setWrap(Texture::WRAP_S, Texture::REPEAT);
texture->setWrap(Texture::WRAP_T, Texture::REPEAT);
} else {
texture->setWrap(Texture::WRAP_S, Texture::CLAMP);
texture->setWrap(Texture::WRAP_T, Texture::CLAMP);
}
texture->setImage(image);
return texture;
}
Program* Objects::shadeUsingFiles()
{
Program *programObject = new Program();
Shader *vertexObject = new Shader(Shader::VERTEX);
Shader *fragmentObject = new Shader(Shader::FRAGMENT);
programObject->addShader(fragmentObject);
programObject->addShader(vertexObject);
loadShaderSource(vertexObject,shaderVertFile);
loadShaderSource(fragmentObject,shaderFragFile);
return programObject;
}
StateSet *Objects::setState(std::string texname,bool transparent,bool shade)
{
TexEnv* texenv = new TexEnv;
StateSet* ss=new StateSet;
if(transparent)
{
osg::Fog* fog = new osg::Fog();
fog->setMode(osg::Fog::LINEAR);
fog->setDensity(0.1f);
fog->setColor(FogColour);
fog->setStart(3.0f);
fog->setEnd(40.0f);
texenv->setMode(TexEnv::BLEND);
ss->ref();
ss->setMode(GL_BLEND, StateAttribute::ON);
ss->setMode(GL_FOG, StateAttribute::ON);
ss->setAttribute(fog,osg::StateAttribute::ON);
ss->setRenderingHint(StateSet::TRANSPARENT_BIN);
ss->setRenderBinDetails(9, "DepthSortedBin");
}
else
{
texenv->setMode(TexEnv::DECAL);
}
if(shade)
{
ss->setAttributeAndModes(shadeUsingFiles(), StateAttribute::ON);
}
else{
if (texname.length() > 0) {
try
{
ss->setTextureAttributeAndModes(0,
createTexture(texname, 0), StateAttribute::ON);
}
catch (char *e)
{
throw e;
}
}
ss->setTextureAttribute(0,texenv);
}
//ss->setAttributeAndModes(programObject, StateAttribute::ON);
return ss;
}
PositionAttitudeTransform* Objects::makeObject(float length,float height,const
float angle,Vec3 Position,std::string texname)
{
const float k=8;
float i=0;
Vec3Array *vertexArray = new Vec3Array();
DrawElementsUInt *faceArray = new DrawElementsUInt(PrimitiveSet::QUADS,
0);
vertexArray->push_back(Vec3(0,0,0));
vertexArray->push_back(Vec3(0,0,height));
while(i < angle)
{
vertexArray->push_back(Vec3(length*cos(DegreesToRadians(i)),
length*sin(DegreesToRadians(i)), 0));
vertexArray->push_back(Vec3(length*cos(DegreesToRadians(i)),
length*sin(DegreesToRadians(i)), height));
faceArray->push_back((i/k));
faceArray->push_back((i/k)+1);
faceArray->push_back((i/k)-1);
faceArray->push_back((i/k)-2);
i=i+(2*k);
}
Geometry *geometry = new Geometry();
geometry->setVertexArray(vertexArray);
geometry->addPrimitiveSet(faceArray);
Geode *objectGeode = new Geode();
objectGeode->addDrawable(geometry);
objectGeode->setCullingActive(false);
objectGeode->setStateSet(setState(texname,1,0));
PositionAttitudeTransform *objectTransform = new
PositionAttitudeTransform();
objectTransform->addChild(objectGeode);
objectTransform->setPosition(Position);
return objectTransform;
}
PositionAttitudeTransform* Objects::makeObject(float length,float width,float
height,Vec3 Position,std::string texname,bool transparent,bool shade)
{
float l=length/2;
float h=height/2;
float w=width/2;
//vertices (hinged at bottom center of thickness face)
Vec3Array *vertexArray = new Vec3Array();
// bottom front left
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, -h+z_pivot));
// bottom front right
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, -h+z_pivot));
// bottom back right
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, -h+z_pivot));
// bottom back left
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, -h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, -h+z_pivot));
// top front left
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, h+z_pivot));
// top front right
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, h+z_pivot));
// top back right
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, h+z_pivot));
// top back left
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, h+z_pivot));
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, h+z_pivot));
//faces
DrawElementsUInt *faceArray = new
DrawElementsUInt(PrimitiveSet::TRIANGLES, 0);
// bottom
faceArray->push_back(0); // face 1
faceArray->push_back(9);
faceArray->push_back(3);
faceArray->push_back(9); // face 2
faceArray->push_back(6);
faceArray->push_back(3);
// top
faceArray->push_back(21); //face 3
faceArray->push_back(12);
faceArray->push_back(18);
faceArray->push_back(12); //face 4
faceArray->push_back(15);
faceArray->push_back(18);
// left
faceArray->push_back(22); //face 5
faceArray->push_back(10);
faceArray->push_back(13);
faceArray->push_back(10); //face 6
faceArray->push_back(1);
faceArray->push_back(13);
// right
faceArray->push_back(16); //face 7
faceArray->push_back(4);
faceArray->push_back(19);
faceArray->push_back(4); //face 8
faceArray->push_back(7);
faceArray->push_back(19);
// front
faceArray->push_back(14); //face 9
faceArray->push_back(2);
faceArray->push_back(17);
faceArray->push_back(2); //face 10
faceArray->push_back(5);
faceArray->push_back(17);
// back
faceArray->push_back(20); //face 11
faceArray->push_back(8);
faceArray->push_back(23);
faceArray->push_back(8); //face 12
faceArray->push_back(11);
faceArray->push_back(23);
// normal array
// normal array
Vec3Array *normalArray = new Vec3Array();
normalArray->push_back(Vec3(+1, 0, 0));
normalArray->push_back(Vec3(-1, 0, 0));
normalArray->push_back(Vec3(0, +1, 0));
normalArray->push_back(Vec3(0, -1, 0));
normalArray->push_back(Vec3(0, 0, +1));
normalArray->push_back(Vec3(0, 0, -1));
// normal index
TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4>
*normalIndexArray;
normalIndexArray = new TemplateIndexArray<unsigned int,
Array::UIntArrayType, 24, 4>();
// bottom front left
normalIndexArray->push_back(5);
normalIndexArray->push_back(3);
normalIndexArray->push_back(0);
// bottom front right
normalIndexArray->push_back(5);
normalIndexArray->push_back(2);
normalIndexArray->push_back(0);
// bottom back right
normalIndexArray->push_back(5);
normalIndexArray->push_back(2);
normalIndexArray->push_back(1);
// bottom back left
normalIndexArray->push_back(5);
normalIndexArray->push_back(3);
normalIndexArray->push_back(1);
// top front left
normalIndexArray->push_back(4);
normalIndexArray->push_back(3);
normalIndexArray->push_back(0);
// top front right
normalIndexArray->push_back(4);
normalIndexArray->push_back(2);
normalIndexArray->push_back(0);
// top back right
normalIndexArray->push_back(4);
normalIndexArray->push_back(2);
normalIndexArray->push_back(1);
// top back left
normalIndexArray->push_back(4);
normalIndexArray->push_back(3);
normalIndexArray->push_back(1);
//end of specifications::enough to enable lighting
//texture scaling on the wall to be done and
Vec2Array* texcoords = new osg::Vec2Array(24);
(*texcoords)[0].set(0.10f,0.10f);
(*texcoords)[1].set(0.10f,0.10f);
(*texcoords)[2].set(0.10f,0.10f);
(*texcoords)[3].set(0.25f,0.10f);
(*texcoords)[4].set(0.25f,0.10f);
(*texcoords)[5].set(0.25f,0.10f);
(*texcoords)[6].set(0.50f,0.10f);
(*texcoords)[7].set(0.50f,0.10f);
(*texcoords)[8].set(0.50f,0.10f);
(*texcoords)[9].set(.75f,0.10f);
(*texcoords)[10].set(.75f,0.10f);
(*texcoords)[11].set(.75f,0.10f);
(*texcoords)[12].set(0.10f,.90f);
(*texcoords)[13].set(0.10f,.90f);
(*texcoords)[14].set(0.10f,.90f);
(*texcoords)[15].set(0.25f,.90f);
(*texcoords)[16].set(0.25f,.90f);
(*texcoords)[17].set(0.25f,.90f);
(*texcoords)[18].set(0.50f,.90f);
(*texcoords)[19].set(0.50f,.90f);
(*texcoords)[20].set(0.50f,.90f);
(*texcoords)[21].set(0.75f,.90f);
(*texcoords)[22].set(0.75f,.90f);
(*texcoords)[23].set(0.75f,.90f);
//end of texcords
Geometry *geometry = new Geometry();
geometry->setVertexArray(vertexArray);
geometry->setNormalArray(normalArray);
geometry->setNormalIndices(normalIndexArray);
geometry->setNormalBinding(Geometry::BIND_PER_VERTEX);
geometry->setTexCoordArray(0,texcoords);
geometry->addPrimitiveSet(faceArray);
Geode *objectGeode = new Geode();
objectGeode->addDrawable(geometry);
objectGeode->setStateSet(setState(texname,transparent,shade));
PositionAttitudeTransform *objectTransform = new
PositionAttitudeTransform();
objectTransform->addChild(objectGeode);
objectTransform->setPosition(Position);
return objectTransform;
}
PositionAttitudeTransform* Objects::makeObject(float length, float width,Vec3
Position,std::string texname ,bool transparent,bool shade)
{
float l=length/2;
float w=width/2;
//vertices (hinged at bottom center of thickness face)
Vec3Array *vertexArray = new Vec3Array();
// bottom front left
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, 0));
vertexArray->push_back(Vec3(-l+x_pivot, -w+y_pivot, 0));
// bottom front right
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot, 0));
vertexArray->push_back(Vec3(l+x_pivot, -w+y_pivot,0));
// bottom back right
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, 0));
vertexArray->push_back(Vec3(l+x_pivot, w+y_pivot, 0));
// bottom back left
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, 0));
vertexArray->push_back(Vec3(-l+x_pivot, w+y_pivot, 0));
//faces
DrawElementsUInt *faceArray = new
DrawElementsUInt(PrimitiveSet::TRIANGLES, 0);
// bottom
faceArray->push_back(0); // face 1
faceArray->push_back(2);
faceArray->push_back(4);
faceArray->push_back(4); // face 2
faceArray->push_back(6);
faceArray->push_back(0);
// left
faceArray->push_back(1); //face 5
faceArray->push_back(3);
faceArray->push_back(5);
faceArray->push_back(5); //face 6
faceArray->push_back(7);
faceArray->push_back(1);
// normal array
Vec3Array *normalArray = new Vec3Array();
normalArray->push_back(Vec3(0, 0, +1));
normalArray->push_back(Vec3(0, 0, -1));
// normal index
TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4>
*normalIndexArray;
normalIndexArray = new TemplateIndexArray<unsigned int,
Array::UIntArrayType, 24, 4>();
// bottom front left
normalIndexArray->push_back(0);
normalIndexArray->push_back(1);
// bottom front right
normalIndexArray->push_back(0);
normalIndexArray->push_back(1);
// bottom back right
normalIndexArray->push_back(0);
normalIndexArray->push_back(1);
// bottom back left
normalIndexArray->push_back(0);
normalIndexArray->push_back(1);
//end of specifications::enough to enable lighting
//texture scaling on the wall to be done and
Vec2Array* texcoords = new osg::Vec2Array(24);
(*texcoords)[0].set(0.0f,0.0f);
(*texcoords)[1].set(0.0f,0.0f);
(*texcoords)[2].set(1.0f,0.0f);
(*texcoords)[3].set(1.0f,0.0f);
(*texcoords)[4].set(1.0f,1.0f);
(*texcoords)[5].set(1.0f,1.0f);
(*texcoords)[6].set(0.0f,1.0f);
(*texcoords)[7].set(0.0f,1.0f);
//end of texcords
Geometry *geometry = new Geometry();
geometry->setVertexArray(vertexArray);
geometry->setNormalArray(normalArray);
geometry->setNormalIndices(normalIndexArray);
geometry->setNormalBinding(Geometry::BIND_PER_VERTEX);
geometry->setTexCoordArray(0,texcoords);
geometry->addPrimitiveSet(faceArray);
Geode *objectGeode = new Geode();
objectGeode->addDrawable(geometry);
objectGeode->setStateSet(setState(texname,transparent,shade));
PositionAttitudeTransform *objectTransform = new
PositionAttitudeTransform();
objectTransform->addChild(objectGeode);
objectTransform->setPosition(Position);
return objectTransform;
}
void Objects::clearMaterial()
{
material = new Material();
}
void Objects::prepareMaterial(Material *givenMaterial)
{
material = givenMaterial;
}
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=28574#28574
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org