lwjgltexturestate.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 1,400 行 · 第 1/5 页

JAVA
1,400
字号
            for (int i = 0; i < numTotalTexUnits; i++) {
                unitRecord = record.units[i];

                // grab a texture for this unit, if available
                texture = getTexture(i);

                // check for invalid textures - ones that have no opengl id and
                // no image data
                if (texture != null && texture.getTextureId() == 0
                        && texture.getImage() == null)
                    texture = null;

                // null textures above fixed limit do not need to be disabled
                // since they are not really part of the pipeline.
                if (texture == null) {
                    if (i >= numFixedTexUnits)
                        continue;
                    else {
                        // a null texture indicates no texturing at this unit
                        // Disable texturing on this unit if enabled.
                        disableTexturing(unitRecord, record, i);

                        if (i < idCache.length)
                            idCache[i] = 0;

                        // next texture!
                        continue;
                    }
                }

                type = texture.getType();

                // disable other texturing types for this unit, if enabled.
                disableTexturing(unitRecord, record, i, type);

                // Time to bind the texture, so see if we need to load in image
                // data for this texture.
                if (texture.getTextureId() == 0) {
                    // texture not yet loaded.
                    // this will load and bind and set the records...
                    load(i);
                    if (texture.getTextureId() == 0)
                        continue;
                } else {
                    // texture already exists in OpenGL, just bind it if needed
                    if (!unitRecord.isValid()
                            || unitRecord.boundTexture != texture
                                    .getTextureId()) {
                        checkAndSetUnit(i, record);
                        GL11.glBindTexture(getGLType(type), texture
                                .getTextureId());
                        if (Debug.stats) {
                            StatCollector.addStat(StatType.STAT_TEXTURE_BINDS, 1);
                        }
                        unitRecord.boundTexture = texture.getTextureId();
                    }
                }

                // Grab our record for this texture
                texRecord = record.getTextureRecord(texture.getTextureId(),
                        texture.getType());

                // Set the idCache value for this unit of this texture state
                // This is done so during state comparison we don't have to
                // spend a lot of time pulling out classes and finding field
                // data.
                idCache[i] = texture.getTextureId();

                // Some texture things only apply to fixed function pipeline
                if (i < numFixedTexUnits) {

                    // Enable 2D texturing on this unit if not enabled.
                    if (!unitRecord.isValid()
                            || !unitRecord.enabled[type.ordinal()]) {
                        checkAndSetUnit(i, record);
                        GL11.glEnable(getGLType(type));
                        unitRecord.enabled[type.ordinal()] = true;
                    }

                    // Set our blend color, if needed.
                    applyBlendColor(texture, unitRecord, i, record);

                    // Set the texture environment mode if this unit isn't
                    // already set properly
                    applyEnvMode(texture.getApply(), unitRecord, i, record);

                    // If our mode is combine, and we support multitexturing
                    // apply combine settings.
                    if (texture.getApply() == ApplyMode.Combine
                            && supportsMultiTexture && supportsEnvCombine) {
                        applyCombineFactors(texture, unitRecord, i, record);
                    }
                }

                // Other items only apply to textures below the frag unit limit
                if (i < numFragmentTexUnits) {

                    // texture specific params
                    applyFilter(texture, texRecord, i, record);
                    applyWrap(texture, texRecord, i, record);
                    applyShadow(texture, texRecord, i, record);
                    
                    // Set our border color, if needed.
                    applyBorderColor(texture, texRecord, i, record);

                    // all states have now been applied for a tex record, so we
                    // can safely make it valid
                    if (!texRecord.isValid())
                        texRecord.validate();

                }

                // Other items only apply to textures below the frag tex coord
                // unit limit
                if (i < numFragmentTexCoordUnits) {

                    // Now time to play with texture matrices
                    // Determine which transforms to do.
                    applyTextureTransforms(texture, i, record);

                    // Now let's look at automatic texture coordinate
                    // generation.
                    applyTexCoordGeneration(texture, unitRecord, i, record);

                }

            }

        } else {
            // turn off texturing
            TextureUnitRecord unitRecord;

            if (supportsMultiTexture) {
                for (int i = 0; i < numFixedTexUnits; i++) {
                    unitRecord = record.units[i];
                    disableTexturing(unitRecord, record, i);
                }
            } else {
                unitRecord = record.units[0];
                disableTexturing(unitRecord, record, 0);
            }
        }

        if (!record.isValid())
            record.validate();
    }

    private static void disableTexturing(TextureUnitRecord unitRecord,
            TextureStateRecord record, int unit, Type exceptedType) {
        if (exceptedType != Type.TwoDimensional) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.TwoDimensional.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(GL11.GL_TEXTURE_2D);
                unitRecord.enabled[Type.TwoDimensional.ordinal()] = false;
            }
        }

        if (exceptedType != Type.OneDimensional) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.OneDimensional.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(GL11.GL_TEXTURE_1D);
                unitRecord.enabled[Type.OneDimensional.ordinal()] = false;
            }
        }

        if (supportsTexture3D && exceptedType != Type.ThreeDimensional) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.ThreeDimensional.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(GL12.GL_TEXTURE_3D);
                unitRecord.enabled[Type.ThreeDimensional.ordinal()] = false;
            }
        }

        if (supportsTextureCubeMap && exceptedType != Type.CubeMap) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.CubeMap.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(ARBTextureCubeMap.GL_TEXTURE_CUBE_MAP_ARB);
                unitRecord.enabled[Type.CubeMap.ordinal()] = false;
            }
        }

    }

    private static void disableTexturing(TextureUnitRecord unitRecord,
            TextureStateRecord record, int unit) {
        if (!unitRecord.isValid()
                || unitRecord.enabled[Type.TwoDimensional.ordinal()]) {
            // Check we are in the right unit
            checkAndSetUnit(unit, record);
            GL11.glDisable(GL11.GL_TEXTURE_2D);
            unitRecord.enabled[Type.TwoDimensional.ordinal()] = false;
        }

        if (!unitRecord.isValid()
                || unitRecord.enabled[Type.OneDimensional.ordinal()]) {
            // Check we are in the right unit
            checkAndSetUnit(unit, record);
            GL11.glDisable(GL11.GL_TEXTURE_1D);
            unitRecord.enabled[Type.OneDimensional.ordinal()] = false;
        }

        if (supportsTexture3D) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.ThreeDimensional.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(GL12.GL_TEXTURE_3D);
                unitRecord.enabled[Type.ThreeDimensional.ordinal()] = false;
            }
        }

        if (supportsTextureCubeMap) {
            if (!unitRecord.isValid()
                    || unitRecord.enabled[Type.CubeMap.ordinal()]) {
                // Check we are in the right unit
                checkAndSetUnit(unit, record);
                GL11.glDisable(ARBTextureCubeMap.GL_TEXTURE_CUBE_MAP_ARB);
                unitRecord.enabled[Type.CubeMap.ordinal()] = false;
            }
        }

    }

    public static void applyCombineFactors(Texture texture,
            TextureUnitRecord unitRecord, int unit, TextureStateRecord record) {
        // check that this is a valid fixed function unit. glTexEnv is only
        // supported for unit < GL_MAX_TEXTURE_UNITS
        if (unit >= numFixedTexUnits) {
            return;
        }

        // first thing's first... if we are doing dot3 and don't
        // support it, disable this texture.
        boolean checked = false;
        if (!supportsEnvDot3
                && (texture.getCombineFuncRGB() == CombinerFunctionRGB.Dot3RGB || texture
                        .getCombineFuncRGB() == CombinerFunctionRGB.Dot3RGBA)) {

            // disable
            disableTexturing(unitRecord, record, unit);

            // No need to continue
            return;
        }

        // Okay, now let's set our scales if we need to:
        // First RGB Combine scale
        if (!unitRecord.isValid()
                || unitRecord.envRGBScale != texture.getCombineScaleRGB()) {
            if (!checked) {
                checkAndSetUnit(unit, record);
                checked = true;
            }
            GL11.glTexEnvf(GL11.GL_TEXTURE_ENV,
                    ARBTextureEnvCombine.GL_RGB_SCALE_ARB, texture
                            .getCombineScaleRGB().floatValue());
            unitRecord.envRGBScale = texture.getCombineScaleRGB();
        }
        // Then Alpha Combine scale
        if (!unitRecord.isValid()
                || unitRecord.envAlphaScale != texture.getCombineScaleAlpha()) {
            if (!checked) {
                checkAndSetUnit(unit, record);
                checked = true;
            }
            GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_ALPHA_SCALE, texture
                    .getCombineScaleAlpha().floatValue());
            unitRecord.envAlphaScale = texture.getCombineScaleAlpha();
        }

        // Time to set the RGB combines
        CombinerFunctionRGB rgbCombineFunc = texture.getCombineFuncRGB();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?