World Wind Forums

Go Back   World Wind Forums > WorldWind JAVA forums > Development Help

Development Help Help for building applications or diagnosing problems with WWJ

Reply
 
Thread Tools Display Modes
Old 04-05-2012, 03:18 PM   #1
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default Transparency Issue

Hi All,
I am drawing triangles using openGL commands since I need a gradient color across the surface of the triangle,and to my knowledge this cannot be done with predefined polygons of world wind,
the problem is when those triangles are set to be transparent(using alpha channel) only the ground are visible through them but any other renderable object like ExtrudedPolygon for example are just hidden when the are looked at through my triangles
I am attaching an image illustrating the problem
mostafa_fahim is offline   Reply With Quote
Old 04-05-2012, 03:21 PM   #2
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default The code used

and here is the code of the renderable class used to draw the triangle grid,the constructor takes a list of positions of the grid,and an array (triangles) which indicate for every triangle the indices of its vertices in the position list , and then takes an array of colors which contains 4 values for every triangle vertex.
So can anybody help why the transparency is not working as expected??
Code:
import com.sun.opengl.util.BufferUtil;
import gov.nasa.worldwind.geom.Intersection;
import gov.nasa.worldwind.geom.Line;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.terrain.Terrain;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.OGLStackHandler;
import gov.nasa.worldwind.util.OGLUtil;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import javax.media.opengl.GL;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class TriangleGrid extends AbstractShape {

    List<Position> vertices;
    int[] triangles;
    public float[] colors;
    static int counter = 0;
    static FloatBuffer vertexArray;
    static IntBuffer indices;
    public boolean change;

    public TriangleGrid(List<Position> vertices, int[] triangles, float[] colors) {
        this.vertices = new ArrayList<Position>();
        this.vertices = vertices;
        this.triangles = triangles;
        this.colors = colors;
        change = false;

    }

    @Override
    public void render(DrawContext dc) {

        if (dc == null) {
            String msg = Logging.getMessage("nullValue.DrawContextIsNull");
            Logging.logger().severe(msg);
            throw new IllegalArgumentException(msg);
        }

        javax.media.opengl.GL gl = dc.getGL();
        OGLStackHandler ogsh = new OGLStackHandler();
        ogsh.pushAttrib(gl, GL.GL_CLIENT_VERTEX_ARRAY_BIT | GL.GL_CURRENT_BIT | GL.GL_ENABLE_BIT | GL.GL_TRANSFORM_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT);

        try {
            gl.glEnable(GL.GL_POLYGON_STIPPLE);
            gl.glEnable(GL.GL_BLEND);
            gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

            gl.glEnable(GL.GL_DEPTH_TEST);
            gl.glDepthFunc(GL.GL_LESS);
            OGLUtil.applyBlending(gl, true);
            float[] points = new float[this.vertices.size() * 3];
            Globe globe = dc.getGlobe();
            int j = 0;
            for (int i = 0; i < this.vertices.size(); i++) {
                points[j] = ((float) globe.computePointFromPosition(this.vertices.get(i)).x);
                j++;
                points[j] = ((float) globe.computePointFromPosition(this.vertices.get(i)).y);
                j++;
                points[j] = ((float) globe.computePointFromPosition(this.vertices.get(i)).z);
                j++;
            }
            vertexArray = BufferUtil.newFloatBuffer(points.length + colors.length);
            for (int i = 0, k = 0; i < points.length; i += 3, k += 4) {
                vertexArray.put(colors[k]);
                vertexArray.put(colors[k + 1]);
                vertexArray.put(colors[k + 2]);
                vertexArray.put(colors[k + 3]);
                vertexArray.put(points[i]);
                vertexArray.put(points[i + 1]);
                vertexArray.put((points[i + 2]));

            }
            vertexArray.rewind();
            indices = BufferUtil.newIntBuffer(this.triangles.length);
            for (int i = 0; i < triangles.length; i++) {
                indices.put(triangles[i]);
            }
            indices.rewind();
            int[] VBO = new int[1];
            gl.glGenBuffersARB(1, VBO, 0);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glPushMatrix();

            gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
            // Enable same as for vertex buffers.
            gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL.GL_COLOR_ARRAY);

            // Init VBOs and transfer data.
            gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBO[0]);
            // Copy data to the server into the VBO.
            gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB,
                    vertexArray.capacity() * BufferUtil.SIZEOF_FLOAT, vertexArray,
                    GL.GL_DYNAMIC_DRAW);
            // Draw.
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER_ARB, VBO[0]);
            gl.glColor4f(1, 1, 1, (float) 0.4);
            gl.glVertexPointer(3, GL.GL_FLOAT, 7 * BufferUtil.SIZEOF_FLOAT, 4 * BufferUtil.SIZEOF_FLOAT);
            gl.glColorPointer(4, GL.GL_FLOAT, 7 * BufferUtil.SIZEOF_FLOAT, 0);
            gl.glDrawElements(GL.GL_TRIANGLES, this.triangles.length, GL.GL_UNSIGNED_INT, indices);
            gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, 0);
            gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL.GL_COLOR_ARRAY);
            gl.glDisable(GL.GL_BLEND);
            gl.glDisable(GL.GL_DEPTH_TEST);
            gl.glPopMatrix();
            gl.glPopAttrib();
        } finally {

            ogsh.pop(gl);

        }


    }

    @Override
    public List<Intersection> intersect(Line line, Terrain terrain) throws InterruptedException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void initialize() {
    }

    @Override
    protected boolean mustApplyTexture(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected boolean doMakeOrderedRenderable(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected boolean isOrderedRenderableValid(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void doDrawOutline(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void doDrawInterior(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void fillVBO(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void doExportAsKML(XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected AbstractShape.AbstractShapeData createCacheEntry(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Position getReferencePosition() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void moveTo(Position position) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Sector getSector() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
mostafa_fahim is offline   Reply With Quote
Old 04-06-2012, 01:21 AM   #3
tag
WWJ Technical Manager
 
Join Date: May 2007
Location: Seattle
Posts: 1,041
tag is on a distinguished road
Default

To achieve transparency, World Wind first sorts the shapes it renders and draws them back to front. Notice the pattern in the subclasses of AbstractShape (as an example -- all non-surface shapes work the same). The render method only causes doMakeOrderedRenderable to be called, at which time the shape needs to add an OrderedRenderable to the draw context's ordered renderable queue. Nothing is actually drawn by the render method (of non-surface shapes). The ordered renderable will be called to render in sort order, relative to its distance from the eye point. The best thing you can do is study one of the simpler subclasses of AbstractShape to discover the pattern.
tag is offline   Reply With Quote
Old 04-11-2012, 11:15 AM   #4
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default

really thank you I read the code of Polygon.java and understood the pattern and it worked ,
but I have now a new issue ,some times the transparent object just blocks the objects behind it and doesn't show them but it does when it is picked and moved around a little bit , this specially happens when the two objects are close to each other.
can you please help me solve this issue??
mostafa_fahim is offline   Reply With Quote
Old 04-11-2012, 04:35 PM   #5
tag
WWJ Technical Manager
 
Join Date: May 2007
Location: Seattle
Posts: 1,041
tag is on a distinguished road
Default

This is almost certainly caused by the more forward object being drawn first, before the more rearward. That order is governed by the respective eye distances. Try to be as accurate as you can computing them, ensuring that the eye distance reflects the most forward position of the shapes. Some WW shapes do not do this, and use only their center position.
tag is offline   Reply With Quote
Old 04-11-2012, 04:45 PM   #6
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default

Aha, I got that. what I do now is that I call computeEyeDistance() in every call to the render function ,also I zoom to the area where I am drawing my objects before drawing them.
Doing so makes things render perfectly but I think this is too much to do in every call to the render function , can you please suggest a way to recompute eye distance for all rendered objects just when needed not with every render call??
mostafa_fahim is offline   Reply With Quote
Old 04-11-2012, 05:01 PM   #7
tag
WWJ Technical Manager
 
Join Date: May 2007
Location: Seattle
Posts: 1,041
tag is on a distinguished road
Default

For eye distance to be accurate you need to update it whenever the view changes. There is an automatic system within AbstractShape to cache computed values for a brief time so that they don't have to be computed each frame. See for example the use of mustRegenerateGeometry in the Polygon AbstractShape. But simply computing the eye distance isn't a very expensive computation.
tag is offline   Reply With Quote
Old 04-13-2012, 09:24 AM   #8
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default

Ok , this is great every thing now working pretty fast and as expected ,but another issue has raised.
when I try to enable lighting ,my drawn objects just flicker , (I enable lighting by computing normals then using gl.glEnable(GL.GL_LIGHTING) in doDrawInterior() method)
I can not use the enableLighting in shape attributes since I am drawing by assigning different color to every vertex but shape attributes will color the whole object the same(as I think).
so can you please help me to enable lighting in my case ??
mostafa_fahim is offline   Reply With Quote
Old 04-13-2012, 11:30 AM   #9
mostafa_fahim
Junior Member
 
Join Date: Mar 2012
Posts: 22
mostafa_fahim is on a distinguished road
Default

aha, forgot also to say that the color is not just flickering but also it loses its transparency after enabling light.
Also, flickering color has nothing to do with the color of the object being drawn but with the color of the light source, pretty weird I think but this what happens
mostafa_fahim is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
World Wind RC2 bull Announcements & News 15 12-29-2006 01:14 AM
NASA World Wind 1.3.3.1 Released bull Announcements & News 0 10-31-2005 06:52 PM
World Wind 1.3.3 released Randy Kim Announcements & News 0 10-22-2005 05:37 AM
World Wind 1.3.2 released Beansprout Announcements & News 0 09-07-2005 11:51 PM
1.3.2 beta 1 released Jessi Developers' Corner 11 07-28-2005 06:30 PM


All times are GMT +1. The time now is 01:24 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.