Search in sources :

Example 6 with Renderer

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

the class DebuggingGL3Test method addContent.

@Override
protected void addContent(maspack.test.GL.MultiViewer mv) {
    SimpleSelectable ss = new SimpleSelectableBase() {

        final File[] shaders = { new File(PathFinder.findSourceDir(GL3Viewer.class) + "/shaders/debug_vertex.glsl"), new File(PathFinder.findSourceDir(GL3Viewer.class) + "/shaders/debug_fragment.glsl") };

        int vao = 0;

        int vbo = 0;

        boolean initialized = false;

        private void init(GL3 gl) {
            int[] ibuff = new int[10];
            gl.glGenVertexArrays(1, ibuff, 0);
            vao = ibuff[0];
            gl.glGenBuffers(1, ibuff, 1);
            vbo = ibuff[1];
            fill(gl);
            initialized = true;
        }

        private void fill(GL3 gl) {
            gl.glBindVertexArray(vao);
            float[] triangle = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
            java.nio.ByteBuffer vbuff = java.nio.ByteBuffer.allocateDirect(4 * triangle.length);
            for (float f : triangle) {
                vbuff.putFloat(f);
            }
            vbuff.flip();
            gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo);
            gl.glBufferData(GL3.GL_ARRAY_BUFFER, vbuff.limit(), vbuff, GL3.GL_STATIC_DRAW);
        }

        @Override
        public void render(Renderer renderer, int flags) {
            GL3Viewer viewer = (GL3Viewer) renderer;
            GL3 gl = viewer.getGL().getGL3();
            GLSupport.checkAndPrintGLError(gl);
            viewer.setShaderOverride(shaders, shaders);
            if (!initialized) {
                init(gl);
            }
            viewer.useProgram(gl, shaders);
            gl.glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
            gl.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
            // re-bind vao
            fill(gl);
            gl.glBindVertexArray(vao);
            gl.glEnableVertexAttribArray(0);
            gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo);
            gl.glVertexAttribPointer(0, 3, GL3.GL_FLOAT, false, 0, 0);
            gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
            gl.glDisableVertexAttribArray(0);
            gl.glBindVertexArray(0);
            viewer.setShaderOverride(null, null);
        // throw new RuntimeException("Exit rendering");
        }
    };
    mv.addRenderable(ss);
}
Also used : SimpleSelectable(maspack.test.GL.MultiViewer.SimpleSelectable) Renderer(maspack.render.Renderer) GL3Viewer(maspack.render.GL.GL3.GL3Viewer) File(java.io.File) GL3(com.jogamp.opengl.GL3)

Example 7 with Renderer

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

the class VolumePrimitivesTest method addContent.

@Override
protected void addContent(MultiViewer mv) {
    SimpleSelectable renderable = new SimpleSelectable() {

        RenderProps props = new RenderProps();

        @Override
        public void updateBounds(Vector3d pmin, Vector3d pmax) {
            Point3d.X_UNIT.updateBounds(pmin, pmax);
            Point3d.Y_UNIT.updateBounds(pmin, pmax);
            Point3d.Z_UNIT.updateBounds(pmin, pmax);
            Point3d.NEG_X_UNIT.updateBounds(pmin, pmax);
            Point3d.NEG_Y_UNIT.updateBounds(pmin, pmax);
            Point3d.NEG_Z_UNIT.updateBounds(pmin, pmax);
        }

        @Override
        public void render(Renderer renderer, int flags) {
            props.setFaceColor(Color.CYAN);
            props.setBackColor(Color.MAGENTA);
            renderer.setFaceColoring(props, /*highlight=*/
            false);
            renderer.setColor(props.getFaceColorF());
            renderer.setBackColor(props.getBackColorF());
            renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
            // renderer.drawSphere (new float[] {0.1f,0.2f,0.3f}, 0.05);
            // renderer.drawSpindle (new float[]{-0.1f,-0.2f,-0.3f}, new float[]{-0.3f,-0.2f,-0.1f}, 0.05);
            // renderer.drawSpindle (new float[]{0.1f,-0.2f,0.3f}, new float[]{0.3f,-0.2f,0.1f}, 0.25);
            // renderer.drawCylinder (new float[]{-0.1f,0.2f,-0.3f}, new float[]{-0.3f,0.2f,-0.1f}, 0.3, true);
            // renderer.drawCylinder (new float[]{0.1f,0.2f,-0.3f}, new float[]{-0.3f,-0.2f,-0.3f}, 0.2, true);
            // renderer.drawCone (new float[]{-0.1f,0.2f,0.3f}, new float[]{0.3f,0.2f,-0.1f}, 0.1, 0, false);
            // renderer.drawCone (new float[]{-0.3f,0.2f,0.3f}, new float[]{0.5f,0.2f,-0.1f}, 0, 0.2, true);
            // renderer.drawSphere (new float[]{-0.1f,-0.2f,-0.3f}, 0.2);
            // renderer.drawSphere (new float[] {0.1f,0.2f,0.3f}, 0.02);
            // renderer.drawSphere (new float[] {0.1f,0.2f,0.6f}, 0.03);
            // renderer.drawSphere (new float[] {0.1f,0.2f,0.8f}, 0.04);
            // renderer.drawSphere (new float[] {0.1f,0.2f,1f}, 0.05);
            // renderer.drawCone (new float[]{0.1f,0.1f,-0.1f}, new float[]{0.3f,0.1f,-0.1f}, 0.05, 0.02, true);
            renderer.drawCube(new float[] { -0.2f, -0.2f, -0.2f }, 0.5);
        }

        @Override
        public void prerender(RenderList list) {
        }

        @Override
        public int getRenderHints() {
            return 0;
        }

        @Override
        public int numSelectionQueriesNeeded() {
            return 0;
        }

        @Override
        public boolean isSelectable() {
            return false;
        }

        @Override
        public void getSelection(LinkedList<Object> list, int qid) {
        }

        @Override
        public void setSelected(boolean set) {
        }

        @Override
        public boolean isSelected() {
            // TODO Auto-generated method stub
            return false;
        }
    };
    mv.addRenderable(renderable);
}
Also used : SimpleSelectable(maspack.test.GL.MultiViewer.SimpleSelectable) Vector3d(maspack.matrix.Vector3d) RenderProps(maspack.render.RenderProps) Renderer(maspack.render.Renderer) RenderList(maspack.render.RenderList) LinkedList(java.util.LinkedList)

Example 8 with Renderer

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

the class PolygonalMeshRenderer method drawFaces.

private void drawFaces(Renderer renderer, RenderProps props, boolean highlight, RenderObject robj, FeatureIndexArray features, boolean featureSelection) {
    boolean useTextures = robj.hasTextureCoords();
    Renderer.FaceStyle savedFaceStyle = renderer.getFaceStyle();
    Shading savedShadeModel = renderer.getShading();
    Shading shading = props.getShading();
    if (!robj.hasNormals() && shading != Shading.FLAT) {
        shading = Shading.NONE;
    }
    renderer.setShading(shading);
    renderer.setFaceColoring(props, highlight);
    // XXX always front and back when selecting?
    if (renderer.isSelecting()) {
        renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
    } else {
        renderer.setFaceStyle(props.getFaceStyle());
    }
    // int i = 0; // i is index of face
    ColorInterpolation savedColorInterp = null;
    if (usingHSV(getMesh())) {
        savedColorInterp = renderer.setColorInterpolation(ColorInterpolation.HSV);
    }
    if (props.getDrawEdges()) {
        renderer.setDepthOffset(-1);
    }
    ColorMapProps oldtprops = null;
    NormalMapProps oldnprops = null;
    BumpMapProps oldbprops = null;
    if (useTextures) {
        ColorMapProps dtprops = props.getColorMap();
        oldtprops = renderer.setColorMap(dtprops);
        NormalMapProps ntprops = props.getNormalMap();
        oldnprops = renderer.setNormalMap(ntprops);
        BumpMapProps btprops = props.getBumpMap();
        oldbprops = renderer.setBumpMap(btprops);
    }
    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.TRIANGLES);
            renderer.endSelectionQuery();
        }
    } else {
        renderer.drawVertices(robj, features.getVertices(), DrawMode.TRIANGLES);
    }
    if (useTextures) {
        // restore diffuse texture properties
        renderer.setColorMap(oldtprops);
        renderer.setNormalMap(oldnprops);
        renderer.setBumpMap(oldbprops);
    }
    if (props.getDrawEdges()) {
        renderer.setDepthOffset(0);
    }
    if (savedColorInterp != null) {
        renderer.setColorInterpolation(savedColorInterp);
    }
    renderer.setFaceStyle(savedFaceStyle);
    renderer.setShading(savedShadeModel);
}
Also used : BumpMapProps(maspack.render.BumpMapProps) FaceStyle(maspack.render.Renderer.FaceStyle) ColorMapProps(maspack.render.ColorMapProps) Renderer(maspack.render.Renderer) Shading(maspack.render.Renderer.Shading) ColorInterpolation(maspack.render.Renderer.ColorInterpolation) NormalMapProps(maspack.render.NormalMapProps)

Example 9 with Renderer

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

the class ColorBar method render.

@Override
public void render(Renderer renderer, int flags) {
    if (!isSelectable() && renderer.isSelecting()) {
        return;
    }
    RenderObject robj = myRenderObject;
    VectorNd labelPos = myLabelPos;
    ArrayList<String> labelText = myLabelText;
    if (robj == null || labelPos == null || labelText == null) {
        System.out.println((robj == null) + " " + (labelPos == null) + " " + (labelText == null));
        return;
    }
    int screenWidth = renderer.getScreenWidth();
    int screenHeight = renderer.getScreenHeight();
    // turn off shading for the labels
    Renderer.Shading savedShadeModel = renderer.getShading();
    renderer.setShading(Renderer.Shading.NONE);
    double x0 = myLoc.x;
    double y0 = myLoc.y;
    double w = myLoc.width;
    double h = myLoc.height;
    if (horizontal) {
        h = myLoc.width;
        w = myLoc.height;
        x0 = myLoc.y;
        y0 = myLoc.x;
    }
    // absolute or normalized -> absolute
    if (Math.abs(x0) <= 1) {
        x0 = x0 * screenWidth;
    }
    if (x0 < 0) {
        x0 = screenWidth + x0;
    }
    if (Math.abs(y0) <= 1) {
        y0 = y0 * screenHeight;
    }
    if (y0 < 0) {
        y0 = screenHeight + y0;
    }
    if (w <= 1) {
        w = w * screenWidth;
    }
    if (h <= 1) {
        h = h * screenHeight;
    }
    renderer.pushModelMatrix();
    renderer.setModelMatrix2d(0, screenWidth, 0, screenHeight);
    // transform so that the colorbar occupies correct location
    AffineTransform3d trans = new AffineTransform3d();
    if (horizontal) {
        trans.setRotation(ROT_Z_270);
        trans.applyScaling(h, w, 1);
    } else {
        trans.applyScaling(w, h, 1);
    }
    trans.setTranslation(x0, y0, 0);
    renderer.mulModelMatrix(trans);
    renderer.setVertexColorMixing(ColorMixing.REPLACE);
    renderer.drawTriangles(robj, 0);
    float savedLineWidth = renderer.getLineWidth();
    LineFaceRenderProps props = (LineFaceRenderProps) getRenderProps();
    renderer.setLineWidth(props.getLineWidth());
    renderer.setLineColoring(props, /*highlight=*/
    false);
    renderer.setVertexColorMixing(ColorMixing.NONE);
    renderer.drawLines(robj, 0);
    renderer.popModelMatrix();
    // return line width
    renderer.setLineWidth(savedLineWidth);
    // labels
    int nLabels = Math.min(labelPos.size(), labelText.size());
    if (nLabels > 0) {
        double tx, ty;
        // // for consistency, assume line top as 3/4 font size
        // double t = myTextSize*0.75;
        // double vc = myTextSize* 0.25;
        // double b = myTextSize*0.25;
        renderer.setFaceColoring(props, isSelected());
        float[] loc = new float[3];
        for (int i = 0; i < nLabels; i++) {
            tx = 0;
            ty = 0;
            // text orientation computation
            String label = labelText.get(i);
            Rectangle2D box = renderer.getTextBounds(myFont, label, myTextSize);
            double bw = box.getWidth();
            double bh = box.getHeight();
            double b = box.getY();
            double vc = b + bh / 2;
            double t = bh + b;
            if (horizontal) {
                switch(hAlignment) {
                    case LEFT:
                        tx = x0 + myTextOffset.x;
                        break;
                    case CENTRE:
                        tx = x0 - bw / 2;
                        break;
                    case RIGHT:
                        tx = x0 - myTextOffset.x - bw;
                        break;
                }
                tx += w * labelPos.get(i);
                switch(vAlignment) {
                    case BOTTOM:
                        ty = y0 - myTextOffset.y - t;
                        break;
                    case CENTRE:
                        ty = y0 - vc + h / 2;
                        break;
                    case TOP:
                        ty = y0 + h + myTextOffset.y + b;
                        break;
                }
            } else {
                switch(hAlignment) {
                    case LEFT:
                        tx = x0 - myTextOffset.x - bw;
                        break;
                    case CENTRE:
                        tx = x0 - bw / 2 + w / 2;
                        break;
                    case RIGHT:
                        tx = x0 + myTextOffset.x + w;
                        break;
                }
                switch(vAlignment) {
                    case BOTTOM:
                        ty = y0 + myTextOffset.y;
                        break;
                    case CENTRE:
                        ty = y0 - vc;
                        break;
                    case TOP:
                        ty = y0 - myTextOffset.y - t;
                        break;
                }
                ty += h * labelPos.get(i);
            }
            loc[0] = (float) tx;
            loc[1] = (float) ty;
            renderer.drawText(myFont, label, loc, myTextSize);
        }
    }
    renderer.setShading(savedShadeModel);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) VectorNd(maspack.matrix.VectorNd) Renderer(maspack.render.Renderer) LineFaceRenderProps(maspack.render.LineFaceRenderProps) RenderObject(maspack.render.RenderObject) AffineTransform3d(maspack.matrix.AffineTransform3d)

Example 10 with Renderer

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

the class MeshColliderTest method displayContacts.

void displayContacts(PolygonalMesh m0, PolygonalMesh m1) {
    final PolygonalMesh mesh0 = m0;
    final PolygonalMesh mesh1 = m1;
    MeshCollider collider = new MeshCollider();
    final ContactInfo info = collider.getContacts(mesh0, mesh1);
    // final ContactInfo info = new ContactInfo(mesh0, mesh1);
    // System.out.println("intersections " + info.intersections.size());
    // System.out.println("regions " + info.regions.size());
    GLViewerFrame frame = new GLViewerFrame("", 512, 512);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mesh0.getRenderProps().setDrawEdges(true);
    // mesh0.getRenderProps().setAlpha(0.3);
    mesh1.getRenderProps().setDrawEdges(true);
    // mesh1.getRenderProps().setAlpha(0.3);
    frame.getViewer().addRenderable(mesh0);
    frame.getViewer().addRenderable(mesh1);
    frame.getViewer().addRenderable(new IsRenderable() {

        public int getRenderHints() {
            // TODO Auto-generated method stub
            return 0;
        }

        public void prerender(RenderList list) {
        }

        public void render(Renderer renderer, int flags) {
            renderer.setShading(Shading.NONE);
            if (info != null) {
                renderer.setColor(0, 0, 1);
                renderer.setPointSize(6);
                ArrayList<TriTriIntersection> intersections = info.getIntersections();
                if (intersections != null) {
                    renderer.beginDraw(DrawMode.POINTS);
                    for (TriTriIntersection isect : intersections) {
                        for (Point3d p : isect.points) {
                            renderer.addVertex(p);
                        }
                    }
                    renderer.endDraw();
                }
                renderer.setColor(1, 0, 0);
                renderer.beginDraw(DrawMode.LINES);
                for (ContactPlane region : info.getContactPlanes()) {
                    Point3d avg = new Point3d();
                    int np = 0;
                    for (Point3d rp : region.points) {
                        avg.add(rp);
                        np++;
                    }
                    avg.scale(1.0 / np);
                    renderer.addVertex(avg);
                    avg.add(region.normal);
                    renderer.addVertex(avg);
                }
                renderer.endDraw();
            }
            ;
            // mesh0.getObbtree().render(renderer);
            // mesh1.getObbtree().render(renderer);
            // ////////////////////////////
            // draw mesh numbers
            Vector3d avg0 = new Vector3d();
            Vector3d avg1 = new Vector3d();
            for (Vertex3d v : mesh0.getVertices()) avg0.add(v.pnt);
            avg0.scale(1.0 / mesh0.getVertices().size());
            avg0.add(mesh0.getMeshToWorld().p);
            for (Vertex3d v : mesh1.getVertices()) avg1.add(v.pnt);
            avg1.scale(1.0 / mesh1.getVertices().size());
            avg1.add(mesh1.getMeshToWorld().p);
            // GLUT glut = new GLUT();
            renderer.setColor(1, 1, 1);
            // gl.glRasterPos3d (avg0.x, avg0.y, avg0.z);
            // glut.glutBitmapString (GLUT.BITMAP_HELVETICA_18, "0");
            // gl.glRasterPos3d (avg1.x, avg1.y, avg1.z);
            // glut.glutBitmapString (GLUT.BITMAP_HELVETICA_18, "1");
            // draw mesh normals
            // //////////////////////////////
            renderer.setShading(Shading.FLAT);
        }

        public void updateBounds(Vector3d pmin, Vector3d pmax) {
        // TODO Auto-generated method stub
        }
    });
    frame.getViewer().rerender();
    frame.getViewer().autoFit();
    frame.setVisible(true);
}
Also used : Vertex3d(maspack.geometry.Vertex3d) ArrayList(java.util.ArrayList) TriTriIntersection(maspack.geometry.TriTriIntersection) PolygonalMesh(maspack.geometry.PolygonalMesh) RenderList(maspack.render.RenderList) IsRenderable(maspack.render.IsRenderable) Vector3d(maspack.matrix.Vector3d) GLViewerFrame(maspack.render.GL.GLViewerFrame) Point3d(maspack.matrix.Point3d) Renderer(maspack.render.Renderer)

Aggregations

Renderer (maspack.render.Renderer)11 Vector3d (maspack.matrix.Vector3d)8 RenderList (maspack.render.RenderList)7 SimpleSelectable (maspack.test.GL.MultiViewer.SimpleSelectable)6 LinkedList (java.util.LinkedList)4 RenderProps (maspack.render.RenderProps)4 Point3d (maspack.matrix.Point3d)3 RigidTransform3d (maspack.matrix.RigidTransform3d)3 Rectangle2D (java.awt.geom.Rectangle2D)2 JFrame (javax.swing.JFrame)2 PolygonalMesh (maspack.geometry.PolygonalMesh)2 BumpMapProps (maspack.render.BumpMapProps)2 ColorMapProps (maspack.render.ColorMapProps)2 IsRenderable (maspack.render.IsRenderable)2 NormalMapProps (maspack.render.NormalMapProps)2 RenderObject (maspack.render.RenderObject)2 Shading (maspack.render.Renderer.Shading)2 GL3 (com.jogamp.opengl.GL3)1 Color (java.awt.Color)1 Font (java.awt.Font)1