i loading models server json format created in three.js buffergeometry , stored position , uv attributes in json file, when load opengl es2 nice faces not appear until rotate view if has texture or appear semi transparent if has color. vertices values , order generated three.js without changes assume there in right order should no reversed faces.
precision mediump float; varying vec4 v_color; uniform sampler2d u_textureunit; varying vec2 v_texturecoordinates; void main() { vec4 tex=texture2d(u_textureunit, v_texturecoordinates); gl_fragcolor=v_color+tex; }
and rendering code:
private static final int bytes_per_float = 4; // private static final int bytes_per_short = 2; private static final int position_component_count = 3; private static final int color_component_count = 4; private static final int uv_component_count = 2; private static final int all_component_count = position_component_count + color_component_count + uv_component_count; private static final int stride = all_component_count * bytes_per_float; private vertexarray vertexarray; private float[] modelmatrix = new float[16]; private final float[] modelviewprojectionmatrix = new float[16]; textureshaderprogram shaderprogram; int textureid; // shortbuffer indexbuffer; public geometry(context context, jsonobject element) throws jsonexception { /* * jsonarray indecies = element.getjsonarray("indexes"); short[] indexes * = new short[indecies.length()]; (int y = 0; y < * indecies.length(); y++) { indexes[y] = (short) indecies.getint(y); } */ // indexbuffer = bytebuffer.allocatedirect(indexes.length * // bytes_per_short).order(byteorder.nativeorder()).asshortbuffer(); // indexbuffer.put(indexes).position(0); jsonarray vertices = element.getjsonarray("vertices"); jsonarray uvs = element.getjsonarray("uvs"); jsonarray matrix = element.getjsonarray("matrix"); (int y = 0; y < matrix.length(); y++) { modelmatrix[y] = (float) matrix.getdouble(y); } jsonobject material = element.getjsonobject("material"); int color = color.parsecolor(material.getstring("color")); string bmpstring = material.getstring("map"); int n = vertices.length() / 3; float[] data; data = new float[n * stride]; int k = 0; (int = 0; < n; i++) { data[i * all_component_count] = (float) vertices.getdouble(i * 3); data[i * all_component_count + 1] = (float) vertices.getdouble(i * 3 + 1); data[i * all_component_count + 2] = (float) vertices.getdouble(i * 3 + 2); data[i * all_component_count + 3] = color.red(color) / 255f; data[i * all_component_count + 4] = color.green(color) / 255f; data[i * all_component_count + 5] = color.blue(color) / 255f; data[i * all_component_count + 6] = color.alpha(color) / 255f; data[i * all_component_count + 7] = bmpstring.equals("") ? 0f : (float) uvs.getdouble(k); data[i * all_component_count + 8] = bmpstring.equals("") ? 0f : (float) uvs.getdouble(k + 1); k += 2; } vertexarray = new vertexarray(data); shaderprogram = new textureshaderprogram(context, r.raw.texture_vertex_shader, r.raw.texture_fragment_shader); textureid = bmpstring.equals("") ? 0 : texturehelper.loadtexture(texturehelper.decodebase64(bmpstring)); } private void binddata(textureshaderprogram shaderprogram) { vertexarray.setvertexattribpointer(0, shaderprogram.getpositionattributelocation(), position_component_count, stride); vertexarray.setvertexattribpointer(position_component_count, shaderprogram.getcolorattributelocation(), color_component_count, stride); vertexarray.setvertexattribpointer(position_component_count + color_component_count, shaderprogram.gettexturecoordinatesattributelocation(), uv_component_count, stride); } public void draw(float[] projectionmatrix) { multiplymm(modelviewprojectionmatrix, 0, projectionmatrix, 0, modelmatrix, 0); shaderprogram.useprogram(); shaderprogram.setuniforms(modelviewprojectionmatrix, textureid); binddata(shaderprogram); gldrawarrays(gl_triangles, 0, vertexarray.getcapacity() / all_component_count); // gldrawelements(gl_triangles, indexbuffer.capacity(), // gl_unsigned_short, indexbuffer); }
the vertexdata class:
private final floatbuffer floatbuffer; private final int capacity; public vertexarray(float[] vertexdata) { floatbuffer = bytebuffer.allocatedirect(vertexdata.length * 4).order(byteorder.nativeorder()).asfloatbuffer().put(vertexdata); capacity = floatbuffer.capacity(); } public void setvertexattribpointer(int dataoffset, int attributelocation, int componentcount, int stride) { floatbuffer.position(dataoffset); glvertexattribpointer(attributelocation, componentcount, gl_float, false, stride, floatbuffer); glenablevertexattribarray(attributelocation); floatbuffer.position(0); } public final int getcapacity() { return capacity; }
the depth function not set, opengl es drawing in order of triangles based on indices data. have enable depth test sort triangles based on depth information camera.
glenable(gl_depth_test);
Comments
Post a Comment