Search in sources :

Example 1 with Matrix4

use of org.rajawali3d.math.Matrix4 in project Rajawali by Rajawali.

the class BlockMeshInstance method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Parse scene block
    RajLog.d("Parsing SceneGraph Block at position: " + dis.getPosition());
    mSceneGraphBlock = new SceneGraphBlock();
    mSceneGraphBlock.readGraphData(blockHeader, dis);
    // Block id for geometry
    mGeometryID = dis.readUnsignedInt();
    // Lookup the geometry or create it if it does not exist.
    final BlockHeader geomHeader = blockHeader.blockHeaders.get((short) mGeometryID);
    if (geomHeader == null) {
        mGeometry = new Object3D(mSceneGraphBlock.lookupName);
    } else {
        if (geomHeader.parser == null || !(geomHeader.parser instanceof ABaseObjectBlockParser))
            throw new ParsingException("Invalid block reference.");
        mGeometry = ((ABaseObjectBlockParser) geomHeader.parser).getBaseObject3D().clone(false, true);
        mGeometry.setName(mSceneGraphBlock.lookupName);
    }
    // Apply the materials
    final int materialCount = dis.readUnsignedShort();
    final Material[] materials = new Material[materialCount];
    for (int i = 0; i < materialCount; ++i) {
        final long materialID = dis.readUnsignedInt();
        if (materialID == 0) {
            materials[i] = getDefaultMaterial();
            materials[i].addTexture(getDefaultTexture());
        } else {
            final BlockHeader materialHeader = blockHeader.blockHeaders.get((short) materialID);
            if (materialHeader == null || materialHeader.parser == null || !(materialHeader.parser instanceof ATextureBlockParser))
                throw new ParsingException("Invalid block reference " + materialID);
            materials[i] = ((ATextureBlockParser) materialHeader.parser).getMaterial();
        }
    }
    // mesh instance properties; does it cast a shadow?
    AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mCastsShadow = (boolean) properties.get(PROP_CASTS_SHADOW, true);
    final Matrix4 matrix = new Matrix4(mSceneGraphBlock.transformMatrix);
    // Set translation
    mGeometry.setPosition(matrix.getTranslation());
    // Set scale
    final Vector3 scale = matrix.getScaling();
    mGeometry.setScale(scale.y, scale.x, scale.z);
    // Set rotation
    mGeometry.setOrientation(new Quaternion().fromMatrix(matrix));
    int m = 0;
    if (!mGeometry.isContainer())
        mGeometry.setMaterial(materials[m++]);
    for (int i = 0; i < mGeometry.getNumChildren(); i++) mGeometry.getChildAt(i).setMaterial(materials[Math.min(materials.length - 1, m++)]);
    // ignore user properties, skip to end of block
    dis.skip(blockHeader.blockEnd - dis.getPosition());
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D) ParsingException(org.rajawali3d.loader.ParsingException) BlockHeader(org.rajawali3d.loader.LoaderAWD.BlockHeader)

Example 2 with Matrix4

use of org.rajawali3d.math.Matrix4 in project Rajawali by Rajawali.

the class Object3D method render.

/**
	 * Renders the object
	 *
	 * @param camera The camera
	 * @param vpMatrix {@link Matrix4} The view-projection matrix
	 * @param projMatrix {@link Matrix4} The projection matrix
	 * @param vMatrix {@link Matrix4} The view matrix
	 * @param parentMatrix {@link Matrix4} This object's parent matrix
	 * @param sceneMaterial The scene-wide Material to use, if any.
	 */
public void render(Camera camera, final Matrix4 vpMatrix, final Matrix4 projMatrix, final Matrix4 vMatrix, final Matrix4 parentMatrix, Material sceneMaterial) {
    if (isDestroyed() || (!mIsVisible && !mRenderChildrenAsBatch) || isZeroScale()) {
        return;
    }
    if (parentMatrix != null) {
        if (mParentMatrix == null) {
            mParentMatrix = new Matrix4();
        }
        mParentMatrix.setAll(parentMatrix);
    }
    Material material = sceneMaterial == null ? mMaterial : sceneMaterial;
    preRender();
    // -- move view matrix transformation first
    boolean modelMatrixWasRecalculated = onRecalculateModelMatrix(parentMatrix);
    // -- calculate model view matrix;
    mMVMatrix.setAll(vMatrix).multiply(mMMatrix);
    //Create MVP Matrix from View-Projection Matrix
    mMVPMatrix.setAll(vpMatrix).multiply(mMMatrix);
    // Transform the bounding volumes if they exist
    if (mGeometry.hasBoundingBox())
        getBoundingBox().transform(getModelMatrix());
    if (mGeometry.hasBoundingSphere())
        mGeometry.getBoundingSphere().transform(getModelMatrix());
    // only if mFrustrumTest == true it check frustum
    mIsInFrustum = true;
    if (mFrustumTest && mGeometry.hasBoundingBox()) {
        BoundingBox bbox = getBoundingBox();
        if (!camera.getFrustum().boundsInFrustum(bbox)) {
            mIsInFrustum = false;
        }
    }
    if (!mIsContainerOnly && mIsInFrustum) {
        mPMatrix = projMatrix;
        if (mDoubleSided) {
            GLES20.glDisable(GLES20.GL_CULL_FACE);
        } else {
            GLES20.glEnable(GLES20.GL_CULL_FACE);
            if (mBackSided) {
                GLES20.glCullFace(GLES20.GL_FRONT);
            } else {
                GLES20.glCullFace(GLES20.GL_BACK);
                GLES20.glFrontFace(GLES20.GL_CCW);
            }
        }
        if (mEnableBlending) {
            GLES20.glEnable(GLES20.GL_BLEND);
            GLES20.glBlendFunc(mBlendFuncSFactor, mBlendFuncDFactor);
        }
        if (!mEnableDepthTest)
            GLES20.glDisable(GLES20.GL_DEPTH_TEST);
        else {
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
            GLES20.glDepthFunc(GLES20.GL_LESS);
        }
        GLES20.glDepthMask(mEnableDepthMask);
        if (!mIsPartOfBatch) {
            if (material == null) {
                RajLog.e("[" + this.getClass().getName() + "] This object can't render because there's no material attached to it.");
                /*throw new RuntimeException(
							"This object can't render because there's no material attached to it.");*/
                if (mEnableBlending) {
                    GLES20.glDisable(GLES20.GL_BLEND);
                }
                if (mDoubleSided) {
                    GLES20.glEnable(GLES20.GL_CULL_FACE);
                } else if (mBackSided) {
                    GLES20.glCullFace(GLES20.GL_BACK);
                }
                if (!mEnableDepthTest) {
                    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
                    GLES20.glDepthFunc(GLES20.GL_LESS);
                }
                return;
            }
            material.useProgram();
            setShaderParams(camera);
            material.bindTextures();
            if (mGeometry.hasTextureCoordinates())
                material.setTextureCoords(mGeometry.getTexCoordBufferInfo());
            if (mGeometry.hasNormals())
                material.setNormals(mGeometry.getNormalBufferInfo());
            if (mMaterial.usingVertexColors())
                material.setVertexColors(mGeometry.getColorBufferInfo());
            material.setVertices(mGeometry.getVertexBufferInfo());
        }
        material.setCurrentObject(this);
        if (mOverrideMaterialColor) {
            material.setColor(mColor);
        }
        material.applyParams();
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        material.setMVPMatrix(mMVPMatrix);
        material.setModelMatrix(mMMatrix);
        material.setModelViewMatrix(mMVMatrix);
        if (mIsVisible) {
            int bufferType = mGeometry.getIndexBufferInfo().bufferType == Geometry3D.BufferType.SHORT_BUFFER ? GLES20.GL_UNSIGNED_SHORT : GLES20.GL_UNSIGNED_INT;
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, mGeometry.getIndexBufferInfo().bufferHandle);
            GLES20.glDrawElements(mDrawingMode, mGeometry.getNumIndices(), bufferType, 0);
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
        }
        if (!mIsPartOfBatch && !mRenderChildrenAsBatch && sceneMaterial == null) {
            material.unbindTextures();
        }
        material.unsetCurrentObject(this);
        if (mEnableBlending) {
            GLES20.glDisable(GLES20.GL_BLEND);
        }
        if (mDoubleSided) {
            GLES20.glEnable(GLES20.GL_CULL_FACE);
        } else if (mBackSided) {
            GLES20.glCullFace(GLES20.GL_BACK);
        }
        if (!mEnableDepthTest) {
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
            GLES20.glDepthFunc(GLES20.GL_LESS);
        }
    }
    if (mShowBoundingVolume) {
        if (mGeometry.hasBoundingBox())
            getBoundingBox().drawBoundingVolume(camera, vpMatrix, projMatrix, vMatrix, mMMatrix);
        if (mGeometry.hasBoundingSphere())
            mGeometry.getBoundingSphere().drawBoundingVolume(camera, vpMatrix, projMatrix, vMatrix, mMMatrix);
    }
    // Draw children without frustum test
    for (int i = 0, j = mChildren.size(); i < j; i++) {
        Object3D child = mChildren.get(i);
        if (mRenderChildrenAsBatch || mIsPartOfBatch) {
            child.setPartOfBatch(true);
        }
        if (modelMatrixWasRecalculated)
            child.markModelMatrixDirty();
        child.render(camera, vpMatrix, projMatrix, vMatrix, mMMatrix, sceneMaterial);
    }
    if (mRenderChildrenAsBatch && sceneMaterial == null) {
        material.unbindTextures();
    }
}
Also used : BoundingBox(org.rajawali3d.bounds.BoundingBox) Material(org.rajawali3d.materials.Material) Matrix4(org.rajawali3d.math.Matrix4)

Example 3 with Matrix4

use of org.rajawali3d.math.Matrix4 in project Rajawali by Rajawali.

the class ArcballCamera method getViewMatrix.

@Override
public Matrix4 getViewMatrix() {
    Matrix4 m = super.getViewMatrix();
    if (mTarget != null) {
        mScratchMatrix.identity();
        mScratchMatrix.translate(mTarget.getPosition());
        m.multiply(mScratchMatrix);
    }
    mScratchMatrix.identity();
    mScratchMatrix.rotate(mEmpty.getOrientation());
    m.multiply(mScratchMatrix);
    if (mTarget != null) {
        mScratchVector.setAll(mTarget.getPosition());
        mScratchVector.inverse();
        mScratchMatrix.identity();
        mScratchMatrix.translate(mScratchVector);
        m.multiply(mScratchMatrix);
    }
    return m;
}
Also used : Matrix4(org.rajawali3d.math.Matrix4)

Example 4 with Matrix4

use of org.rajawali3d.math.Matrix4 in project Rajawali by Rajawali.

the class ArcballCamera method initialize.

private void initialize() {
    mStartFOV = mFieldOfView;
    mLookAtEnabled = true;
    setLookAt(0, 0, 0);
    mEmpty = new Object3D();
    mScratchMatrix = new Matrix4();
    mScratchVector = new Vector3();
    mCameraStartPos = new Vector3();
    mPrevSphereCoord = new Vector3();
    mCurrSphereCoord = new Vector3();
    mPrevScreenCoord = new Vector2();
    mCurrScreenCoord = new Vector2();
    mStartOrientation = new Quaternion();
    mCurrentOrientation = new Quaternion();
}
Also used : Vector2(org.rajawali3d.math.vector.Vector2) Quaternion(org.rajawali3d.math.Quaternion) Vector3(org.rajawali3d.math.vector.Vector3) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D)

Example 5 with Matrix4

use of org.rajawali3d.math.Matrix4 in project Rajawali by Rajawali.

the class ScreenQuad method render.

public void render(Camera camera, final Matrix4 vpMatrix, final Matrix4 projMatrix, final Matrix4 vMatrix, final Matrix4 parentMatrix, Material sceneMaterial) {
    final Matrix4 pMatrix = mCamera.getProjectionMatrix();
    final Matrix4 viewMatrix = mCamera.getViewMatrix();
    mVPMatrix.setAll(pMatrix).multiply(viewMatrix);
    super.render(mCamera, mVPMatrix, projMatrix, viewMatrix, null, sceneMaterial);
}
Also used : Matrix4(org.rajawali3d.math.Matrix4)

Aggregations

Matrix4 (org.rajawali3d.math.Matrix4)13 Vector3 (org.rajawali3d.math.vector.Vector3)6 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 Test (org.junit.Test)2 Object3D (org.rajawali3d.Object3D)2 Camera2D (org.rajawali3d.cameras.Camera2D)2 Material (org.rajawali3d.materials.Material)2 Quaternion (org.rajawali3d.math.Quaternion)2 Vector2 (org.rajawali3d.math.vector.Vector2)2 SkeletalAnimationChildObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D)1 SkeletalAnimationFrame (org.rajawali3d.animation.mesh.SkeletalAnimationFrame)1 SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)1 SkeletalAnimationObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D)1 SkeletalAnimationSequence (org.rajawali3d.animation.mesh.SkeletalAnimationSequence)1 BoundingBox (org.rajawali3d.bounds.BoundingBox)1 Camera (org.rajawali3d.cameras.Camera)1 LensFlare (org.rajawali3d.extras.LensFlare)1 FlareInfo (org.rajawali3d.extras.LensFlare.FlareInfo)1 AwdProperties (org.rajawali3d.loader.LoaderAWD.AwdProperties)1 BlockHeader (org.rajawali3d.loader.LoaderAWD.BlockHeader)1