android - Mapping texture to square like the picture itself -
i'm orking since 2 weeks opengl es , try draw image on square. square made of 2 triangles. when draw original image (http://satgeo.zum.de/satgeo/methoden/dhm_web/bilder/grundidee/raster.gif) ends way https://pl.vc/tfuc
that's code:
public class square { public static final string tag = "screen"; private static final int position_component_count = 3; private static final int texture_coordinates_component_count = 2; private static final int stride = (position_component_count + texture_coordinates_component_count) * constants.bytes_per_float; private static final float[] vertex_data = { -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f }; private final vertexarray vertexarray; public square(){ vertexarray = new vertexarray(vertex_data); } public void binddata(textureshaderprogram textureshader){ vertexarray.setvertexattribpointer(0, textureshader.getpositionattributelocation(), position_component_count, stride); vertexarray.setvertexattribpointer(position_component_count, textureshader.gettexturecoordinatesattributelocation(), texture_coordinates_component_count, stride); } public void draw(){ int anzvertices = vertex_data.length / (position_component_count + texture_coordinates_component_count); gles20.gldrawarrays(gles20.gl_triangles, 0, anzvertices); } }
how can draw texture without transformation , looks original one?
for case, don't need vertex attribute texture coordinates. since direct mapping of position, can calculate texture coordinates in vertex shader, based on value of position attribute.
you of course have adjust naming, glsl code in vertex shader this:
attribute vec3 position; varying vec2 texcoord; ... gl_position = vec4(position, 1.0); texcoord = 0.5 * position.xy + 0.5;
this maps coordinates in range [-1.0, 1.0] texture coordinates in range [0.0, 1.0].
this save code, more efficient, , less error prone.
if use texture coordinates, ones tried wrong. can tell can't possibly right fact you're using same texture coordinates both triangles. same triangular part of image mapped both of triangles use render quad.
not functionality problem, recommend use gl_triangle_strip
instead of gl_triangles
. way, need 4 vertices instead of 6. vertex data (including texture coordinates, long use them) this:
private static final float[] vertex_data = { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
and draw call:
gles20.gldrawarrays(gles20.gl_triangle_strip, 0, anzvertices);
again, make code simpler, more efficient, , less redundancy have fewer sources of errors.
Comments
Post a Comment