Hello everyone:I got a cube texture problem in my program base on ES2.0. The
texture is black, does anyone know what is the problem.Here is my shader code:
*Vertex shader *
varying vec3 Normal;
varying vec3 EyeDir;
uniform samplerCube cubeMap;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
Normal = gl_NormalMatrix * gl_Normal;
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
}
*Fragment shader *
varying vec3 Normal;
varying vec3 EyeDir;
uniform samplerCube cubeMap;
void main(void)
{
vec3 reflectedDirection = normalize(reflect(EyeDir, normalize(Normal)));
reflectedDirection.y = -reflectedDirection.y;
vec4 fragColor = textureCube(cubeMap, reflectedDirection);
gl_FragColor = fragColor;
}
Here is my osg code:
void CSkyBox::setSkyBoxRadius(osg::Vec3 centre, double r)
{
m_centre=centre;
m_r=r;
}
osg::TextureCubeMap* CSkyBox::readCubeMap(osg::Image* imagePosX, osg::Image*
imageNegX,
osg::Image* imagePosY, osg::Image* imageNegY,
osg::Image* imagePosZ, osg::Image* imageNegZ)
{
osg::TextureCubeMap* cubemap = NULL;
if (imagePosX && imageNegX && imagePosY && imageNegY && imagePosZ && imageNegZ)
{
cubemap = new osg::TextureCubeMap;
cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
cubemap->setFilter(osg::Texture::MIN_FILTER,
osg::Texture::LINEAR_MIPMAP_LINEAR);
cubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
}
return cubemap;
}
osg::TextureCubeMap* CSkyBox::readCubeMap()
{
std::string path=getImagesPath();
osg::Image* imagePosX = osgDB::readImageFile(path+"\\posx.jpg");
osg::Image* imageNegX = osgDB::readImageFile(path+"\\negx.jpg");
osg::Image* imagePosY = osgDB::readImageFile(path+"\\posy.jpg");
osg::Image* imageNegY = osgDB::readImageFile(path+"\\negy.jpg");
osg::Image* imagePosZ = osgDB::readImageFile(path+"\\posz.jpg");
osg::Image* imageNegZ = osgDB::readImageFile(path+"\\negz.jpg");
return readCubeMap(imagePosX, imageNegX, imagePosY, imageNegY, imagePosZ,
imageNegZ);
}
struct TexMatCallback : public osg::NodeCallback
{
public:
TexMatCallback(osg::TexMat& tm) :
_texMat(tm)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv)
{
const osg::Matrix& MV = *(cv->getModelViewMatrix());
const osg::Matrix R = osg::Matrix::rotate( osg::DegreesToRadians(112.0f),
0.0f,0.0f,1.0f)*
osg::Matrix::rotate( osg::DegreesToRadians(90.0f), 1.0f,0.0f,0.0f);
osg::Quat q = MV.getRotate();
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texMat.setMatrix( C*R );
}
traverse(node,nv);
}
osg::TexMat& _texMat;
};
class MoveEarthySkyWithEyePointTransform : public osg::Transform
{
public:
/** Get the transformation matrix which moves from local coords to world
coords.*/
virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor*
nv) const
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.preMult(osg::Matrix::translate(eyePointLocal));
}
return true;
}
/** Get the transformation matrix which moves from world coords to local
coords.*/
virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*
nv) const
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.postMult(osg::Matrix::translate(-eyePointLocal));
}
return true;
}
};
osg::Node* CSkyBox::createSkyBoxWithTextureCubeMap(osg::TextureCubeMap* skymap)
{
osg::StateSet* stateset = new osg::StateSet();
osg::TexMat *tm = new osg::TexMat;
stateset->setTextureAttribute(0, tm);
if (skymap)
{
stateset->setDataVariance(osg::Object::DYNAMIC);
osg::ref_ptr<osg::Program> skyProgram = new osg::Program;
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX,skybox_vert);
skyProgram->addShader(vertex_shader);
osg::Shader* fragment_shader = new
osg::Shader(osg::Shader::FRAGMENT,skybox_frag);
skyProgram->addShader(fragment_shader);
stateset->addUniform(new osg::Uniform("uEnvironmentMap",0));
stateset->setAttributeAndModes(skyProgram);
stateset->setTextureAttribute(0,skymap);
}
//stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON);
stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF );
// clear the depth to the far plane.
osg::Depth* depth = new osg::Depth;
depth->setFunction(osg::Depth::ALWAYS);
depth->setRange(1.0,1.0);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON );
stateset->setRenderBinDetails(-1,"RenderBin");
osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(m_centre,m_r));
drawable->setName("skyboxGeom");
osg::Geode* geode = new osg::Geode;
geode->setCullingActive(false);
geode->setStateSet( stateset );
geode->addDrawable(drawable);
osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;
transform->setCullingActive(false);
transform->addChild(geode);
return transform;
}
osg::Node* CSkyBox::createSkyBox()
{
osg::TextureCubeMap* skymap = readCubeMap();
return createSkyBoxWithTextureCubeMap(skymap);
}
Thanks for any help!
Forgy Peng
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org