Search in sources :

Example 1 with TextureException

use of org.rajawali3d.materials.textures.ATexture.TextureException in project Rajawali by Rajawali.

the class RajawaliVRExampleRenderer method createTerrain.

public void createTerrain() {
    //
    // -- Load a bitmap that represents the terrain. Its color values will
    //    be used to generate heights.
    //
    Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.terrain);
    try {
        SquareTerrain.Parameters terrainParams = SquareTerrain.createParameters(bmp);
        // -- set terrain scale
        terrainParams.setScale(4f, 54f, 4f);
        // -- the number of plane subdivisions
        terrainParams.setDivisions(128);
        // -- the number of times the textures should be repeated
        terrainParams.setTextureMult(4);
        //
        // -- Terrain colors can be set by manually specifying base, middle and
        //    top colors.
        //
        // --  terrainParams.setBasecolor(Color.argb(255, 0, 0, 0));
        //     terrainParams.setMiddleColor(Color.argb(255, 200, 200, 200));
        //     terrainParams.setUpColor(Color.argb(255, 0, 30, 0));
        //
        // -- However, for this example we'll use a bitmap
        //
        terrainParams.setColorMapBitmap(bmp);
        //
        // -- create the terrain
        //
        terrain = TerrainGenerator.createSquareTerrainFromBitmap(terrainParams, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //
    // -- The bitmap won't be used anymore, so get rid of it.
    //
    bmp.recycle();
    //
    // -- A normal map material will give the terrain a bit more detail.
    //
    Material material = new Material();
    material.enableLighting(true);
    material.useVertexColors(true);
    material.setDiffuseMethod(new DiffuseMethod.Lambert());
    try {
        Texture groundTexture = new Texture("ground", R.drawable.ground);
        groundTexture.setInfluence(.5f);
        material.addTexture(groundTexture);
        material.addTexture(new NormalMapTexture("groundNormalMap", R.drawable.groundnor));
        material.setColorInfluence(0);
    } catch (TextureException e) {
        e.printStackTrace();
    }
    //
    // -- Blend the texture with the vertex colors
    //
    material.setColorInfluence(.5f);
    terrain.setY(-100);
    terrain.setMaterial(material);
    getCurrentScene().addChild(terrain);
}
Also used : Bitmap(android.graphics.Bitmap) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) SquareTerrain(org.rajawali3d.terrain.SquareTerrain) DiffuseMethod(org.rajawali3d.materials.methods.DiffuseMethod) Material(org.rajawali3d.materials.Material) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException)

Example 2 with TextureException

use of org.rajawali3d.materials.textures.ATexture.TextureException in project Rajawali by Rajawali.

the class LoaderFBX method setMeshTextures.

private void setMeshTextures(Object3D o, String name) throws TextureException, ParsingException {
    Stack<FBXValues.Objects.Texture> textures = mFbx.objects.textures;
    Stack<Connect> connections = mFbx.connections.connections;
    int numTex = textures.size();
    int numCon = connections.size();
    for (int i = 0; i < numTex; ++i) {
        FBXValues.Objects.Texture tex = textures.get(i);
        for (int j = 0; j < numCon; ++j) {
            Connect conn = connections.get(j);
            if (conn.object2.equals(name) && conn.object1.equals(tex.textureName)) {
                // -- one texture for now
                String textureName = tex.fileName;
                Bitmap bitmap = null;
                if (mFile == null) {
                    int identifier = mResources.getIdentifier(getFileNameWithoutExtension(textureName).toLowerCase(Locale.US), "drawable", mResources.getResourcePackageName(mResourceId));
                    bitmap = BitmapFactory.decodeResource(mResources, identifier);
                } else {
                    try {
                        String filePath = mFile.getParent() + File.separatorChar + getOnlyFileName(textureName);
                        bitmap = BitmapFactory.decodeFile(filePath);
                    } catch (Exception e) {
                        throw new ParsingException("[" + getClass().getCanonicalName() + "] Could not find file " + getOnlyFileName(textureName));
                    }
                }
                o.getMaterial().setColorInfluence(0);
                o.getMaterial().addTexture(new Texture(textureName.replaceAll("[\\W]|_", ""), bitmap));
                return;
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Connect(org.rajawali3d.loader.fbx.FBXValues.Connections.Connect) ParsingException(org.rajawali3d.loader.ParsingException) Texture(org.rajawali3d.materials.textures.Texture) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) ParsingException(org.rajawali3d.loader.ParsingException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with TextureException

use of org.rajawali3d.materials.textures.ATexture.TextureException in project Rajawali by Rajawali.

the class Loader3DSMax method parse.

@Override
public AMeshLoader parse() throws ParsingException {
    RajLog.i("Start parsing 3DS");
    final InputStream stream;
    if (mFile == null) {
        stream = new BufferedInputStream(mResources.openRawResource(mResourceId));
    } else {
        try {
            stream = new BufferedInputStream(new FileInputStream(mFile));
        } catch (Exception e) {
            throw new ParsingException(e);
        }
    }
    try {
        readHeader(stream);
        if (mChunkID != IDENTIFIER_3DS) {
            RajLog.e("Not a valid 3DS file");
            return null;
        }
        while (!mEndReached) {
            readChunk(stream);
        }
        try {
            build();
        } catch (TextureException tme) {
            throw new ParsingException(tme);
        }
        if (mRootObject.getNumChildren() == 1)
            mRootObject = mRootObject.getChildAt(0);
        stream.close();
        RajLog.i("End parsing 3DS");
    } catch (IOException e) {
        RajLog.e("Error parsing");
        throw new ParsingException(e);
    }
    return this;
}
Also used : TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) IOException(java.io.IOException)

Example 4 with TextureException

use of org.rajawali3d.materials.textures.ATexture.TextureException in project Rajawali by Rajawali.

the class SideBySideRenderer method initScene.

@Override
public void initScene() {
    mCameraLeft = new Camera();
    mCameraLeft.setNearPlane(.01f);
    mCameraLeft.setFieldOfView(getCurrentCamera().getFieldOfView());
    mCameraLeft.setNearPlane(getCurrentCamera().getNearPlane());
    mCameraLeft.setFarPlane(getCurrentCamera().getFarPlane());
    mCameraRight = new Camera();
    mCameraRight.setNearPlane(.01f);
    mCameraRight.setFieldOfView(getCurrentCamera().getFieldOfView());
    mCameraRight.setNearPlane(getCurrentCamera().getNearPlane());
    mCameraRight.setFarPlane(getCurrentCamera().getFarPlane());
    setPupilDistance(mPupilDistance);
    mLeftQuadMaterial = new Material();
    mLeftQuadMaterial.setColorInfluence(0);
    mRightQuadMaterial = new Material();
    mRightQuadMaterial.setColorInfluence(0);
    mSideBySideScene = new Scene(this);
    mLeftQuad = new ScreenQuad();
    mLeftQuad.setScaleX(.5);
    mLeftQuad.setX(-.25);
    mLeftQuad.setMaterial(mLeftQuadMaterial);
    mSideBySideScene.addChild(mLeftQuad);
    mRightQuad = new ScreenQuad();
    mRightQuad.setScaleX(.5);
    mRightQuad.setX(.25);
    mRightQuad.setMaterial(mRightQuadMaterial);
    mSideBySideScene.addChild(mRightQuad);
    addScene(mSideBySideScene);
    mViewportWidthHalf = (int) (mDefaultViewportWidth * .5f);
    mLeftRenderTarget = new RenderTarget("sbsLeftRT", mViewportWidthHalf, mDefaultViewportHeight);
    mLeftRenderTarget.setFullscreen(false);
    mRightRenderTarget = new RenderTarget("sbsRightRT", mViewportWidthHalf, mDefaultViewportHeight);
    mRightRenderTarget.setFullscreen(false);
    mCameraLeft.setProjectionMatrix(mViewportWidthHalf, mDefaultViewportHeight);
    mCameraRight.setProjectionMatrix(mViewportWidthHalf, mDefaultViewportHeight);
    addRenderTarget(mLeftRenderTarget);
    addRenderTarget(mRightRenderTarget);
    try {
        mLeftQuadMaterial.addTexture(mLeftRenderTarget.getTexture());
        mRightQuadMaterial.addTexture(mRightRenderTarget.getTexture());
    } catch (TextureException e) {
        e.printStackTrace();
    }
}
Also used : TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) ScreenQuad(org.rajawali3d.primitives.ScreenQuad) Material(org.rajawali3d.materials.Material) Camera(org.rajawali3d.cameras.Camera) Scene(org.rajawali3d.scene.Scene)

Example 5 with TextureException

use of org.rajawali3d.materials.textures.ATexture.TextureException in project Rajawali by Rajawali.

the class LoaderOBJ method parse.

@Override
public LoaderOBJ parse() throws ParsingException {
    super.parse();
    BufferedReader buffer = null;
    if (mFile == null) {
        InputStream fileIn = mResources.openRawResource(mResourceId);
        buffer = new BufferedReader(new InputStreamReader(fileIn));
    } else {
        try {
            buffer = new BufferedReader(new FileReader(mFile));
        } catch (FileNotFoundException e) {
            RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
            e.printStackTrace();
        }
    }
    String line;
    ObjIndexData currObjIndexData = new ObjIndexData(new Object3D(generateObjectName()));
    ArrayList<ObjIndexData> objIndices = new ArrayList<ObjIndexData>();
    ArrayList<Float> vertices = new ArrayList<Float>();
    ArrayList<Float> texCoords = new ArrayList<Float>();
    ArrayList<Float> normals = new ArrayList<Float>();
    MaterialLib matLib = new MaterialLib();
    String currentMaterialName = null;
    boolean currentObjHasFaces = false;
    Object3D currentGroup = mRootObject;
    mRootObject.setName("default");
    Map<String, Object3D> groups = new HashMap<String, Object3D>();
    try {
        while ((line = buffer.readLine()) != null) {
            // Skip comments and empty lines.
            if (line.length() == 0 || line.charAt(0) == '#')
                continue;
            StringTokenizer parts = new StringTokenizer(line, " ");
            int numTokens = parts.countTokens();
            if (numTokens == 0)
                continue;
            String type = parts.nextToken();
            if (type.equals(VERTEX)) {
                vertices.add(Float.parseFloat(parts.nextToken()));
                vertices.add(Float.parseFloat(parts.nextToken()));
                vertices.add(Float.parseFloat(parts.nextToken()));
            } else if (type.equals(FACE)) {
                currentObjHasFaces = true;
                boolean isQuad = numTokens == 5;
                int[] quadvids = new int[4];
                int[] quadtids = new int[4];
                int[] quadnids = new int[4];
                boolean emptyVt = line.indexOf("//") > -1;
                if (emptyVt)
                    line = line.replace("//", "/");
                parts = new StringTokenizer(line);
                parts.nextToken();
                StringTokenizer subParts = new StringTokenizer(parts.nextToken(), "/");
                int partLength = subParts.countTokens();
                boolean hasuv = partLength >= 2 && !emptyVt;
                boolean hasn = partLength == 3 || (partLength == 2 && emptyVt);
                int idx;
                for (int i = 1; i < numTokens; i++) {
                    if (i > 1)
                        subParts = new StringTokenizer(parts.nextToken(), "/");
                    idx = Integer.parseInt(subParts.nextToken());
                    if (idx < 0)
                        idx = (vertices.size() / 3) + idx;
                    else
                        idx -= 1;
                    if (!isQuad)
                        currObjIndexData.vertexIndices.add(idx);
                    else
                        quadvids[i - 1] = idx;
                    if (hasuv) {
                        idx = Integer.parseInt(subParts.nextToken());
                        if (idx < 0)
                            idx = (texCoords.size() / 2) + idx;
                        else
                            idx -= 1;
                        if (!isQuad)
                            currObjIndexData.texCoordIndices.add(idx);
                        else
                            quadtids[i - 1] = idx;
                    }
                    if (hasn) {
                        idx = Integer.parseInt(subParts.nextToken());
                        if (idx < 0)
                            idx = (normals.size() / 3) + idx;
                        else
                            idx -= 1;
                        if (!isQuad)
                            currObjIndexData.normalIndices.add(idx);
                        else
                            quadnids[i - 1] = idx;
                    }
                }
                if (isQuad) {
                    int[] indices = new int[] { 0, 1, 2, 0, 2, 3 };
                    for (int i = 0; i < 6; ++i) {
                        int index = indices[i];
                        currObjIndexData.vertexIndices.add(quadvids[index]);
                        currObjIndexData.texCoordIndices.add(quadtids[index]);
                        currObjIndexData.normalIndices.add(quadnids[index]);
                    }
                }
            } else if (type.equals(TEXCOORD)) {
                texCoords.add(Float.parseFloat(parts.nextToken()));
                texCoords.add(1f - Float.parseFloat(parts.nextToken()));
            } else if (type.equals(NORMAL)) {
                normals.add(Float.parseFloat(parts.nextToken()));
                normals.add(Float.parseFloat(parts.nextToken()));
                normals.add(Float.parseFloat(parts.nextToken()));
            } else if (type.equals(GROUP)) {
                int numGroups = parts.countTokens();
                Object3D previousGroup = null;
                for (int i = 0; i < numGroups; i++) {
                    String groupName = parts.nextToken();
                    if (!groups.containsKey(groupName)) {
                        groups.put(groupName, new Object3D(groupName));
                    }
                    Object3D group = groups.get(groupName);
                    if (previousGroup != null) {
                        addChildSetParent(group, previousGroup);
                    } else {
                        currentGroup = group;
                    }
                    previousGroup = group;
                }
                RajLog.i("Parsing group: " + currentGroup.getName());
                if (currentObjHasFaces) {
                    objIndices.add(currObjIndexData);
                    currObjIndexData = new ObjIndexData(new Object3D(generateObjectName()));
                    RajLog.i("Parsing object: " + currObjIndexData.targetObj.getName());
                    currObjIndexData.materialName = currentMaterialName;
                    currentObjHasFaces = false;
                }
                addChildSetParent(currentGroup, currObjIndexData.targetObj);
            } else if (type.equals(OBJECT)) {
                String objName = parts.hasMoreTokens() ? parts.nextToken() : generateObjectName();
                if (currentObjHasFaces) {
                    objIndices.add(currObjIndexData);
                    currObjIndexData = new ObjIndexData(new Object3D(currObjIndexData.targetObj.getName()));
                    currObjIndexData.materialName = currentMaterialName;
                    addChildSetParent(currentGroup, currObjIndexData.targetObj);
                    RajLog.i("Parsing object: " + currObjIndexData.targetObj.getName());
                    currentObjHasFaces = false;
                }
                currObjIndexData.targetObj.setName(objName);
            } else if (type.equals(MATERIAL_LIB)) {
                if (!parts.hasMoreTokens())
                    continue;
                String materialLibPath = mNeedToRenameMtl ? parts.nextToken().replace(".", "_") : parts.nextToken();
                RajLog.d("Found Material Lib: " + materialLibPath);
                if (mFile != null)
                    matLib.parse(materialLibPath, null, null);
                else
                    matLib.parse(materialLibPath, mResources.getResourceTypeName(mResourceId), mResources.getResourcePackageName(mResourceId));
            } else if (type.equals(USE_MATERIAL)) {
                currentMaterialName = parts.nextToken();
                if (currentObjHasFaces) {
                    objIndices.add(currObjIndexData);
                    currObjIndexData = new ObjIndexData(new Object3D(generateObjectName()));
                    RajLog.i("Parsing object: " + currObjIndexData.targetObj.getName());
                    addChildSetParent(currentGroup, currObjIndexData.targetObj);
                    currentObjHasFaces = false;
                }
                currObjIndexData.materialName = currentMaterialName;
            }
        }
        buffer.close();
        if (currentObjHasFaces) {
            RajLog.i("Parsing object: " + currObjIndexData.targetObj.getName());
            objIndices.add(currObjIndexData);
        }
    } catch (IOException e) {
        throw new ParsingException(e);
    }
    int numObjects = objIndices.size();
    for (int j = 0; j < numObjects; ++j) {
        ObjIndexData oid = objIndices.get(j);
        int i;
        float[] aVertices = new float[oid.vertexIndices.size() * 3];
        float[] aTexCoords = new float[oid.texCoordIndices.size() * 2];
        float[] aNormals = new float[oid.normalIndices.size() * 3];
        float[] aColors = new float[oid.colorIndices.size() * 4];
        int[] aIndices = new int[oid.vertexIndices.size()];
        for (i = 0; i < oid.vertexIndices.size(); ++i) {
            int faceIndex = oid.vertexIndices.get(i) * 3;
            int vertexIndex = i * 3;
            try {
                aVertices[vertexIndex] = vertices.get(faceIndex);
                aVertices[vertexIndex + 1] = vertices.get(faceIndex + 1);
                aVertices[vertexIndex + 2] = vertices.get(faceIndex + 2);
                aIndices[i] = i;
            } catch (ArrayIndexOutOfBoundsException e) {
                RajLog.d("Obj array index out of bounds: " + vertexIndex + ", " + faceIndex);
            }
        }
        if (texCoords != null && texCoords.size() > 0) {
            for (i = 0; i < oid.texCoordIndices.size(); ++i) {
                int texCoordIndex = oid.texCoordIndices.get(i) * 2;
                int ti = i * 2;
                aTexCoords[ti] = texCoords.get(texCoordIndex);
                aTexCoords[ti + 1] = texCoords.get(texCoordIndex + 1);
            }
        }
        for (i = 0; i < oid.colorIndices.size(); ++i) {
            int colorIndex = oid.colorIndices.get(i) * 4;
            int ti = i * 4;
            aTexCoords[ti] = texCoords.get(colorIndex);
            aTexCoords[ti + 1] = texCoords.get(colorIndex + 1);
            aTexCoords[ti + 2] = texCoords.get(colorIndex + 2);
            aTexCoords[ti + 3] = texCoords.get(colorIndex + 3);
        }
        for (i = 0; i < oid.normalIndices.size(); ++i) {
            int normalIndex = oid.normalIndices.get(i) * 3;
            int ni = i * 3;
            if (normals.size() == 0) {
                RajLog.e("[" + getClass().getName() + "] There are no normals specified for this model. Please re-export with normals.");
                throw new ParsingException("[" + getClass().getName() + "] There are no normals specified for this model. Please re-export with normals.");
            }
            aNormals[ni] = normals.get(normalIndex);
            aNormals[ni + 1] = normals.get(normalIndex + 1);
            aNormals[ni + 2] = normals.get(normalIndex + 2);
        }
        oid.targetObj.setData(aVertices, aNormals, aTexCoords, aColors, aIndices, false);
        try {
            matLib.setMaterial(oid.targetObj, oid.materialName);
        } catch (TextureException tme) {
            throw new ParsingException(tme);
        }
        if (oid.targetObj.getParent() == null)
            addChildSetParent(mRootObject, oid.targetObj);
    }
    for (Object3D group : groups.values()) {
        if (group.getParent() == null)
            addChildSetParent(mRootObject, group);
    }
    if (mRootObject.getNumChildren() == 1 && !mRootObject.getChildAt(0).isContainer())
        mRootObject = mRootObject.getChildAt(0);
    for (int i = 0; i < mRootObject.getNumChildren(); i++) mergeGroupsAsObjects(mRootObject.getChildAt(i));
    return this;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Object3D(org.rajawali3d.Object3D) StringTokenizer(java.util.StringTokenizer) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Aggregations

TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)8 IOException (java.io.IOException)4 Material (org.rajawali3d.materials.Material)4 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 Bitmap (android.graphics.Bitmap)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Camera (org.rajawali3d.cameras.Camera)2 ParsingException (org.rajawali3d.loader.ParsingException)2 Texture (org.rajawali3d.materials.textures.Texture)2 ScreenQuad (org.rajawali3d.primitives.ScreenQuad)2 BufferedInputStream (java.io.BufferedInputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Stack (java.util.Stack)1 StringTokenizer (java.util.StringTokenizer)1