opengl es - Getters in Android Open GLES 2.0 don't work -
for quite long time looking answer in different forums why in below mentioned case getters not work. decided register in stackoverflow , try ask here. please, let me know doing wrong. have cube 27 class:
public class cube27 { private floatbuffer vertexbuffer; // buffer vertex-array private shortbuffer indexbuffer; float x; float y; float z; float s; float xoff; float yoff; float zoff; private int colorhandle; private final string vertexshadercode = "uniform mat4 umvpmatrix;" + "attribute vec4 vposition;" + "void main() {" + " gl_position = umvpmatrix * vposition;" + "}"; private final string fragmentshadercode = "precision mediump float;" + "uniform vec4 vcolor;" + "void main() {" + " gl_fragcolor = vcolor;" + "}"; private int mvpmatrixhandle; private int positionhandle; private final int program; public float[] mmodelmatrix = new float[16]; //přidáno kvůli translaci !!! static final int coords_per_vertex = 3; private final int vertexstride = coords_per_vertex * 4; // 4 bytes per vertex private float[][] colors = { // colors of 6 faces {1.0f, 0.5f, 0.0f, 0.3f}, // 0. orange front {1.0f, 0.0f, 1.0f, 0.3f}, // 1. violet {1.0f, 0.0f, 0.0f, 0.3f}, // 2. red bottom {0.0f, 0.0f, 1.0f, 0.3f}, // 3. blue top {0.0f, 1.0f, 0.0f, 0.3f}, // 4. green right {1.0f, 1.0f, 0.0f, 0.3f} // 5. yellow left }; short[][] indexesordered = { {0, 1, 3, 0, 3, 2}, // orange front {4, 6, 7, 4, 7, 5}, // violet {2, 5, 4, 2, 0, 4}, // green bottom {1, 6, 3, 6, 3, 7}, // blue top {2, 3, 5, 3, 5, 7}, // red right {6, 0, 1, 4, 6, 0} // yellow left }; // constructor - set buffers public cube27(float x, float y, float z, float s, float xoff, float yoff, float zoff) { matrix.setidentitym(mmodelmatrix, 0); //přidáno kvůli translaci !!! float[] vertices = { // vertices of 6 faces x+xoff-s, y+yoff-s, z+zoff-s, // 0. left-bottom-front x+xoff+s, y+yoff-s, z+zoff-s, // 1. right-bottom-front x+xoff-s, y+yoff+s, z+zoff-s, // 2. left-top-front x+xoff+s, y+yoff+s, z+zoff-s, // 3. right-top-front x+xoff-s, y+yoff-s, z+zoff+s, // 4. left-bottom-back x+xoff-s, y+yoff+s, z+zoff+s, // 5. left-top-back x+xoff+s, y+yoff-s, z+zoff+s, // 6. right-bottom-back x+xoff+s, y+yoff+s, z+zoff+s, // 7. right-top-back }; // setup vertex-array buffer. vertices in float. float has 4 bytes bytebuffer vbb = bytebuffer.allocatedirect(vertices.length * 4); vbb.order(byteorder.nativeorder()); // use native byte order vertexbuffer = vbb.asfloatbuffer(); // convert byte float vertexbuffer.put(vertices); // copy data buffer vertexbuffer.position(0); // rewind int vertexshader = myglrenderer.loadshader(gles20.gl_vertex_shader, vertexshadercode); int fragmentshader = myglrenderer.loadshader(gles20.gl_fragment_shader, fragmentshadercode); program = gles20.glcreateprogram(); gles20.glattachshader(program, vertexshader); gles20.glattachshader(program, fragmentshader); gles20.gllinkprogram(program); } public float getx() { return x; } public void setx(float sourx) { x = sourx; } public float gety() { return y; } public void sety(float soury) { y = soury; } public float getz() { return z; } public void setz(float sourz) { z = sourz; } public float gets() { return s; } public void sets(float sours) { s = sours; } public float getxoff() { return xoff; } public void setxoff(float sourxoff) { xoff = sourxoff; } public float getyoff() { return yoff; } public void setyoff(float souryoff) { yoff = souryoff; } public float getzoff() { return zoff; } public void setzoff(float sourzoff) { zoff = sourzoff; } // draw shape public void draw(float[] mvpmatrix) { gles20.gluseprogram(program); positionhandle = gles20.glgetattriblocation(program, "vposition"); gles20.glenablevertexattribarray(positionhandle); gles20.glvertexattribpointer(positionhandle, coords_per_vertex, gles20.gl_float, false, vertexstride, vertexbuffer); mvpmatrixhandle = gles20.glgetuniformlocation(program, "umvpmatrix"); gles20.gluniformmatrix4fv(mvpmatrixhandle, 1, false, mvpmatrix, 0); // render faces int numfaces = 6; (int face = 0; face < numfaces; face++) { bytebuffer idb = bytebuffer.allocatedirect(indexesordered[face].length * 2); idb.order(byteorder.nativeorder()); indexbuffer = idb.asshortbuffer(); indexbuffer.put(indexesordered[face]); indexbuffer.position(0); // set color each of faces colorhandle = gles20.glgetuniformlocation(program, "vcolor"); gles20.gluniform4fv(colorhandle, 1, colors[face], 0); gles20.gldrawelements(gles20.gl_triangles, indexesordered[face].length, gles20.gl_unsigned_short, indexbuffer); } gles20.gldisablevertexattribarray(positionhandle); } }
in myglsurfaceview set renderer drawing on glsurfaceview in ontouchevent method select cube it’s index:
public boolean ontouchevent(motionevent e) { if((math.abs(dx)+math.abs(dy))<5) { if (x>22.0f && x<230.0f) mrenderer.setindy(0); else if (x>256.0f && x<464.0f) mrenderer.setindy(1); else if (x>488.0f && x<696.0f) mrenderer.setindy(2); if (y>326.0f && y<538.0f) mrenderer.setindx(2); else if (y>560.0f && y<768.0f) mrenderer.setindx(1); else if (y>794.0f && y<1002.0f) mrenderer.setindx(0); mrenderer.setindz(0); }
then have myglrenderer class, have declared (among others ):
public class myglrenderer implements glsurfaceview.renderer { private int indx=0; private int indy=0; private int indz=0; int pocet = 3; cube27[][][] mcube27 = new cube27[pocet][pocet][pocet];
and in onsurfacecreated method have created 3 * 3 * 3 cubes = 27 cubes, create 1 big cube looking rubik cube.
public void onsurfacecreated(gl10 unused, eglconfig config) { (int = 0; < pocet ; i++) { (int j = 0; j < pocet ; j++) { (int k = 0; k < pocet ; k++) { if ((i==1)&&(j==1)&&(k==1)) //x,y,z,s,xoff,yoff,zoff mcube27[i][j][k]= new cube27(i-pocet/2,j-pocet/2,kpocet/2,0.15f, 0, 0, 0); else mcube27[i][j][k] = new cube27(i-pocet/2,j-pocet/2,k-pocet/2,0.35f, 0, 0, 0); } } }
and draw cubes in ondrawframes method
public void ondrawframe(gl10 unused) { float[] scratchr = new float[16]; gles20.glenable(gl10.gl_depth_test); //all necessary code make open gles 2.0 work ending matrix.multiplymm(scratchr, 0, mmvpmatrix, 0, mrotationmatrix, 0);
then declare 6 variables , assign them values this:
float gx = mcube27[indx][indy][indz].getx(); float gy = mcube27[indx][indy][indz].gety(); float gz = mcube27[indx][indy][indz].getz(); float gs = mcube27[indx][indy][indz].gets(); float gxf = mcube27[indx][indy][indz].getxoff(); float gyf = mcube27[indx][indy][indz].getyoff(); float gzf = mcube27[indx][indy][indz].getzoff();
and created new cube27[indx][indy][indz] instead of selected 1 values same old 1 had , new this:
mcube27[indx][indy][indz]= new cube27(indx-pocet/2,indy-pocet/2,indz-pocet/2, 0.2f, gxf, gyf+0.5f, gzf);
this new cube should moved 0.5 in direction of y axis , every further touch 0.5.
finally draw cubes in cycle this:
for (int = 0; < pocet ; i++) { (int j = 0; j < pocet ; j++) { (int k = 0; k < pocet ; k++) { mcube27[i][j][k].draw(scratchr);//rotace pomocí setrotatem } } }
and realized did not work. when touch screen first time new cube has been created @ right place , side 0.2, because values gx, gy , gz 0. when touch screen second time, nothing changes. indicates there not new value of 0.5 in gyf, still 0, because getyoff did not read new value. tryied this:
mcube27[indx][indy][indz]= new cube27( gx, gy, gz, gs + 0.35f, gxf, gyf, gzf);
what should make bigger cube (0.35 + 0.35 = 0.7) @ same place , result follows: mcube27[indx][indy][indz] disappeared , central cube, side of 0.15 of same size 0.35 other cubes. indicates mcube27[indx][indy][indz] created @ place of central cube coords of 0, 0, 0. confirms values gx, gy , gz 0, because gety, gety , getz did not work.
very interesting when set indexes in ontouchevent(motionevent e) method of myglsurfaceview class using setindx, setindy , setindz methods, works , when use indexes in myglrenderer correctly set.
can explain doing wrong ?
Comments
Post a Comment