Search in sources :

Example 1 with ATexture

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

the class ATextureFragmentShaderFragment method setLocations.

@Override
public void setLocations(int programHandle) {
    if (mTextures == null)
        return;
    for (int i = 0; i < mTextures.size(); i++) {
        ATexture texture = mTextures.get(i);
        muTextureHandles[i] = getUniformLocation(programHandle, texture.getTextureName());
        muInfluenceHandles[i] = getUniformLocation(programHandle, DefaultShaderVar.U_INFLUENCE, texture.getTextureName());
        if (texture.getWrapType() == WrapType.REPEAT)
            muRepeatHandles[i] = getUniformLocation(programHandle, DefaultShaderVar.U_REPEAT, i);
        if (texture.offsetEnabled())
            muOffsetHandles[i] = getUniformLocation(programHandle, DefaultShaderVar.U_OFFSET, i);
    }
}
Also used : ATexture(org.rajawali3d.materials.textures.ATexture)

Example 2 with ATexture

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

the class ATextureFragmentShaderFragment method initialize.

@Override
public void initialize() {
    super.initialize();
    if (mTextures == null)
        return;
    int numTextures = mTextures.size();
    int textureCount = 0, cubeTextureCount = 0, videoTextureCount = 0;
    for (int i = 0; i < mTextures.size(); i++) {
        ATexture texture = mTextures.get(i);
        if (texture.getTextureType() == TextureType.CUBE_MAP)
            cubeTextureCount++;
        else if (texture.getTextureType() == TextureType.VIDEO_TEXTURE)
            videoTextureCount++;
        else
            textureCount++;
    }
    if (textureCount > 0)
        muTextures = new RSampler2D[textureCount];
    if (cubeTextureCount > 0)
        muCubeTextures = new RSamplerCube[cubeTextureCount];
    if (videoTextureCount > 0)
        muVideoTextures = new RSamplerExternalOES[videoTextureCount];
    muInfluence = new RFloat[numTextures];
    muRepeat = new RVec2[numTextures];
    muOffset = new RVec2[numTextures];
    muTextureHandles = new int[numTextures];
    muInfluenceHandles = new int[numTextures];
    muRepeatHandles = new int[numTextures];
    muOffsetHandles = new int[numTextures];
    textureCount = 0;
    cubeTextureCount = 0;
    videoTextureCount = 0;
    for (int i = 0; i < mTextures.size(); i++) {
        ATexture texture = mTextures.get(i);
        if (texture.getTextureType() == TextureType.CUBE_MAP)
            muCubeTextures[textureCount++] = (RSamplerCube) addUniform(texture.getTextureName(), DataType.SAMPLERCUBE);
        else if (texture.getTextureType() == TextureType.VIDEO_TEXTURE)
            muVideoTextures[videoTextureCount++] = (RSamplerExternalOES) addUniform(texture.getTextureName(), DataType.SAMPLER_EXTERNAL_EOS);
        else
            muTextures[textureCount++] = (RSampler2D) addUniform(texture.getTextureName(), DataType.SAMPLER2D);
        muInfluence[i] = (RFloat) addUniform(DefaultShaderVar.U_INFLUENCE, texture.getTextureName());
        if (texture.getWrapType() == WrapType.REPEAT)
            muRepeat[i] = (RVec2) addUniform(DefaultShaderVar.U_REPEAT, i);
        if (texture.offsetEnabled())
            muOffset[i] = (RVec2) addUniform(DefaultShaderVar.U_OFFSET, i);
    }
}
Also used : ATexture(org.rajawali3d.materials.textures.ATexture)

Example 3 with ATexture

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

the class Material method unbindTextures.

/**
     * Unbinds the texture from an OpenGL texturing target.
     */
public void unbindTextures() {
    int num = mTextureList.size();
    if (mPlugins != null)
        for (IMaterialPlugin plugin : mPlugins) plugin.unbindTextures();
    for (int i = 0; i < num; i++) {
        ATexture texture = mTextureList.get(i);
        GLES20.glBindTexture(texture.getGLTextureType(), 0);
    }
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}
Also used : IMaterialPlugin(org.rajawali3d.materials.plugins.IMaterialPlugin) ATexture(org.rajawali3d.materials.textures.ATexture)

Example 4 with ATexture

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

the class Material method createShaders.

/**
     * Takes all material parameters and creates the vertex shader and fragment shader and then compiles the program.
     * This method should only be called on initialization or when parameters have changed.
     */
protected void createShaders() {
    if (!mIsDirty)
        return;
    if (mCustomVertexShader == null && mCustomFragmentShader == null) {
        //
        // -- Check textures
        //
        List<ATexture> diffuseTextures = null;
        List<ATexture> normalMapTextures = null;
        List<ATexture> envMapTextures = null;
        List<ATexture> skyTextures = null;
        List<ATexture> specMapTextures = null;
        List<ATexture> alphaMapTextures = null;
        boolean hasCubeMaps = false;
        boolean hasVideoTexture = false;
        for (int i = 0; i < mTextureList.size(); i++) {
            ATexture texture = mTextureList.get(i);
            switch(texture.getTextureType()) {
                case VIDEO_TEXTURE:
                    hasVideoTexture = true;
                // no break statement, add the video texture to the diffuse textures
                case DIFFUSE:
                case RENDER_TARGET:
                    if (diffuseTextures == null)
                        diffuseTextures = new ArrayList<>();
                    diffuseTextures.add(texture);
                    break;
                case NORMAL:
                    if (normalMapTextures == null)
                        normalMapTextures = new ArrayList<>();
                    normalMapTextures.add(texture);
                    break;
                case CUBE_MAP:
                    hasCubeMaps = true;
                case SPHERE_MAP:
                    boolean isSkyTexture = false;
                    boolean isEnvironmentTexture = false;
                    if (texture.getClass() == SphereMapTexture.class) {
                        isSkyTexture = ((SphereMapTexture) texture).isSkyTexture();
                        isEnvironmentTexture = ((SphereMapTexture) texture).isEnvironmentTexture();
                    } else if (texture.getClass() == CubeMapTexture.class) {
                        isSkyTexture = ((CubeMapTexture) texture).isSkyTexture();
                        isEnvironmentTexture = ((CubeMapTexture) texture).isEnvironmentTexture();
                    }
                    if (isSkyTexture) {
                        if (skyTextures == null)
                            skyTextures = new ArrayList<>();
                        skyTextures.add(texture);
                    } else if (isEnvironmentTexture) {
                        if (envMapTextures == null)
                            envMapTextures = new ArrayList<>();
                        envMapTextures.add(texture);
                    }
                    break;
                case SPECULAR:
                    if (specMapTextures == null)
                        specMapTextures = new ArrayList<>();
                    specMapTextures.add(texture);
                    break;
                case ALPHA:
                    if (alphaMapTextures == null)
                        alphaMapTextures = new ArrayList<>();
                    alphaMapTextures.add(texture);
                    break;
                default:
                    break;
            }
        }
        mVertexShader = new VertexShader();
        mVertexShader.enableTime(mTimeEnabled);
        mVertexShader.hasCubeMaps(hasCubeMaps);
        mVertexShader.hasSkyTexture(skyTextures != null && skyTextures.size() > 0);
        mVertexShader.useVertexColors(mUseVertexColors);
        onPreVertexShaderInitialize(mVertexShader);
        mVertexShader.initialize();
        mFragmentShader = new FragmentShader();
        mFragmentShader.enableTime(mTimeEnabled);
        mFragmentShader.hasCubeMaps(hasCubeMaps);
        onPreFragmentShaderInitialize(mFragmentShader);
        mFragmentShader.initialize();
        if (diffuseTextures != null && diffuseTextures.size() > 0) {
            DiffuseTextureFragmentShaderFragment fragment = new DiffuseTextureFragmentShaderFragment(diffuseTextures);
            mFragmentShader.addShaderFragment(fragment);
        }
        if (normalMapTextures != null && normalMapTextures.size() > 0) {
            NormalMapFragmentShaderFragment fragment = new NormalMapFragmentShaderFragment(normalMapTextures);
            mFragmentShader.addShaderFragment(fragment);
        }
        if (envMapTextures != null && envMapTextures.size() > 0) {
            EnvironmentMapFragmentShaderFragment fragment = new EnvironmentMapFragmentShaderFragment(envMapTextures);
            mFragmentShader.addShaderFragment(fragment);
        }
        if (skyTextures != null && skyTextures.size() > 0) {
            SkyTextureFragmentShaderFragment fragment = new SkyTextureFragmentShaderFragment(skyTextures);
            mFragmentShader.addShaderFragment(fragment);
        }
        if (hasVideoTexture)
            mFragmentShader.addPreprocessorDirective("#extension GL_OES_EGL_image_external : require");
        checkForPlugins(PluginInsertLocation.PRE_LIGHTING);
        if (mLightingEnabled && mLights != null && mLights.size() > 0) {
            mVertexShader.setLights(mLights);
            mFragmentShader.setLights(mLights);
            mLightsVertexShaderFragment = new LightsVertexShaderFragment(mLights);
            mLightsVertexShaderFragment.setAmbientColor(mAmbientColor);
            mLightsVertexShaderFragment.setAmbientIntensity(mAmbientIntensity);
            mVertexShader.addShaderFragment(mLightsVertexShaderFragment);
            mFragmentShader.addShaderFragment(new LightsFragmentShaderFragment(mLights));
            checkForPlugins(PluginInsertLocation.PRE_DIFFUSE);
            if (mDiffuseMethod != null) {
                mDiffuseMethod.setLights(mLights);
                IShaderFragment fragment = mDiffuseMethod.getVertexShaderFragment();
                if (fragment != null)
                    mVertexShader.addShaderFragment(fragment);
                fragment = mDiffuseMethod.getFragmentShaderFragment();
                mFragmentShader.addShaderFragment(fragment);
            }
            checkForPlugins(PluginInsertLocation.PRE_SPECULAR);
            if (mSpecularMethod != null) {
                mSpecularMethod.setLights(mLights);
                mSpecularMethod.setTextures(specMapTextures);
                IShaderFragment fragment = mSpecularMethod.getVertexShaderFragment();
                if (fragment != null)
                    mVertexShader.addShaderFragment(fragment);
                fragment = mSpecularMethod.getFragmentShaderFragment();
                if (fragment != null)
                    mFragmentShader.addShaderFragment(fragment);
            }
        }
        checkForPlugins(PluginInsertLocation.PRE_ALPHA);
        if (alphaMapTextures != null && alphaMapTextures.size() > 0) {
            AlphaMapFragmentShaderFragment fragment = new AlphaMapFragmentShaderFragment(alphaMapTextures);
            mFragmentShader.addShaderFragment(fragment);
        }
        checkForPlugins(PluginInsertLocation.PRE_TRANSFORM);
        checkForPlugins(PluginInsertLocation.POST_TRANSFORM);
        mVertexShader.buildShader();
        mFragmentShader.buildShader();
    } else {
        mVertexShader = mCustomVertexShader;
        mFragmentShader = mCustomFragmentShader;
        if (mVertexShader.needsBuild())
            mVertexShader.initialize();
        if (mFragmentShader.needsBuild())
            mFragmentShader.initialize();
        if (mVertexShader.needsBuild())
            mVertexShader.buildShader();
        if (mFragmentShader.needsBuild())
            mFragmentShader.buildShader();
    }
    if (RajLog.isDebugEnabled()) {
        RajLog.d("-=-=-=- VERTEX SHADER -=-=-=-");
        RajLog.d(mVertexShader.getShaderString());
        RajLog.d("-=-=-=- FRAGMENT SHADER -=-=-=-");
        RajLog.d(mFragmentShader.getShaderString());
    }
    mProgramHandle = createProgram(mVertexShader.getShaderString(), mFragmentShader.getShaderString());
    if (mProgramHandle == 0) {
        mIsDirty = false;
        return;
    }
    mVertexShader.setLocations(mProgramHandle);
    mFragmentShader.setLocations(mProgramHandle);
    for (String name : mTextureHandles.keySet()) {
        setTextureHandleForName(name);
    }
    for (int i = 0; i < mTextureList.size(); i++) {
        setTextureParameters(mTextureList.get(i));
    }
    mIsDirty = false;
}
Also used : FragmentShader(org.rajawali3d.materials.shaders.FragmentShader) VertexShader(org.rajawali3d.materials.shaders.VertexShader) NormalMapFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.texture.NormalMapFragmentShaderFragment) EnvironmentMapFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.texture.EnvironmentMapFragmentShaderFragment) ArrayList(java.util.ArrayList) DiffuseTextureFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.texture.DiffuseTextureFragmentShaderFragment) SkyTextureFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.texture.SkyTextureFragmentShaderFragment) IShaderFragment(org.rajawali3d.materials.shaders.IShaderFragment) ATexture(org.rajawali3d.materials.textures.ATexture) LightsVertexShaderFragment(org.rajawali3d.materials.shaders.fragments.LightsVertexShaderFragment) AlphaMapFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.texture.AlphaMapFragmentShaderFragment) CubeMapTexture(org.rajawali3d.materials.textures.CubeMapTexture) LightsFragmentShaderFragment(org.rajawali3d.materials.shaders.fragments.LightsFragmentShaderFragment)

Example 5 with ATexture

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

the class ATextureFragmentShaderFragment method applyParams.

@Override
public void applyParams() {
    super.applyParams();
    if (mTextures == null)
        return;
    for (int i = 0; i < mTextures.size(); i++) {
        ATexture texture = mTextures.get(i);
        GLES20.glUniform1f(muInfluenceHandles[i], texture.getInfluence());
        if (texture.getWrapType() == WrapType.REPEAT)
            GLES20.glUniform2fv(muRepeatHandles[i], 1, texture.getRepeat(), 0);
        if (texture.offsetEnabled())
            GLES20.glUniform2fv(muOffsetHandles[i], 1, texture.getOffset(), 0);
    }
}
Also used : ATexture(org.rajawali3d.materials.textures.ATexture)

Aggregations

ATexture (org.rajawali3d.materials.textures.ATexture)6 ArrayList (java.util.ArrayList)1 IMaterialPlugin (org.rajawali3d.materials.plugins.IMaterialPlugin)1 FragmentShader (org.rajawali3d.materials.shaders.FragmentShader)1 IShaderFragment (org.rajawali3d.materials.shaders.IShaderFragment)1 VertexShader (org.rajawali3d.materials.shaders.VertexShader)1 LightsFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.LightsFragmentShaderFragment)1 LightsVertexShaderFragment (org.rajawali3d.materials.shaders.fragments.LightsVertexShaderFragment)1 AlphaMapFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.texture.AlphaMapFragmentShaderFragment)1 DiffuseTextureFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.texture.DiffuseTextureFragmentShaderFragment)1 EnvironmentMapFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.texture.EnvironmentMapFragmentShaderFragment)1 NormalMapFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.texture.NormalMapFragmentShaderFragment)1 SkyTextureFragmentShaderFragment (org.rajawali3d.materials.shaders.fragments.texture.SkyTextureFragmentShaderFragment)1 CubeMapTexture (org.rajawali3d.materials.textures.CubeMapTexture)1