[osg-users] Material Properties to Change the Normal Intensity

Rômulo Cerqueira romulogcerqueira at gmail.com
Mon Dec 26 09:39:32 PST 2016


Hi Sebastian,

this is my vertex shader:


Code:

#version 130

out vec3 pos;
out vec3 normal;
out mat3 TBN;

void main() {

pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
normal = gl_NormalMatrix * gl_Normal;

vec3 n = normalize(normal);
vec3 t = normalize(gl_NormalMatrix[0]);
vec3 b = cross(t, n);
TBN = transpose(mat3(t, b, n));

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;

}





and this is my fragment code:


Code:

#version 130

in vec3 pos;
in vec3 normal;
in mat3 TBN;

uniform float farPlane;
uniform bool drawNormal;
uniform bool drawDepth;
uniform sampler2D normalTexture;

out vec4 out_data;

void main() {

out_data = vec4(0, 0, 0, 0);

vec3 normNormal;

// Normal for textured scenes (by bump mapping)
if (textureSize(normalTexture, 0).x > 1) {
vec3 bumpedNormal = (texture2D(normalTexture, gl_TexCoord[0].st).rgb * 2.0 - 1.0) * TBN;
normNormal = normalize(bumpedNormal);
}

// Normal for untextured scenes
else {
normNormal = normalize(normal);
}

vec3 normPosition = normalize(-pos);
float linearDepth = sqrt(pos.z * pos.z + pos.x * pos.x + pos.y * pos.y);
linearDepth = linearDepth / farPlane;

if (!(linearDepth > 1)) {
if (drawNormal) {
out_data.zw = vec2(max(dot(normPosition, normNormal), 0), 1.0);
}

if (drawDepth) {
out_data.yw = vec2(linearDepth, 1.0);
}

gl_FragDepth = linearDepth;
}





and this is my code:


Code:

// define texture attributes
osg::ref_ptr<osg::StateSet> insertBumpMapTexture(osg::ref_ptr<osg::Image> diffuseImage, osg::ref_ptr<osg::Image> normalImage, osg::ref_ptr<osg::Image> specularImage) {
osg::ref_ptr<osg::Texture2D> diffuse = new osg::Texture2D();
osg::ref_ptr<osg::Texture2D> normal = new osg::Texture2D();
osg::ref_ptr<osg::Texture2D> specular = new osg::Texture2D();

diffuse->setImage(diffuseImage);
diffuse->setDataVariance(osg::Object::DYNAMIC);
diffuse->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
diffuse->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
diffuse->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
diffuse->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
diffuse->setResizeNonPowerOfTwoHint(false);
diffuse->setMaxAnisotropy(8.0f);

normal->setImage(normalImage);
normal->setDataVariance(osg::Object::DYNAMIC);
normal->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
normal->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
normal->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normal->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
normal->setResizeNonPowerOfTwoHint(false);
normal->setMaxAnisotropy(8.0f);

specular->setImage(specularImage);
specular->setDataVariance(osg::Object::DYNAMIC);
specular->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
specular->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
specular->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
specular->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
specular->setResizeNonPowerOfTwoHint(false);
specular->setMaxAnisotropy(8.0f);

osg::ref_ptr<osg::StateSet> bumpState = new osg::StateSet();
bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_DIFFUSE, diffuse, osg::StateAttribute::ON);
bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_NORMAL, normal, osg::StateAttribute::ON);
bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_SPECULAR, specular, osg::StateAttribute::ON);
return bumpState;
}





How can I pass this attribute by alpha channel and by uniform per drawable?

Cheers,
Rômulo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=69766#69766








More information about the osg-users mailing list