Search in sources :

Example 1 with Vector2

use of org.rajawali3d.math.vector.Vector2 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 2 with Vector2

use of org.rajawali3d.math.vector.Vector2 in project Rajawali by Rajawali.

the class LensFlarePlugin method render.

@Override
public void render() {
    super.render();
    int f, i, numLensFlares = mLensFlares.size();
    // Calculate world space position to normalized screen space.
    double viewportWidth = mRenderer.getViewportWidth(), viewportHeight = mRenderer.getDefaultViewportHeight();
    double invAspect = viewportHeight / viewportWidth;
    double size;
    Vector2 scale = new Vector2();
    double halfViewportWidth = viewportWidth / 2;
    double halfViewportHeight = viewportHeight / 2;
    Vector3 screenPosition = new Vector3();
    double screenPositionPixels_x, screenPositionPixels_y;
    Camera camera = mRenderer.getCurrentScene().getCamera();
    Matrix4 viewMatrix = camera.getViewMatrix().clone(), projMatrix = camera.getProjectionMatrix().clone();
    useProgram(mProgram);
    // Push the VBOs to the GPU.
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGeometry.getVertexBufferInfo().bufferHandle);
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glVertexAttribPointer(maPositionHandle, 2, GLES20.GL_FLOAT, false, 0, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    // Push texture coordinates to the GPU.
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGeometry.getTexCoordBufferInfo().bufferHandle);
    GLES20.glEnableVertexAttribArray(maTextureCoordHandle);
    GLES20.glVertexAttribPointer(maTextureCoordHandle, 2, GLES20.GL_FLOAT, false, 0, 0);
    // Push vertex element indices to the GPU.
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, mGeometry.getIndexBufferInfo().bufferHandle);
    // Set up texture locations.
    GLES20.glUniform1i(muOcclusionMapTextureHandle, 0);
    GLES20.glUniform1i(muMapTextureHandle, 1);
    GLES20.glDisable(GLES20.GL_CULL_FACE);
    GLES20.glDepthMask(false);
    // Calculate camera direction vector.
    Vector3 cameraPosition = camera.getPosition().clone();
    Vector3 cameraLookAt = camera.getLookAt() != null ? camera.getLookAt().clone() : new Vector3(0, 0, 1);
    Vector3 cameraDirection = cameraLookAt.clone().subtract(cameraPosition);
    cameraDirection.normalize();
    synchronized (mLensFlares) {
        for (i = 0; i < numLensFlares; i++) {
            size = 16 / viewportHeight;
            scale.setX(size * invAspect);
            scale.setY(size);
            LensFlare lensFlare = mLensFlares.get(i);
            // Calculate normalized device coordinates.
            screenPosition.setAll(lensFlare.getPosition().clone());
            screenPosition.multiply(viewMatrix);
            screenPosition.project(projMatrix);
            // Calculate actual device coordinates.
            screenPositionPixels_x = screenPosition.x * halfViewportWidth + halfViewportWidth;
            screenPositionPixels_y = screenPosition.y * halfViewportHeight + halfViewportHeight;
            // Calculate the angle between the camera and the light vector.
            Vector3 lightToCamDirection = lensFlare.getPosition().clone().subtract(cameraPosition);
            lightToCamDirection.normalize();
            double angleLightCamera = lightToCamDirection.dot(cameraDirection);
            // viewing frustum.
            if (mVertexTextureSupported || (angleLightCamera > 0 && screenPositionPixels_x > -64 && screenPositionPixels_x < viewportWidth + 64 && screenPositionPixels_y > -64 && screenPositionPixels_y < viewportHeight + 64)) {
                // Bind current framebuffer to texture.
                GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mMapTexture.getTextureId());
                GLES20.glCopyTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, (int) screenPositionPixels_x - 8, (int) screenPositionPixels_y - 8, 16, 16, 0);
                // First render pass.
                GLES20.glUniform1i(muRenderTypeHandle, 1);
                GLES20.glUniform2fv(muScaleHandle, 1, new float[] { (float) scale.getX(), (float) scale.getY() }, 0);
                GLES20.glUniform3fv(muScreenPositionHandle, 1, new float[] { (float) screenPosition.x, (float) screenPosition.y, (float) screenPosition.z }, 0);
                GLES20.glDisable(GLES20.GL_BLEND);
                GLES20.glEnable(GLES20.GL_DEPTH_TEST);
                GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_INT, 0);
                // Copy result to occlusion map.
                GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mOcclusionMapTexture.getTextureId());
                GLES20.glCopyTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, (int) screenPositionPixels_x - 8, (int) screenPositionPixels_y - 8, 16, 16, 0);
                // Second render pass.
                GLES20.glUniform1i(muRenderTypeHandle, 2);
                GLES20.glDisable(GLES20.GL_DEPTH_TEST);
                GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mMapTexture.getTextureId());
                GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_INT, 0);
                // Update the flare's screen positions.
                lensFlare.setPositionScreen(screenPosition);
                lensFlare.updateLensFlares();
                // Third render pass.
                GLES20.glUniform1i(muRenderTypeHandle, 3);
                GLES20.glEnable(GLES20.GL_BLEND);
                for (f = 0; f < lensFlare.getLensFlares().size(); f++) {
                    FlareInfo sprite = lensFlare.getLensFlares().get(f);
                    // Don't bother rendering if the sprite's too transparent or too small.
                    if (sprite.getOpacity() > 0.001 && sprite.getScale() > 0.001) {
                        screenPosition.setAll(sprite.getScreenPosition());
                        // Calculate pixel size to normalized size
                        size = sprite.getSize() * sprite.getScale() / viewportHeight;
                        scale.setX(size * invAspect);
                        scale.setY(size);
                        GLES20.glUniform3fv(muScreenPositionHandle, 1, new float[] { (float) screenPosition.x, (float) screenPosition.y, (float) screenPosition.z }, 0);
                        GLES20.glUniform2fv(muScaleHandle, 1, new float[] { (float) scale.getX(), (float) scale.getY() }, 0);
                        GLES20.glUniform1f(muRotationHandle, (float) sprite.getRotation());
                        GLES20.glUniform1f(muOpacityHandle, (float) sprite.getOpacity());
                        GLES20.glUniform3fv(muColorHandle, 1, new float[] { (float) sprite.getColor().x, (float) sprite.getColor().y, (float) sprite.getColor().z }, 0);
                        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
                        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, sprite.getTexture().getTextureId());
                        //GLES20.glBlendEquation(GLES20.GL_FUNC_ADD);
                        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
                        // Draw the elements.
                        GLES20.glDrawElements(GLES20.GL_TRIANGLES, mGeometry.getNumIndices(), GLES20.GL_UNSIGNED_INT, 0);
                        // Unbind texture.
                        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
                        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
                    }
                }
            }
        }
    }
    // Unbind element array.
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthMask(true);
}
Also used : FlareInfo(org.rajawali3d.extras.LensFlare.FlareInfo) Vector2(org.rajawali3d.math.vector.Vector2) Vector3(org.rajawali3d.math.vector.Vector3) Camera(org.rajawali3d.cameras.Camera) Matrix4(org.rajawali3d.math.Matrix4) LensFlare(org.rajawali3d.extras.LensFlare)

Example 3 with Vector2

use of org.rajawali3d.math.vector.Vector2 in project Rajawali by Rajawali.

the class LoaderFBX method readLine.

private void readLine(BufferedReader buffer, String line) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    if (line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY).length() == 0)
        return;
    if (line.contains("{")) {
        // -- found new object
        Object last = mObjStack.peek();
        if (line.contains(":")) {
            if (line.contains(OBJECT_TYPE)) {
                String val = line.split(":")[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                Object ot = last.getClass().getDeclaredMethod("addObjectType", String.class).invoke(last, val);
                mObjStack.push(ot);
                return;
            } else if (line.contains(MODEL)) {
                String[] vals = line.split(",");
                if (vals.length < 2) {
                    // TODO add model object for Take
                    mObjStack.push(new Object());
                    return;
                }
                vals[0] = vals[0].split(": ")[1].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY);
                vals[1] = vals[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                Object mo = last.getClass().getDeclaredMethod("addModel", String.class, String.class).invoke(last, vals[0], vals[1]);
                mObjStack.push(mo);
                return;
            } else if (line.contains(MATERIAL) && !line.contains(LAYER_ELEMENT)) {
                String[] vals = line.split(": ")[1].split(",");
                vals[0] = vals[0].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY);
                Object ma = last.getClass().getDeclaredMethod("addMaterial", String.class).invoke(last, vals[0]);
                mObjStack.push(ma);
                return;
            } else if (line.contains(POSE_NODE)) {
                Object pn = last.getClass().getDeclaredMethod("addPoseNode").invoke(last);
                mObjStack.push(pn);
                return;
            } else if (line.contains(PROPERTIES)) {
                line = "Properties";
            } else if (line.contains(LAYER_ELEMENT)) {
                line = line.replaceAll("\\W|\\d", REPLACE_EMPTY);
            } else if (line.contains(LAYER)) {
                line = LAYER;
            } else if (line.contains(POSE)) {
                String val = line.split(":")[1];
                String[] vals = val.split(",");
                last.getClass().getDeclaredMethod("setPoseName", String.class).invoke(last, vals[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY));
                line = POSE;
            } else if (line.contains(TEXTURE)) {
                String val = line.split(": ")[1];
                String[] vals = val.split(",");
                Object te = last.getClass().getDeclaredMethod("addTexture", String.class, String.class).invoke(last, vals[0].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY), vals[1].replace(REGEX_NO_QUOTE, REPLACE_EMPTY));
                mObjStack.push(te);
                return;
            }
        }
        line = line.replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
        line = line.replaceAll(FBX_U, FBX_L);
        line = line.substring(0, 1).toLowerCase(Locale.US) + line.substring(1);
        try {
            Field field = last.getClass().getField(line);
            mObjStack.push(field.get(last));
        } catch (NoSuchFieldException e) {
            // -- create a generic object
            mObjStack.push(new Object());
            return;
        }
    } else if (line.contains("}")) {
        // -- end of object
        mObjStack.pop();
    } else {
        // -- found property
        Object last = mObjStack.peek();
        String[] spl = line.split(": ");
        if (spl.length == 0)
            return;
        String prop = spl[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
        prop = prop.replaceAll(FBX_U, FBX_L);
        prop = prop.substring(0, 1).toLowerCase(Locale.US) + prop.substring(1);
        boolean processNextLine = false;
        Object obj = mObjStack.peek();
        try {
            if (spl.length < 2)
                return;
            String val = spl[1];
            if (line.contains(PROPERTY)) {
                String[] vals = val.split(",");
                prop = vals[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                prop = prop.substring(0, 1).toLowerCase(Locale.US) + prop.substring(1);
                String type = vals[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                if (type.equals(TYPE_VECTOR3D) || type.equals(TYPE_COLOR) || type.equals(TYPE_COLOR_RGB) || type.equals(TYPE_LCL_ROTATION) || type.equals(TYPE_LCL_SCALING) || type.equals(TYPE_LCL_TRANSLATION) || type.equals(TYPE_VECTOR)) {
                    val = vals[3] + "," + vals[4] + "," + vals[5];
                } else {
                    if (vals.length < 4)
                        return;
                    val = vals[3].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY);
                }
            } else if (line.contains(CONNECT)) {
                String[] vals = line.substring(line.indexOf(':')).split(",");
                last.getClass().getDeclaredMethod("addConnection", String.class, String.class, String.class).invoke(last, vals[0].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY), vals[1].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY), vals[2].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY));
                return;
            }
            Field field = obj.getClass().getField(prop);
            Class<?> clazz = field.getType();
            if (clazz.equals(Integer.class)) {
                // TODO investigate why there are multiple values in TextureId sometimes
                if (val.split(",").length > 0)
                    val = val.split(",")[0];
                field.set(obj, Integer.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            } else if (clazz.equals(String.class))
                field.set(obj, val.replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY));
            else if (clazz.equals(Long.class))
                field.set(obj, Long.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            else if (clazz.equals(Float.class))
                field.set(obj, Float.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            else if (clazz.equals(Vector3.class)) {
                field.set(obj, new Vector3(val.split(",")));
            } else if (clazz.equals(FBXFloatBuffer.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll("\\s", REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXFloatBuffer(sb.toString()));
            } else if (clazz.equals(FBXIntBuffer.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll("\\s", REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXIntBuffer(sb.toString()));
            } else if (clazz.equals(FBXMatrix.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXMatrix(sb.toString()));
            } else if (clazz.equals(FBXColor4.class)) {
                field.set(obj, new FBXColor4(val));
            } else if (clazz.equals(Vector2.class)) {
                field.set(obj, new Vector2(val.replaceAll("\\s", REPLACE_EMPTY).split(",")));
            }
            if (processNextLine && line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY).length() > 0)
                readLine(buffer, line);
        } catch (NoSuchFieldException e) {
            return;
        }
    }
}
Also used : FBXColor4(org.rajawali3d.loader.fbx.FBXValues.FBXColor4) FBXMatrix(org.rajawali3d.loader.fbx.FBXValues.FBXMatrix) Vector3(org.rajawali3d.math.vector.Vector3) Field(java.lang.reflect.Field) FBXFloatBuffer(org.rajawali3d.loader.fbx.FBXValues.FBXFloatBuffer) FBXIntBuffer(org.rajawali3d.loader.fbx.FBXValues.FBXIntBuffer) Vector2(org.rajawali3d.math.vector.Vector2)

Aggregations

Vector2 (org.rajawali3d.math.vector.Vector2)3 Vector3 (org.rajawali3d.math.vector.Vector3)3 Matrix4 (org.rajawali3d.math.Matrix4)2 Field (java.lang.reflect.Field)1 Object3D (org.rajawali3d.Object3D)1 Camera (org.rajawali3d.cameras.Camera)1 LensFlare (org.rajawali3d.extras.LensFlare)1 FlareInfo (org.rajawali3d.extras.LensFlare.FlareInfo)1 FBXColor4 (org.rajawali3d.loader.fbx.FBXValues.FBXColor4)1 FBXFloatBuffer (org.rajawali3d.loader.fbx.FBXValues.FBXFloatBuffer)1 FBXIntBuffer (org.rajawali3d.loader.fbx.FBXValues.FBXIntBuffer)1 FBXMatrix (org.rajawali3d.loader.fbx.FBXValues.FBXMatrix)1 Quaternion (org.rajawali3d.math.Quaternion)1