shader - OpenGL ES 2.0, texture image not filling screen -
i want upload simple texture overlay square have drawn on screen. code without texture shows red square in centre of screen. im editing code overlay texture on top, every time try apply texture square distorts image , moves across screen. edit: whole code available here: http://codetidy.com/6291/
before texture applied:
after texture applied:
some sample code:
void init() // create opengl 2d texture box resource. glenable(gl_texture_2d); glenable(gl_blend); glblendfunc(gl_one, gl_src_color); glgentextures(1, &texturehandle); glbindtexture(gl_texture_2d, texturehandle); // set texture parameters. gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat); maopenglteximage2d(texture);
edit: whole draw() function
void draw() glfloat vvertices[] = { -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f }; glfloat texcoord[] = {0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0}; // set viewport glviewport(0, 0, mwidth, mheight); // clear color buffer glclear(gl_color_buffer_bit); // use program object gluseprogram(mshader); checkglerror("gluseprogram"); glbindtexture(gl_texture_2d, texturehandle); // set uniform function gluniformmatrix4fv(mmvploc, 1, false, mymatrix); checkglerror("gluniform4fv"); //glactivetexture(gl_texture0); gluniform1i(mtextureloc, 0); // load vertex data, i.e attributes , quads glvertexattribpointer(0, 3, gl_float, gl_false, 0, vvertices); glvertexattribpointer(1, 2, gl_float, gl_false, 0, texcoord); checkglerror("glvertexattribpointer"); glenablevertexattribarray(0); glenablevertexattribarray(1); checkglerror("glenablevertexattribarray"); gldrawarrays(gl_triangle_strip, 0, 4); checkglerror("gldrawarrays");
vertex , fragment shader:
char vertexshadersource[] = "attribute vec4 vposition; \n" "uniform mat4 umvp; \n" "attribute vec2 texcoordin; \n" "varying vec2 texcoordout; \n" "void main() \n" "{ \n" "gl_position = umvp * vposition; \n" "texcoordout = texcoordin.xy; \n" "} \n"; char fragmentshadersource[] = "precision highp float;\n" "varying vec2 texcoordout;\n" "uniform sampler2d texture;\n" "void main()\n" "{\n" "gl_fragcolor = texture2d(texture, texcoordout);\n" "}\n";
perhaps vertex , texture attributes swapped?
i'm suspicious part:
glvertexattribpointer(0, 3, gl_float, gl_false, 0, vvertices); glvertexattribpointer(1, 2, gl_float, gl_false, 0, texcoord);
are sure texcoord attribute @ location 1?
i suggest using glgetattriblocation query these locations.
Comments
Post a Comment