Search in sources :

Example 1 with FloatBuffer

use of java.nio.FloatBuffer in project android_frameworks_base by ParanoidAndroid.

the class GLLogWrapper method doArrayElement.

private void doArrayElement(StringBuilder builder, boolean enabled, String name, PointerInfo pointer, int index) {
    if (!enabled) {
        return;
    }
    builder.append(" ");
    builder.append(name + ":{");
    if (pointer == null || pointer.mTempByteBuffer == null) {
        builder.append("undefined }");
        return;
    }
    if (pointer.mStride < 0) {
        builder.append("invalid stride");
        return;
    }
    int stride = pointer.getStride();
    ByteBuffer byteBuffer = pointer.mTempByteBuffer;
    int size = pointer.mSize;
    int type = pointer.mType;
    int sizeofType = pointer.sizeof(type);
    int byteOffset = stride * index;
    for (int i = 0; i < size; i++) {
        if (i > 0) {
            builder.append(", ");
        }
        switch(type) {
            case GL_BYTE:
                {
                    byte d = byteBuffer.get(byteOffset);
                    builder.append(Integer.toString(d));
                }
                break;
            case GL_UNSIGNED_BYTE:
                {
                    byte d = byteBuffer.get(byteOffset);
                    builder.append(Integer.toString(0xff & d));
                }
                break;
            case GL_SHORT:
                {
                    ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
                    short d = shortBuffer.get(byteOffset / 2);
                    builder.append(Integer.toString(d));
                }
                break;
            case GL_FIXED:
                {
                    IntBuffer intBuffer = byteBuffer.asIntBuffer();
                    int d = intBuffer.get(byteOffset / 4);
                    builder.append(Integer.toString(d));
                }
                break;
            case GL_FLOAT:
                {
                    FloatBuffer intBuffer = byteBuffer.asFloatBuffer();
                    float d = intBuffer.get(byteOffset / 4);
                    builder.append(Float.toString(d));
                }
                break;
            default:
                builder.append("?");
                break;
        }
        byteOffset += sizeofType;
    }
    builder.append("}");
}
Also used : IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 2 with FloatBuffer

use of java.nio.FloatBuffer in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method parseAsFloatArray.

private FloatBuffer parseAsFloatArray(ReadableArray array) {
    int size = array.size();
    FloatBuffer buf = ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    for (int i = 0; i < size; i++) buf.put((float) array.getDouble(i));
    buf.position(0);
    return buf;
}
Also used : FloatBuffer(java.nio.FloatBuffer)

Example 3 with FloatBuffer

use of java.nio.FloatBuffer in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method recSyncData.

private GLRenderData recSyncData(GLData data, HashMap<Uri, GLImage> images) {
    Map<Uri, GLImage> prevImages = this.images;
    GLShader shader = getShader(data.shader);
    if (shader == null || !shader.ensureCompile())
        return null;
    Map<String, Integer> uniformsInteger = new HashMap<>();
    Map<String, Float> uniformsFloat = new HashMap<>();
    Map<String, IntBuffer> uniformsIntBuffer = new HashMap<>();
    Map<String, FloatBuffer> uniformsFloatBuffer = new HashMap<>();
    Map<String, GLTexture> textures = new HashMap<>();
    List<GLRenderData> contextChildren = new ArrayList<>();
    List<GLRenderData> children = new ArrayList<>();
    for (GLData child : data.contextChildren) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        contextChildren.add(node);
    }
    for (GLData child : data.children) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        children.add(node);
    }
    Map<String, Integer> uniformTypes = shader.getUniformTypes();
    List<String> uniformNames = shader.getUniformNames();
    Map<String, Integer> uniformSizes = shader.getUniformSizes();
    int units = 0;
    ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
    while (iterator.hasNextKey()) {
        String uniformName = iterator.nextKey();
        int type = uniformTypes.get(uniformName);
        int size = uniformSizes.get(uniformName);
        ReadableMap dataUniforms = data.uniforms;
        if (type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
            uniformsInteger.put(uniformName, units++);
            if (dataUniforms.isNull(uniformName)) {
                GLTexture emptyTexture = new GLTexture(this);
                emptyTexture.setPixelsEmpty();
                textures.put(uniformName, emptyTexture);
            } else {
                ReadableMap value = null;
                try {
                    value = dataUniforms.getMap(uniformName);
                } catch (Exception e) {
                    shader.runtimeException("texture uniform '" + uniformName + "': you cannot directly give require('./img.png') " + "to gl-react, use resolveAssetSource(require('./img.png')) instead.");
                    return null;
                }
                String t = value.getString("type");
                if (t.equals("content")) {
                    int id = value.getInt("id");
                    if (id >= contentTextures.size()) {
                        resizeUniformContentTextures(id + 1);
                    }
                    textures.put(uniformName, contentTextures.get(id));
                } else if (t.equals("fbo")) {
                    int id = value.getInt("id");
                    GLFBO fbo = getFBO(id);
                    textures.put(uniformName, fbo.color.get(0));
                } else if (t.equals("uri")) {
                    final Uri src = srcResource(value);
                    if (src == null) {
                        shader.runtimeException("texture uniform '" + uniformName + "': Invalid uri format '" + value + "'");
                    }
                    GLImage image = images.get(src);
                    if (image == null) {
                        image = prevImages.get(src);
                        if (image != null)
                            images.put(src, image);
                    }
                    if (image == null) {
                        image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {

                            public void run() {
                                onImageLoad(src);
                            }
                        });
                        image.setSrc(src);
                        images.put(src, image);
                    }
                    textures.put(uniformName, image.getTexture());
                } else {
                    shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
                }
            }
        } else {
            if (size == 1) {
                switch(type) {
                    case GL_INT:
                        uniformsInteger.put(uniformName, dataUniforms.getInt(uniformName));
                        break;
                    case GL_BOOL:
                        uniformsInteger.put(uniformName, dataUniforms.getBoolean(uniformName) ? 1 : 0);
                        break;
                    case GL_FLOAT:
                        uniformsFloat.put(uniformName, (float) dataUniforms.getDouble(uniformName));
                        break;
                    case GL_FLOAT_VEC2:
                    case GL_FLOAT_VEC3:
                    case GL_FLOAT_VEC4:
                    case GL_FLOAT_MAT2:
                    case GL_FLOAT_MAT3:
                    case GL_FLOAT_MAT4:
                        ReadableArray arr = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr));
                        break;
                    case GL_INT_VEC2:
                    case GL_INT_VEC3:
                    case GL_INT_VEC4:
                    case GL_BOOL_VEC2:
                    case GL_BOOL_VEC3:
                    case GL_BOOL_VEC4:
                        ReadableArray arr2 = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr2.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                        break;
                    default:
                        shader.runtimeException("uniform '" + uniformName + "': type not supported: " + type);
                }
            } else {
                ReadableArray array = dataUniforms.getArray(uniformName);
                if (size != array.size()) {
                    shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + array.size() + ". Expected: " + size);
                }
                for (int i = 0; i < size; i++) {
                    String name = uniformName + "[" + i + "]";
                    switch(type) {
                        case GL_INT:
                            uniformsInteger.put(name, array.getInt(i));
                            break;
                        case GL_BOOL:
                            uniformsInteger.put(name, array.getBoolean(i) ? 1 : 0);
                            break;
                        case GL_FLOAT:
                            uniformsFloat.put(name, (float) array.getDouble(i));
                            break;
                        case GL_FLOAT_VEC2:
                        case GL_FLOAT_VEC3:
                        case GL_FLOAT_VEC4:
                        case GL_FLOAT_MAT2:
                        case GL_FLOAT_MAT3:
                        case GL_FLOAT_MAT4:
                            ReadableArray arr = array.getArray(i);
                            if (arraySizeForType(type) != arr.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsFloatBuffer.put(name, parseAsFloatArray(arr));
                            break;
                        case GL_INT_VEC2:
                        case GL_INT_VEC3:
                        case GL_INT_VEC4:
                        case GL_BOOL_VEC2:
                        case GL_BOOL_VEC3:
                        case GL_BOOL_VEC4:
                            ReadableArray arr2 = array.getArray(i);
                            if (arraySizeForType(type) != arr2.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsIntBuffer.put(name, parseAsIntArray(arr2));
                            break;
                        default:
                            shader.runtimeException("uniform '" + name + "': type not supported: " + type);
                    }
                }
            }
        }
    }
    int[] maxTextureUnits = new int[1];
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxTextureUnits, 0);
    if (units > maxTextureUnits[0]) {
        shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
    }
    for (String uniformName : uniformNames) {
        int size = uniformSizes.get(uniformName);
        if (size == 1) {
            if (!uniformsFloat.containsKey(uniformName) && !uniformsInteger.containsKey(uniformName) && !uniformsFloatBuffer.containsKey(uniformName) && !uniformsIntBuffer.containsKey(uniformName)) {
                shader.runtimeException("All defined uniforms must be provided. Missing '" + uniformName + "'");
            }
        } else {
            for (int i = 0; i < size; i++) {
                String name = uniformName + "[" + i + "]";
                if (!uniformsFloat.containsKey(name) && !uniformsInteger.containsKey(name) && !uniformsFloatBuffer.containsKey(name) && !uniformsIntBuffer.containsKey(name)) {
                    shader.runtimeException("All defined uniforms must be provided. Missing '" + name + "'");
                }
            }
        }
    }
    return new GLRenderData(shader, uniformsInteger, uniformsFloat, uniformsIntBuffer, uniformsFloatBuffer, textures, (int) (data.width * data.pixelRatio), (int) (data.height * data.pixelRatio), data.fboId, contextChildren, children);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) Uri(android.net.Uri) ReadableMap(com.facebook.react.bridge.ReadableMap) ReadableArray(com.facebook.react.bridge.ReadableArray) GLException(android.opengl.GLException) ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) IntBuffer(java.nio.IntBuffer)

Example 4 with FloatBuffer

use of java.nio.FloatBuffer in project gl-react-native by ProjectSeptemberInc.

the class GLShader method makeProgram.

private void makeProgram() throws GLShaderCompilationFailed {
    int vertex = compileShader(vert, GL_VERTEX_SHADER);
    if (vertex == -1)
        return;
    int fragment = compileShader(frag, GL_FRAGMENT_SHADER);
    if (fragment == -1)
        return;
    program = glCreateProgram();
    glAttachShader(program, vertex);
    glAttachShader(program, fragment);
    glLinkProgram(program);
    int[] linkSuccess = new int[1];
    glGetProgramiv(program, GL_LINK_STATUS, linkSuccess, 0);
    if (linkSuccess[0] == GL_FALSE) {
        runtimeException(glGetProgramInfoLog(program));
    }
    glUseProgram(program);
    validate();
    computeMeta();
    pointerLoc = glGetAttribLocation(program, "position");
    buffer = new int[1];
    glGenBuffers(1, buffer, 0);
    glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
    float[] buf = { -1.0f, -1.0f, -1.0f, 4.0f, 4.0f, -1.0f };
    FloatBuffer bufferData = ByteBuffer.allocateDirect(buf.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    bufferData.put(buf).position(0);
    glBufferData(GL_ARRAY_BUFFER, buf.length * 4, bufferData, GL_STATIC_DRAW);
}
Also used : FloatBuffer(java.nio.FloatBuffer)

Example 5 with FloatBuffer

use of java.nio.FloatBuffer in project Rajawali by Rajawali.

the class Object3D method setAtlasTile.

/**
	 * Maps the (x,y) coordinates of <code>tileName</code> in <code>atlas</code>
	 * to the TextureCoordinates of this BaseObject3D
	 *
	 * @param tileName
	 * @param atlas
	 */
public void setAtlasTile(String tileName, TextureAtlas atlas) {
    Tile tile = atlas.getTileNamed(tileName);
    FloatBuffer fb = this.getGeometry().getTextureCoords();
    for (int i = 0; i < fb.capacity(); i++) {
        double uvIn = fb.get(i);
        double uvOut;
        if (i % 2 == 0)
            uvOut = (uvIn * (tile.width / atlas.getWidth())) + tile.x / atlas.getWidth();
        else
            uvOut = (uvIn * (tile.height / atlas.getHeight())) + tile.y / atlas.getHeight();
        fb.put(i, (float) uvOut);
    }
    mGeometry.changeBufferData(mGeometry.getTexCoordBufferInfo(), fb, 0);
}
Also used : Tile(org.rajawali3d.materials.textures.TexturePacker.Tile) FloatBuffer(java.nio.FloatBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)287 ByteBuffer (java.nio.ByteBuffer)82 IntBuffer (java.nio.IntBuffer)43 ShortBuffer (java.nio.ShortBuffer)39 Vector3f (com.jme3.math.Vector3f)27 VertexBuffer (com.jme3.scene.VertexBuffer)27 DoubleBuffer (java.nio.DoubleBuffer)17 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)16 LongBuffer (java.nio.LongBuffer)10 Mesh (com.jme3.scene.Mesh)9 CharBuffer (java.nio.CharBuffer)9 FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)8 OutputPort (androidx.media.filterfw.OutputPort)7 Matrix4f (com.jme3.math.Matrix4f)7 Buffer (java.nio.Buffer)7 TempVars (com.jme3.util.TempVars)6 IOException (java.io.IOException)6 BufferOverflowException (java.nio.BufferOverflowException)6 BufferUnderflowException (java.nio.BufferUnderflowException)6 ArrayList (java.util.ArrayList)6