Search in sources :

Example 1 with RenderingMode

use of maspack.render.GL.GLProgramInfo.RenderingMode in project artisynth_core by artisynth.

the class GL3ProgramManager method getSelectionProgram.

public GLShaderProgram getSelectionProgram(GL3 gl, GLProgramInfo info) {
    // basic flat program
    GLProgramInfo select = new GLProgramInfo();
    // use rounded points by default
    select.setSelecting(true);
    RenderingMode mode = info.getMode();
    if (mode == RenderingMode.POINTS) {
        select.setRoundPointsEnabled(info.hasRoundPoints());
    }
    select.setMode(mode);
    select.setNumLights(0);
    select.setNumClipPlanes(numClipPlanes);
    // disable everything else
    select.setVertexColorsEnabled(false);
    select.setVertexNormalsEnabled(false);
    select.setVertexTexturesEnabled(false);
    select.setInstanceColorsEnabled(false);
    select.setLineColorsEnabled(false);
    select.setShading(Shading.NONE);
    return getProgram(gl, select);
}
Also used : RenderingMode(maspack.render.GL.GLProgramInfo.RenderingMode) GLProgramInfo(maspack.render.GL.GLProgramInfo)

Example 2 with RenderingMode

use of maspack.render.GL.GLProgramInfo.RenderingMode in project artisynth_core by artisynth.

the class GLSLGenerator method addFragmentInfo.

private static void addFragmentInfo(StringBuilder hb, GLProgramInfo info) {
    appendln(hb, "// fragment color output");
    appendln(hb, "out vec4 fragment_color;");
    appendln(hb);
    if (info.isSelecting()) {
        appendln(hb, "uniform vec4 selection_color;  // fragment color for selection");
        appendln(hb);
    }
    RenderingMode instanced = info.getMode();
    boolean hasColors = !info.isSelecting() && (info.getVertexColorMixing() != ColorMixing.NONE) && (info.hasVertexColors() || ((instanced == RenderingMode.INSTANCED_POINTS || instanced == RenderingMode.INSTANCED_FRAMES || instanced == RenderingMode.INSTANCED_AFFINES) && info.hasInstanceColors()) || (instanced == RenderingMode.INSTANCED_LINES && info.hasLineColors()));
    boolean hasTextures = hasTextures(info);
    if (hasColors) {
        appendln(hb, "// fragment colors from previous shader");
        appendln(hb, "in ColorData {");
        if (info.getColorInterpolation() == ColorInterpolation.NONE) {
            appendln(hb, "   flat vec4 diffuse;");
        } else {
            appendln(hb, "   vec4 diffuse;");
        }
        appendln(hb, "} colorIn;");
        appendln(hb);
    }
    if (hasTextures) {
        appendln(hb, "// texture info");
        appendln(hb, "in TextureData {");
        appendln(hb, "   vec2 texcoord;");
        appendln(hb, "} textureIn;");
        if (info.hasColorMap()) {
            appendln(hb, "uniform sampler2D color_map;");
        }
        // only allow normal/bump mapping in per fragment lighting
        if (hasFragmentLighting(info.getShading())) {
            if (info.hasNormalMap()) {
                appendln(hb, "uniform sampler2D normal_map;");
                appendln(hb, "uniform float normal_scale;");
            }
            if (info.hasBumpMap()) {
                appendln(hb, "uniform sampler2D bump_map;");
                appendln(hb, "uniform float bump_scale;");
            }
        }
        appendln(hb);
    }
}
Also used : RenderingMode(maspack.render.GL.GLProgramInfo.RenderingMode)

Example 3 with RenderingMode

use of maspack.render.GL.GLProgramInfo.RenderingMode in project artisynth_core by artisynth.

the class GLSLGenerator method buildVertexShaderMain.

private static void buildVertexShaderMain(StringBuilder mb, GLProgramInfo info) {
    appendln(mb, "// main vertex shader");
    appendln(mb, "void main() {");
    appendln(mb);
    appendln(mb, "   vec3 position;  // transformed vertex position");
    if (info.hasVertexNormals() && info.getShading() != Shading.NONE) {
        appendln(mb, "   vec3 normal;  // transformed vertex normal");
    }
    appendln(mb);
    RenderingMode mode = info.getMode();
    boolean computeNormals = !info.isSelecting() && info.hasVertexNormals() && info.getShading() != Shading.NONE;
    // transform vertex using instance info
    switch(mode) {
        case INSTANCED_AFFINES:
            appendln(mb, "   // instance vertex, affine transform");
            appendln(mb, "   position = (instance_affine_matrix *  vec4(instance_scale * vertex_position, 1.0) ).xyz;");
            if (computeNormals) {
                appendln(mb, "   normal = (instance_normal_matrix *  vec4(vertex_normal, 0.0) ).xyz;");
            }
            appendln(mb);
            break;
        case INSTANCED_FRAMES:
            appendln(mb, "   // instance vertex, scale-rotate-translate");
            appendln(mb, "   position = qrot(instance_orientation, (instance_scale * vertex_position)) + instance_position;");
            if (computeNormals) {
                appendln(mb, "   normal = qrot(instance_orientation, vertex_normal);");
            }
            appendln(mb);
            break;
        case INSTANCED_LINES:
            appendln(mb, "   // instance vertex, scale radially, rotate/translate");
            appendln(mb, "   vec3  u = line_top_position-line_bottom_position;");
            appendln(mb, "   float line_length = length(u);  // target length");
            appendln(mb, "   u = u/line_length;              // target direction vector");
            if (info.hasLineScaleOffset()) {
                appendln(mb, "   float line_rad = line_radius*mix(line_bottom_scale_offset.r, line_top_scale_offset.r, vertex_position.z);  // adjust radius");
                appendln(mb, "   float line_offset = mix(line_bottom_scale_offset.y, line_length+line_bottom_scale_offset.z, line_bottom_scale_offset.w);");
                appendln(mb, "   float line_top = mix(line_top_scale_offset.y, line_length+line_top_scale_offset.z, line_top_scale_offset.w);");
                appendln(mb, "   line_length = line_top-line_offset;");
            } else {
                appendln(mb, "   float line_rad = line_radius;  // radius length");
            }
            appendln(mb);
            appendln(mb, "   // transform position");
            appendln(mb, "   position = vec3(line_rad*vertex_position.xy, line_length*vertex_position.z);");
            appendln(mb, "   position = line_bottom_position + zrot(u, position);");
            if (info.hasLineScaleOffset()) {
                appendln(mb, "   position = position + line_offset*u;");
            }
            if (computeNormals) {
                appendln(mb, "   // transform normal");
                appendln(mb, "   normal = vec3( vertex_normal.xy*line_length, vertex_normal.z*line_radius);");
                appendln(mb, "   normal = zrot(u, normal);");
                if (info.hasLineScaleOffset()) {
                    appendln(mb, "   float rdiff = line_bottom_scale_offset.r-line_top_scale_offset.r;");
                    appendln(mb, "   float cost = 1.0/sqrt(rdiff*rdiff+1);");
                    appendln(mb, "   float sint = rdiff*cost;");
                    appendln(mb, "   vec3 trot = vec3(vertex_position.y, -vertex_position.x, 0);");
                    appendln(mb, "   normal = rodrigues(normal, trot, sint, cost);");
                }
            }
            appendln(mb);
            break;
        case INSTANCED_POINTS:
            appendln(mb, "   // instance vertex, scale-translate");
            appendln(mb, "   position = instance_scale * vertex_position + instance_position;");
            if (!info.isSelecting() && info.hasVertexNormals() && info.getShading() != Shading.NONE) {
                appendln(mb, "   normal = vertex_normal;");
            }
            break;
        case DEFAULT:
        case POINTS:
            appendln(mb, "   position = vertex_position;");
            if (computeNormals) {
                appendln(mb, "   normal = vertex_normal;");
            }
            break;
    }
    // at this point, position and normal should be correct, compute output position
    appendln(mb, "   // vertex output");
    appendln(mb, "   gl_Position = pvm_matrix * vec4(position, 1.0);");
    appendln(mb);
    // vertex colors
    ColorInterpolation cinterp = info.getColorInterpolation();
    if (!info.isSelecting() && info.getVertexColorMixing() != ColorMixing.NONE) {
        switch(mode) {
            case INSTANCED_POINTS:
            case INSTANCED_FRAMES:
            case INSTANCED_AFFINES:
                if (info.hasInstanceColors()) {
                    if (cinterp == ColorInterpolation.HSV) {
                        appendln(mb, "   colorOut.diffuse = rgba2hsva(instance_color);");
                    } else {
                        appendln(mb, "   colorOut.diffuse = instance_color;");
                    }
                    appendln(mb);
                } else if (info.hasVertexColors()) {
                    if (cinterp == ColorInterpolation.HSV) {
                        appendln(mb, "   colorOut.diffuse = rgba2hsva(vertex_color);");
                    } else {
                        appendln(mb, "   colorOut.diffuse = vertex_color;");
                    }
                    appendln(mb);
                }
                break;
            case INSTANCED_LINES:
                if (info.hasLineColors()) {
                    appendln(mb, "   // interpolate color based on line");
                    appendln(mb, "   float cz = vertex_position.z;");
                    if (info.hasLineScaleOffset()) {
                        appendln(mb, "   // interpolate as fraction of bottom-to-top");
                        appendln(mb, "   cz = (cz*line_length+line_offset)/(line_length+line_offset);");
                    }
                    if (cinterp == ColorInterpolation.HSV) {
                        appendln(mb, "   colorOut.diffuse = mix(rgba2hsva(line_bottom_color), rgba2hsva(line_top_color), cz);");
                    } else {
                        appendln(mb, "   colorOut.diffuse = mix(line_bottom_color, line_top_color, cz);");
                    }
                    appendln(mb);
                } else if (info.hasVertexColors()) {
                    if (cinterp == ColorInterpolation.HSV) {
                        appendln(mb, "   colorOut.diffuse = rgba2hsva(vertex_color);");
                    } else {
                        appendln(mb, "   colorOut.diffuse = vertex_color;");
                    }
                    appendln(mb);
                }
                break;
            case DEFAULT:
            case POINTS:
                if (info.hasVertexColors()) {
                    if (cinterp == ColorInterpolation.HSV) {
                        appendln(mb, "   colorOut.diffuse = rgba2hsva(vertex_color);");
                    } else {
                        appendln(mb, "   colorOut.diffuse = vertex_color;");
                    }
                    appendln(mb);
                }
                break;
        }
    }
    // do lighting computations
    if (!info.isSelecting()) {
        switch(info.getShading()) {
            case FLAT:
                // case GOURAUD:
                if (info.numLights() > 0) {
                    appendln(mb, "   // per-vertex lighting computations");
                    appendln(mb, "   // compute camera position/normal");
                    appendln(mb, "   vec4 camera_position = vm_matrix * vec4(position, 1.0);");
                    if (info.hasVertexNormals()) {
                        appendln(mb, "   vec4 camera_normal = normal_matrix * vec4(normal, 0.0);");
                    } else {
                        appendln(mb, "   vec4 camera_normal = -camera_position;  // assume pointed at camera");
                    }
                    appendln(mb, "   vec3 nfront = normalize(camera_normal.xyz);");
                    appendln(mb, "   vec3 nback = -nfront;");
                    appendln(mb, "   vec3 eye = normalize(-camera_position.xyz);");
                    appendln(mb, "   ");
                    appendln(mb, "   // accumulated light colors");
                    appendln(mb, "   vec3 fldiff = vec3(0.0);  // front");
                    appendln(mb, "   vec3 flambi = vec3(0.0);");
                    appendln(mb, "   vec3 flspec = vec3(0.0);");
                    appendln(mb, "   vec3 bldiff = vec3(0.0);  // back");
                    appendln(mb, "   vec3 blambi = vec3(0.0);");
                    appendln(mb, "   vec3 blspec = vec3(0.0);");
                    appendln(mb, "   ");
                    appendln(mb, "   // lights");
                    appendln(mb, "   for (int i=0; i<" + info.numLights() + "; ++i) {");
                    appendln(mb, "      vec3  light_to_vertex = camera_position.xyz-light[i].position.xyz;");
                    appendln(mb, "      float lightdist = length(light_to_vertex);");
                    appendln(mb, "      light_to_vertex = light_to_vertex/lightdist;");
                    appendln(mb, "      vec3 light_direction = normalize(light[i].direction.xyz);");
                    appendln(mb, "      ");
                    appendln(mb, "      float spotatt = 1.0;  // spot attentuation initially zero if non-spotlight");
                    appendln(mb, "      float coslimit = light[i].direction.w;");
                    appendln(mb, "      if (coslimit > 0) {");
                    appendln(mb, "         // check angle");
                    appendln(mb, "         float coslight = dot(light_direction, light_to_vertex);");
                    appendln(mb, "         if (coslight < coslimit) {");
                    appendln(mb, "            spotatt = 0;");
                    appendln(mb, "         } else {");
                    appendln(mb, "            spotatt = pow(coslight, light[i].attenuation.w);");
                    appendln(mb, "         }");
                    appendln(mb, "      }");
                    appendln(mb, "      ");
                    appendln(mb, "      // distance attenuation doesn't affect directional lights");
                    appendln(mb, "      float att = mix(1.0, 1.0 / (light[i].attenuation.x + light[i].attenuation.y*lightdist +");
                    appendln(mb, "         light[i].attenuation.z*lightdist*lightdist), light[i].position.w);");
                    appendln(mb, "      att *= spotatt;  // combine into a single attenuation parameter");
                    appendln(mb, "      ");
                    appendln(mb, "      // determine direction either from point or direction using direction indicator");
                    appendln(mb, "      light_direction = mix(light_direction, light_to_vertex, light[i].position.w);");
                    appendln(mb, "      vec2 ds = blinnPhongCoeffs( nfront, -light_direction, eye, front_material.specular.a);");
                    appendln(mb, "      flambi += intensity_scale*light[i].ambient.rgb;");
                    appendln(mb, "      fldiff += intensity_scale*att*ds.x*light[i].diffuse.rgb;");
                    appendln(mb, "      flspec += intensity_scale*att*ds.y*light[i].specular.rgb;");
                    appendln(mb);
                    appendln(mb, "      ds = blinnPhongCoeffs( nback, -light_direction, eye, back_material.specular.a);");
                    appendln(mb, "      blambi += intensity_scale*light[i].ambient.rgb;");
                    appendln(mb, "      bldiff += intensity_scale*att*ds.x*light[i].diffuse.rgb;");
                    appendln(mb, "      blspec += intensity_scale*att*ds.y*light[i].specular.rgb;");
                    appendln(mb, "   }");
                    appendln(mb, "   ");
                    appendln(mb, "   // accumulate");
                    appendln(mb, "   lightOut.front_ambient  = flambi;");
                    appendln(mb, "   lightOut.front_diffuse  = fldiff;");
                    appendln(mb, "   lightOut.front_specular = flspec;");
                    appendln(mb, "   lightOut.back_ambient   = blambi;");
                    appendln(mb, "   lightOut.back_diffuse   = bldiff;");
                    appendln(mb, "   lightOut.back_specular  = blspec;");
                    appendln(mb);
                }
                break;
            case SMOOTH:
            case METAL:
                // forward along direction information for vertices
                if (info.numLights() > 0) {
                    appendln(mb, "   // per-fragment lighting info, vertex normal and eye directions");
                    appendln(mb, "   vec4 camera_position = vm_matrix * vec4(position, 1.0);");
                    if (info.hasVertexNormals()) {
                        appendln(mb, "   vec4 camera_normal = normal_matrix * vec4(normal, 0.0);");
                    } else {
                        appendln(mb, "   vec4 camera_normal = -camera_position;  // assume pointed at camera");
                    }
                    appendln(mb, "   surfOut.normal = normalize(camera_normal.xyz);");
                    appendln(mb, "   surfOut.to_eye = -camera_position.xyz;");
                    appendln(mb, "");
                }
                break;
            case NONE:
                break;
        }
    }
    // textures
    boolean hasTextures = hasTextures(info);
    if (hasTextures) {
        appendln(mb, "   // forward vertex texture coordinates");
        appendln(mb, "   textureOut.texcoord = (texture_matrix*vec4(vertex_texcoord, 0, 1)).xy;");
        appendln(mb);
    }
    if (info.numClipPlanes() > 0) {
        appendln(mb, "   // clipping planes, in world coordinates");
        appendln(mb, "   vec4 world_position = m_matrix * vec4(position, 1.0);");
        appendln(mb, "   for (int i=0; i<" + info.numClipPlanes() + "; ++i) {");
        appendln(mb, "      gl_ClipDistance[i] = dot(world_position, clip_plane[i].plane);");
        appendln(mb, "   }");
        appendln(mb);
    }
    appendln(mb, "}");
}
Also used : RenderingMode(maspack.render.GL.GLProgramInfo.RenderingMode) ColorInterpolation(maspack.render.Renderer.ColorInterpolation)

Example 4 with RenderingMode

use of maspack.render.GL.GLProgramInfo.RenderingMode in project artisynth_core by artisynth.

the class GLSLGenerator method buildFragmentShaderMain.

private static void buildFragmentShaderMain(StringBuilder mb, GLProgramInfo info) {
    RenderingMode mode = info.getMode();
    ColorInterpolation cinterp = info.getColorInterpolation();
    boolean hasTextures = hasTextures(info);
    appendln(mb, "// main fragment shader");
    appendln(mb, "void main() {");
    appendln(mb, "");
    // points
    if (mode == RenderingMode.POINTS && info.hasRoundPoints()) {
        appendln(mb, "   vec2 point_texcoord = 2.0*gl_PointCoord-1.0;");
        appendln(mb, "   float point_dist = dot(point_texcoord, point_texcoord);");
        appendln(mb, "   if ( point_dist > 1) {");
        appendln(mb, "      discard;");
        appendln(mb, "   }");
    }
    // do lighting computations
    boolean lights = false;
    if (!info.isSelecting()) {
        if (info.getShading() != Shading.NONE && info.numLights() > 0) {
            lights = true;
            appendln(mb, "   vec3 ambient = vec3(0.0);");
            appendln(mb, "   vec3 diffuse = vec3(0.0);");
            appendln(mb, "   vec3 specular = vec3(0.0);");
            appendln(mb, "   vec3 emission = vec3(0.0);");
            appendln(mb, "   Material material;");
            switch(info.getShading()) {
                case FLAT:
                    // case GOURAUD:
                    appendln(mb, "   // imported per-vertex lighting");
                    appendln(mb, "   if( gl_FrontFacing ) {");
                    appendln(mb, "      ambient  = lightIn.front_ambient;");
                    appendln(mb, "      diffuse  = lightIn.front_diffuse;");
                    appendln(mb, "      specular = lightIn.front_specular;");
                    appendln(mb, "      material = front_material;");
                    appendln(mb, "   } else {");
                    appendln(mb, "      diffuse  = lightIn.back_diffuse;");
                    appendln(mb, "      ambient  = lightIn.back_ambient;");
                    appendln(mb, "      specular = lightIn.back_specular;");
                    appendln(mb, "      material = back_material;");
                    appendln(mb, "   }");
                    appendln(mb);
                    break;
                case SMOOTH:
                case METAL:
                    appendln(mb, "   // fragment normal and eye location for lighting");
                    if (!info.hasVertexNormals()) {
                        appendln(mb, "   // compute normal using derivatives");
                        appendln(mb, "   vec3 deyedx = dFdx(surfIn.to_eye);");
                        appendln(mb, "   vec3 deyedy = dFdy(surfIn.to_eye);");
                        appendln(mb, "   vec3 normal = normalize( cross( deyedx, deyedy ) );");
                    } else {
                        appendln(mb, "   vec3 normal = normalize(surfIn.normal);");
                    }
                    appendln(mb, "   vec3 eye = normalize(surfIn.to_eye);");
                    appendln(mb, "   ");
                    if (hasTextures) {
                        if (info.hasNormalMap()) {
                            appendln(mb, "   // normal-map purturbation");
                            appendln(mb, "   normal = perturbNormal(-surfIn.to_eye, normal);");
                            appendln(mb, "   ");
                        }
                        if (info.hasBumpMap()) {
                            appendln(mb, "   // bump-map normal purturbation");
                            appendln(mb, "   normal = perturbNormalBump(-surfIn.to_eye, normal, computedHdxy());");
                            appendln(mb, "   ");
                        }
                    }
                    appendln(mb, "   // choose material based on face orientation");
                    appendln(mb, "   if (gl_FrontFacing) {");
                    appendln(mb, "      material = front_material;");
                    appendln(mb, "   } else {");
                    appendln(mb, "      material = back_material;");
                    appendln(mb, "      normal = -normal;  // flip fragment normal");
                    appendln(mb, "   }");
                    appendln(mb, "   ");
                    appendln(mb, "   // per-fragment lighting computations");
                    appendln(mb, "   for (int i=0; i<" + info.numLights() + "; ++i) {");
                    appendln(mb, "      vec3 light_to_vertex = -surfIn.to_eye-light[i].position.xyz;");
                    appendln(mb, "      float lightdist = length(light_to_vertex);");
                    appendln(mb, "      light_to_vertex = normalize(light_to_vertex);");
                    appendln(mb, "      // determine direction either from point or direction using direction indicator");
                    appendln(mb, "      vec3 light_direction = normalize(light[i].direction.xyz);");
                    appendln(mb, "      ");
                    appendln(mb, "      float spotatt = 1.0;  // spot attentuation initially zero if non-spotlight");
                    appendln(mb, "      float coslimit = light[i].direction.w;");
                    appendln(mb, "      if (coslimit > 0) {");
                    appendln(mb, "         // check angle");
                    appendln(mb, "         float coslight = dot(light_direction, light_to_vertex);");
                    appendln(mb, "         if (coslight < coslimit) {");
                    appendln(mb, "            spotatt = 0;");
                    appendln(mb, "         } else {");
                    appendln(mb, "            spotatt = pow(coslight, light[i].attenuation.w);");
                    appendln(mb, "         }");
                    appendln(mb, "      }");
                    appendln(mb, "      ");
                    appendln(mb, "      // distance attenuation doesn't affect directional lights");
                    appendln(mb, "      float att = mix(1.0, 1.0 / (light[i].attenuation.x + light[i].attenuation.y*lightdist +");
                    appendln(mb, "         light[i].attenuation.z*lightdist*lightdist), light[i].position.w);");
                    appendln(mb, "      att *= spotatt;  // combine into a single attenuation parameter");
                    appendln(mb, "      ");
                    appendln(mb, "      // position vs directional light");
                    appendln(mb, "      light_direction = mix(light_direction, light_to_vertex, light[i].position.w);");
                    appendln(mb, "      vec2 ds = blinnPhongCoeffs( normal, -light_direction, eye, material.specular.a);");
                    appendln(mb, "      ambient  += intensity_scale*light[i].ambient.rgb;");
                    appendln(mb, "      diffuse  += intensity_scale*att*ds.x*light[i].diffuse.rgb;");
                    appendln(mb, "      specular += intensity_scale*att*ds.y*light[i].specular.rgb;");
                    appendln(mb, "      ");
                    appendln(mb, "   }");
                    appendln(mb, "   ");
                    break;
                case NONE:
                    // never here
                    break;
            }
        } else {
            appendln(mb, "   // material to use;");
            appendln(mb, "   Material material;");
            appendln(mb, "   if( gl_FrontFacing ) {");
            appendln(mb, "      material = front_material;");
            appendln(mb, "   } else {");
            appendln(mb, "      material = back_material;");
            appendln(mb, "   }");
        }
        // combine colors
        boolean hasFragmentColors = (info.getVertexColorMixing() != ColorMixing.NONE) && (info.hasVertexColors() || ((mode == RenderingMode.INSTANCED_POINTS || mode == RenderingMode.INSTANCED_FRAMES || mode == RenderingMode.INSTANCED_AFFINES) && info.hasInstanceColors()) || (mode == RenderingMode.INSTANCED_LINES && info.hasLineColors()));
        appendln(mb, "   // compute final color, starting with material");
        appendln(mb, "   vec4 fdiffuse = material.diffuse;");
        appendln(mb, "   vec3 fspecular = material.specular.rgb;");
        appendln(mb, "   vec3 femission = material.emission.rgb;");
        appendln(mb);
        if (hasFragmentColors) {
            // mix colors
            appendln(mb, "   // incoming vertex color");
            if (cinterp == ColorInterpolation.HSV) {
                appendln(mb, "   vec4 vcolor = hsva2rgba(colorIn.diffuse);");
            } else {
                appendln(mb, "   vec4 vcolor = colorIn.diffuse;");
            }
            appendln(mb, "   // mix");
            ColorMixing cmix = info.getVertexColorMixing();
            switch(cmix) {
                case DECAL:
                    if (info.isMixVertexColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = vec4(mix(fdiffuse.rgb,vcolor.rgb,vcolor.a), fdiffuse.a); // decal");
                    }
                    if (info.isMixVertexColorSpecular()) {
                        appendln(mb, "   fspecular = mix(fspecular,vcolor.rgb,vcolor.a); // decal");
                    }
                    if (info.isMixVertexColorEmission()) {
                        appendln(mb, "   femission = mix(femission,vcolor.rgb,vcolor.a); // decal");
                    }
                    break;
                case MODULATE:
                    if (info.isMixVertexColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = fdiffuse*vcolor;      // modulate");
                    }
                    if (info.isMixVertexColorSpecular()) {
                        appendln(mb, "   fspecular = fspecular*vcolor.rgb; // modulate");
                    }
                    if (info.isMixVertexColorEmission()) {
                        appendln(mb, "   femission = femission*vcolor.rgb; // modulate");
                    }
                    break;
                case REPLACE:
                    if (info.isMixVertexColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = vcolor;     // replace");
                    }
                    if (info.isMixVertexColorSpecular()) {
                        appendln(mb, "   fspecular = vcolor.rgb; // replace");
                    }
                    if (info.isMixVertexColorEmission()) {
                        appendln(mb, "   femission = vcolor.rgb; // replace");
                    }
                    break;
                case NONE:
                    appendln(mb, "   // vcolor ignored");
                default:
            }
        }
        if (hasTextures && info.hasColorMap()) {
            appendln(mb, "   // grab texture color and mix");
            appendln(mb, "   vec4 texture_color = texture( color_map, textureIn.texcoord );");
            ColorMixing cmix = info.getTextureColorMixing();
            switch(cmix) {
                case DECAL:
                    if (info.isMixTextureColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = vec4(mix(fdiffuse.rgb,texture_color.rgb,texture_color.a), fdiffuse.a); // decal");
                    }
                    if (info.isMixTextureColorSpecular()) {
                        appendln(mb, "   fspecular = mix(fspecular,texture_color.rgb,texture_color.a); // decal");
                    }
                    if (info.isMixTextureColorEmission()) {
                        appendln(mb, "   femission = mix(femission,texture_color.rgb,texture_color.a); // decal");
                    }
                    break;
                case MODULATE:
                    if (info.isMixTextureColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = fdiffuse*texture_color;      // modulate");
                    }
                    if (info.isMixTextureColorSpecular()) {
                        appendln(mb, "   fspecular = fspecular*texture_color.rgb; // modulate");
                    }
                    if (info.isMixTextureColorEmission()) {
                        appendln(mb, "   femission = femission*texture_color.rgb; // modulate");
                    }
                    break;
                case REPLACE:
                    if (info.isMixTextureColorDiffuse()) {
                        appendln(mb, "   fdiffuse  = texture_color;     // replace");
                    }
                    if (info.isMixTextureColorSpecular()) {
                        appendln(mb, "   fspecular = texture_color.rgb; // replace");
                    }
                    if (info.isMixTextureColorEmission()) {
                        appendln(mb, "   femission = texture_color.rgb; // replace");
                    }
                    break;
                case NONE:
                    appendln(mb, "   // texture_color ignored");
                    break;
                default:
            }
            appendln(mb);
        }
    }
    // lighting
    if (info.isSelecting()) {
        appendln(mb, "   fragment_color = selection_color;");
    } else if (lights) {
        appendln(mb, "   // apply lighting");
        appendln(mb, "   ambient  = fdiffuse.rgb*ambient*material.power.x;");
        appendln(mb, "   diffuse  = fdiffuse.rgb*diffuse*material.power.y;");
        appendln(mb, "   specular = fspecular*specular*material.power.z;");
        appendln(mb, "   emission = femission*material.power.w;  // emission only material-related");
        appendln(mb, "   fragment_color = vec4(max(diffuse+specular+emission, ambient), fdiffuse.a);");
    } else {
        appendln(mb, "   fragment_color = fdiffuse;");
    }
    appendln(mb);
    // appendln(mb, "   // gamma correction");
    // appendln(mb, "   vec3 gamma = vec3(1.0/2.2);");
    // appendln(mb, "   fragment_color = vec4(pow(fragment_color.rgb, gamma), fragment_color.a);");
    appendln(mb, "}");
}
Also used : RenderingMode(maspack.render.GL.GLProgramInfo.RenderingMode) ColorMixing(maspack.render.Renderer.ColorMixing) ColorInterpolation(maspack.render.Renderer.ColorInterpolation)

Example 5 with RenderingMode

use of maspack.render.GL.GLProgramInfo.RenderingMode in project artisynth_core by artisynth.

the class GLSLGenerator method addVertexOutputs.

private static void addVertexOutputs(StringBuilder hb, GLProgramInfo info) {
    RenderingMode instanced = info.getMode();
    boolean hasColors = (!info.isSelecting()) && (info.getVertexColorMixing() != ColorMixing.NONE) && (info.hasVertexColors() || ((instanced == RenderingMode.INSTANCED_POINTS || instanced == RenderingMode.INSTANCED_FRAMES || instanced == RenderingMode.INSTANCED_AFFINES) && info.hasInstanceColors()) || (instanced == RenderingMode.INSTANCED_LINES && info.hasLineColors()));
    boolean hasTextures = hasTextures(info);
    if (hasColors) {
        appendln(hb, "// per-vertex color info");
        appendln(hb, "out ColorData {");
        if (info.getColorInterpolation() == ColorInterpolation.NONE) {
            appendln(hb, "   flat vec4 diffuse;");
        } else {
            appendln(hb, "   vec4 diffuse;");
        }
        appendln(hb, "} colorOut;");
        appendln(hb);
    }
    if (hasTextures) {
        appendln(hb, "// per-vertex texture info");
        appendln(hb, "out TextureData {");
        appendln(hb, "   vec2 texcoord;");
        appendln(hb, "} textureOut;");
        appendln(hb);
    }
}
Also used : RenderingMode(maspack.render.GL.GLProgramInfo.RenderingMode)

Aggregations

RenderingMode (maspack.render.GL.GLProgramInfo.RenderingMode)5 ColorInterpolation (maspack.render.Renderer.ColorInterpolation)2 GLProgramInfo (maspack.render.GL.GLProgramInfo)1 ColorMixing (maspack.render.Renderer.ColorMixing)1