opengl es - Android OpenGLES Brown square -
so i'm trying learn opengles 2.0 , create textured rectangle. apparently didn't follow instructions , i've ended odd color square.
heres shaders.
final string vertexshader = "uniform mat4 u_mvpmatrix; \n" // constant representing combined model/view/projection matrix. + "attribute vec2 a_texcoordinate;\n" // per-vertex texture coordinate information pass in. + "attribute vec4 a_position; \n" // per-vertex position information pass in. // + "attribute vec4 a_color; \n" // per-vertex color information pass in. // + "varying vec4 v_color; \n" // passed fragment shader. + "varying vec2 v_texcoordinate; \n" // passed fragment shader. + "void main() \n" // entry point our vertex shader. + "{ \n" + " v_texcoordinate = a_texcoordinate;\n" // pass texture coordinate through fragment shader. // interpolated across triangle. + " gl_position = u_mvpmatrix \n" // gl_position special variable used store final position. + " * a_position; \n" // multiply vertex matrix final point in + "} \n"; final string fragmentshader = "precision mediump float; \n" // set default precision medium. don't need high of // precision in fragment shader. + "uniform sampler2d u_texture; \n" // input texture. + "uniform vec4 u_color; \n" // color vertex shader interpolated across // triangle per fragment. + "varying vec2 v_texcoordinate; \n" // interpolated texture coordinate per fragment. + "void main() \n" // entry point our fragment shader. + "{ \n" + " gl_fragcolor = texture2d(u_texture, v_texcoordinate); \n" + "} \n";
here load texture function
public static int loadtexture(final context context, final int resourceid) { final int[] texturehandle = new int[1]; gles20.glgentextures(1, texturehandle, 0); if (texturehandle[0] != 0) { final bitmapfactory.options options = new bitmapfactory.options(); options.inscaled = false; // no pre-scaling // read in resource final bitmap bitmap = bitmapfactory.decoderesource(context.getresources(), resourceid, options); // bind texture in opengl gles20.glbindtexture(gles20.gl_texture_2d, texturehandle[0]); // set filtering gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_min_filter, gles20.gl_nearest); gles20.gltexparameteri(gles20.gl_texture_2d, gles20.gl_texture_mag_filter, gles20.gl_nearest); // load bitmap bound texture. glutils.teximage2d(gles20.gl_texture_2d, 0, bitmap, 0); // recycle bitmap, since data has been loaded opengl. bitmap.recycle(); } if (texturehandle[0] == 0) { throw new runtimeexception("error loading texture."); } return texturehandle[0]; }
and here drawframe
@override public void ondrawframe(gl10 gl) { // set background clear color gray. gles20.glclear(gles20.gl_depth_buffer_bit | gles20.gl_color_buffer_bit); gles20.glclearcolor(0.5f, 0.5f, 0.5f, 0.5f); mtextureuniformhandle = gles20.glgetuniformlocation(programhandle, "u_texture"); mtexturecoordinatehandle = gles20.glgetattriblocation(programhandle, "a_texcoordinate"); // set active texture unit texture unit 0. gles20.glactivetexture(gles20.gl_texture0); // bind texture unit. gles20.glbindtexture(gles20.gl_texture_2d, mtexturedatahandle); // tell texture uniform sampler use texture in shader binding texture unit 0. gles20.gluniform1i(mtextureuniformhandle, 0); // draw triangle facing straight on. matrix.setidentitym(mmodelmatrix, 0); matrix.translatem(mmodelmatrix, 0, fields.screen_width / 2, fields.screen_height / 2, 0); matrix.scalem(mmodelmatrix, 0, fields.screen_width / 4, fields.screen_width / 4, 0); drawtriangle(mtriangle1vertices); }
any advice getting rid of wierd brown square , displaying texture?
edit: here texture coordinates
public gl20renderer(context context) { final float[] triangle1verticesdata = { // x, y, z, // r, g, b, -1f, 1f, 0.0f, -1, -1, 0.0f, 1f, 1f, 0.0f, -1f, -1f, 0.0f, 1f, -1f, 0.0f, 1f, 1f, 0.0f, }; // initialize buffers. mtriangle1vertices = bytebuffer.allocatedirect(triangle1verticesdata.length * mbytesperfloat) .order(byteorder.nativeorder()).asfloatbuffer(); mtriangle1vertices.put(triangle1verticesdata).position(0); final float[] triangle1texturecoordinatedata = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }; mtriangletexturecoordinates = bytebuffer.allocatedirect(triangle1texturecoordinatedata.length * mbytesperfloat) .order(byteorder.nativeorder()).asfloatbuffer(); mtriangletexturecoordinates.put(triangle1texturecoordinatedata).position(0); this.context = context; }
and here drawtriangle function
private void drawtriangle(final floatbuffer atrianglebuffer) { // pass in position information atrianglebuffer.position(mpositionoffset); gles20.glvertexattribpointer(mpositionhandle, mpositiondatasize, gles20.gl_float, false, mstridebytes, atrianglebuffer); gles20.glenablevertexattribarray(mpositionhandle); int colorhandle = gles20.glgetuniformlocation(programhandle, "u_color"); // set color drawing triangle gles20.gluniform4f(colorhandle, 0.0f, 0.8f, 1.0f, 1.0f); // multiplies view matrix model matrix, , stores result in mvp matrix // (which contains model * view). matrix.multiplymm(mmvpmatrix, 0, mviewmatrix, 0, mmodelmatrix, 0); // multiplies modelview matrix projection matrix, , stores result in mvp matrix // (which contains model * view * projection). matrix.multiplymm(mmvpmatrix, 0, mprojectionmatrix, 0, mmvpmatrix, 0); gles20.gluniformmatrix4fv(mmvpmatrixhandle, 1, false, mmvpmatrix, 0); gles20.gldrawarrays(gles20.gl_triangles, 0, 6); }
your drawtriangle
code doesn't set texture co-ordinates anywhere. add code this:
triangle1texturecoordinatedata.position(mpositionoffset * 2 / 3); gles20.glvertexattribpointer(mtexturecoordinatehandle, 2, gles20.gl_float, false, 0, triangle1texturecoordinatedata); gles20.glenablevertexattribarray(mtexturecoordinatehandle);
note can set stride 0 if data packed in buffer.
Comments
Post a Comment