android - Correct vertex shader code? OpenGL ES 2.0 -
edit code added, please see below
edit 2 - screenshots device included @ bottom along explanation
edit 3 - new code added
i have 2 classes, rendered , custom 'quad' class.
i have these declared @ class level in renderer class:
final float[] mmvpmatrix = new float[16]; final float[] mprojmatrix = new float[16]; final float[] mvmatrix = new float[16];
and in onsurfacechanged method have:
@override public void onsurfacechanged(gl10 gl, int width, int height) {
gles20.glviewport(0, 0, width, height); float ratio = (float) width / height; matrix.frustumm(mprojmatrix, 0, -ratio, ratio, -1, 1, 3, 7); }
and....
public void onsurfacecreated(gl10 gl, eglconfig config) { // todo auto-generated method stub mybitmap = bitmapfactory.decoderesource(curview.getresources(), r.drawable.box); //create new dot objects dot1 = new quad(); dot1.settexture(curview, mybitmap); dot1.setsize(300,187); //these numbers size redundant/not used @ moment. mybitmap.recycle(); //set colour black gles20.glclearcolor(0, 0, 0, 1); }
and class, ondrawframe:
@override public void ondrawframe(gl10 gl) { // todo auto-generated method stub //paint screen colour defined in onsurfacecreated gles20.glclear(gles20.gl_color_buffer_bit); // set camera position (view matrix) looking front matrix.setlookatm(mvmatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // combine matrix.multiplymm(mmvpmatrix, 0, mprojmatrix, 0, mvmatrix, 0); dot1.rotatequad(0,0,45, mmvpmatrix); //x,y,angle , matrix passed in }
then, in quad class:
this declared @ class level:
private float[] mrotationmatrix = new float[16]; private final float[] mmvpmatrix = new float[16]; private final float[] mprojmatrix = new float[16]; private final float[] mvmatrix = new float[16]; private int mmvpmatrixhandle; private int mpositionhandle; private int mrotationhandle; //create our vertex shader string strvshader = "uniform mat4 umvpmatrix;" + "uniform mat4 urotate;" + "attribute vec4 a_position;\n"+ "attribute vec2 a_texcoords;" + "varying vec2 v_texcoords;" + "void main()\n" + "{\n" + // "gl_position = a_position * urotate;\n"+ // "gl_position = urotate * a_position;\n"+ "gl_position = a_position * umvpmatrix;\n"+ // "gl_position = umvpmatrix * a_position;\n"+ "v_texcoords = a_texcoords;" + "}"; //fragment shader string strfshader = "precision mediump float;" + "varying vec2 v_texcoords;" + "uniform sampler2d u_basemap;" + "void main()" + "{" + "gl_fragcolor = texture2d(u_basemap, v_texcoords);" + "}";
then method setting texture (don't think relevant problem though!!)
public void settexture(glsurfaceview view, bitmap imgtexture){ this.imgtexture=imgtexture; iprogid = utils.loadprogram(strvshader, strfshader); ibasemap = gles20.glgetuniformlocation(iprogid, "u_basemap"); iposition = gles20.glgetattriblocation(iprogid, "a_position"); itexcoords = gles20.glgetattriblocation(iprogid, "a_texcoords"); texid = utils.loadtexture(view, imgtexture); }
and finally, 'rotatequad' method (which supposed draw , rotate quad).
public void rotatequad(float x, float y, int angle, float[] mvpmatrix){ matrix.setrotatem(mrotationmatrix, 0, angle, 0, 0, 0.1f); // matrix.translatem(mrotationmatrix, 0, 0, 0, 0); //removed temporarily // combine rotation matrix projection , camera view matrix.multiplymm(mvpmatrix, 0, mrotationmatrix, 0, mvpmatrix, 0); float[] vertices = { -.5f,.5f,0, 0,0, .5f,.5f,0, 1,0, -.5f,-.5f,0, 0,1, .5f,-.5f,0, 1,1 }; vertexbuf = bytebuffer.allocatedirect(vertices.length * 4).order(byteorder.nativeorder()).asfloatbuffer(); vertexbuf.put(vertices).position(0); //bind correct texture gles20.glbindtexture(gles20.gl_texture_2d, texid); //use program gles20.gluseprogram(iprogid); // handle shape's transformation matrix mmvpmatrixhandle = gles20.glgetuniformlocation(iprogid, "umvpmatrix"); // apply projection , view transformation gles20.gluniformmatrix4fv(mmvpmatrixhandle, 1, false, mvpmatrix, 0); // handle shape's rotation matrix mrotationhandle = gles20.glgetuniformlocation(iprogid, "urotate"); // apply projection , view transformation gles20.gluniformmatrix4fv(mrotationhandle, 1, false, mrotationmatrix, 0); //set starting position vertices vertexbuf.position(0); //specify attributes vertex gles20.glvertexattribpointer(iposition, 3, gles20.gl_float, false, 5 * 4, vertexbuf); //enable attribute position gles20.glenablevertexattribarray(iposition); //set starting position texture vertexbuf.position(3); //specify attributes vertex gles20.glvertexattribpointer(itexcoords, 2, gles20.gl_float, false, 5 * 4, vertexbuf); //enable attribute texture gles20.glenablevertexattribarray(itexcoords); //draw gles20.gldrawarrays(gles20.gl_triangle_strip, 0, 4); }
for edit 2.
this quad drawn in center of screen. no rotation.
this same quad rotated @ +45 degrees code "gl_position = a_position * umvpmatrix;" + in vertex shader (it's different project shader variable a_position , not vposition), it's looks correct!
however, same quad rotated @ +45 degrees 2 shader variables switched (so read "gl_position = umvpmatrix * a_position;" - can see, it's not quite right.
also side note, can't see here quare symetrical, each method rotates in opposite direction other....
any appreciated.
it's impossible tell because don't know passing these 2 variables.
opengl column-major format, if vposition
in fact vector, , umvpmatrix
matrix, first option correct, if in shader.
if not in shader in program code, there not enough information.
if using first option getting unexpected results, not computing matrix or not passing correct vertices.
Comments
Post a Comment