Sign in to follow this  
nicomantelier

pb GLSL in osg

Recommended Posts

Hello every body! first sory for my english. I am not realy good, but i will be my best. I whant to make an program using GLSL shader i work with osg and QT, and i have some problem with my rendering shader. My shader use only 1 of the object's material. And i dont know that i must do for solve the problem.

Share this post


Link to post
Share on other sites
You should describe your problem further, also a few lines of code, maybe the shadercode or a part where you think something is wrong, could be usefull to help you.

Share this post


Link to post
Share on other sites
Ok you have right i forgot
I will try to explain my probleme best I can
I make a filer for display model 3D in windows for this i use osg and QT
But some model are incorect and have inversed normal.
that why i whant use shader for make my own lightning and solves this probleme.
But when i use my shader the program use the same material for all my object.

that is my call of my shader in my program

// ------------------------------------------------- My program ---------------------------------------------------------//

bool PreviewObject3D::loadShaderSource(osg::Shader* obj, const std::string& fileName )
{
std::string fqFileName = osgDB::findDataFile(fileName);
if( fqFileName.length() == 0 )
{
// std::count << "File \"" << fileName << "\" not found." << std::endl;
return false;
}
bool success = obj->loadShaderSourceFromFile( fqFileName.c_str());
if ( !success )
{
//std::count << "Couldn't load file: " << fileName << std::endl;
return false;
}
else
{
return true;
}
}

void PreviewObject3D::applyShader(osg::Node* node)
{
osg::StateSet* pstate = node->getOrCreateStateSet();
osg::Program* m_pEclairageProgram;
osg::Shader* m_pEclairageVertObj;
osg::Shader* m_pEclairageFragObj;
std::vector< osg::ref_ptr<osg::Program> > _programList;

m_pEclairageProgram = new osg::Program;
m_pEclairageProgram->setName( "eclairage" );
m_pEclairageVertObj = new osg::Shader( osg::Shader::VERTEX );
m_pEclairageFragObj = new osg::Shader( osg::Shader::FRAGMENT );
_programList.push_back( m_pEclairageProgram );

bool ok = loadShaderSource( m_pEclairageVertObj, QFileInfo(qApp->argv()[0]).dir().filePath("data/shaders/MyVertexShader.vert").latin1())
&& loadShaderSource( m_pEclairageFragObj, QFileInfo(qApp->argv()[0]).dir().filePath("data/shaders/MyFragmentShader.frag").latin1()) ;
if(ok)
{
m_pEclairageProgram->addShader( m_pEclairageFragObj );
m_pEclairageProgram->addShader( m_pEclairageVertObj );
pstate->setAttributeAndModes(m_pEclairageProgram, osg::StateAttribute::ON);
}
}

void main()
{
[....]
osg::Node* poLoadedNode = m_pthread_MyThread->getNodeLoad().get();
applyShader(poLoadedNode);
}

//------------------------------------------ VERTEX SHADER -----------------------//
the shader but it is not the probleme for the moment
varying vec3 normal, eyeVec;
void main(void)
{

normal = gl_NormalMatrix * gl_Normal;
vec3 vVertex = vec3 ( gl_ModelViewMatrix*gl_Vertex);
eyeVec = -vVertex;

gl_Position = ftransform();
}

//-----------------------------------------FRAGMENT SHADER -------------------------------------------//
varying vec3 normal , eyeVec;
void main()
{
vec4 diffuse = vec4 (0.0);
vec4 specular = vec4 (0.0);
float att = 0.0;
vec3 norm = normalize ( normal );

vec3 light = vec3( gl_LightSource[0].position.xyz + eyeVec );
light = normalize(light);

vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

float NdotL = abs(dot ( norm , light ));


diffuse = (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * NdotL) + gl_FrontMaterial.emission;
diffuse = (diffuse + diffuse*gl_FrontMaterial.emission)/2.0;

vec3 eye = normalize (eyeVec);
vec3 refl = reflect ( -light, norm);
float spec = max( dot ( refl , eye ), 0.0 );

spec = pow( spec , gl_FrontMaterial.shininess );
specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * spec;

float dist = length ( light );
att = 1.0 / (gl_LightSource[0].constantAttenuation + (gl_LightSource[0].linearAttenuation*dist) + (gl_LightSource[0].quadraticAttenuation*dist*dist) );

gl_FragColor = ambient + ( diffuse + specular) * att;

//gl_FragColor = gl_FrontMaterial.diffuse; <- same probleme only first material apply for all object
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this