Skip to content

Commit

Permalink
New XRay shader and minor fixes (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Jul 18, 2021
1 parent 8afff59 commit f45919c
Show file tree
Hide file tree
Showing 51 changed files with 376 additions and 6,501 deletions.
2 changes: 1 addition & 1 deletion Resources/shaders/Gooch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void main() {
}
vec3 v = normalize(vV);
vec3 h = normalize(l+v);
float specular = pow(dot(n,h),Specular * 80.0);
float specular = pow(max(0.0,dot(n,h)),Specular * 80.0);
vec3 Color = min(mix(cool,warm,diffuse)+specular,1.0);
if (dot(n,v) < OutlineWidth) Color=vec3(0,0,0);
color = vec4(Color, 1.0);
Expand Down
45 changes: 45 additions & 0 deletions Resources/shaders/XRay.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//pref
Ambient|float|0|0.5|1
EdgeFallOff|float|0|1|4
Intensity|float|0|0.5|4
DimBackfaces|bool|false
AOradius|float|0|0|16
XRay|set|1|1|1
Use with dark background color.|note
//vert
#version 330
layout(location = 0) in vec3 Vert;
layout(location = 3) in vec3 Norm;
layout(location = 6) in vec4 Clr;
out vec3 vClr, vN, vV;
out vec4 vP;
uniform mat4 ModelViewProjectionMatrix;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform vec3 LightPos = vec3(0.0, 20.0, 30.0);
void main() {
vN = normalize((NormalMatrix * Norm));
vP = vec4(Vert, 1.0);
gl_Position = ModelViewProjectionMatrix * vec4(Vert, 1.0);
vV = -vec3(ModelViewMatrix*vec4(Vert,1.0));
vClr = Clr.rgb;
}
//frag
#version 330
in vec4 vP;
in vec3 vClr, vN, vV;
out vec4 color;
uniform float Ambient = 0.5;
uniform float EdgeFallOff = 1.0;
uniform float Intensity = 60.0;
uniform bool DimBackfaces;
uniform vec4 ClipPlane = vec4(2.0, 0.0, 0.0, 0.0);
void main() {
if ((ClipPlane[0] < 1.5) && (dot( ClipPlane, vP) > 0.0)) discard;
float opac = dot(normalize(-vN), normalize(-vV));
opac = abs(opac);
opac = Ambient + Intensity*(1.0-pow(opac, EdgeFallOff));
float backface = 1.0 - step(0.0, vN.z);
opac = mix(opac, 0.0, backface * float(DimBackfaces)); //reverse normal if backface AND two-sided lighting
color = vec4(opac * vClr, opac);
}
2 changes: 1 addition & 1 deletion Resources/shadersOld/Gooch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main() {
}
vec3 v = normalize(vV);
vec3 h = normalize(l+v);
float specular = pow(dot(n,h),Specular * 80.0);
float specular = pow(max(0.0,dot(n,h)),Specular * 80.0);
vec4 color = vec4(min(mix(cool,warm,diffuse)+specular,1.0), 1.0);
if (dot(n,v) < OutlineWidth) color.rgb *= 0.0;
gl_FragColor = color;
Expand Down
26 changes: 26 additions & 0 deletions Resources/shadersOld/XRay.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//pref
Ambient|float|0.0|0.5|1
EdgeFallOff|float|0|1|4
Intensity|float|0|0.5|4
DimBackfaces|bool|false
AOradius|float|0|0|16
XRay|set|1|1|1
Use with dark background color.|note
//frag
#version 120
varying vec3 vN, vV;
varying vec4 vP, vClr;
uniform float Ambient = 0.5;
uniform float EdgeFallOff = 1.0;
uniform float Intensity = 60.0;
uniform bool DimBackfaces;
uniform vec4 ClipPlane = vec4(2.0, 0.0, 0.0, 0.0);
void main() {
if ((ClipPlane[0] < 1.5) && (dot( ClipPlane, vP) > 0.0)) discard;
float opac = dot(normalize(-vN), normalize(-vV));
opac = abs(opac);
opac = Ambient + Intensity*(1.0-pow(opac, EdgeFallOff));
float backface = 1.0 - step(0.0, vN.z);
opac = mix(opac, 0.0, backface * float(DimBackfaces)); //reverse normal if backface AND two-sided lighting
gl_FragColor = vec4(opac * vClr.rgb, opac);
}
12 changes: 0 additions & 12 deletions Test.txt

This file was deleted.

2 changes: 1 addition & 1 deletion define_types.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface
{$endif}

const
kVers = 'v1.0.20210716';
kVers = 'v1.0.20210717';
NaN : double = 1/0;
kTab = chr(9);
kCR = chr (13);
Expand Down
34 changes: 12 additions & 22 deletions gl_core_3d.pas
Original file line number Diff line number Diff line change
Expand Up @@ -939,35 +939,25 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
var
clr: TRGBA;
displace: float;
XRay : integer = kXRayNo;
begin
clr := asRGBA(lPrefs.ObjColor);
//glClearColor( Red(lPrefs.backColor)/255, Green(lPrefs.backColor)/255, Blue(lPrefs.backColor)/255, 1.0); //Set blue background
glClearColor(red(lPrefs.BackColor)/255, green(lPrefs.BackColor)/255, blue(lPrefs.BackColor)/255, 0); //Set background
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
if (gShader.isXRay) then begin
if (red(lPrefs.BackColor) + green(lPrefs.BackColor) + blue(lPrefs.BackColor)) > 384 then
XRay := kXRayBrightBackground
else
XRay := kXRayDarkBackground;
end;
//distance *= 0.77;
if (length(lMesh.faces) > 0) then
displace := lMesh.bilateralOffset + lPrefs.DisplaceLHRH
else
displace := lPrefs.DisplaceLHRH ;
SetModelView(w,h, isMultiSample, lPrefs, origin, displace, scale, distance, elevation, azimuth);

(* nglMatrixMode(nGL_PROJECTION);
nglLoadIdentity();
nSetOrtho(w, h, Distance, kMaxDistance, isMultiSample, lPrefs.Perspective);
nglTranslatef(lPrefs.ScreenPan.X, lPrefs.ScreenPan.Y, 0 );
nglMatrixMode (nGL_MODELVIEW);
nglLoadIdentity ();
//object size normalized to be -1...+1 in largest dimension.
//closest/furthest possible vertex is therefore -1.73..+1.73 (e.g. cube where corner is sqrt(1+1+1) from origin)
nglScalef(0.5/Scale, 0.5/Scale, 0.5/Scale);
if lPrefs.Perspective then
nglTranslatef(0,0, -Scale*2*Distance )
else
nglTranslatef(0,0, -Scale*2 );
nglRotatef(90-Elevation,-1,0,0);
nglRotatef(-Azimuth,0,0,1);
nglTranslatef(-origin.X, -origin.Y, -origin.Z); *)
if lTrack.n_count > 0 then begin
if lTrack.isTubes then
RunMeshGLSL (asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), lPrefs.ShaderForBackgroundOnly) //disable clip plane
Expand All @@ -990,16 +980,16 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
end;
if length(lNode.nodes) > 0 then begin
RunMeshGLSL (asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), lPrefs.ShaderForBackgroundOnly); //disable clip plane
lNode.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lNode.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
end;
if (length(lMesh.faces) > 0) then begin
lMesh.isVisible := isDrawMesh;
{$IFDEF LHRH}if not lMesh.isShowLH then lMesh.isVisible := false; {$ENDIF}
RunMeshGLSL (clipPlane, false);
if not isOverlayClipped then
lMesh.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay )
lMesh.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay, XRay )
else
lMesh.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lMesh.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
lMesh.isVisible := true;
end;
{$IFDEF LHRH}
Expand All @@ -1010,9 +1000,9 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
lMesh.RH.isVisible := isDrawMesh;
RunMeshGLSL (clipPlane, false);
if not isOverlayClipped then
lMesh.RH.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay )
lMesh.RH.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay, XRay )
else
lMesh.RH.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lMesh.RH.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
lMesh.RH.isVisible := true;
end;
{$ENDIF}
Expand Down
18 changes: 12 additions & 6 deletions gl_legacy_3d.pas
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
var
clr: TRGBA;
displace: float;
XRay : integer = kXRayNo;
begin
clr := asRGBA(lPrefs.ObjColor);
{$IFDEF DGL}
Expand All @@ -331,6 +332,12 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
//glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT );
glEnable(GL_NORMALIZE);
if (gShader.isXRay) then begin
if (red(lPrefs.BackColor) + green(lPrefs.BackColor) + blue(lPrefs.BackColor)) > 384 then
XRay := kXRayBrightBackground
else
XRay := kXRayDarkBackground;
end;

(* nglMatrixMode(nGL_PROJECTION);
nglLoadIdentity();
Expand Down Expand Up @@ -367,16 +374,16 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
end;
if length(lNode.nodes) > 0 then begin
RunMeshGLSL (asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), lPrefs.ShaderForBackgroundOnly); //disable clip plane
lNode.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lNode.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
end;
if (length(lMesh.faces) > 0) then begin
lMesh.isVisible := isDrawMesh;
{$IFDEF LHRH}if not lMesh.isShowLH then lMesh.isVisible := false; {$ENDIF}
RunMeshGLSL (ClipPlane, false);
if not isOverlayClipped then
lMesh.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W),isFlipMeshOverlay )
lMesh.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W),isFlipMeshOverlay, XRay )
else
lMesh.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lMesh.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
lMesh.isVisible := true;
end;
{$IFDEF LHRH}
Expand All @@ -386,13 +393,12 @@ procedure DrawScene(w,h: integer; isFlipMeshOverlay, isOverlayClipped, isDrawMes
lMesh.RH.isVisible := isDrawMesh;
RunMeshGLSL (clipPlane, false);
if not isOverlayClipped then
lMesh.RH.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay )
lMesh.RH.DrawGL(clr, asPt4f(2,ClipPlane.Y,ClipPlane.Z,ClipPlane.W), isFlipMeshOverlay, XRay )
else
lMesh.RH.DrawGL(clr, clipPlane, isFlipMeshOverlay);
lMesh.RH.DrawGL(clr, clipPlane, isFlipMeshOverlay, XRay);
lMesh.RH.isVisible := true;
end;
{$ENDIF}

end; //DrawScene

{$IFDEF LEGACY_INDEXING}
Expand Down
8 changes: 4 additions & 4 deletions mainunit.lfm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object GLForm1: TGLForm1
Left = 654
Left = 483
Height = 659
Top = 25
Width = 1004
Expand Down Expand Up @@ -1830,16 +1830,16 @@ object GLForm1: TGLForm1
Caption = '-'
end
object PrevOverlayMenu: TMenuItem
Caption = 'Previous volume'
Caption = 'Previous (earlier) volume'
Enabled = False
ShortCut = 32848
ShortCut = 16464
OnClick = PrevOverlayMenuClick
end
object NextOverlayMenu: TMenuItem
Tag = 1
Caption = 'Next volume'
Enabled = False
ShortCut = 32846
ShortCut = 16460
OnClick = PrevOverlayMenuClick
end
end
Expand Down
18 changes: 6 additions & 12 deletions mainunit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ function PyMESHLOADBILATERAL(Self, Args : PPyObject): PPyObject; cdecl;
GLForm1.ScriptOutputMemo.lines.Add('meshloadbilateral() function has changed: now a filename is expected.');
end else
MESHLOAD(StrName);
gPrefs.LoadBilateralLHRHx:= true;
gPrefs.LoadBilateralLHRHx:= false;
Result:= GetPythonEngine.PyBool_FromLong(Ord(True));
end;
end;
Expand Down Expand Up @@ -2064,17 +2064,11 @@ function PyATLASSTATMAP(Self, Args : PPyObject; isHide: boolean): PPyObject; cde
exit;
end;
ob := GetPythonEngine.PyTuple_GetItem(Args,0);
{$IFDEF PY27} //see notes in opts.inc
atlasname := GetPythonEngine.PyString_AsDelphiString(ob);
{$ELSE}
//atlasname := GetPythonEngine.PyString_AsDelphiString(ob);
atlasname := GetPythonEngine.PyUnicode_AsUTF8(ob);
{$ENDIF}
ob := GetPythonEngine.PyTuple_GetItem(Args,1);
{$IFDEF PY27} //see notes in opts.inc
statname := GetPythonEngine.PyString_AsDelphiString(ob);
{$ELSE}
//statname := GetPythonEngine.PyString_AsDelphiString(ob);
statname := GetPythonEngine.PyUnicode_AsUTF8(ob);
{$ENDIF}
//get indices
ob := GetPythonEngine.PyTuple_GetItem(Args,2);
n := GetPythonEngine.PyTuple_Size(ob);
Expand Down Expand Up @@ -6383,14 +6377,14 @@ procedure TGLForm1.DisplayMenuClick(Sender: TObject);
if (ScriptMemo.SelLength < 1) then
ScriptMemo.SelectAll();
ScriptMemo.CopyToClipboard;
exit;
//exit;
end;
case (Sender as TMenuItem).tag of
0: gAzimuth := 270; //left
1: gAzimuth := 90; //right
3: gAzimuth := 180;//anterior
4: gAzimuth := 180;//inferior
else gAzimuth := 0; //posterior, inferior, superior
else gAzimuth := 0; //posterior (2), superior (5)
end;
case (Sender as TMenuItem).tag of
4: gElevation := -90; //inferior
Expand Down Expand Up @@ -6581,7 +6575,7 @@ procedure TGLForm1.OpenBilateralMenuClick(Sender: TObject);
begin
gPrefs.LoadBilateralLHRHx:= true;
OpenMenuClick(sender);
gPrefs.LoadBilateralLHRHx:= false;
gPrefs.LoadBilateralLHRHx:= false;
end;

procedure TGLForm1.OverlayBoxCreate;
Expand Down
Loading

0 comments on commit f45919c

Please sign in to comment.