WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #1  
Old 05-08-2009, 12:01 PM
shivanangel shivanangel is offline
Member
 
Join Date: Feb 2006
Location: New Jersey
Posts: 180
Multiple Textures for Diffuse and Specularity Shader Issue

Hello again,

I am having a small issue with the shader I had posted earlier (Phong lighting shader).

In my shader I am moving between two textures through the UV coordinates with a value that ranges between zero to one.

I am also using two additional textures to control the specularity across the surface of the two textures.

The blending and the phong shader work just fine. The issue I am having is that the specular highlight does not seem to be respecting the specular textures I load.

If I only use 2 textures and assign the first to the 0 layer and the second to the 1 layer and use those to control the specular highlight everything is fine. But as soon as I try to use more than two textures everything seems to blow up and all I get is a model with the standard Phong specular highlight running along the length.

Any help would be greatly appreciated.

Vizard Script

Code:
import viz

from Shaders import Phong_TexBlend

viz.sensitivity(3,0.5)

viz.go()

#Get a handle to the main headlight and disable it
headLight = viz.MainView.getHeadLight()
headLight.disable()

viz.MainView.setPosition(-3.4, 1.8, -104.5)

#Load Models
Bottom = viz.add("Models\\Bottom.obj")
Top = viz.add("Models\\Top.obj")
Clamp = viz.add("Models\\Clamp.obj")

#Load Textures
TexMap_1 = viz.addTexture("Textures\\BrushedSteel_TexMap.bmp")
TexMap_2 = viz.addTexture("Textures\\IceCrystals_TexMap.bmp")
SpecMap_1 = viz.addTexture("Textures\\BrushedSteel_SpecMap.bmp")
SpecMap_2 = viz.addTexture("Textures\\IceCrystals_SpecMap.bmp")

#Associate Textures with Models
Top.texture(TexMap_1, '' ,0)
Top.texture(TexMap_2, '' ,1)
Top.texture(SpecMap_1, '' ,2)
Top.texture(SpecMap_2, '' ,3)

#Create Shaders
TopShader = Phong_TexBlend()

#Apply Shaders to Geometry
TopShader.ApplyShader(Top)
Phong Texture Blend Class

Code:
#Shader - Phong
#Created by: George Lecakes
#May 08, 2009

import viz

class Phong_TexBlend:
	
	def __init__(self):
		
		#Variables to manipulate Shader
		self.lightposition = [-60, -16, 20]
		self.ambient = [0.0,0.0,0.0,1.0]
		self.diffuse = [0.7882,0.7970,0.7862,1.0]
		self.specular = [0.814,0.814,0.814,1.0]
		self.specularpower = 4.96
		self.frostline = 0.5
		
		#Load the Vertex and Fragment Shaders, which should always accomany this file and be labeled .vp and .fp
		self.shader = viz.addShader(flag = 0, vert = "Vertex_Fragments\\Shader_Phong.vp", frag = "Vertex_Fragments\\Shader_Phong.fp")

		#Add Uniform Variables from Vertex Shader
		self.LightPosition = viz.addUniformFloat( 'fvLightPosition', self.lightposition )
		self.EyePosition =  viz.addUniformFloat( 'fvEyePosition', [0.0,0.0,0.0] )
		
		#AddUniform Variables from Fragment Shader
		self.Ambient =  viz.addUniformFloat( 'fvAmbient', self.ambient )
		self.Specular =  viz.addUniformFloat( 'fvSpecular', self.specular )
		self.Diffuse =  viz.addUniformFloat( 'fvDiffuse', self.diffuse )
		self.SpecularPower =  viz.addUniformFloat( 'fSpecularPower', self.specularpower )

		self.TexMap_1 = viz.addUniformBool( 'TexMap_1', 0 )
		self.TexMap_2 = viz.addUniformBool( 'TexMap_2', 1 )
		self.SpecMap_1 = viz.addUniformBool( 'SpecMap_1', 2 )
		self.SpecMap_2 = viz.addUniformBool( 'SpecMap_2', 3 )

		self.FrostLine = viz.addUniformFloat( 'FrostLine', self.frostline)
		
		#Attach the uniform variables to the shader
		
		self.shader.attach(self.Ambient)
		self.shader.attach(self.Specular)
		self.shader.attach(self.Diffuse)
		self.shader.attach(self.SpecularPower)
		self.shader.attach(self.TexMap_1)
		self.shader.attach(self.TexMap_2)
		self.shader.attach(self.SpecMap_1)
		self.shader.attach(self.SpecMap_2)
		self.shader.attach(self.FrostLine)
		
		
	def ApplyShader(self, node):
		node.apply(self.shader)
		
	def SetFrostLine(self, fl):
		self.FrostLine.set(fl)
Vertex Shader

Code:
uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
   
void main( void )
{
   gl_Position = ftransform();
   Texcoord    = gl_MultiTexCoord0.xy;
    
   vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
   
   ViewDirection  = fvEyePosition - fvObjectPosition.xyz;
   LightDirection = fvLightPosition - fvObjectPosition.xyz;
   Normal         = gl_NormalMatrix * gl_Normal;
   
}
Fragment Shader

Code:
uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform float fSpecularPower;

uniform sampler2D TexMap_1;
uniform sampler2D TexMap_2;
uniform sampler2D SpecMap_1;
uniform sampler2D SpecMap_2;

uniform float FrostLine;

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

void main( void )
{
   vec3  fvLightDirection = normalize( LightDirection );
   vec3  fvNormal         = normalize( Normal );
   float fNDotL           = dot( fvNormal, fvLightDirection ); 
   
   vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection ); 
   vec3  fvViewDirection  = normalize( ViewDirection );
   float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) );
   

       
       
   //Separate into two areas, that above frost line (Use First set)
   if (Texcoord.y > FrostLine) 
   {
   vec4  fvBaseColor      = texture2D( TexMap_1, Texcoord );
   
   vec4  fvTotalAmbient   = fvAmbient * fvBaseColor; 
   vec4  fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor; 
   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) ) * texture2D(SpecMap_1, Texcoord);
  
   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );
   
   }
   
   
   //Area below frost line (Use second set of textures)
   else
   {
   vec4  fvBaseColor      = texture2D( TexMap_2, Texcoord );
   
   vec4  fvTotalAmbient   = fvAmbient * fvBaseColor; 
   vec4  fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor; 
   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) ) * texture2D(SpecMap_2, Texcoord);
  
   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );
   
   }
       
}
Once again, I am probably making some horribly novice mistake and I apologize if that is the case again.

~George
Reply With Quote
  #2  
Old 05-11-2009, 10:44 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
You are using a boolean uniform to specify the texture uniforms, you should be using an integer uniform, addUniformInt.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:39 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC