Sign in to follow this  
ankatrooper

GLSL Cube Mapping

Recommended Posts

ankatrooper    123
Can someone desribe how to implement Cube Mapping(without reflactions only for skyboxing) in GLSL?
I'm using following codes for Vertex and Fragment Shaders but getting "Validation Failed! - Different sampler types for same sample texture unit in fragment shader." error.

Vertex Shader
[code]
vec4 ePosition4;
vec3 ePosition3;
vec3 eNormal;
vec3 eye;
vec4 color;
vec4 amb = vec4(0.0);
vec4 dif = vec4(0.0);
vec4 spe = vec4(0.0);

void DirectionalLight(in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular);

void PointLight(in vec3 eye,
in vec3 ecPosition3,
in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular);

void main(void)
{
// transform vertex position into eye space
ePosition4 = gl_ModelViewMatrix * gl_Vertex;
ePosition3 = (vec3(ePosition4)) / ePosition4.w;

//transform vertex normal into eye space
eNormal = gl_NormalMatrix * gl_Normal;
eNormal = normalize(eNormal);

eye = -normalize(ePosition3);

//Compute lightning
if (gl_LightSource[0].position.w == 0.0)
{
DirectionalLight(eNormal, amb, dif, spe);
}
else if (gl_LightSource[0].position.w == 1.0)
{
PointLight(eye, ePosition3, eNormal, amb, dif, spe);
}

color = gl_FrontLightModelProduct.sceneColor + amb * gl_FrontMaterial.ambient + dif * gl_FrontMaterial.diffuse + spe * gl_FrontMaterial.specular;

//texture
gl_TexCoord[0] = gl_MultiTexCoord0;

//transform vertex
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;

//copy color
gl_FrontColor = color;
}

void DirectionalLight(in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular)
{
float nDotVP; // normal . light direction
float nDotHV; // normal . light half vector
float pf; // power factor

nDotVP = max(0.0, dot(normal,
normalize(vec3(gl_LightSource[0].position))));
nDotHV = max(0.0, dot(normal, vec3(gl_LightSource[0].halfVector)));
if (nDotVP == 0.0)
{
pf = 0.0;
}
else
{
pf = pow(nDotHV, gl_FrontMaterial.shininess);
}

ambient += gl_LightSource[0].ambient;
diffuse += gl_LightSource[0].diffuse * nDotVP;
specular += gl_LightSource[0].specular * pf;
}

void PointLight(in vec3 eye,
in vec3 ecPosition3,
in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular)
{
float nDotVP; // normal . light direction
float nDotHV; // normal . light half vector
float pf; // power factor
float attenuation; // computed attenuation factor
float d; // distance from surface to light source
vec3 VP; // direction from surface to light position
vec3 halfVector; // direction of maximum highlights

// Compute vector from surface to light position
VP = vec3(gl_LightSource[0].position) - ecPosition3;

// Compute distance between surface and light position
d = length(VP);

// Normalize the vector from surface to light position
VP = normalize(VP);

// Compute attenuation
attenuation = 1.0 / (gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation * d +
gl_LightSource[0].quadraticAttenuation * d * d);
halfVector = normalize(VP + eye);

nDotVP = max(0.0, dot(normal, VP));
nDotHV = max(0.0, dot(normal, halfVector));
if (nDotVP == 0.0)
{
pf = 0.0;
}
else
{
pf = pow(nDotHV, gl_FrontMaterial.shininess);
}

ambient += gl_LightSource[0].ambient * attenuation;
diffuse += gl_LightSource[0].diffuse * nDotVP * attenuation;
specular += gl_LightSource[0].specular * pf * attenuation;
}
[/code]

Fragment Shader
[code]
uniform sampler2D Texture;
uniform samplerCube Cubemap;
uniform int IsCubeMap;

void main(void)
{
// Apply texture and multiply by the gl_Color
if(IsCubeMap == 1)
{
gl_FragColor = vec4(textureCube(Cubemap, vec3(gl_TexCoord[0].st,0.0f),0.0f));
}
else
{
vec3 lightColor = vec3(texture2D(Texture, gl_TexCoord[0].st));
gl_FragColor = gl_Color * vec4(lightColor, 1.0);
}
}
[/code]

When i use following fragment shader i get no error but skybox is textured with the model's texture
[url="http://www.imageupload.org/?d=130955371"][img]http://www.imageupload.org/skin/stylish2/thumb/thumb_bmp.png[/img][/url]
[code]
uniform sampler2D Texture;

void main(void)
{
vec3 lightColor = vec3(texture2D(Texture, gl_TexCoord[0].st));
gl_FragColor = gl_Color * vec4(lightColor, 1.0);
}
[/code]

When using fixed-function pipeline everything is ok but when it comes to shading i can't render right or getting error. Problem is i don't know the proper shading code for rendering skybox with other models in the scene. Also everything is fine when using seperate shaders for skybox and model.

Thanks.

Share this post


Link to post
Share on other sites
procgen    108
There are f's after the numbers in the fragment shader. If you add something like #version 120 at the top of the program f's should work, not sure which exact GLSL version they added the cutting-edge support for f's :)

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