剪裁球体

时间:2015-04-29 06:37:35

标签: opengl 3d

这里问了一个问题: Drawing a portion of a hemisphere airspace

可能它更多地涉及到GIS,因此已经转向OpenGL中更基本和特定的实现以获得所需的输出。

我简单地覆盖/复制了绘制半球并将GL部分改为基本INSERT裁剪的函数。我能够以一个位置(纬度,经度)绘制半球,半径可能是2000.但是当我用飞机切割它时没有任何反应。请检查飞机的方程(它与地球表面平行的高度可能是1000,所以(0,0,+ 1,1000))

基类有drawUnitSphere,因此可能会导致一些问题,因此尝试使用GL的gluSphere()。但是,甚至无法看到地球上的球体。使用翻译将其转移到我的位置(lat / lon),但仍然无法看到它。可能是lat / lon和笛卡尔坐标或放置剪辑代码的一些问题。请检查。

以下是代码:

@Override
public void drawSphere(DrawContext dc)
{
    double[] altitudes = this.getAltitudes(dc.getVerticalExaggeration());
    boolean[] terrainConformant = this.isTerrainConforming();
    int subdivisions = this.getSubdivisions();

    if (this.isEnableLevelOfDetail())
    {
        DetailLevel level = this.computeDetailLevel(dc);

        Object o = level.getValue(SUBDIVISIONS);
        if (o != null && o instanceof Integer)
            subdivisions = (Integer) o;            
    }

    Vec4 centerPoint = this.computePointFromPosition(dc,
        this.location.getLatitude(), this.location.getLongitude(), altitudes[0], terrainConformant[0]);

    Matrix modelview = dc.getView().getModelviewMatrix();
    modelview = modelview.multiply(Matrix.fromTranslation(centerPoint));
    modelview = modelview.multiply(Matrix.fromScale(this.getRadius()));
    double[] matrixArray = new double[16];
    modelview.toArray(matrixArray, 0, false);

    this.setExpiryTime(-1L); // Sphere geometry never expires.

    GL gl = dc.getGL(); // GL initialization checks for GL2 compatibility.
    gl.glPushAttrib(GL.GL_POLYGON_BIT | GL.GL_TRANSFORM_BIT);
    try
    {

        gl.glEnable(GL.GL_CULL_FACE);
        gl.glFrontFace(GL.GL_CCW);

        // Were applying a scale transform on the modelview matrix, so the normal vectors must be re-normalized
        // before lighting is computed. In this case we're scaling by a constant factor, so GL_RESCALE_NORMAL
        // is sufficient and potentially less expensive than GL_NORMALIZE (or computing unique normal vectors
        // for each value of radius). GL_RESCALE_NORMAL was introduced in OpenGL version 1.2.
        gl.glEnable(GL.GL_RESCALE_NORMAL);

        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glPushMatrix();

        //clipping
        DoubleBuffer eqn1 = BufferUtils.createDoubleBuffer(8).put(new double[] {0, 0, 1, 100});
        eqn1.flip();
        gl.glClipPlane(GL.GL_CLIP_PLANE0, eqn1);
        gl.glEnable(GL.GL_CLIP_PLANE0);
        try
        {
            gl.glLoadMatrixd(matrixArray, 0);
            //this.drawUnitSphere(dc, subdivisions);
            gl.glLoadIdentity();
            gl.glTranslatef(75.2f, 32.5f, 0.0f);
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            GLU glu = dc.getGLU();
            GLUquadric qd=glu.gluNewQuadric();
            glu.gluSphere(qd,3.0f,20,20);
        }
        finally
        {
            gl.glPopMatrix();
        }
    }
    finally
    {
        gl.glPopAttrib();
    }
}

@Override
public void drawUnitSphere(DrawContext dc, int subdivisions)
{
    Object cacheKey = new Geometry.CacheKey(this.getClass(), "Sphere", subdivisions);
    Geometry geom = (Geometry) this.getGeometryCache().getObject(cacheKey);
    if (geom == null || this.isExpired(dc, geom))
    {
        if (geom == null)
            geom = new Geometry();
        this.makeSphere(1.0, subdivisions, geom);
        this.updateExpiryCriteria(dc, geom);
        this.getGeometryCache().add(cacheKey, geom);
    }

    this.getRenderer().drawGeometry(dc, geom);
}

@Override
public void makeSphere(double radius, int subdivisions, Geometry dest)
{
    GeometryBuilder gb = this.getGeometryBuilder();
    gb.setOrientation(GeometryBuilder.OUTSIDE);

    GeometryBuilder.IndexedTriangleArray ita = gb.tessellateSphere((float) radius, subdivisions);
    float[] normalArray = new float[3 * ita.getVertexCount()];
    gb.makeIndexedTriangleArrayNormals(ita, normalArray);

    dest.setElementData(GL.GL_TRIANGLES, ita.getIndexCount(), ita.getIndices());
    dest.setVertexData(ita.getVertexCount(), ita.getVertices());
    dest.setNormalData(ita.getVertexCount(), normalArray);
}

0 个答案:

没有答案