View Single Post
  #4  
Old 08-03-2016, 11:20 AM
Vaquero Vaquero is offline
Member
 
Join Date: Nov 2015
Posts: 62
The effect I'm trying to achieve is a modern approach to energy conserving shading, but there's an extra thread for that (http://forum.worldviz.com/showthread.php?t=5535). Here, I'd like to get information on how to use the custom effects and shaders in general, because it's unclear to me what's supported and what is not.

I tried using the addShader function instead of the effects framework, but this doesn't seem to work in Vizard as I would expect it to, whereas the exact same code produces visible results in ShaderMaker (http://cgvr.cs.uni-bremen.de/teaching/shader_maker/), though the shader code must contain some errors in the calculations of _normalDir, _viewDir or light0Dir.

Here's the Vizard script:
Code:
import viz
import vizfx
import vizcam

viz.setMultiSample(8)
viz.fov(60)
viz.go()

# Setup camera navigation
cam = vizcam.PivotNavigate(distance=8.0, center=(0,1,0))

# Setup lighting
viz.MainView.getHeadLight().remove()
light = vizfx.addPointLight(color=viz.WHITE, pos=(4,4,-4))
vizfx.setAmbientColor(viz.BLACK) # disable ambient light

# Setup environment
vizfx.addChild('sky_day.osgb').renderToBackground()

# Setup models
logo1 = vizfx.addChild('logo.osgb')

vertCode = """
varying vec3 _viewDir;
varying vec3 _normalDir;
varying vec3 light0Dir;

void main()
{
	vec4 eyeVertex = gl_Vertex;
	vec3 eyeNormal = gl_Normal;

	eyeVertex = gl_ModelViewMatrix * eyeVertex;
	eyeNormal = normalize(gl_NormalMatrix * eyeNormal);
	vec4 eyeViewDir = vec4(eyeVertex.xyz,0.0);
	_viewDir = eyeViewDir.xyz;
	_normalDir = eyeNormal;
	vec3 dir = gl_LightSource[0].position.xyz - eyeVertex.xyz;
	light0Dir = dir;

	gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""

# PBR Calculations
fragCode = """
uniform float roughness;

varying vec3 _viewDir;
varying vec3 _normalDir;
varying vec3 light0Dir;

const float PI = 3.14159265358979323846;

float sqr(float x) { return x*x; }

float GGX(float alpha, float cosThetaM)
{
    float CosSquared = cosThetaM*cosThetaM;
    float TanSquared = (1.0-CosSquared)/CosSquared;
    return (1.0/PI) * sqr(alpha/(CosSquared * (alpha*alpha + TanSquared)));
}

vec3 BRDF( vec3 L, vec3 V, vec3 N)
{
    vec3 H = normalize( L + V );
    float D = GGX(roughness, dot(N,H));
    return vec3(D);
}

void main()
{
	vec3 color = BRDF(light0Dir, _viewDir, _normalDir);
	gl_FragColor.rgb = color;
}
"""
pbrShader = viz.addShader(vert=vertCode, frag=fragCode)
logo1.apply(pbrShader)

roughnessUniform = viz.addUniformFloat('roughness',0.6)
logo1.apply(roughnessUniform)
The model doesn't show up at all!


I saw that in the composer.vizfx file matrices like gl_ModelViewProjectionMatrix are used, that's why I used them here and because I have no idea how to get the matrices from vizard. But these matrices are deprecated since GLSL version 4.1. So which version is Vizard using?

Are varying variables between vertex and fragment shader supported?
Reply With Quote