我试图在使用samplerCube和textureCube着色器的多维数据集上应用不同的纹理。将纹理应用到多维数据集,立方体的每个表面上的不同纹理
但我无法在多维数据集的面上绘制纹理,只有单一颜色出现。
下面是我的shader代码:
的Vertex Shader
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, -2,2,2, -2,-2,2, 2,-2,2, //0-1-2-3 front
2,2,2, 2,-2,2, 2,-2,-2, 2,2,-2,//0-3-4-5 right
2,-2,-2, -2,-2,-2, -2,2,-2, 2,2,-2,//4-7-6-5 back
-2,2,2, -2,2,-2, -2,-2,-2, -2,-2,2,//1-6-7-2 left
2,2,2, 2,2,-2, -2,2,-2, -2,2,2, //top
2,-2,2, -2,-2,2, -2,-2,-2, 2,-2,-2,//bottom
};
short[] indeces = {0,1,2, 0,2,3,
4,5,6, 4,6,7,
8,9,10, 8,10,11,
12,13,14, 12,14,15,
16,17,18, 16,18,19,
20,21,22, 20,22,23,
};
float[] normals = {
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, //front
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // right
0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, //back
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // left
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // top
0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 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, 3, GLES20.GL_FLOAT, false, 0, cubeBuffer);
GLES20.glEnableVertexAttribArray(iPosition);
GLES20.glVertexAttribPointer(iNormal, 3, GLES20.GL_FLOAT, false, 0, 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, 0, -xAngle, 0, 1, 0);
Matrix.rotateM(m_fIdentity, 0, -yAngle, 1, 0, 0);
Matrix.multiplyMM(m_fVPMatrix, 0, m_fViewMatrix, 0, m_fIdentity, 0);
Matrix.multiplyMM(m_fVPMatrix, 0, m_fProjMatrix, 0, m_fVPMatrix, 0);
GLES20.glUniformMatrix4fv(iVPMatrix, 1, false, m_fVPMatrix, 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_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP, 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, 0, GLES20.GL_RGBA, img.getWidth(),img.getHeight() , 0,GLES20.GL_RGBA ,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, 0, GLES20.GL_RGBA, img.getWidth(),img.getHeight(), 0,GLES20.GL_RGBA ,GLES20.GL_UNSIGNED_BYTE, fcbuffer);
fcbuffer = null;
img.recycle();
img = BitmapFactory.decodeResource(curView.getResources(), 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, 0, GLES20.GL_RGBA, img.getWidth(),img.getHeight(), 0,GLES20.GL_RGBA ,GLES20.GL_UNSIGNED_BYTE, 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, 0, GLES20.GL_RGBA, img.getWidth(),img.getHeight(), 0,GLES20.GL_RGBA ,GLES20.GL_UNSIGNED_BYTE, fcbuffer);
fcbuffer = null;
img.recycle();
img = BitmapFactory.decodeResource(curView.getResources(), 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, 0, GLES20.GL_RGBA,img.getWidth(),img.getHeight(), 0,GLES20.GL_RGBA ,GLES20.GL_UNSIGNED_BYTE, fcbuffer);
fcbuffer = null;
img.recycle();
img = BitmapFactory.decodeResource(curView.getResources(), 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, 0, GLES20.GL_RGBA, img.getWidth(),img.getHeight(), 0,GLES20.GL_RGBA ,GLES20.GL_UNSIGNED_BYTE, fcbuffer);
fcbuffer = null;
img.recycle();
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_CUBE_MAP);
return cubeTex[0];
}
我无法明白的地方,我会犯错。
如果你想看完整的代码。
解决方案:
使用相同的立方体绘制坐标的纹理坐标
感谢名单全部
请把你的答案到下面一个真正的答案。您可以在两天后将其标记为已接受。这将使问题从“未答复的问题”选项卡中消失。 – 2012-01-12 08:58:51