我試圖在使用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