// Copyright (c) 2018 GeometryFactory Sarl (France). // All rights reserved. // // This file is part of CGAL (www.cgal.org). // // $URL$ // $Id$ // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial // // // Author(s) : Guillaume Damiand #ifndef CGAL_BASIC_SHADERS_H #define CGAL_BASIC_SHADERS_H #include namespace CGAL { //------------------------------------------------------------------------------ const char VERTEX_SOURCE_COLOR[]=R"DELIM( #version 150 in highp vec3 a_Pos; in highp vec3 a_Normal; in mediump vec3 a_Color; out highp vec4 vs_fP; // view space position out highp vec4 ls_fP; // local space position out highp vec3 fN; out mediump vec4 fColor; uniform highp mat4 u_Mvp; uniform highp mat4 u_Mv; uniform mediump float u_PointSize; uniform mediump vec3 u_DefaultColor; uniform bool u_UseDefaultColor; void main(void) { fColor = vec4(a_Color, 1.0); if (u_UseDefaultColor) { fColor = vec4(u_DefaultColor, 1.0); } vec4 pos = vec4(a_Pos, 1.0); ls_fP = pos; vs_fP = u_Mv * pos; fN = mat3(u_Mv)* a_Normal; gl_Position = u_Mvp * pos; gl_PointSize = u_PointSize; } )DELIM"; const char FRAGMENT_SOURCE_COLOR[]=R"DELIM( #version 150 in highp vec4 vs_fP; in highp vec4 ls_fP; in highp vec3 fN; in mediump vec4 fColor; out mediump vec4 out_color; uniform highp vec4 u_LightPos; uniform mediump vec4 u_LightDiff; uniform mediump vec4 u_LightSpec; uniform mediump vec4 u_LightAmb; uniform mediump float u_SpecPower; uniform highp vec4 u_ClipPlane; uniform highp vec4 u_PointPlane; uniform mediump float u_RenderingMode; uniform mediump float u_RenderingTransparency; void main(void) { highp vec3 L = u_LightPos.xyz - vs_fP.xyz; highp vec3 V = -vs_fP.xyz; highp vec3 a_Normal = normalize(fN); L = normalize(L); V = normalize(V); highp vec3 R = reflect(-L, a_Normal); highp vec4 diffuse = vec4(max(dot(a_Normal,L), 0.0) * u_LightDiff.rgb * fColor.rgb, 1.0); highp vec4 ambient = vec4(u_LightAmb.rgb * fColor.rgb, 1.0); highp vec4 specular = pow(max(dot(R,V), 0.0), u_SpecPower) * u_LightSpec; // onPlane == 1: inside clipping plane, should be solid; // onPlane == -1: outside clipping plane, should be transparent; // onPlane == 0: on clipping plane, whatever; float onPlane = sign(dot((ls_fP.xyz-u_PointPlane.xyz), u_ClipPlane.xyz)); // rendering_mode == -1: draw all solid; // rendering_mode == 0: draw solid only; // rendering_mode == 1: draw transparent only; if (u_RenderingMode == (onPlane+1)/2) { // discard other than the corresponding half when rendering discard; } // draw corresponding part out_color = u_RenderingMode < 1 ? (diffuse + ambient) : vec4(diffuse.rgb + ambient.rgb, u_RenderingTransparency); } )DELIM"; const char VERTEX_SOURCE_P_L[]=R"DELIM( #version 150 in highp vec3 a_Pos; in mediump vec3 a_Color; out mediump vec4 fColor; out highp vec4 ls_fP; // local space uniform highp mat4 u_Mvp; uniform mediump float u_PointSize; uniform bool u_IsOrthographic; uniform mediump vec3 u_DefaultColor; uniform bool u_UseDefaultColor; bool EqualZero(float value) { return abs(value) < 0.00001; } void main(void) { fColor = vec4(a_Color, 1.0); if (u_UseDefaultColor) { fColor = vec4(u_DefaultColor, 1.0); } vec4 pos = vec4(a_Pos, 1.0); ls_fP = pos; gl_Position = u_Mvp * pos; float distance = gl_Position.w; if (u_IsOrthographic) { distance = u_PointSize; } float effectiveDistance = EqualZero(distance) ? 0.00001 : distance; gl_PointSize = u_PointSize / effectiveDistance * 5.0; } )DELIM"; const char VERTEX_SOURCE_SHAPE[]=R"DELIM( #version 150 in highp vec3 a_Pos; in mediump vec3 a_Color; out mediump vec4 gColor; out highp vec4 ls_fP; uniform highp mat4 u_Mvp; uniform mediump vec3 u_DefaultColor; uniform bool u_UseDefaultColor; void main(void) { gColor = vec4(a_Color, 1.0); if (u_UseDefaultColor) { gColor = vec4(u_DefaultColor, 1.0); } gl_Position = vec4(a_Pos, 1.0); } )DELIM"; const char GEOMETRY_SOURCE_SPHERE[]=R"DELIM( #version 150 layout(points) in; layout(triangle_strip, max_vertices = 72) out; // max_vertices = (resolution+1) * 2 * latResolution #define PI 3.14159265358979323846 in mediump vec4 gColor[]; out mediump vec4 fColor; out highp vec4 ls_fP; uniform highp mat4 u_Mvp; uniform mediump float u_Radius; void drawSphere(in vec4 center, in float radius, in float resolution) { float latResolution = resolution*0.5; float stepTheta = PI/latResolution; float stepPhi = 2*PI/resolution; for(int i=0; i