coin3d Shaders in FreeCAD

Need help, or want to share a macro? Post here!
User avatar
furti
Posts: 277
Joined: Mon Nov 27, 2017 5:27 pm

coin3d Shaders in FreeCAD

Postby furti » Mon Jun 17, 2019 6:05 pm

I played around a bit with shaders in coin3d and wanted to get some example shaders to work.

I tried to recreated the shaders in the openinventor documentation https://developer90.openinventor.com/co ... ing-shader
Then I tried to Import the sample iv file that is mentioned here: https://grey.colorado.edu/coin3d/classS ... ogram.html

but it seems the shader is simply ignored. Even when I give the Vertex and Fragement Shaders an invalid file path or ad some random stuff to the shader files, no error is shown.
yorik wrote:
Sun Jun 09, 2019 4:58 pm
ping
wmayer wrote:
Fri Aug 10, 2018 1:52 pm
ping
Has one of you already managed to get shaders to work in FreeCAD? Is there some special setting needed to get them to work?

Code: Select all

print(vertexShader.isSupported(coin.SoShaderObject.GLSL_PROGRAM))
gives me "1" which I think means GLSL is supported on my machine.
wmayer
Site Admin
Posts: 14451
Joined: Thu Feb 19, 2009 10:32 am

Re: coin3d Shaders in FreeCAD

Postby wmayer » Mon Jun 17, 2019 7:15 pm

The only way I got shaders working so far is with this example: https://coin3d.bitbucket.io/Coin/coin_shaders.html
If the .glsl files are not in the current working directory you can also set absolute path names in the .iv file. When you load the .iv file into FreeCAD it may happen that everything is black. In this case you must switch off backlight coloring in the preferences.

If you create another .iv file without the shader stuff and load both files into FreeCAD then there is a noticeable difference.
User avatar
Chris_G
Posts: 1058
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: coin3d Shaders in FreeCAD

Postby Chris_G » Tue Jun 18, 2019 12:30 pm

I also gave a try at this.
I was hoping I could add Ambient Occlusion to the 3D view.
That was beyond my abilities :cry:
But I had a basic thing working.
glsl.jpeg
glsl.jpeg (9.15 KiB) Viewed 306 times
User avatar
yorik
Site Admin
Posts: 11397
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: coin3d Shaders in FreeCAD

Postby yorik » Wed Jun 19, 2019 6:49 pm

It might be useful to try this in a separate viewer outside of FreeCAD (might help ruling out possible problems due to the stuff that FreeCAD automatically adds to the scene). This creates a simple viewer:

Code: Select all

# Initialize Coin. This returns a main window to use
from pivy import sogui
win = sogui.SoGui.init()
if win == None:
    print("Unable to create a SoGui window")
    return
win.setBackgroundColor(coin.SbColor(1.0,1.0,1.0))

# build a quick default scene
mat = coin.SoMaterial()
mat.diffuseColor = (1.0, 0.0, 0.0)
# Make a scene containing a red cone
scene = coin.SoSeparator()
scene.addChild(mat)
scene.addChild(coin.SoCone())

# ref the scene so it doesn't get garbage-collected while rendering
scene.ref()

# Create a viewer in which to see our scene graph
viewer = sogui.SoGuiExaminerViewer(win)

# Put our scene into viewer, change the title
viewer.setSceneGraph(scene)
viewer.setTitle("Coin viewer")
viewer.show()

sogui.SoGui.show(win) # Display main window
sogui.SoGui.mainLoop()     # Main Coin event loop
You might need to adapt a bit to read the scene from an .iv file, but something like this should do it:

Code: Select all

f = open("/some/file.iv","r")
buf = f.read()
f.close()
inp = coin.SoInput()
inp.setBuffer(buf)
scene = coin.SoDB.readAll(inp)
User avatar
furti
Posts: 277
Joined: Mon Nov 27, 2017 5:27 pm

Re: coin3d Shaders in FreeCAD

Postby furti » Thu Jun 20, 2019 3:56 am

Thanks for all your answers. :)

Will give it a try when i have some time. First i wantbto add bump/normal mapping support to the texture workbench :D
RickDeckard
Posts: 43
Joined: Tue Jan 16, 2018 12:10 pm

Re: coin3d Shaders in FreeCAD

Postby RickDeckard » Sat Jun 22, 2019 8:52 pm

I also did some experiments with shaders in FreeCAD some time ago. My original goal was to implement MatCaps and Ambient Occlusion, but never had the time to achieve anything substantial.

This might be useful to you:

Vertex shader (vshader.glsl):

Code: Select all

varying vec4 eposition;
varying vec3 normal;
varying vec3 diffuseColor;

void main()
{
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	eposition = gl_ModelViewMatrix * gl_Vertex;
	normal = gl_NormalMatrix * gl_Normal;
}
Fragment shader (fshader.glsl):

Code: Select all

varying vec4 eposition;
varying vec3 normal;
varying vec3 diffuseColor;

void main(void)
{
	const vec3 lightColor = vec3(1, 1, 1);
	//vec3 eye = -normalize(eposition);
	float nDotVP = max(0.0, dot(normalize(normal), normalize(lightColor)));
	gl_FragColor.xyz = vec3(1,1,1) *  nDotVP;
	gl_FragColor.w = 1.0;
}
Python Code:

Code: Select all

from pivy.coin import *

shaderSearchDir = "/path_to_freecad_projects/FreeCAD_Shading_Experiments/"

def createShader():
	vertexShader = SoVertexShader()
	vertexShader.sourceProgram.setValue(shaderSearchDir + 'vshader.glsl')

	fragmentShader = SoFragmentShader()
	fragmentShader.sourceProgram.setValue(shaderSearchDir + 'fshader.glsl')

	shaderProgram = SoShaderProgram()
	shaderProgram.shaderObject.set1Value(0, vertexShader);
	shaderProgram.shaderObject.set1Value(1, fragmentShader);

	return shaderProgram

def createDirLight():
	light = SoDirectionalLight()
	light.direction.setValue(SbVec3f(0, 0, -1))
	light.color.setValue(SbColor(0, 0, 1))
	return light

def createSphere():
	s = SoSphere()
	return s

def printSceneGraph(root, level):
	indent = ' ' * level
	print('{0} name:"{1}" type:{2} id:{3} : {4}'.format(indent, root.getName(), root.getNodeType(), root.getNodeId(), type(root)))
	if isinstance(root, SoGroup):
		num = root.getNumChildren()
		for i in range(num):
			c = root.getChild(i)
			printSceneGraph(c, level + 1)

def addCustomShadedSphere(root, shader):
	sep1 = SoSeparator()
	
	ssep = SoSeparator()
	sph = createSphere()
	ssep.addChild(sph)

	sep1.addChild(shader)
	sep1.addChild(ssep)

	root.addChild(sep1)

	printSceneGraph(sep1, 0)

view = Gui.ActiveDocument.ActiveView
root = view.getSceneGraph()
#dir(graphRoot)
printSceneGraph(root, 0)

shader = createShader()
addCustomShadedSphere(root, shader)
# root.addChild(shader)

#light = createDirLight()
#graphRoot.insertChild(shader, 0)
#graphRoot.insertChild(light, 0)
User avatar
saso
Posts: 1279
Joined: Fri May 16, 2014 1:14 pm
Contact:

Re: coin3d Shaders in FreeCAD

Postby saso » Sat Jun 22, 2019 9:08 pm

Chris_G wrote:
Tue Jun 18, 2019 12:30 pm
I also gave a try at this.
I was hoping I could add Ambient Occlusion to the 3D view.
That was beyond my abilities :cry:
But I had a basic thing working.
glsl.jpeg
could this be used for draft analysis ?
http://catiadoc.free.fr/online/cfyugfss ... ut0306.htm
https://www.youtube.com/watch?v=rx508dWvr8M