i using opengl es. have loaded obj file (mesh) , displayed when applying texture not displaying.
i have added code below.
public void draw(gl10 gl) { // bind generated texture gl.glbindtexture(gl10.gl_texture_2d, textures[0]); gl.glenableclientstate(gl10.gl_vertex_array); gl.glenableclientstate(gl10.gl_texture_coord_array); // set face rotation gl.glfrontface(gl10.gl_cw); gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer); gl.gltexcoordpointer(2, gl10.gl_float, 0, texturebuffer); //disable client state before leaving gl.gldisableclientstate(gl10.gl_texture_coord_array); for(int i=0; i<parts.size(); i++){ tdmodelpart t=parts.get(i); gl.glnormalpointer(gl10.gl_float, 0, t.getnormalbuffer()); gl.gldrawelements(gl10.gl_triangles,t.getfacescount(),gl10.gl_unsigned_short,t.getfacebuffer()); gl.gldisableclientstate(gl10.gl_vertex_array); } } public void buildvertexbuffer(){ bytebuffer vbuf = bytebuffer.allocatedirect(v.size() * 4); vbuf.order(byteorder.nativeorder()); vertexbuffer = vbuf.asfloatbuffer(); vertexbuffer.put(toprimitivearrayf(v)); vertexbuffer.position(0); vbuf = bytebuffer.allocatedirect(texture.length * 4); vbuf.order(byteorder.nativeorder()); texturebuffer = vbuf.asfloatbuffer(); texturebuffer.put(texture); texturebuffer.position(0); } private static float[] toprimitivearrayf(vector<float> vector){ float[] f; f=new float[vector.size()]; (int i=0; i<vector.size(); i++){ f[i]=vector.get(i); } return f; } /** * load texture square * @param gl * @param context */ public void loadgltexture(gl10 gl, context context) { // loading texture bitmap bitmap = bitmapfactory.decoderesource(context.getresources(), r.drawable.nehe); // 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(); } public void onsurfacecreated(gl10 gl, eglconfig config) { gl.gllightfv(gl10.gl_light0, gl10.gl_ambient, lightambientbuffer); gl.gllightfv(gl10.gl_light0, gl10.gl_diffuse, lightdiffusebuffer); gl.gllightfv(gl10.gl_light0, gl10.gl_position, lightpositionbuffer); gl.glenable(gl10.gl_light0); model.loadgltexture(gl, context); gl.glenable(gl10.gl_texture_2d); gl.glshademodel(gl10.gl_smooth); gl.glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); gl.glcleardepthf(1.0f); gl.glenable(gl10.gl_depth_test); gl.gldepthfunc(gl10.gl_lequal); gl.glhint(gl10.gl_perspective_correction_hint, gl10.gl_nicest); }
//disable client state before leaving gl.gldisableclientstate(gl10.gl_texture_coord_array); for(int i=0; i<parts.size(); i++) { tdmodelpart t=parts.get(i); gl.glnormalpointer(gl10.gl_float, 0, t.getnormalbuffer()); gl.gldrawelements(gl10.gl_triangles,t.getfacescount(),gl10.gl_unsigned_short,t.getfacebuffer()); gl.gldisableclientstate(gl10.gl_vertex_array); }
of course disabling texcoord array before drawing not idea, since cause object not have valid texture coordinates (well, stricty speaking vertices have current default texture coordinates). in same way shouldn't disable gl_vertex_array
@ end of loop either, since course object not have valid position data following loop iterations. code should like:
// bind generated texture gl.glbindtexture(gl10.gl_texture_2d, textures[0]); gl.glenableclientstate(gl10.gl_vertex_array); gl.glenableclientstate(gl10.gl_texture_coord_array); // set face rotation gl.glfrontface(gl10.gl_cw); gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer); gl.gltexcoordpointer(2, gl10.gl_float, 0, texturebuffer); for(int i=0; i<parts.size(); i++){ tdmodelpart t=parts.get(i); gl.glnormalpointer(gl10.gl_float, 0, t.getnormalbuffer()); gl.gldrawelements(gl10.gl_triangles,t.getfacescount(),gl10.gl_unsigned_short,t.getfacebuffer()); } //disable client state before leaving gl.gldisableclientstate(gl10.gl_vertex_array); gl.gldisableclientstate(gl10.gl_texture_coord_array);
(for i'll ignore set glnormalpointer
while not enabling gl_normal_array
.)
always keep in mind opengl state machine , operations, draw calls, operate on current state @ point called , state won't ever change except you change yourself (with "you" meaning application code here, including other thrid party components using same opengl context).
Comments
Post a Comment