java - different textures in android -
i want make more 1 texture on cube easy possible. there should different texture (picture 1 loaded in loadtexture method, android.png) on each side of cube. part of code:
public class square2 { private floatbuffer vertexbuffer; // buffer holding vertices private floatbuffer vertexbuffer2; // buffer holding vertices [and on 6 sides] private float vertices[] = { -1.0f, -1.0f, -1.0f, // v1 - bottom left -1.0f, 1.0f, -1.0f, // v2 - top left 1.0f, -1.0f, -1.0f, // v3 - bottom right 1.0f, 1.0f, 1.0f // v4 - top right }; private float vertices2[] = { 1.0f, -1.0f, -1.0f, // v1 - bottom left 1.0f, 1.0f, -1.0f, // v2 - top left 1.0f, -1.0f, 1.0f, // v3 - bottom right 1.0f, 1.0f, 1.0f // v4 - top right }; [for 6 sides too] private floatbuffer texturebuffer; // buffer holding texture coordinates private floatbuffer texturebuffer2; // buffer holding texture coordinates [and on 6 sides] private float texture[] = { // mapping coordinates vertices 0.0f, 1.0f, // top left (v2) 0.0f, 0.0f, // bottom left (v1) 1.0f, 1.0f, // top right (v4) 1.0f, 0.0f // bottom right (v3) }; private float texture2[] = { // mapping coordinates vertices 0.0f, 1.0f, // top left (v2) 0.0f, 0.0f, // bottom left (v1) 1.0f, 1.0f, // top right (v4) 1.0f, 0.0f // bottom right (v3) }; [dont understand texture array, 1 enough sides, if want different textures?] /** texture pointer */ private int[] textures = new int[6]; public square2() { // float has 4 bytes allocate each coordinate 4 bytes bytebuffer bytebuffer = bytebuffer.allocatedirect(vertices.length * 4); bytebuffer.order(byteorder.nativeorder()); // allocates memory byte buffer vertexbuffer = bytebuffer.asfloatbuffer(); // fill vertexbuffer vertices vertexbuffer.put(vertices); // set cursor position beginning of buffer vertexbuffer.position(0); bytebuffer bytebuffer2 = bytebuffer.allocatedirect(vertices2.length * 4); bytebuffer2.order(byteorder.nativeorder()); // allocates memory byte buffer vertexbuffer2 = bytebuffer2.asfloatbuffer(); // fill vertexbuffer vertices vertexbuffer2.put(vertices2); // set cursor position beginning of buffer vertexbuffer2.position(0); [and on 6 sides] bytebuffer = bytebuffer.allocatedirect(texture.length * 4); bytebuffer.order(byteorder.nativeorder()); texturebuffer = bytebuffer.asfloatbuffer(); texturebuffer.put(texture); texturebuffer.position(0); [and on] } /** * load texture square * @param gl * @param context */ public void loadgltexture(gl10 gl, context context) { // loading texture bitmap bitmap = bitmapfactory.decoderesource(context.getresources(), r.drawable.android); // generate 1 texture pointer gl.glgentextures(1, textures, 0); // ...and bind our array gl.glbindtexture(gl10.gl_texture_2d, textures[0]); // create nearest filtered texture gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_min_filter, gl10.gl_nearest); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_mag_filter, gl10.gl_linear); //different possible texture parameters, e.g. gl10.gl_clamp_to_edge //gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_s, gl10.gl_repeat); //gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_t, gl10.gl_repeat); // use android glutils specify two-dimensional texture image our bitmap glutils.teximage2d(gl10.gl_texture_2d, 0, bitmap, 0); // clean bitmap.recycle(); } /** draw method square gl context */ public void draw(gl10 gl) { // bind generated texture gl.glbindtexture(gl10.gl_texture_2d, textures[0]); gl.glbindtexture(gl10.gl_texture_2d, textures[1]); // point our buffers gl.glenableclientstate(gl10.gl_vertex_array); gl.glenableclientstate(gl10.gl_texture_coord_array); // set face rotation gl.glfrontface(gl10.gl_cw); // point our vertex buffer1 vorn gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer); gl.gltexcoordpointer(2, gl10.gl_float, 0, texturebuffer); // draw vertices triangle strip gl.gldrawarrays(gl10.gl_triangle_strip, 0, vertices.length / 3); //2 rechts gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer2); gl.gltexcoordpointer(2, gl10.gl_float, 0, texturebuffer2); // draw vertices triangle strip gl.gldrawarrays(gl10.gl_triangle_strip, 0, vertices2.length / 3); [and on] //disable client state before leaving gl.gldisableclientstate(gl10.gl_vertex_array); gl.gldisableclientstate(gl10.gl_texture_coord_array); } }
unfortunately gives me same texture on sides. how can make different textures on each side? have call loadtexture method more 1 time or edit it? thought not recycling bitmap, help?
thera may ways achieve this. straight forward load textures, bind different 1 before each side drawn (so missing glbindtexture
before each gldrawarrays
). case not need multiple texture coordinate buffers , overall wouldn't need multiple vertex buffers. can create 1 square buffer , position (rotate) matrices each side (+ binding correct texture).
another approach use single texture can load 6 images via gltexturesubimage
(all being @ different parts of texture, no overlapping) , use different texture coordinates each side of cube. doing can create single vertex/texture buffer whole cube , able draw single gldrawarrays
call. though in specific case no less 4x6=24 vertices.
as said not understand texture coordinate arrays.. texture coordinates coordinates corresponding relative position on texture. being, (0,0) top left part of texture(image) , (1,1) bottom right part of it. if texture had 4 images on in 2x2 , example want use bottom left one, texture coordinates this:
private float texture[] = { 0.0f, 1.0f, 0.0f, .5f, .5f, 1.0f, .5f, 0.5f };
as coordinate values being larger 1.0 or smaller .0 believe can used ether clamp part of object or repeat texture (those 2 bought possible parameters texture, set when loading texture). number of texture coordinates should @ least many last parameter in gldrawarrays
or many largest index+1 when using gldrawelements
. specific case can have 1 texture coordinate buffer unless @ point change mind , want 1 of sides have rotated image on , solve changing texture coordinates.
Comments
Post a Comment