package com.seewo.factoryaging.opengl3d.j;

import android.opengl.Matrix;
import android.util.Log;
import com.seewo.factoryaging.opengl3d.h.h;
import com.seewo.factoryaging.opengl3d.i.d;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Iterator;

/* JADX INFO: compiled from: Object3DBuilder.java */
/* JADX INFO: loaded from: classes.dex */
public final class b {
    private static float[] a = {1.0f, 1.0f, 0.0f, 1.0f};
    static final float[] b = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.95f, 0.05f, 0.0f, 1.0f, 0.0f, 0.0f, 0.95f, -0.05f, 0.0f, 1.0f, 0.0f, 0.0f, -0.95f, 0.05f, 0.0f, -1.0f, 0.0f, 0.0f, -0.95f, -0.05f, 0.0f, -1.0f, 0.0f, 0.0f, -0.05f, 0.95f, 0.0f, 0.0f, 1.0f, 0.0f, 0.05f, 0.95f, 0.0f, 0.0f, 1.0f, 0.0f, -0.05f, 0.0f, 0.95f, 0.0f, 0.0f, 1.0f, 0.05f, 0.0f, 0.95f, 0.0f, 0.0f, 1.0f, 1.05f, 0.05f, 0.0f, 1.1f, -0.05f, 0.0f, 1.05f, -0.05f, 0.0f, 1.1f, 0.05f, 0.0f, -0.05f, 1.05f, 0.0f, 0.05f, 1.1f, 0.0f, -0.05f, 1.1f, 0.0f, 0.0f, 1.075f, 0.0f, -0.05f, 0.05f, 1.05f, 0.05f, 0.05f, 1.05f, 0.05f, 0.05f, 1.05f, -0.05f, -0.05f, 1.05f, -0.05f, -0.05f, 1.05f, 0.05f, -0.05f, 1.05f};

    public static c a() {
        c cVar = new c(g(b.length * 4).asFloatBuffer().put(b));
        cVar.P(1);
        cVar.S(new d.c(0));
        return cVar;
    }

    private static void b(com.seewo.factoryaging.opengl3d.h.a aVar, h hVar, float[] fArr, float[] fArr2, int i2, FloatBuffer floatBuffer) {
        float[] fArr3 = new float[4];
        float[] fArr4 = new float[16];
        Matrix.multiplyMM(fArr4, 0, fArr, 0, hVar.e(), 0);
        Matrix.multiplyMV(fArr3, 0, fArr4, 0, new float[]{0.0f, 0.0f, 0.0f, 1.0f}, 0);
        float[] fArrF = com.seewo.factoryaging.opengl3d.k.d.f(fArr3, fArr2);
        float[] fArr5 = {fArr3[0], fArr3[1], fArr3[2] - (Matrix.length(fArrF[0], fArrF[1], fArrF[2]) * 0.05f)};
        float[] fArr6 = {fArr3[0], fArr3[1], fArr3[2] + (Matrix.length(fArrF[0], fArrF[1], fArrF[2]) * 0.05f)};
        float[] fArrC = com.seewo.factoryaging.opengl3d.k.d.c(fArr2, fArr5, fArr6);
        aVar.F().put(fArr2[0]);
        aVar.F().put(fArr2[1]);
        aVar.F().put(fArr2[2]);
        aVar.F().put(fArr5[0]);
        aVar.F().put(fArr5[1]);
        aVar.F().put(fArr5[2]);
        aVar.F().put(fArr6[0]);
        aVar.F().put(fArr6[1]);
        aVar.F().put(fArr6[2]);
        aVar.I().put(fArrC);
        aVar.I().put(fArrC);
        aVar.I().put(fArrC);
        float f = i2;
        aVar.n0().put(f);
        aVar.n0().put(f);
        aVar.n0().put(f);
        for (int i3 = 3; i3 < 9; i3++) {
            aVar.n0().put(hVar.g());
        }
        for (int i4 = 0; i4 < 9; i4 += 3) {
            aVar.p0().put(i2 >= 0 ? 1.0f : 0.0f);
            aVar.p0().put(0.0f);
            aVar.p0().put(0.0f);
        }
        Iterator<h> it = hVar.f().iterator();
        while (it.hasNext()) {
            b(aVar, it.next(), fArr4, fArr3, hVar.g(), floatBuffer);
        }
    }

    public static c c(c cVar) {
        FloatBuffer floatBufferAsFloatBuffer;
        if (cVar.e() != 4) {
            return null;
        }
        FloatBuffer floatBufferF = cVar.F() != null ? cVar.F() : cVar.G();
        if (floatBufferF == null) {
            Log.v("Builder", "Generating face normals for '" + cVar.m() + "' I found that there is no vertex data");
            return null;
        }
        IntBuffer intBufferG = cVar.g();
        if (intBufferG != null) {
            Log.v("Builder", "Generating face normals for '" + cVar.m() + "' using indices...");
            floatBufferAsFloatBuffer = g((intBufferG.capacity() / 3) * 6 * 4).asFloatBuffer();
            intBufferG.position(0);
            for (int i2 = 0; i2 < intBufferG.capacity(); i2 += 3) {
                int i3 = intBufferG.get() * 3;
                int i4 = intBufferG.get() * 3;
                int i5 = intBufferG.get() * 3;
                float[][] fArrB = com.seewo.factoryaging.opengl3d.k.d.b(new float[]{floatBufferF.get(i3), floatBufferF.get(i3 + 1), floatBufferF.get(i3 + 2)}, new float[]{floatBufferF.get(i4), floatBufferF.get(i4 + 1), floatBufferF.get(i4 + 2)}, new float[]{floatBufferF.get(i5), floatBufferF.get(i5 + 1), floatBufferF.get(i5 + 2)});
                floatBufferAsFloatBuffer.put(fArrB[0]).put(fArrB[1]);
            }
        } else {
            if (floatBufferF.capacity() % 9 != 0) {
                Log.v("Builder", "Generating face normals for '" + cVar.m() + "' I found that vertices are not multiple of 9 (3*3): " + floatBufferF.capacity());
                return null;
            }
            Log.v("Builder", "Generating face normals for '" + cVar.m() + "'...");
            floatBufferAsFloatBuffer = g(((floatBufferF.capacity() * 6) / 9) * 4).asFloatBuffer();
            floatBufferF.position(0);
            for (int i6 = 0; i6 < (floatBufferF.capacity() / 3) / 3; i6++) {
                float[][] fArrB2 = com.seewo.factoryaging.opengl3d.k.d.b(new float[]{floatBufferF.get(), floatBufferF.get(), floatBufferF.get()}, new float[]{floatBufferF.get(), floatBufferF.get(), floatBufferF.get()}, new float[]{floatBufferF.get(), floatBufferF.get(), floatBufferF.get()});
                floatBufferAsFloatBuffer.put(fArrB2[0]).put(fArrB2[1]);
                String str = fArrB2[0][0] + "," + fArrB2[0][1] + "," + fArrB2[0][2] + "-" + fArrB2[1][0] + "," + fArrB2[1][1] + "," + fArrB2[1][2];
            }
        }
        c cVar2 = new c(floatBufferAsFloatBuffer);
        cVar2.P(1);
        cVar2.N(cVar.d());
        cVar2.V(cVar.r());
        cVar2.d0(1);
        return cVar2;
    }

    public static c d(float[] fArr) {
        c cVar = new c(g(fArr.length * 4).asFloatBuffer().put(fArr));
        cVar.P(0);
        cVar.T("Point");
        return cVar;
    }

    public static com.seewo.factoryaging.opengl3d.h.a e(com.seewo.factoryaging.opengl3d.h.a aVar) {
        float[] fArr = new float[16];
        Matrix.setIdentityM(fArr, 0);
        com.seewo.factoryaging.opengl3d.h.a aVar2 = new com.seewo.factoryaging.opengl3d.h.a(g(aVar.m0() * 3 * 3 * 4).asFloatBuffer());
        aVar2.h0(g(aVar.m0() * 3 * 3 * 4).asFloatBuffer());
        aVar2.P(4);
        aVar2.r0(aVar.o0().clone(), aVar.m0(), aVar.l0(), true);
        aVar2.q0(g(aVar2.m0() * 3 * 3 * 4).asFloatBuffer());
        aVar2.j0(aVar.k0());
        aVar2.s0(g(aVar2.m0() * 3 * 3 * 4).asFloatBuffer());
        aVar2.V(aVar.r());
        aVar2.Y(aVar.x());
        Log.i("Object3DBuilder", "Building " + aVar2.m0() + " bones...");
        b(aVar2, aVar2.o0(), fArr, new float[]{0.0f, 0.0f, 0.0f}, -1, aVar.G());
        aVar2.T(aVar.m() + "-skeleton");
        return aVar2;
    }

    public static c f(c cVar) {
        if (cVar.g() != null) {
            try {
                Log.i("Object3DBuilder", "Building wireframe...");
                IntBuffer intBufferG = cVar.g();
                IntBuffer intBufferAsIntBuffer = g(intBufferG.capacity() * 2 * 4).asIntBuffer();
                for (int i2 = 0; i2 < intBufferG.capacity(); i2 += 3) {
                    int i3 = intBufferG.get(i2);
                    int i4 = i2 + 1;
                    int i5 = intBufferG.get(i4);
                    int i6 = i2 + 2;
                    int i7 = intBufferG.get(i6);
                    if (cVar.L()) {
                        i3 = i2;
                    } else {
                        i4 = i5;
                        i6 = i7;
                    }
                    intBufferAsIntBuffer.put(i3);
                    intBufferAsIntBuffer.put(i4);
                    intBufferAsIntBuffer.put(i4);
                    intBufferAsIntBuffer.put(i6);
                    intBufferAsIntBuffer.put(i6);
                    intBufferAsIntBuffer.put(i3);
                }
                if (!(cVar instanceof com.seewo.factoryaging.opengl3d.h.a)) {
                    c cVar2 = new c(cVar.F());
                    cVar2.f0(cVar.G());
                    cVar2.Q(intBufferAsIntBuffer);
                    cVar2.h0(cVar.I());
                    cVar2.N(cVar.d());
                    cVar2.g0(cVar.H());
                    cVar2.Z(cVar.C());
                    cVar2.V(cVar.r());
                    cVar2.W(cVar.v());
                    cVar2.Y(cVar.x());
                    cVar2.P(1);
                    cVar2.R(false);
                    return cVar2;
                }
                com.seewo.factoryaging.opengl3d.h.a aVar = new com.seewo.factoryaging.opengl3d.h.a(cVar.F());
                aVar.f0(cVar.G());
                aVar.Q(intBufferAsIntBuffer);
                aVar.h0(cVar.I());
                aVar.N(cVar.d());
                aVar.g0(cVar.H());
                aVar.Z(cVar.C());
                aVar.V(cVar.r());
                aVar.W(cVar.v());
                aVar.Y(cVar.x());
                aVar.P(1);
                aVar.R(false);
                aVar.s0(((com.seewo.factoryaging.opengl3d.h.a) cVar).p0());
                aVar.q0(((com.seewo.factoryaging.opengl3d.h.a) cVar).n0());
                aVar.r0(((com.seewo.factoryaging.opengl3d.h.a) cVar).o0(), ((com.seewo.factoryaging.opengl3d.h.a) cVar).m0(), ((com.seewo.factoryaging.opengl3d.h.a) cVar).l0(), false);
                aVar.j0(((com.seewo.factoryaging.opengl3d.h.a) cVar).k0());
                return aVar;
            } catch (Exception e) {
                Log.e("Object3DBuilder", e.getMessage(), e);
            }
        } else if (cVar.F() != null) {
            Log.i("Object3DBuilder", "Building wireframe...");
            FloatBuffer floatBufferF = cVar.F();
            IntBuffer intBufferAsIntBuffer2 = g((floatBufferF.capacity() / 3) * 2 * 4).asIntBuffer();
            for (int i8 = 0; i8 < floatBufferF.capacity() / 3; i8 += 3) {
                intBufferAsIntBuffer2.put(i8);
                int i9 = i8 + 1;
                intBufferAsIntBuffer2.put(i9);
                intBufferAsIntBuffer2.put(i9);
                int i10 = i8 + 2;
                intBufferAsIntBuffer2.put(i10);
                intBufferAsIntBuffer2.put(i10);
                intBufferAsIntBuffer2.put(i8);
            }
            c cVar3 = new c(cVar.F());
            cVar3.f0(cVar.G());
            cVar3.Q(intBufferAsIntBuffer2);
            cVar3.h0(cVar.I());
            cVar3.N(cVar.d());
            cVar3.g0(cVar.H());
            cVar3.Z(cVar.C());
            cVar3.V(cVar.r());
            cVar3.W(cVar.v());
            cVar3.Y(cVar.x());
            cVar3.P(1);
            cVar3.R(false);
            return cVar3;
        }
        return cVar;
    }

    private static ByteBuffer g(int i2) {
        ByteBuffer byteBufferAllocateDirect = ByteBuffer.allocateDirect(i2);
        byteBufferAllocateDirect.order(ByteOrder.nativeOrder());
        return byteBufferAllocateDirect;
    }

    public static c h(c cVar) throws IOException {
        int i2;
        FloatBuffer floatBufferAsFloatBuffer;
        String strD;
        d.C0012d c0012dA;
        d.C0012d c0012dA2;
        Log.i("Object3DBuilder", "Generating arrays for " + cVar.m());
        d.c cVarL = cVar.l();
        d.b bVarK = cVar.k();
        d.e eVarO = cVar.o();
        if (cVarL == null) {
            Log.i("Object3DBuilder", "No faces. Not generating arrays");
            return cVar;
        }
        Log.i("Object3DBuilder", "Allocating vertex array buffer... Vertices (" + cVarL.d() + ")");
        int i3 = 3;
        FloatBuffer floatBufferAsFloatBuffer2 = g(cVarL.d() * 3 * 4).asFloatBuffer();
        cVar.e0(floatBufferAsFloatBuffer2);
        int i4 = 1;
        cVar.R(true);
        Log.i("Object3DBuilder", "Populating vertex array...");
        FloatBuffer floatBufferJ = cVar.J();
        IntBuffer intBufferB = cVarL.b();
        int i5 = 0;
        while (true) {
            i2 = 2;
            if (i5 >= cVarL.d()) {
                break;
            }
            int i6 = i5 * 3;
            floatBufferAsFloatBuffer2.put(i6, floatBufferJ.get(intBufferB.get(i5) * 3));
            floatBufferAsFloatBuffer2.put(i6 + 1, floatBufferJ.get((intBufferB.get(i5) * 3) + 1));
            floatBufferAsFloatBuffer2.put(i6 + 2, floatBufferJ.get((intBufferB.get(i5) * 3) + 2));
            i5++;
        }
        Log.i("Object3DBuilder", "Allocating vertex normals buffer... Total normals (" + cVarL.d.size() + ")");
        FloatBuffer floatBufferAsFloatBuffer3 = g(cVarL.c() * 3 * 3 * 4).asFloatBuffer();
        cVar.h0(floatBufferAsFloatBuffer3);
        FloatBuffer floatBufferQ = cVar.q();
        if (floatBufferQ == null || floatBufferQ.capacity() <= 0) {
            Log.i("Object3DBuilder", "Model without normals. Calculating [" + (cVarL.b().capacity() / 3) + "] normals...");
            float[] fArr = new float[3];
            float[] fArr2 = new float[3];
            float[] fArr3 = new float[3];
            int i7 = 0;
            while (i7 < cVarL.b().capacity()) {
                try {
                    fArr[0] = floatBufferJ.get(cVarL.b().get(i7) * i3);
                    fArr[i4] = floatBufferJ.get((cVarL.b().get(i7) * i3) + i4);
                    fArr[i2] = floatBufferJ.get((cVarL.b().get(i7) * i3) + i2);
                    int i8 = i7 + 1;
                    fArr2[0] = floatBufferJ.get(cVarL.b().get(i8) * i3);
                    fArr2[i4] = floatBufferJ.get((cVarL.b().get(i8) * i3) + i4);
                    fArr2[i2] = floatBufferJ.get((cVarL.b().get(i8) * i3) + i2);
                    int i9 = i7 + 2;
                    fArr3[0] = floatBufferJ.get(cVarL.b().get(i9) * i3);
                    fArr3[i4] = floatBufferJ.get((cVarL.b().get(i9) * i3) + i4);
                    fArr3[i2] = floatBufferJ.get((cVarL.b().get(i9) * i3) + i2);
                    float[] fArrC = com.seewo.factoryaging.opengl3d.k.d.c(fArr, fArr2, fArr3);
                    int i10 = i7 * 3;
                    floatBufferAsFloatBuffer3.put(i10, fArrC[0]);
                    floatBufferAsFloatBuffer3.put(i10 + 1, fArrC[i4]);
                    floatBufferAsFloatBuffer3.put(i10 + 2, fArrC[2]);
                    floatBufferAsFloatBuffer3.put(i10 + 3, fArrC[0]);
                    floatBufferAsFloatBuffer3.put(i10 + 4, fArrC[1]);
                    floatBufferAsFloatBuffer3.put(i10 + 5, fArrC[2]);
                    floatBufferAsFloatBuffer3.put(i10 + 6, fArrC[0]);
                    floatBufferAsFloatBuffer3.put(i10 + 7, fArrC[1]);
                    floatBufferAsFloatBuffer3.put(i10 + 8, fArrC[2]);
                    i7 += 3;
                    i3 = 3;
                    i4 = 1;
                    i2 = 2;
                } catch (BufferOverflowException unused) {
                    throw new RuntimeException("Error calculating normal for face [" + (i7 / 3) + "]");
                }
            }
        } else {
            Log.i("Object3DBuilder", "Populating normals buffer...");
            for (int i11 = 0; i11 < cVarL.d.size(); i11++) {
                int[] iArr = cVarL.d.get(i11);
                for (int i12 = 0; i12 < iArr.length; i12++) {
                    int i13 = (i11 * 9) + (i12 * 3);
                    floatBufferAsFloatBuffer3.put(i13, floatBufferQ.get(iArr[i12] * 3));
                    floatBufferAsFloatBuffer3.put(i13 + 1, floatBufferQ.get((iArr[i12] * 3) + 1));
                    floatBufferAsFloatBuffer3.put(i13 + 2, floatBufferQ.get((iArr[i12] * 3) + 2));
                }
            }
        }
        if (eVarO != null) {
            Log.i("Object3DBuilder", "Reading materials...");
            try {
                InputStream inputStreamC = com.seewo.factoryaging.opengl3d.k.a.c(eVarO.b);
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreamC));
                    eVarO.b(bufferedReader);
                    eVarO.d();
                    bufferedReader.close();
                    if (inputStreamC != null) {
                        inputStreamC.close();
                    }
                } finally {
                }
            } catch (Exception e) {
                Log.e("Object3DBuilder", "Couldn't load material file " + eVarO.b + ". " + e.getMessage(), e);
                StringBuilder sb = new StringBuilder();
                sb.append(eVarO.b);
                sb.append(":");
                sb.append(e.getMessage());
                cVar.a(sb.toString());
            }
        }
        if (eVarO == null || bVarK.c()) {
            floatBufferAsFloatBuffer = null;
        } else {
            Log.i("Object3DBuilder", "Processing face materials...");
            floatBufferAsFloatBuffer = g(cVarL.d() * 4 * 4).asFloatBuffer();
            float[] fArrB = a;
            boolean z = false;
            for (int i14 = 0; i14 < cVarL.c(); i14++) {
                if (bVarK.b(i14) != null && (c0012dA2 = eVarO.a(bVarK.b(i14))) != null) {
                    if (c0012dA2.b() != null) {
                        fArrB = c0012dA2.b();
                    }
                    z = z || c0012dA2.b() != null;
                }
                floatBufferAsFloatBuffer.put(fArrB);
                floatBufferAsFloatBuffer.put(fArrB);
                floatBufferAsFloatBuffer.put(fArrB);
            }
            if (!z) {
                Log.i("Object3DBuilder", "Using single color.");
                floatBufferAsFloatBuffer = null;
            }
        }
        cVar.g0(floatBufferAsFloatBuffer);
        if (eVarO == null || eVarO.a.isEmpty()) {
            Log.i("Object3DBuilder", "No materials -> No texture");
            strD = null;
        } else {
            Iterator<d.C0012d> it = eVarO.a.values().iterator();
            while (true) {
                if (!it.hasNext()) {
                    strD = null;
                    break;
                }
                d.C0012d next = it.next();
                if (next.d() != null) {
                    strD = next.d();
                    break;
                }
            }
            if (strD != null) {
                cVar.b0(strD);
                Log.i("Object3DBuilder", "Texture " + strD);
            } else {
                Log.i("Object3DBuilder", "Found material(s) but no texture");
            }
        }
        ArrayList<d.g> arrayListB = cVar.B();
        if (arrayListB != null && arrayListB.size() > 0) {
            Log.i("Object3DBuilder", "Allocating/populating texture buffer (flipTexCoord:" + cVar.M() + ")...");
            FloatBuffer floatBufferAsFloatBuffer4 = g(arrayListB.size() * 2 * 4).asFloatBuffer();
            for (d.g gVar : arrayListB) {
                floatBufferAsFloatBuffer4.put(gVar.a());
                floatBufferAsFloatBuffer4.put(cVar.M() ? 1.0f - gVar.b() : gVar.b());
            }
            Log.i("Object3DBuilder", "Populating texture array buffer...");
            FloatBuffer floatBufferAsFloatBuffer5 = g(cVarL.d() * 2 * 4).asFloatBuffer();
            cVar.Z(floatBufferAsFloatBuffer5);
            try {
                Log.i("Object3DBuilder", "Populating texture array buffer...");
                boolean z2 = false;
                int i15 = 0;
                String strD2 = null;
                for (int i16 = 0; i16 < cVarL.c.size(); i16++) {
                    if (!bVarK.c() && bVarK.b(i16) != null && (c0012dA = eVarO.a(bVarK.b(i16))) != null && c0012dA.d() != null) {
                        strD2 = c0012dA.d();
                    }
                    if (strD2 != null) {
                        strD2.equals(strD);
                    }
                    int[] iArr2 = cVarL.c.get(i16);
                    for (int i17 = 0; i17 < iArr2.length; i17++) {
                        if (iArr2[i17] * 2 < 0 || iArr2[i17] * 2 >= floatBufferAsFloatBuffer4.limit()) {
                            Log.v("Object3DBuilder", "Wrong texture for face " + i16);
                            int i18 = i15 + 1;
                            floatBufferAsFloatBuffer5.put(i15, 0.0f);
                            i15 = i18 + 1;
                            floatBufferAsFloatBuffer5.put(i18, 0.0f);
                        } else {
                            int i19 = i15 + 1;
                            floatBufferAsFloatBuffer5.put(i15, floatBufferAsFloatBuffer4.get(iArr2[i17] * 2));
                            floatBufferAsFloatBuffer5.put(i19, floatBufferAsFloatBuffer4.get((iArr2[i17] * 2) + 1));
                            i15 = i19 + 1;
                            z2 = true;
                        }
                    }
                }
                if (!z2) {
                    Log.i("Object3DBuilder", "Texture is wrong. Applying global texture");
                    int i20 = 0;
                    for (int i21 = 0; i21 < cVarL.c.size(); i21++) {
                        int[] iArr3 = cVarL.c.get(i21);
                        for (int i22 = 0; i22 < iArr3.length; i22++) {
                            int i23 = i20 + 1;
                            floatBufferAsFloatBuffer5.put(i20, floatBufferAsFloatBuffer4.get(iArr3[i22] * 2));
                            i20 = i23 + 1;
                            floatBufferAsFloatBuffer5.put(i23, floatBufferAsFloatBuffer4.get((iArr3[i22] * 2) + 1));
                        }
                    }
                }
            } catch (Exception e2) {
                Log.e("Object3DBuilder", "Failure to load texture coordinates", e2);
            }
        }
        cVar.a0(null);
        return cVar;
    }
}
