Ich versuche, verschiedene Textur auf Würfel mit Shadern mit samplerCube und textureCube anzuwenden.
Aber ich bin nicht in der Lage, Textur auf Gesichtern des Würfels gezeichnet bekommen nur einzelne Farbe erscheint.
Unten ist mein Shader-Code:
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;" +
"}";
Fragment-Shader
String strFShader = "precision mediump float;" +
"uniform samplerCube u_texId;" +
"varying vec3 v_normal;" +
"void main()" +
"{" +
"gl_FragColor = textureCube(u_texId, v_normal);" +
"}";
Würfel Definition
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);
}
Code für Würfelkarten erstellen
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];
}
Ich kann nicht verstehen, wo ich einen Fehler mache.
Wenn Sie den vollständigen Code sehen wollen.
Lösung:
Verwendung derselben Würfelzeichnungskoordinaten für Texturkoordinaten
Vielen Dank an alle