Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple bitmaps #3

Closed
liyihongcug opened this issue Dec 30, 2016 · 4 comments
Closed

Add support for multiple bitmaps #3

liyihongcug opened this issue Dec 30, 2016 · 4 comments

Comments

@liyihongcug
Copy link

Thanks a lot for this software at first. Now I meet with one problem.I want to add different texture mapping bitmap in one cube. I search all codes many times. I choose Object3DBuilder .java.
Now it only support one bitmap. Can you help us realize add different texture mapping bitmap in one cube. (
bitmap[] bm.
bmp[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.aa);
bmp[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bb);
bmp[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.cc);
bmp[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.dd);
bmp[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ee);
bmp[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ff)
not byte[] textureData and I want to change it to bimtap[]
)

file : Object3DBuilder.java
org.andresoviedo.app.model3D.model;public final class Object3DBuilder
public static Object3DData buildCubeV3(byte[] textureData) {
return new Object3DData(
createNativeByteBuffer(cubePositionData.length * 4).asFloatBuffer().put(cubePositionData),
createNativeByteBuffer(cubeTextureCoordinateData.length * 4).asFloatBuffer()
.put(cubeTextureCoordinateData).asReadOnlyBuffer(),
textureData).setDrawMode(GLES20.GL_TRIANGLES).setId("cubeV3").centerAndScale(1.0f);
}

@liyihongcug
Copy link
Author

Another prolem:
I need only mapping one face texture mapping of 8 faces.
I try it and this software api can't support it. Please think it over.
I only handle with one texture mapping of many faces.

@andresoviedo
Copy link
Member

Hey body. Support for multiple textures is not implemented.
I am wondering if it's possible to map all the cube faces with just 1 bitmap.
Gonna change the title to add the feature in the future ;)

@andresoviedo andresoviedo changed the title How to add different texture mapping bitmap in buildCubeV3? Add support for multiple bitmaps Mar 2, 2017
@andresoviedo
Copy link
Member

Ir order to support multiple textures there is 2 options:

  1. Break the mesh into multiple objects, so you can map the texture independently to every object.

  2. Map multiple textures into the FragmentShader, That is:

a. Load multiple textures into opengl. Change this method so you can load multiple textures (as many as you like) instead of only 1:
https://github.com/andresoviedo/android-3D-model-viewer/blob/master/app/src/main/java/org/andresoviedo/app/model3D/util/GLUtil.java#L104

final int[] textureId = new int[NUMBER_OF_TEXTURES_YOU_WANT];
GLES20.glGenTextures(1, textureId, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap0, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[1]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap1, 0);

...

b. In the vertex shader declare an attribute of type int to store the index of the texture unit to use and pass it to the fragment shader:

attribute int a_TexCoordinateUnit;
varying int v_TexCoordinateUnit;
void main(){
  v_TexCoordinateUnit = a_TexCoordinateUnit;
}

c. In the fragment shader, declare an array of sample2D (you may have at most 8 textures active)

uniform sampler2D u_Texture[8];
varying int v_TexCoordinateUnit;
void main(){
  gl_FragColor = v_Color * texture2D(u_Texture[v_TexCoordinateUnit], v_TexCoordinate);
}

d. Load the different texture units into OpenGL as many times as texture units you want to load (maximum 8):

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLES20.glUniform1i(mTextureUniformHandle, 0);

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[1]);
GLES20.glUniform1i(mTextureUniformHandle, 1);

...
e. In the obj loader map every vertex with the corresponding texture into an IntBuffer (texture_unit_map)
f. Bind the texture_unit_map array into the vertex shader:

int mTextureCoordinateUnitHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinateUnit");
GLES20.glEnableVertexAttribArray(mTextureCoordinateUnitHandle );
obj.getTextureUnitMap().position(0);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 1, GLES20.GL_INT, false, 0, obj.getTextureUnitMap());

@andresoviedo
Copy link
Member

Dear @liyihongcug
Latest version of the app should solve most of the texture issues
so I am closing this for now
kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants