Search in sources :

Example 1 with ColorMixing

use of maspack.render.Renderer.ColorMixing in project artisynth_core by artisynth.

the class MeshBase method setVertexColorMixing.

/**
 * Sets the color mixing method to be used for vertex-based coloring.
 *
 * @param cmix new color mixing method
 * @return previous color mixing method
 */
public ColorMixing setVertexColorMixing(ColorMixing cmix) {
    ColorMixing prev = myVertexColorMixing;
    myVertexColorMixing = cmix;
    return prev;
}
Also used : ColorMixing(maspack.render.Renderer.ColorMixing)

Example 2 with ColorMixing

use of maspack.render.Renderer.ColorMixing 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 3 with ColorMixing

use of maspack.render.Renderer.ColorMixing in project artisynth_core by artisynth.

the class Transrotator3d method render.

public void render(Renderer renderer, int flags) {
    if (!myVisibleP) {
        return;
    }
    Shading savedShading = renderer.setShading(Shading.NONE);
    renderer.setLineWidth(myLineWidth);
    ColorMixing savedMixing = renderer.setVertexColorMixing(ColorMixing.REPLACE);
    renderer.pushModelMatrix();
    renderer.mulModelMatrix(myXDraggerToWorld);
    float[] coords = new float[3];
    if (myDragMode != DragMode.OFF && mySelectedComponent != NONE) {
        renderer.setColor(1, 1, 0);
        renderer.setPointSize(3);
        myPnt0.get(coords);
        renderer.drawPoint(coords);
        renderer.setPointSize(1);
    }
    renderer.scaleModelMatrix(mySize);
    if (renderObject == null) {
        renderObject = createTransrotatorRenderable();
    }
    // select appropriate color buffer
    if (mySelectedComponent != 0) {
        renderer.drawLines(renderObject, mySelectedComponent);
    }
    renderer.drawLines(renderObject, 0);
    renderer.popModelMatrix();
    if (myDragMode != DragMode.OFF && (mySelectedComponent == X_ROTATE || mySelectedComponent == Y_ROTATE || mySelectedComponent == Z_ROTATE)) {
        // Draw rotation lines using the orientation at the time the drag was
        // started
        RigidTransform3d X = new RigidTransform3d(myXDraggerToWorld0);
        X.p.set(myXDraggerToWorld.p);
        renderer.pushModelMatrix();
        renderer.mulModelMatrix(X);
        final float[] coords0 = new float[] { 0, 0, 0 };
        renderer.setColor(0.5f, 0.5f, 0.5f);
        myPnt0.get(coords);
        renderer.drawLine(coords0, coords);
        renderer.setColor(1, 1, 0);
        myRotPnt.get(coords);
        renderer.drawLine(coords0, coords);
        renderer.popModelMatrix();
    }
    renderer.setLineWidth(1);
    renderer.setShading(savedShading);
    renderer.setVertexColorMixing(savedMixing);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) ColorMixing(maspack.render.Renderer.ColorMixing) Shading(maspack.render.Renderer.Shading)

Example 4 with ColorMixing

use of maspack.render.Renderer.ColorMixing in project artisynth_core by artisynth.

the class PolygonalMeshRenderer method render.

public void render(Renderer renderer, RenderProps props, boolean highlight, FeatureIndexArray faces, FeatureIndexArray edges, boolean featureSelection) {
    PolygonalMesh mesh = getMesh();
    if (mesh.numVertices() == 0) {
        return;
    }
    RenderObject rob = getRenderObject();
    renderer.pushModelMatrix();
    if (mesh.isRenderBuffered()) {
        renderer.mulModelMatrix(mesh.getXMeshToWorldRender());
    } else {
        renderer.mulModelMatrix(mesh.XMeshToWorld);
    }
    boolean drawFaces = (props.getFaceStyle() != Renderer.FaceStyle.NONE);
    ColorMixing savedColorMixing = null;
    if (mesh.hasColors()) {
        savedColorMixing = renderer.setVertexColorMixing(mesh.getVertexColorMixing());
    }
    if (props.getDrawEdges()) {
        drawEdges(renderer, props, highlight, drawFaces, rob, edges, featureSelection);
    }
    if (drawFaces) {
        drawFaces(renderer, props, highlight, rob, faces, featureSelection);
    }
    if (mesh.hasColors()) {
        renderer.setVertexColorMixing(savedColorMixing);
    }
    renderer.popModelMatrix();
}
Also used : ColorMixing(maspack.render.Renderer.ColorMixing) RenderObject(maspack.render.RenderObject)

Example 5 with ColorMixing

use of maspack.render.Renderer.ColorMixing in project artisynth_core by artisynth.

the class PolygonalMeshRenderer method drawEdges.

/**
 * Draws the edges associated with this mesh. Edge drawing is done using
 * edgeWidth and edgeColor (or LineColor if edgeColor is undefined), according
 * to the following rules:
 *
 * <p>If faces <i>are</i> also being drawn and there <i>is</i> vertex
 * coloring, then edges should render using the edge color, with whatever
 * shading is selected, and should respond to highlighting.
 *
 * <p>If faces <i>are</i> also being drawn and there <i>is no</i> vertex
 * coloring, then (a) edges should not respond to highlighting (the faces
 * will instead), and (b) edges should be rendered with whatever shading is
 * selected, <i>unless</i> the edge color is the same as the face color, in
 * which case shading is turned off so that the edges can be seen.
 *
 * <p>If faces <i>are not</i> also being drawn and there <i>is</i> vertex
 * coloring, then edges should render using the vertex coloring, with
 * whatever shading is selected, unless the mesh being highlighted, in which
 * case it should they should be rendered with the highlight color.
 *
 * <p>If faces <i>are not</i> also being drawn and there <i>is no</i> vertex
 * coloring, then edges should be rendered with whatever shading is
 * selected, and should respond to highlighting.
 */
private void drawEdges(Renderer renderer, RenderProps props, boolean highlight, boolean alsoDrawingFaces, RenderObject robj, FeatureIndexArray features, boolean featureSelection) {
    float savedLineWidth = renderer.getLineWidth();
    Shading savedShadeModel = renderer.getShading();
    boolean disableColors = false;
    renderer.setLineWidth(props.getEdgeWidth());
    Shading shading = props.getShading();
    if (!robj.hasNormals() && shading != Shading.FLAT) {
        shading = Shading.NONE;
    }
    float[] edgeColor = getEffectiveEdgeColor(props);
    if (alsoDrawingFaces) {
        highlight = false;
        if (robj.hasColors()) {
            disableColors = true;
        } else {
            if (colorsEqual(edgeColor, props.getFaceColorF())) {
                // turn off shading so we can see edges
                shading = Shading.NONE;
            }
        }
    } else {
        if (robj.hasColors()) {
            if (highlight) {
                disableColors = true;
            }
        }
    }
    renderer.setEdgeColoring(props, highlight);
    renderer.setShading(shading);
    ColorInterpolation savedColorInterp = null;
    if (usingHSV(getMesh())) {
        savedColorInterp = renderer.setColorInterpolation(ColorInterpolation.HSV);
    }
    ColorMixing savedColorMixing = null;
    if (disableColors) {
        savedColorMixing = renderer.getVertexColorMixing();
        renderer.setVertexColorMixing(ColorMixing.NONE);
    }
    if (renderer.isSelecting() && featureSelection) {
        for (int i = 0; i < features.numFeatures(); ++i) {
            renderer.beginSelectionQuery(features.getFeature(i));
            renderer.drawVertices(robj, features.getVertices(), features.getFeatureOffset(i), features.getFeatureLength(i), DrawMode.LINES);
            renderer.endSelectionQuery();
        }
    } else {
        renderer.drawVertices(robj, features.getVertices(), DrawMode.LINES);
    }
    if (savedColorInterp != null) {
        renderer.setColorInterpolation(savedColorInterp);
    }
    if (disableColors) {
        renderer.setVertexColorMixing(savedColorMixing);
    }
    renderer.setLineWidth(savedLineWidth);
    renderer.setShading(savedShadeModel);
}
Also used : ColorMixing(maspack.render.Renderer.ColorMixing) Shading(maspack.render.Renderer.Shading) ColorInterpolation(maspack.render.Renderer.ColorInterpolation)

Aggregations

ColorMixing (maspack.render.Renderer.ColorMixing)5 ColorInterpolation (maspack.render.Renderer.ColorInterpolation)2 Shading (maspack.render.Renderer.Shading)2 RigidTransform3d (maspack.matrix.RigidTransform3d)1 RenderingMode (maspack.render.GL.GLProgramInfo.RenderingMode)1 RenderObject (maspack.render.RenderObject)1