PDA

View Full Version : Specular Shader Issue


shivanangel
05-06-2009, 08:23 AM
Hello,

I was wondering if I could get a help with a Specular Shader problem.

I made a shader in the program AMD RenderMonkey, which if you are not familiar with is a 'sandbox' for making shaders and testing them out.

Everything works fine in RenderMonkey, but I run into the issue of nothing
rendering when I move into Vizard.

My Vertex Shader is:


//Uniform

uniform vec3 LightPosition;
uniform vec3 EyePosition;
//Varying

varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
varying vec2 Texcoord;
//Attributes

void main(void)
{
gl_Position = ftransform();

Texcoord = gl_MultiTexCoord0.xy;

vec4 objectPosition = gl_ModelViewMatrix * gl_Vertex;

LightDirection = (gl_ModelViewMatrix * vec4(LightPosition, 1)).xyz - objectPosition.xyz;
ViewDirection = EyePosition - objectPosition.xyz;
Normal = gl_NormalMatrix * gl_Normal;
}



My Fragment Shader:


uniform vec4 Diffuse;
uniform vec4 Ambient;
uniform vec4 Specular;
uniform vec3 LightPosition;
uniform vec3 EyePosition;
uniform float SpecularPower;

uniform sampler2D TexMap;
uniform sampler2D SpecMap;

varying vec3 LightDirection;
varying vec3 ViewDirection;
varying vec3 Normal;

varying vec2 Texcoord;

void main(void)
{
vec3 nLightDirection = normalize(LightDirection);
vec3 nNormal = normalize(Normal);

float NDotL = dot(nNormal, nLightDirection);

vec3 Reflection = normalize(((2.0 * nNormal) * NDotL) - nLightDirection);
vec3 nViewDirection = normalize(ViewDirection);
float RDotV = max(0.0,dot(Reflection, nViewDirection));


vec4 diffColor = texture2D(TexMap,Texcoord);
vec4 specColor = texture2D(SpecMap, Texcoord);

vec4 DiffuseColor = diffColor * Diffuse * NDotL;
vec4 AmbientColor = Ambient * diffColor;
vec4 SpecularColor = (Specular * (pow(RDotV, SpecularPower))) * specColor;

gl_FragColor = DiffuseColor + AmbientColor + SpecularColor;
}



And my Vizard code for bringing in the shader and attaching it to the model:


import viz

viz.go()

shader = viz.addShader(flag = 0, vert = "SpecMap.vp", frag = "SpecMap.fp")

viz.MainView.eyeheight(0.0)

#Load model
trex = viz.add("TREX//TRex.CFG")

trex2 = viz.add('sphere.obj')
trex2.setPosition(15,0,0)
#Load Textures
TexMap = viz.addTexture('TRexTex_10_TexMap.bmp')
SpecMap = viz.addTexture('TRexTex_10_SpecMap.bmp')

#Attach textures
trex.texture(TexMap, '', 0)
trex.texture(SpecMap, '', 1)

trex2.texture(TexMap, '', 0)
trex2.texture(SpecMap, '', 1)

#Vertex Uniforms
EyePosition = viz.addUniformFloat('EyePosition', [0.0,0.0,0.0])
LightPosition = viz.addUniformFloat('fvLightPosition', [100,200,100])

#Fragment Uniforms
Diffuse = viz.addUniformFloat('Diffuse',[1.0,1.0,1.0,1.0])
Ambient = viz.addUniformFloat('Ambient',[1.0,1.0,1.0,1.0])
Specular = viz.addUniformFloat('Specular', [1.0,1.0,1.0,1.0])
SpecularPower = viz.addUniformFloat('SpecularPower', 3)

Texture = viz.addUniformInt( 'TexMap', 0 )
SpecularMap = viz.addUniformInt( 'SpecMap', 1 )

shader.attach(Texture)
shader.attach(SpecularMap)

trex.apply(shader)
trex2.apply(shader)

farshizzo
05-06-2009, 10:11 AM
In your Vizard code, you are only attaching the texture uniforms to the shader. You need to attach all the other uniforms as well (LightPosition, EyePosition, Diffuse, etc...).

shivanangel
05-06-2009, 11:08 AM
Thank you for the keen eye.

Will you be including shader help anytime soon in your documentation?
I've had quite a time finding information across this forum and
peeking through other people's code.

Thanks again,
George