Java Code Examples for android.opengl.GLES20#glGenerateMipmap()

The following examples show how to use android.opengl.GLES20#glGenerateMipmap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Texture.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public int texture() {
    if (texture != 0) {
        return texture;
    }

    if (bitmap.isRecycled()) {
        return 0;
    }

    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    texture = textures[0];

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);

    boolean mipMappable = false; //isPOT(bitmap.getWidth()) && isPOT(bitmap.getHeight());
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, mipMappable ? GLES20.GL_LINEAR_MIPMAP_LINEAR : GLES20.GL_LINEAR);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    if (mipMappable) {
        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    }

    Utils.HasGLError();

    return texture;
}
 
Example 2
Source File: TextureHelper.java    From sealrtc-android with MIT License 5 votes vote down vote up
public static int loadTexture(Bitmap bmp) {
    // 生成纹理ID
    final int[] textureObjectIds = new int[1];
    GLES20.glGenTextures(1, textureObjectIds, 0);
    if (textureObjectIds[0] == 0) {
        Log.e(TAG, "loadTexture: texture Id is 0");
        return 0;
    }
    if (bmp == null) {
        Log.e(TAG, "loadTexture: BitMap is NUll");
        GLES20.glDeleteTextures(1, textureObjectIds, 0);
        return 0;
    }
    // 绑定纹理ID
    // 参数1:告诉OPenGl这个纹理是个二维纹理
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureObjectIds[0]);
    // 设置过滤方式
    // GL_TEXTURE_MIN_FILTER:表示缩小时使用的过滤方式GL_LINEAR_MIPMAP_LINEAR(MIP贴图级别直接插值的最近邻过滤)
    GLES20.glTexParameteri(
            GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
    // GL_TEXTURE_MAG_FILTER: 表示放大时使用的过滤方式GL_LINEAR(双线性过滤)
    GLES20.glTexParameteri(
            GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    // 加载纹理到OpenGl并返回其ID
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
    // 释放BitMap图像
    bmp.recycle();
    // 生成MIP贴图
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    // 完成纹理加载后就可以解绑这个纹理了,以免调用意外调用其他方法改变这个纹理
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    return textureObjectIds[0];
}
 
Example 3
Source File: Texture.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public int texture() {
    if (texture != 0) {
        return texture;
    }

    if (bitmap.isRecycled()) {
        return 0;
    }

    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    texture = textures[0];

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);

    boolean mipMappable = false; //isPOT(bitmap.getWidth()) && isPOT(bitmap.getHeight());
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, mipMappable ? GLES20.GL_LINEAR_MIPMAP_LINEAR : GLES20.GL_LINEAR);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    if (mipMappable) {
        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    }

    Utils.HasGLError();

    return texture;
}
 
Example 4
Source File: Utils.java    From LearnOpenGL with MIT License 5 votes vote down vote up
public static int loadTexture(Context context, @DrawableRes int resId) {
    int[] textureObjectIds = new int[1];
    GLES20.glGenTextures(1, textureObjectIds, 0);
    if (textureObjectIds[0] == 0) {
        Log.e(TAG, "Could not generate a new OpenGL texture object.");
        return 0;
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId, options);
    if (bitmap == null) {
        Log.e(TAG, "Resource ID " + resId + " could not be decoded.");
        GLES20.glDeleteTextures(1, textureObjectIds, 0);
        return 0;
    }

    // bind
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureObjectIds[0]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    // unbind
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    return textureObjectIds[0];
}
 
Example 5
Source File: ShaderRenderer.java    From ShaderEditor with MIT License 5 votes vote down vote up
private void createTarget(
		int idx,
		int width,
		int height,
		BackBufferParameters tp) {
	GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tx[idx]);

	boolean useBitmap = tp.setPresetBitmap(width, height);
	if (!useBitmap) {
		GLES20.glTexImage2D(
				GLES20.GL_TEXTURE_2D,
				0,
				GLES20.GL_RGBA,
				width,
				height,
				0,
				GLES20.GL_RGBA,
				GLES20.GL_UNSIGNED_BYTE,
				null);
	}

	tp.setParameters(GLES20.GL_TEXTURE_2D);
	GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

	GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[idx]);
	GLES20.glFramebufferTexture2D(
			GLES20.GL_FRAMEBUFFER,
			GLES20.GL_COLOR_ATTACHMENT0,
			GLES20.GL_TEXTURE_2D,
			tx[idx],
			0);

	if (!useBitmap) {
		// clear texture because some drivers
		// don't initialize texture memory
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
				GLES20.GL_DEPTH_BUFFER_BIT);
	}
}
 
Example 6
Source File: ShaderRenderer.java    From ShaderEditor with MIT License 5 votes vote down vote up
private void createTexture(
		int id,
		Bitmap bitmap,
		TextureParameters tp) {
	GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id);
	tp.setParameters(GLES20.GL_TEXTURE_2D);
	TextureParameters.setBitmap(bitmap);
	GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
}
 
Example 7
Source File: PlaneRenderer.java    From react-native-arcore with MIT License 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source and texture PNG.
 * @param gridDistanceTextureName  Name of the PNG file containing the grid texture.
 */
public void createOnGlThread(Context context, String gridDistanceTextureName)
        throws IOException {
    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_VERTEX_SHADER, R.raw.plane_vertex);
    int passthroughShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_FRAGMENT_SHADER, R.raw.plane_fragment);

    mPlaneProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mPlaneProgram, vertexShader);
    GLES20.glAttachShader(mPlaneProgram, passthroughShader);
    GLES20.glLinkProgram(mPlaneProgram);
    GLES20.glUseProgram(mPlaneProgram);

    ShaderUtil.checkGLError(TAG, "Program creation");

    // Read the texture.
    Bitmap textureBitmap = BitmapFactory.decodeStream(
        context.getAssets().open(gridDistanceTextureName));

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glGenTextures(mTextures.length, mTextures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0);
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    ShaderUtil.checkGLError(TAG, "Texture loading");

    mPlaneXZPositionAlphaAttribute = GLES20.glGetAttribLocation(mPlaneProgram,
        "a_XZPositionAlpha");

    mPlaneModelUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Model");
    mPlaneModelViewProjectionUniform =
        GLES20.glGetUniformLocation(mPlaneProgram, "u_ModelViewProjection");
    mTextureUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Texture");
    mLineColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_lineColor");
    mDotColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_dotColor");
    mGridControlUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_gridControl");
    mPlaneUvMatrixUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_PlaneUvMatrix");

    ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 8
Source File: PlaneRenderer.java    From poly-sample-android with Apache License 2.0 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the plane renderer. Must be called on the
 * OpenGL thread, typically in {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source and texture PNG.
 * @param gridDistanceTextureName Name of the PNG file containing the grid texture.
 */
public void createOnGlThread(Context context, String gridDistanceTextureName) throws IOException {
  int vertexShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME);
  int passthroughShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME);

  planeProgram = GLES20.glCreateProgram();
  GLES20.glAttachShader(planeProgram, vertexShader);
  GLES20.glAttachShader(planeProgram, passthroughShader);
  GLES20.glLinkProgram(planeProgram);
  GLES20.glUseProgram(planeProgram);

  ShaderUtil.checkGLError(TAG, "Program creation");

  // Read the texture.
  Bitmap textureBitmap =
      BitmapFactory.decodeStream(context.getAssets().open(gridDistanceTextureName));

  GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  GLES20.glGenTextures(textures.length, textures, 0);
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

  GLES20.glTexParameteri(
      GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0);
  GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

  ShaderUtil.checkGLError(TAG, "Texture loading");

  planeXZPositionAlphaAttribute = GLES20.glGetAttribLocation(planeProgram, "a_XZPositionAlpha");

  planeModelUniform = GLES20.glGetUniformLocation(planeProgram, "u_Model");
  planeNormalUniform = GLES20.glGetUniformLocation(planeProgram, "u_Normal");
  planeModelViewProjectionUniform =
      GLES20.glGetUniformLocation(planeProgram, "u_ModelViewProjection");
  textureUniform = GLES20.glGetUniformLocation(planeProgram, "u_Texture");
  lineColorUniform = GLES20.glGetUniformLocation(planeProgram, "u_lineColor");
  dotColorUniform = GLES20.glGetUniformLocation(planeProgram, "u_dotColor");
  gridControlUniform = GLES20.glGetUniformLocation(planeProgram, "u_gridControl");
  planeUvMatrixUniform = GLES20.glGetUniformLocation(planeProgram, "u_PlaneUvMatrix");

  ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 9
Source File: AndroidGL.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void generateMipmap(int target) {
    GLES20.glGenerateMipmap(target);
}
 
Example 10
Source File: FileTextureSource.java    From Tanks with MIT License 4 votes vote down vote up
private int loadTexture()
{
  ResultRunnable<Integer> runnable = new ResultRunnable<Integer>()
  {
    @Override
    protected Integer onRun()
    {
      try
      {
        String fileName = getTextureFileName(name);
        InputStream stream = GameContext.context.getAssets().open(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(stream);
        stream.close();

        int type = GLUtils.getType(bitmap);
        int format = GLUtils.getInternalFormat(bitmap);
        int error;

        int[] textures = new int[1];

        GLES20.glGenTextures(1, textures, 0);
        if ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
          throw new GLException(error);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        if ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
          throw new GLException(error);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
        if ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
          throw new GLException(error);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, format, bitmap, type, 0);
        if ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
          throw new GLException(error);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

        if (generateMipmap)
        {
          GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
          if ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR)
            throw new GLException(error, Integer.toString(error));
        }

        bitmap.recycle();
        return textures[0];
      }
      catch(Exception e)
      {
        throw new RuntimeException(e);
      }
    }
  };

  GameContext.glThread.sendEvent(runnable);
  return runnable.getResult();
}
 
Example 11
Source File: ShaderRenderer.java    From ShaderEditor with MIT License 4 votes vote down vote up
private void createCubeTexture(
		int id,
		Bitmap bitmap,
		TextureParameters tp) {
	GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, id);
	tp.setParameters(GLES20.GL_TEXTURE_CUBE_MAP);

	int bitmapWidth = bitmap.getWidth();
	int bitmapHeight = bitmap.getHeight();
	int sideWidth = (int) Math.ceil(bitmapWidth / 2f);
	int sideHeight = Math.round(bitmapHeight / 3f);
	int sideLength = Math.min(sideWidth, sideHeight);
	int x = 0;
	int y = 0;

	for (int target : CUBE_MAP_TARGETS) {
		Bitmap side = Bitmap.createBitmap(
				bitmap,
				x,
				y,
				// cube textures need to be quadratic
				sideLength,
				sideLength,
				// don't flip cube textures
				null,
				true);

		GLUtils.texImage2D(
				target,
				0,
				GLES20.GL_RGBA,
				side,
				GLES20.GL_UNSIGNED_BYTE,
				0);

		side.recycle();

		if ((x += sideWidth) >= bitmapWidth) {
			x = 0;
			y += sideHeight;
		}
	}

	GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_CUBE_MAP);
}
 
Example 12
Source File: OGLESShaderRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * <code>updateTexImageData</code> activates and binds the texture
 * @param img
 * @param type
 * @param mips
 */
public void updateTexImageData(Image img, Texture.Type type, boolean mips) {
    int texId = img.getId();
    if (texId == -1) {
        // create texture
        if (verboseLogging) {
            logger.info("GLES20.glGenTexture(1, buffer)");
        }

        GLES20.glGenTextures(1, intBuf1);
        texId = intBuf1.get(0);
        img.setId(texId);
        objManager.registerForCleanup(img);

        statistics.onNewTexture();
    }

    // bind texture
    int target = convertTextureType(type);
    if (context.boundTextureUnit != 0) {
        if (verboseLogging) {
            logger.info("GLES20.glActiveTexture(GLES20.GL_TEXTURE0)");
        }

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        context.boundTextureUnit = 0;
    }
    if (context.boundTextures[0] != img) {

        if (verboseLogging) {
            logger.info("GLES20.glBindTexture(" + target + ", " + texId + ")");
        }

        GLES20.glBindTexture(target, texId);
        context.boundTextures[0] = img;
    }


    if (target == GLES20.GL_TEXTURE_CUBE_MAP) {
        // Upload a cube map / sky box
        @SuppressWarnings("unchecked")
        List<Bitmap> bmps = (List<Bitmap>) img.getEfficentData();
        if (bmps != null) {
            // Native android bitmap                                       
            if (bmps.size() != 6) {
                throw new UnsupportedOperationException("Invalid texture: " + img
                        + "Cubemap textures must contain 6 data units.");
            }
            for (int i = 0; i < 6; i++) {
                TextureUtil.uploadTextureBitmap(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, bmps.get(i), false, powerOf2);
            }
        } else {
            // Standard jme3 image data
            List<ByteBuffer> data = img.getData();
            if (data.size() != 6) {
                logger.log(Level.WARNING, "Invalid texture: {0}\n"
                        + "Cubemap textures must contain 6 data units.", img);
                return;
            }
            for (int i = 0; i < 6; i++) {
                TextureUtil.uploadTexture(img, GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc, false, powerOf2);
            }
        }
    } else {
        TextureUtil.uploadTexture(img, target, 0, 0, tdc, false, powerOf2);

        if (verboseLogging) {
            logger.info("GLES20.glTexParameteri(" + target + "GLES11.GL_GENERATE_MIMAP, GLES20.GL_TRUE)");
        }

        if (!img.hasMipmaps() && mips) {
            // No pregenerated mips available,
            // generate from base level if required
            if (verboseLogging) {
                logger.info("GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D)");
            }
            GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
        }
    }

    img.clearUpdateNeeded();
}