微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 将纹理应用于立方体,立方体的每个面上都有不同的纹理

我正在尝试使用samplerCube和textureCube在着色器上应用不同的纹理.

但是我无法在立方体的面上绘制纹理,只出现单一颜色.

Screenshots of output

以下是我的着色器代码

顶点着色器

String strVShader = "attribute vec4 a_position;" +
            "uniform mat4 u_VPMatrix;" +
            "attribute vec3 a_normal;" +
            "varying vec3 v_normal;" +
            "void main()" +
            "{" +
                "gl_Position = u_VPMatrix * a_position;" +
                "v_normal = a_normal;" +
            "}";

片段着色器

String strFShader = "precision mediump float;" +
            "uniform samplerCube u_texId;" +
            "varying vec3 v_normal;" +
            "void main()" +
            "{" +
                "gl_FragColor = textureCube(u_texId,v_normal);" +
            "}";

立方体定义

float[] cube = {
        2,2,-2,//0-1-2-3 front
        2,//0-3-4-5 right
        2,//4-7-6-5 back
        -2,//1-6-7-2 left
        2,//top
        2,//bottom
    };

short[] indeces = {0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,};




float[] normals = {
                  0,//front
                   1,// right
                   0,-1,//back
                   -1,// left
                   0,//  top                  
                   0,// bottom

     }; 

OnDrawFrame

public void onDrawFrame(GL10 arg0) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        GLES20.gluseProgram(iProgId);
        cubeBuffer.position(0);
        GLES20.glVertexAttribPointer(iPosition,GLES20.GL_FLOAT,false,cubeBuffer);
        GLES20.glEnabLevertexAttribArray(iPosition);

        GLES20.glVertexAttribPointer(inormal,normBuffer);
        GLES20.glEnabLevertexAttribArray(inormal);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP,iTexId);
        GLES20.gluniform1i(iTexLoc,0);

        Matrix.setIdentityM(m_fIdentity,0);
        Matrix.rotateM(m_fIdentity,-xAngle,-yAngle,0);
        Matrix.multiplyMM(m_fVPMatrix,m_fViewMatrix,m_fIdentity,m_fProjMatrix,m_fVPMatrix,0);
        GLES20.gluniformMatrix4fv(iVPMatrix,0);

        GLES20.glDrawElements(GLES20.GL_TRIANGLES,36,GLES20.GL_UNSIGNED_SHORT,indexBuffer);
    }

创建多维数据集地图代码

public int CreateCubeTexture()
    {
            ByteBuffer fcbuffer = null;

            int[] cubeTex = new int[1];

            GLES20.glGenTextures(1,cubeTex,0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP,cubeTex[0]);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
            Bitmap img = null;
            img = BitmapFactory.decodeResource(curView.getResources(),R.raw.brick1);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);

            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            Log.d("alpha",""+img.hasAlpha());
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X,GLES20.GL_RGBA,img.getWidth(),img.getHeight(),GLES20.GL_UNSIGNED_BYTE,fcbuffer);
            fcbuffer = null;
            img.recycle();

            img = BitmapFactory.decodeResource(curView.getResources(),R.raw.brick2);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);
            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,R.raw.brick3);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);
            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,fcbuffer);
            fcbuffer = null;
            img.recycle();


            img = BitmapFactory.decodeResource(curView.getResources(),R.raw.brick4);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);
            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,R.raw.brick5);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);
            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,R.raw.brick6);
            fcbuffer = ByteBuffer.allocateDirect(img.getHeight() * img.getWidth() * 4);
            img.copyPixelsToBuffer(fcbuffer);
            fcbuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,fcbuffer);
            fcbuffer = null;
            img.recycle();

            GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_CUBE_MAP);

            return cubeTex[0];
    }

我无法理解我犯错误的地方.

如果你想看到完整的代码.

解:

使用相同的立方体绘制坐标用于纹理坐标

Thanx all

CODE Link

最佳答案
虽然这个问题现在已经解决了,但我想解释为什么使用不同的坐标实际上有帮助(因为上面缺少这个).

当我第一次实现立方体映射时,我有同样的错误,因为误解了立方体贴图的工作原理.立方体贴图在内部是一组6个2D纹理,排列在立方体的六个面上.从数学的角度来看,它定义了一个查找函数,其中参数是3D方向,输出是RGBA颜色.

这很重要,因为在上面的示例中,查找的参数是正常的.正常是一个方向,这是正确的.但是法线在整个立方体面上也是恒定的(除非计算平滑着色样式法线,否则不是这样).如果法线(输入到查找)是常量,那么当然输出(颜色)也必须是常量.我对此的误解是,我认为OpenGL会以某种方式考虑位置和方向,但遗憾的是并非如此.

在这种特殊情况下,可以使用cubeMap(position)或cubeMap(位置方向),并获得非常相似的结果.这是因为立方体贴图的另一个重要特性,即在从纹理中读出颜色之前,首先将输入方向标准化(将长度更改为1,而不改变其方向).这用于较旧的图形卡,使用特殊的立方体贴图纹理计算快速矢量标准化(因为在着色器中计算平方根比纹理查找慢).

关于立方体的最后一个想法 – 立方体贴图不是为立方体的每个面分配不同纹理的正确方法.它适用于简单的情况,但很难使它变得实用,例如在游戏中,因为I)一个立方体上不同纹理的组合数量可能需要不必要的大量纹理和II)因为这样,纹理重复不能使用.

原文地址:https://www.jb51.cc/android/430424.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐