aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/ThreadingImpl.java
blob: 7b405e5244b1920d18924626f74cba71fab3733b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (c) 2009 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (c) 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 */

package jogamp.opengl;

import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.media.nativewindow.NativeWindowFactory;
import javax.media.opengl.GLException;
import javax.media.opengl.GLProfile;
import javax.media.opengl.Threading.Mode;

import com.jogamp.common.JogampRuntimeException;
import com.jogamp.common.util.PropertyAccess;
import com.jogamp.common.util.ReflectionUtil;

/** Implementation of the {@link javax.media.opengl.Threading} class. */

public class ThreadingImpl {
    protected static final boolean DEBUG = Debug.debug("Threading");

    private static boolean singleThreaded;
    private static Mode mode;
    private static boolean hasAWT;
    // We need to know whether we're running on X11 platforms to change
    // our behavior when the Java2D/JOGL bridge is active
    private static boolean _isX11;

    private static final ToolkitThreadingPlugin threadingPlugin;

    static {
        threadingPlugin =
            AccessController.doPrivileged(new PrivilegedAction<ToolkitThreadingPlugin>() {
                    @Override
                    public ToolkitThreadingPlugin run() {
                        final String singleThreadProp;
                        {
                            final String w = PropertyAccess.getProperty("jogl.1thread", true);
                            singleThreadProp = null != w ? w.toLowerCase() : null;
                        }
                        final ClassLoader cl = ThreadingImpl.class.getClassLoader();
                        // Default to using the AWT thread on all platforms except
                        // Windows. On OS X there is instability apparently due to
                        // using the JAWT on non-AWT threads. On X11 platforms there
                        // are potential deadlocks which can be caused if the AWT
                        // EventQueue thread hands work off to the GLWorkerThread
                        // while holding the AWT lock. The optimization of
                        // makeCurrent / release calls isn't worth these stability
                        // problems.
                        hasAWT = GLProfile.isAWTAvailable();

                        _isX11 = NativeWindowFactory.TYPE_X11 == NativeWindowFactory.getNativeWindowType(false);

                        if (singleThreadProp != null) {
                            if (singleThreadProp.equals("true") ||
                                singleThreadProp.equals("auto")) {
                                mode  = ( hasAWT ? Mode.ST_AWT : Mode.MT );
                            } else if (singleThreadProp.equals("worker")) {
                                mode = Mode.ST_WORKER;
                            } else if (hasAWT && singleThreadProp.equals("awt")) {
                                mode = Mode.ST_AWT;
                            } else if (singleThreadProp.equals("false")) {
                                mode = Mode.MT;
                            } else {
                                throw new RuntimeException("Unsupported value for property jogl.1thread: "+singleThreadProp+", should be [true/auto, worker, awt or false]");
                            }
                        } else {
                            mode  = ( hasAWT ? Mode.ST_AWT : Mode.MT );
                        }
                        singleThreaded = Mode.MT != mode;

                        ToolkitThreadingPlugin threadingPlugin=null;
                        if(hasAWT) {
                            // try to fetch the AWTThreadingPlugin
                            Exception error=null;
                            try {
                                threadingPlugin = (ToolkitThreadingPlugin) ReflectionUtil.createInstance("jogamp.opengl.awt.AWTThreadingPlugin", cl);
                            } catch (final JogampRuntimeException jre) { error = jre; }
                            if( Mode.ST_AWT == mode && null==threadingPlugin ) {
                                throw new GLException("Mode is AWT, but class 'jogamp.opengl.awt.AWTThreadingPlugin' is not available", error);
                            }
                        }
                        if(DEBUG) {
                            System.err.println("Threading: jogl.1thread "+singleThreadProp+", singleThreaded "+singleThreaded+", hasAWT "+hasAWT+", mode "+mode+", plugin "+threadingPlugin);
                        }
                        return threadingPlugin;
                    }
                });
    }

    /** No reason to ever instantiate this class */
    private ThreadingImpl() {}

    public static boolean isX11() { return _isX11; }
    public static Mode getMode() { return mode; }

    /** If an implementation of the javax.media.opengl APIs offers a
        multithreading option but the default behavior is single-threading,
        this API provides a mechanism for end users to disable single-threading
        in this implementation.  Users are strongly discouraged from
        calling this method unless they are aware of all of the
        consequences and are prepared to enforce some amount of
        threading restrictions in their applications. Disabling
        single-threading, for example, may have unintended consequences
        on GLAutoDrawable implementations such as GLCanvas, GLJPanel and
        GLPbuffer. Currently there is no supported way to re-enable it
        once disabled, partly to discourage careless use of this
        method. This method should be called as early as possible in an
        application. */
    public static final void disableSingleThreading() {
        if( Mode.MT != mode ) {
            singleThreaded = false;
            if (Debug.verbose()) {
                System.err.println("Application forced disabling of single-threading of javax.media.opengl implementation");
            }
        }
    }

    /** Indicates whether OpenGL work is being automatically forced to a
        single thread in this implementation. */
    public static final boolean isSingleThreaded() {
        return singleThreaded;
    }

    /**
     * Indicates whether the current thread is capable of
     * performing OpenGL-related work.
     * <p>
     * Method always returns <code>true</code>
     * if {@link #getMode()} == {@link Mode#MT} or {@link #isSingleThreaded()} == <code>false</code>.
     * </p>
     */
    public static final boolean isOpenGLThread() throws GLException {
        if( Mode.MT == mode || !singleThreaded ) {
            return true;
        } else if( null != threadingPlugin ) {
            return threadingPlugin.isOpenGLThread();
        } else {
            switch (mode) {
                case ST_AWT:
                    throw new InternalError();
                case ST_WORKER:
                    return GLWorkerThread.isWorkerThread();
                default:
                    throw new InternalError("Illegal single-threading mode " + mode);
            }
        }
    }

    public static final boolean isToolkitThread() throws GLException {
        if(null!=threadingPlugin) {
            return threadingPlugin.isToolkitThread();
        }
        return false;
    }

    /** Executes the passed Runnable on the single thread used for all
        OpenGL work in this javax.media.opengl API implementation. It is
        not specified exactly which thread is used for this
        purpose. This method should only be called if the single-thread
        model is in use and if the current thread is not the OpenGL
        thread (i.e., if <code>isOpenGLThread()</code> returns
        false). It is up to the end user to check to see whether the
        current thread is the OpenGL thread and either execute the
        Runnable directly or perform the work inside it. */
    public static final void invokeOnOpenGLThread(final boolean wait, final Runnable r) throws GLException {
        if(null!=threadingPlugin) {
            threadingPlugin.invokeOnOpenGLThread(wait, r);
            return;
        }

        switch (mode) {
            case ST_WORKER:
                invokeOnWorkerThread(wait, r);
                break;

            case MT:
                r.run();
                break;

            default:
                throw new InternalError("Illegal single-threading mode " + mode);
        }
    }

    public static final void invokeOnWorkerThread(final boolean wait, final Runnable r) throws GLException {
        GLWorkerThread.start(); // singleton start via volatile-dbl-checked-locking
        try {
            GLWorkerThread.invoke(wait, r);
        } catch (final InvocationTargetException e) {
            throw new GLException(e.getTargetException());
        } catch (final InterruptedException e) {
            throw new GLException(e);
        }
    }
}
>(e); } } } public static class Frame implements Cloneable { public PositionNormal[] pn; // [pn_index] public Plane[] triplane; // [tri_num] public Object clone() { Frame res = new Frame(); res.pn = new PositionNormal[pn.length]; for (int i = 0; i < pn.length; i++) { res.pn[i] = (PositionNormal) pn[i].clone(); } res.triplane = new Plane[triplane.length]; for (int i = 0; i < triplane.length; i++) { res.triplane[i] = (Plane) triplane[i].clone(); } return res; } } public static class Model { public Frame[] f; public Triangle[] tri; // [tri_num] public WingedEdge[] edge; // [edge_num] } public static void computePlane(PositionNormal a, PositionNormal b, PositionNormal c, Plane p) { float[] v0 = new float[3]; v0[0] = b.x - a.x; v0[1] = b.y - a.y; v0[2] = b.z - a.z; float[] v1 = new float[3]; v1[0] = c.x - a.x; v1[1] = c.y - a.y; v1[2] = c.z - a.z; float[] cr = new float[3]; cr[0] = v0[1] * v1[2] - v0[2] * v1[1]; cr[1] = v0[2] * v1[0] - v0[0] * v1[2]; cr[2] = v0[0] * v1[1] - v0[1] * v1[0]; float l = (float) Math.sqrt(cr[0] * cr[0] + cr[1] * cr[1] + cr[2] * cr[2]); if (l == 0) { // degenerate triangle p.a = p.b = p.c = p.d = 0; return; } p.a = cr[0] / l; p.b = cr[1] / l; p.c = cr[2] / l; p.d = -(p.a * a.x + p.b * a.y + p.c * a.z); // signed distance of a point on the plane from the origin } //---------------------------------------------------------------------- // Internals only below this point // private static Model computeModel(List/*<IFrame>*/ ifr) throws IOException { if (!compareFrames(ifr)) { throw new IOException("unsuitable model -- frames aren't same"); } Model m = new Model(); m.tri = ((IFrame) ifr.get(0)).tri; m.f = new Frame[ifr.size()]; for (int i = 0; i < ifr.size(); i++) { Frame f = new Frame(); m.f[i] = f; IFrame it = (IFrame) ifr.get(i); f.pn = it.pn; computeFramePlanes(m.tri, f); } computeWingedEdges(m); return m; } private static class IFrame { PositionNormal[] pn; Triangle[] tri; } // normal table lifted from Mark Kilgard's md2bump demo private static final float[] normalTable = new float[] { -0.525731f, 0.000000f, 0.850651f, -0.442863f, 0.238856f, 0.864188f, -0.295242f, 0.000000f, 0.955423f, -0.309017f, 0.500000f, 0.809017f, -0.162460f, 0.262866f, 0.951056f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.850651f, 0.525731f, -0.147621f, 0.716567f, 0.681718f, 0.147621f, 0.716567f, 0.681718f, 0.000000f, 0.525731f, 0.850651f, 0.309017f, 0.500000f, 0.809017f, 0.525731f, 0.000000f, 0.850651f, 0.295242f, 0.000000f, 0.955423f, 0.442863f, 0.238856f, 0.864188f, 0.162460f, 0.262866f, 0.951056f, -0.681718f, 0.147621f, 0.716567f, -0.809017f, 0.309017f, 0.500000f, -0.587785f, 0.425325f, 0.688191f, -0.850651f, 0.525731f, 0.000000f, -0.864188f, 0.442863f, 0.238856f, -0.716567f, 0.681718f, 0.147621f, -0.688191f, 0.587785f, 0.425325f, -0.500000f, 0.809017f, 0.309017f, -0.238856f, 0.864188f, 0.442863f, -0.425325f, 0.688191f, 0.587785f, -0.716567f, 0.681718f, -0.147621f, -0.500000f, 0.809017f, -0.309017f, -0.525731f, 0.850651f, 0.000000f, 0.000000f, 0.850651f, -0.525731f, -0.238856f, 0.864188f, -0.442863f, 0.000000f, 0.955423f, -0.295242f, -0.262866f, 0.951056f, -0.162460f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 0.955423f, 0.295242f, -0.262866f, 0.951056f, 0.162460f, 0.238856f, 0.864188f, 0.442863f, 0.262866f, 0.951056f, 0.162460f, 0.500000f, 0.809017f, 0.309017f, 0.238856f, 0.864188f, -0.442863f, 0.262866f, 0.951056f, -0.162460f, 0.500000f, 0.809017f, -0.309017f, 0.850651f, 0.525731f, 0.000000f, 0.716567f, 0.681718f, 0.147621f, 0.716567f, 0.681718f, -0.147621f, 0.525731f, 0.850651f, 0.000000f, 0.425325f, 0.688191f, 0.587785f, 0.864188f, 0.442863f, 0.238856f, 0.688191f, 0.587785f, 0.425325f, 0.809017f, 0.309017f, 0.500000f, 0.681718f, 0.147621f, 0.716567f, 0.587785f, 0.425325f, 0.688191f, 0.955423f, 0.295242f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 0.951056f, 0.162460f, 0.262866f, 0.850651f, -0.525731f, 0.000000f, 0.955423f, -0.295242f, 0.000000f, 0.864188f, -0.442863f, 0.238856f, 0.951056f, -0.162460f, 0.262866f, 0.809017f, -0.309017f, 0.500000f, 0.681718f, -0.147621f, 0.716567f, 0.850651f, 0.000000f, 0.525731f, 0.864188f, 0.442863f, -0.238856f, 0.809017f, 0.309017f, -0.500000f, 0.951056f, 0.162460f, -0.262866f, 0.525731f, 0.000000f, -0.850651f, 0.681718f, 0.147621f, -0.716567f, 0.681718f, -0.147621f, -0.716567f, 0.850651f, 0.000000f, -0.525731f, 0.809017f, -0.309017f, -0.500000f, 0.864188f, -0.442863f, -0.238856f, 0.951056f, -0.162460f, -0.262866f, 0.147621f, 0.716567f, -0.681718f, 0.309017f, 0.500000f, -0.809017f, 0.425325f, 0.688191f, -0.587785f, 0.442863f, 0.238856f, -0.864188f, 0.587785f, 0.425325f, -0.688191f, 0.688191f, 0.587785f, -0.425325f, -0.147621f, 0.716567f, -0.681718f, -0.309017f, 0.500000f, -0.809017f, 0.000000f, 0.525731f, -0.850651f, -0.525731f, 0.000000f, -0.850651f, -0.442863f, 0.238856f, -0.864188f, -0.295242f, 0.000000f, -0.955423f, -0.162460f, 0.262866f, -0.951056f, 0.000000f, 0.000000f, -1.000000f, 0.295242f, 0.000000f, -0.955423f, 0.162460f, 0.262866f, -0.951056f, -0.442863f, -0.238856f, -0.864188f, -0.309017f, -0.500000f, -0.809017f, -0.162460f, -0.262866f, -0.951056f, 0.000000f, -0.850651f, -0.525731f, -0.147621f, -0.716567f, -0.681718f, 0.147621f, -0.716567f, -0.681718f, 0.000000f, -0.525731f, -0.850651f, 0.309017f, -0.500000f, -0.809017f, 0.442863f, -0.238856f, -0.864188f, 0.162460f, -0.262866f, -0.951056f, 0.238856f, -0.864188f, -0.442863f, 0.500000f, -0.809017f, -0.309017f, 0.425325f, -0.688191f, -0.587785f, 0.716567f, -0.681718f, -0.147621f, 0.688191f, -0.587785f, -0.425325f, 0.587785f, -0.425325f, -0.688191f, 0.000000f, -0.955423f, -0.295242f, 0.000000f, -1.000000f, 0.000000f, 0.262866f, -0.951056f, -0.162460f, 0.000000f, -0.850651f, 0.525731f, 0.000000f, -0.955423f, 0.295242f, 0.238856f, -0.864188f, 0.442863f, 0.262866f, -0.951056f, 0.162460f, 0.500000f, -0.809017f, 0.309017f, 0.716567f, -0.681718f, 0.147621f, 0.525731f, -0.850651f, 0.000000f, -0.238856f, -0.864188f, -0.442863f, -0.500000f, -0.809017f, -0.309017f, -0.262866f, -0.951056f, -0.162460f, -0.850651f, -0.525731f, 0.000000f, -0.716567f, -0.681718f, -0.147621f, -0.716567f, -0.681718f, 0.147621f, -0.525731f, -0.850651f, 0.000000f, -0.500000f, -0.809017f, 0.309017f, -0.238856f, -0.864188f, 0.442863f, -0.262866f, -0.951056f, 0.162460f, -0.864188f, -0.442863f, 0.238856f, -0.809017f, -0.309017f, 0.500000f, -0.688191f, -0.587785f, 0.425325f, -0.681718f, -0.147621f, 0.716567f, -0.442863f, -0.238856f, 0.864188f, -0.587785f, -0.425325f, 0.688191f, -0.309017f, -0.500000f, 0.809017f, -0.147621f, -0.716567f, 0.681718f, -0.425325f, -0.688191f, 0.587785f, -0.162460f, -0.262866f, 0.951056f, 0.442863f, -0.238856f, 0.864188f, 0.162460f, -0.262866f, 0.951056f, 0.309017f, -0.500000f, 0.809017f, 0.147621f, -0.716567f, 0.681718f, 0.000000f, -0.525731f, 0.850651f, 0.425325f, -0.688191f, 0.587785f, 0.587785f, -0.425325f, 0.688191f, 0.688191f, -0.587785f, 0.425325f, -0.955423f, 0.295242f, 0.000000f, -0.951056f, 0.162460f, 0.262866f, -1.000000f, 0.000000f, 0.000000f, -0.850651f, 0.000000f, 0.525731f, -0.955423f, -0.295242f, 0.000000f, -0.951056f, -0.162460f, 0.262866f, -0.864188f, 0.442863f, -0.238856f, -0.951056f, 0.162460f, -0.262866f, -0.809017f, 0.309017f, -0.500000f, -0.864188f, -0.442863f, -0.238856f, -0.951056f, -0.162460f, -0.262866f, -0.809017f, -0.309017f, -0.500000f, -0.681718f, 0.147621f, -0.716567f, -0.681718f, -0.147621f, -0.716567f, -0.850651f, 0.000000f, -0.525731f, -0.688191f, 0.587785f, -0.425325f, -0.587785f, 0.425325f, -0.688191f, -0.425325f, 0.688191f, -0.587785f, -0.425325f, -0.688191f, -0.587785f, -0.587785f, -0.425325f, -0.688191f, -0.688191f, -0.587785f, -0.425325f }; private static void loadFrames(String filename, List/*<IFrame>*/ md2p) throws IOException { FileModel mf = loadMD2File(filename); computeFrames(mf, md2p); } private static void loadFrames(InputStream in, List/*<IFrame>*/ md2p) throws IOException { FileModel mf = loadMD2File(in); computeFrames(mf, md2p); } private static void computeFrames(FileModel mf, List/*<IFrame>*/ md2p) throws IOException { for (int i = 0; i < mf.frames.length; i++) { IFrame f = new IFrame(); md2p.add(f); FileFrame curframe = mf.frames[i]; f.pn = new PositionNormal[curframe.verts.length]; for (int j = 0; j < curframe.verts.length; j++) { PositionNormal pn = new PositionNormal(); pn.x = (((curframe.verts[j].v[0] & 0xFF) * curframe.scale[0]) + curframe.translate[0]) * .025f; pn.y = (((curframe.verts[j].v[1] & 0xFF) * curframe.scale[1]) + curframe.translate[1]) * .025f; pn.z = (((curframe.verts[j].v[2] & 0xFF) * curframe.scale[2]) + curframe.translate[2]) * .025f; int normal_index = curframe.verts[j].lightnormalindex & 0xFF; pn.nx = normalTable[3 * normal_index + 0]; pn.ny = normalTable[3 * normal_index + 1]; pn.nz = normalTable[3 * normal_index + 2]; f.pn[j] = pn; } List/*<Triangle>*/ tris = new ArrayList(); int[] idx = new int[1]; while (mf.glcmds[idx[0]] != 0) { int vertnum; boolean is_strip; if (mf.glcmds[idx[0]] > 0) { vertnum = mf.glcmds[idx[0]++]; is_strip = true; // triangle strip } else { vertnum = -mf.glcmds[idx[0]++]; is_strip = false; // triangle fan } if (is_strip) { Vertex[] prev = new Vertex[2]; prev[0] = extractVertex(mf.glcmds, idx); prev[1] = extractVertex(mf.glcmds, idx); for (int j = 2; j < vertnum; j++) { Triangle tri = new Triangle(); if ((j % 2) == 0) { tri.v[0] = prev[0]; tri.v[1] = prev[1]; tri.v[2] = extractVertex(mf.glcmds, idx); prev[0] = tri.v[2]; } else { tri.v[0] = prev[1]; tri.v[1] = extractVertex(mf.glcmds, idx); tri.v[2] = prev[0]; prev[1] = tri.v[1]; } // swap v[1] and v[2] to fix triangle winding Vertex hold = tri.v[1]; tri.v[1] = tri.v[2]; tri.v[2] = hold; tris.add(tri); } } else { // is fan Vertex ctr = extractVertex(mf.glcmds, idx); Vertex prev = extractVertex(mf.glcmds, idx); for (int j = 2; j < vertnum; j++) { Triangle tri = new Triangle(); tri.v[0] = ctr; tri.v[1] = prev; tri.v[2] = extractVertex(mf.glcmds, idx); prev = tri.v[2]; // swap v[1] and v[2] to fix triangle winding Vertex hold = tri.v[1]; tri.v[1] = tri.v[2]; tri.v[2] = hold; tris.add(tri); } } } f.tri = (Triangle[]) tris.toArray(new Triangle[0]); } } private static FileModel loadMD2File(ByteBuffer buf) throws IOException { buf.order(ByteOrder.LITTLE_ENDIAN); FileModel md2p = new FileModel(); FileHeader header = readHeader(buf); buf.position(header.ofs_frames); readFrames(buf, header, md2p); buf.position(header.ofs_glcmds); readGLCommands(buf, header, md2p); return md2p; } private static FileModel loadMD2File(InputStream in) throws IOException { in = new BufferedInputStream(in); int avail = in.available(); byte[] data = new byte[avail]; int numRead = 0; int pos = 0; do { if (pos + avail > data.length) { byte[] newData = new byte[pos + avail]; System.arraycopy(data, 0, newData, 0, pos); data = newData; } numRead = in.read(data, pos, avail); if (numRead >= 0) { pos += numRead; } avail = in.available(); } while (avail > 0 && numRead >= 0); ByteBuffer buf = ByteBuffer.allocateDirect(pos); buf.put(data, 0, pos); buf.rewind(); return loadMD2File(buf); } private static FileModel loadMD2File(String filename) throws IOException { FileInputStream fis = new FileInputStream(filename); FileChannel chan = fis.getChannel(); ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, fis.available()); FileModel md2p = loadMD2File(buf); chan.close(); fis.close(); return md2p; } private static FileHeader readHeader(ByteBuffer buf) { FileHeader header = new FileHeader(); header.ident = buf.getInt(); header.version = buf.getInt(); header.skinwidth = buf.getInt(); header.skinheight = buf.getInt(); header.framesize = buf.getInt(); header.num_skins = buf.getInt(); header.num_xyz = buf.getInt(); header.num_st = buf.getInt(); header.num_tris = buf.getInt(); header.num_glcmds = buf.getInt(); header.num_frames = buf.getInt(); header.ofs_skins = buf.getInt(); header.ofs_st = buf.getInt(); header.ofs_tris = buf.getInt(); header.ofs_frames = buf.getInt(); header.ofs_glcmds = buf.getInt(); header.ofs_end = buf.getInt(); return header; } private static int numVerts(int framesize) { return (framesize >> 2) - 10; } private static void readFrames(ByteBuffer buf, FileHeader header, FileModel md2p) throws IOException { int numframes = header.num_frames; int framesize = header.framesize; int numVerts = numVerts(framesize); FileFrame[] frames = new FileFrame[numframes]; byte[] name = new byte[16]; for (int i = 0; i < numframes; i++) { FileFrame frame = new FileFrame(); frame.scale[0] = buf.getFloat(); frame.scale[1] = buf.getFloat(); frame.scale[2] = buf.getFloat(); frame.translate[0] = buf.getFloat(); frame.translate[1] = buf.getFloat(); frame.translate[2] = buf.getFloat(); buf.get(name); try { frame.name = new String(name, "US-ASCII"); } catch (UnsupportedEncodingException e) { throw new IOException(e.toString()); } frame.verts = new FileCompressedVertex[numVerts]; for (int j = 0; j < numVerts; j++) { FileCompressedVertex vert = new FileCompressedVertex(); buf.get(vert.v); vert.lightnormalindex = buf.get(); frame.verts[j] = vert; } frames[i] = frame; } md2p.frames = frames; } private static void readGLCommands(ByteBuffer buf, FileHeader header, FileModel md2p) { int num_glcmds = header.num_glcmds; int[] glcmds = new int[num_glcmds]; for (int i = 0; i < num_glcmds; i++) { glcmds[i] = buf.getInt(); } md2p.glcmds = glcmds; } private static Vertex extractVertex(int[] glcmds, int[] idx) { Vertex v = new Vertex(); v.tc.s = Float.intBitsToFloat(glcmds[idx[0]++]); v.tc.t = Float.intBitsToFloat(glcmds[idx[0]++]); v.pn_index = glcmds[idx[0]++]; return v; } private static boolean compareFrames(List/*<IFrame>*/ m) { IFrame f0 = (IFrame) m.get(0); boolean same_topology = true; boolean same_texcoords = true; for (int i = 1; i < m.size(); i++) { IFrame f = (IFrame) m.get(i); if (f.pn.length != f0.pn.length) { System.err.println("pn size different for iframe " + i + " : " + f0.pn.length + " != " + f.pn.length); same_topology = false; } if (f.tri.length != f0.tri.length) { System.err.println("tri size different for iframe " + i + " : " + f0.tri.length + " != " + f.tri.length); same_topology = false; } if (same_topology) { for (int j = 0; j < f.tri.length; j++) { Triangle t0 = f0.tri[j]; Triangle t = f.tri[j]; for (int k = 0; k < 3; k++) { if (t0.v[k].pn_index != t.v[k].pn_index) { System.err.println("tri " + j + " triangle pn_index " + k + " different!"); same_topology = false; } if (t0.v[k].tc.s != t.v[k].tc.s || t0.v[k].tc.t != t.v[k].tc.t) { System.err.println("tri " + j + " triangle tc " + k + " different!"); same_texcoords = false; } } } } } return same_topology && same_texcoords; } /** Computes the plane equations for each polygon of a frame. */ private static void computeFramePlanes(Triangle[] tri, Frame f) { f.triplane = new Plane[tri.length]; for (int i = 0; i < tri.length; i++) { Triangle t = tri[i]; int ia = t.v[0].pn_index; int ib = t.v[1].pn_index; int ic = t.v[2].pn_index; Plane p = new Plane(); computePlane(f.pn[ia], f.pn[ib], f.pn[ic], p); f.triplane[i] = p; } } private static int computeWingedEdges(Model m) { Triangle[] tri = m.tri; List/*<WingedEdge>*/ edge = new ArrayList/*<WingedEdge>*/(); // for each triangle, try to add each edge to the winged_edge vector, // but check first to see if it's already there int tsize = tri.length; for (int i = 0; i < tsize; i++) { Triangle t = tri[i]; for (int j = 0; j < 3; j++) { WingedEdge we = new WingedEdge(); we.e[0] = t.v[ j ].pn_index; we.e[1] = t.v[(j+1)%3].pn_index; we.w[0] = i; we.w[1] = -1; // subsequent attempt to add this edge will replace w[1] addEdge(edge, we); } } int open_edge = 0; for (int i = 0; i < edge.size(); i++) { if (((WingedEdge) edge.get(i)).w[1] == -1) open_edge++; } //fprintf(stderr, "out of % edges, there were %d open edges\n", edge.size(), open_edge); m.edge = (WingedEdge[]) edge.toArray(new WingedEdge[0]); return open_edge; } /** add_edge will look to see if the current edge is already in the list. If it is not, it will add it. If it is, it will replace the w[1] in the existing table with w[0] from the edge being added. */ private static void addEdge(List/*<WingedEdge>*/ edge, WingedEdge we) { int esize = edge.size(); for (int i=0; i < esize; i++) { WingedEdge we0 = (WingedEdge) edge.get(i); if (we0.e[0] == we.e[0] && we0.e[1] == we.e[1]) { System.err.println("facingness different between polys on edge!"); } if(we0.e[0] == we.e[1] && we0.e[1] == we.e[0]) { if(we0.w[1] != -1) { System.err.println("triple edge! bad..."); } we0.w[1] = we.w[0]; // pair the edge and return return; } } edge.add(we); // otherwise, add the new edge } public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { MD2.Model model = loadMD2(args[i]); System.err.println("Successfully parsed " + args[i]); } catch (IOException e) { System.err.println("Error parsing " + args[i] + ":"); e.printStackTrace(); } } } }