android - OpenGL-es projection doesn't seem to work -
i've tried following lessons drawing shapes , applying projections them found here, projection doesn't seem work. far know i've copy-pasted pieces of code in project, here important code parts:
in renderer
class:
@override public void ondrawframe(gl10 unused) { gles20.glclear(gles20.gl_color_buffer_bit); // set camera position (view matrix) matrix.setlookatm(mvmatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // calculate projection , view transformation matrix.multiplymm(mmvpmatrix, 0, mprojmatrix, 0, mvmatrix, 0); // draw shape mtriangle.draw(mmvpmatrix); } @override public void onsurfacechanged(gl10 unused, 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); }
the draw
method of triangle
class:
public void draw(float[] mvpmatrix) { gles20.gluseprogram(mprogram); mpositionhandle = gles20.glgetattriblocation(mprogram, "vposition"); // handle vertex shader's vposition member gles20.glenablevertexattribarray(mpositionhandle); // enable handle triangle vertices gles20.glvertexattribpointer(mpositionhandle, coords_per_vertex, gles20.gl_float, false, vertexstride, vertexbuffer); // prepare triangle coordinate data mcolorhandle = gles20.glgetuniformlocation(mprogram, "vcolor"); // handle fragment shader's vcolor member gles20.gluniform4fv(mcolorhandle, 1, color, 0); // set color drawing triangle mmvpmatrixhandle = gles20.glgetuniformlocation(mprogram, "umvpmatrix"); // handle shape's transformation matrix gles20.gluniformmatrix4fv(mmvpmatrixhandle, 1, false, mvpmatrix, 0); // apply projection , view transformation gles20.gldrawarrays(gles20.gl_triangles, 0, vertexcount); // draw triangle gles20.gldisablevertexattribarray(mpositionhandle); // disable vertex array }
the triangle still being stretched. doing wrong?
did try debug in onsurfacechanged? happens in lines? especially:
- what passed values
width
,height
? - which value
float ratio = (float) width / height;
calculate? - do see changes values of mprojmatrix after call
matrix.frustumm(mprojmatrix, 0, -ratio, ratio, -1, 1, 3, 7);
?
- what passed values
how
private final string fragmentshadercode
inclass triangle
like? order of multiplication in line" gl_position = umvpmatrix * vposition;" +
important.
your provided code looks quiet good, difficult issue here.
Comments
Post a Comment