2

我有许多(113)由搅拌机创建的纹理图像(一个 obj 和一个引用纹理的 mtl 文件)不是 2 的幂。当我尝试使用单个纹理(2 的幂)渲染一个简单对象时,它工作正常,但是对于我上面描述的复杂对象,它只绘制几何图形(一切都是白色的,没有纹理)。

这是因为我的纹理尺寸吗?如果是,是否有解决方案可以在运行时制作许多纹理/位图的二次幂?(我不知道尺寸。)

我也怀疑是否glbindtexture正确使用(我正在使用 Android。)首先,我调用glgentextures(<number_of_objects>, textureArray). 然后,在每个对象的循环中,我调用glbindtexture(..._2D, textureArray[i])了 GLutils.texImage2D(...)。最后,在绘图时间,我调用glbindtexture(..., textureArray[i])然后gldrawarrays

这有什么问题吗?(已编辑)我忘了说,我正在使用 opengl es 1.1,我在某处读到 opengl es 1.1 不支持 NPOT 纹理。

提前致谢。

4

2 回答 2

3

运行此方法以检查 OpenGL 驱动程序状态错误:

public void checkGlError(String op) {
    int error;
    while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
            Log.e("ShadingZen", op + ": glError " + error);
            //throw new RuntimeException(op + ": glError " + error);
    }
 }

根据您的测试设备,可能无法使用两种纹理的非幂次。此代码向您展示如何将它们转换为 ^2 大小(在 android 中):

int calculateUpperPowerOfTwo(int v)
{
    v--;
    v |= v >>> 1;
    v |= v >>> 2;
    v |= v >>> 4;
    v |= v >>> 8;
    v |= v >>> 16;
    v++;
    return v;

}

boolean isPowerOfTwo(int i){
    return ( i & (i - 1)) == 0;
}


boolean loadAsTexture2D(Context context, String id, int resource_id, BitmapTexture.Parameters params){
    _bmps = new Bitmap[1];
    Matrix flip = new Matrix();
    flip.postScale(1f, -1f);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inScaled = false;
    Bitmap textureBmp = BitmapFactory.decodeResource(context.getResources(), resource_id, opts);

    if(!isPowerOfTwo(textureBmp.getWidth()) || !isPowerOfTwo(textureBmp.getHeight())){
        int target_width = calculateUpperPowerOfTwo(textureBmp.getWidth());
        int target_height = calculateUpperPowerOfTwo(textureBmp.getHeight());

        Log.i("ShadingZen", "Texture id=" + id + " has no power of two dimesions " + textureBmp.getWidth() + "x" + textureBmp.getHeight() + " adjusting to " + target_width + "x" + target_height);

        Bitmap temp =  Bitmap.createBitmap(textureBmp, 0, 0, textureBmp.getWidth(), textureBmp.getHeight(), flip, false);
        _bmps[0] = Bitmap.createScaledBitmap(temp, target_width, target_height, false);
        temp.recycle();
    } else{
        _bmps[0]  = Bitmap.createBitmap(textureBmp, 0, 0, textureBmp.getWidth(), textureBmp.getHeight(), flip, false);
    }

    textureBmp.recycle();
    // At this point _bmp[0] contains a ^2 bitmap

}

检查此类以获取更多信息:https ://github.com/TraxNet/ShadingZen/blob/master/library/src/main/java/org/traxnet/shadingzen/core/BitmapTexture.java

于 2012-12-06T08:06:17.347 回答
2

当您生成纹理时,要使用非 2 个纹理的幂,您需要启用这些参数

glGenTextures(1, &nID);
glBindTexture(GL_TEXTURE_2D, nID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //should probably use these
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //these let you use NPOT textures
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
于 2012-12-06T08:08:24.447 回答