Android OpenGL ES 2.0 - Texture black -


i using opengl|es 2.0 create simple rectangle. not able textures working.my renderer & other class files below.

texturerenderer.java

public class texturerenderer  implements renderer {  private final context context; private final float[] projectionmatrix = new float[16]; private final float[] modelmatrix = new float[16]; private table table; private mallet mallet; private textureshaderprogram textureprogram; private colorshaderprogram colorprogram; private int texture;  public texturerenderer(context context) {     this.context = context; }     @override public void ondrawframe(gl10 gl) {     // todo auto-generated method stub     // clear rendering surface.     glclear(gl_color_buffer_bit);         // draw table.     textureprogram.useprogram();     textureprogram.setuniforms(projectionmatrix, texture);     table.binddata(textureprogram);     table.draw();      // draw mallets.     colorprogram.useprogram();     colorprogram.setuniforms(projectionmatrix);     mallet.binddata(colorprogram);     mallet.draw();  }  @override public void onsurfacechanged(gl10 gl, int width, int height) {     // todo auto-generated method stub     // set opengl viewport fill entire surface.     glviewport(0, 0, width, height); }  @override public void onsurfacecreated(gl10 gl, eglconfig config) {     // todo auto-generated method stub       glclearcolor(0.0f, 0.0f, 0.0f, 0.0f);     table = new table();     mallet = new mallet();     textureprogram = new textureshaderprogram(context);     colorprogram = new colorshaderprogram(context);     texture = texturehelper.loadtexture(context, r.drawable.air_hockey_surface);      log.d("tag",""+texture);  }} 

table.java

public class table {  private static final int position_component_count = 2; 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 final vertexarray vertexarray;  private static final float[] vertex_data = {     // order of coordinates: x, y, s, t     // triangle fan     0f, 0f, 0.5f, 0.5f,     -0.5f, -0.8f, 0f, 0.9f,     0.5f, -0.8f, 1f, 0.9f,     0.5f, 0.8f, 1f, 0.1f,     -0.5f, 0.8f, 0f, 0.1f,     -0.5f, -0.8f, 0f, 0.9f };  public table() {     vertexarray = new vertexarray(vertex_data);     }  public void binddata(textureshaderprogram textureprogram) {     vertexarray.setvertexattribpointer(0,             textureprogram.getpositionattributelocation(),             position_component_count, stride);     vertexarray.setvertexattribpointer(position_component_count,             textureprogram.gettexturecoordinatesattributelocation(),             texture_coordinates_component_count, stride); }  public void draw() {     gldrawarrays(gl_triangle_fan, 0, 6);     }} 

mallet.java

public class mallet {  private static final int position_component_count = 2; private static final int color_component_count = 3; private static final int stride = (position_component_count + color_component_count) * constants.bytes_per_float; private static final float[] vertex_data = { // order of coordinates: x, y, r, g, b 0f, -0.4f, 0f, 0f, 1f, 0f, 0.4f, 1f, 0f, 0f }; private final vertexarray vertexarray;  public mallet() {     vertexarray = new vertexarray(vertex_data); }  public void binddata(colorshaderprogram colorprogram) {     vertexarray.setvertexattribpointer(0,             colorprogram.getpositionattributelocation(),             position_component_count, stride);     vertexarray.setvertexattribpointer(position_component_count,             colorprogram.getcolorattributelocation(),             color_component_count, stride); }  public void draw() {     gldrawarrays(gl_points, 0, 2); }} 

shaderprogram.java

public class shaderprogram { // uniform constants protected static final string u_matrix = "u_matrix"; protected static final string u_texture_unit = "u_textureunit"; // attribute constants protected static final string a_position = "a_position"; protected static final string a_color = "a_color"; protected static final string a_texture_coordinates = "a_texturecoordinates"; // shader program protected final int program;  protected shaderprogram(context context, int vertexshaderresourceid,         int fragmentshaderresourceid) {     // compile shaders , link program.     program = shaderhelper.buildprogram(textresourcereader             .readtextfilefromresource(context, vertexshaderresourceid),             textresourcereader.readtextfilefromresource(context,                     fragmentshaderresourceid)); }  public void useprogram() {     // set current opengl shader program program.     gluseprogram(program); }} 

colorshaderprogram.java

public class colorshaderprogram extends shaderprogram {  // uniform locations private final int umatrixlocation; // attribute locations private final int apositionlocation; private final int acolorlocation;  public colorshaderprogram(context context) {     super(context, r.raw.simple_vertex_shader, r.raw.simple_fragment_shader);     // retrieve uniform locations shader program.     umatrixlocation = glgetuniformlocation(program, u_matrix);     // retrieve attribute locations shader program.     apositionlocation = glgetattriblocation(program, a_position);     acolorlocation = glgetattriblocation(program, a_color); }  public void setuniforms(float[] matrix) {     // pass matrix shader program.     gluniformmatrix4fv(umatrixlocation, 1, false, matrix, 0); }  public int getpositionattributelocation() {     return apositionlocation; }  public int getcolorattributelocation() {     return acolorlocation; }} 

textureshaderprogram.java

public class textureshaderprogram extends shaderprogram { // uniform locations private final int umatrixlocation; private final int utextureunitlocation; // attribute locations private final int apositionlocation; private final int atexturecoordinateslocation;  public textureshaderprogram(context context) {     super(context, r.raw.texture_vertex_shader,             r.raw.texture_fragment_shader);     // retrieve uniform locations shader program.     umatrixlocation = glgetuniformlocation(program, u_matrix);     utextureunitlocation = glgetuniformlocation(program, u_texture_unit);     // retrieve attribute locations shader program.     apositionlocation = glgetattriblocation(program, a_position);     atexturecoordinateslocation = glgetattriblocation(program,             a_texture_coordinates); }  public void setuniforms(float[] matrix, int textureid) {     // pass matrix shader program.     gluniformmatrix4fv(umatrixlocation, 1, false, matrix, 0);     // set active texture unit texture unit 0.     glactivetexture(gl_texture0);     // bind texture unit.     glbindtexture(gl_texture_2d, textureid);     // tell texture uniform sampler use texture in shader     // telling read texture unit 0.     gluniform1i(utextureunitlocation, 0); }  public int getpositionattributelocation() {     return apositionlocation; }  public int gettexturecoordinatesattributelocation() {     return atexturecoordinateslocation; }} 

texturehelper.java

public class texturehelper { private static final string tag = "texturehelper";  public static int loadtexture(context context, int resourceid) {      final int[] textureobjectids = new int[1];     glgentextures(1, textureobjectids, 0);     if (textureobjectids[0] == 0) {         if (loggerconfig.on) {             log.w(tag, "could not generate new opengl texture object.");         }         return 0;     }      final bitmapfactory.options options = new bitmapfactory.options();     options.inscaled = false;     final bitmap bitmap = bitmapfactory.decoderesource(             context.getresources(), resourceid, options);     if (bitmap == null) {         if (loggerconfig.on) {             log.w(tag, "resource id " + resourceid                     + " not decoded.");         }         gldeletetextures(1, textureobjectids, 0);         return 0;     }     glbindtexture(gl_texture_2d, textureobjectids[0]);      gltexparameteri(gl_texture_2d, gl_texture_min_filter,             gl_linear_mipmap_linear);     gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear);      gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge);     gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge);      teximage2d(gl_texture_2d, 0, bitmap, 0);     bitmap.recycle();      glgeneratemipmap(gl_texture_2d);     glbindtexture(gl_texture_2d, 0); // unbind texture      return textureobjectids[0];  }} 

shaderhelper.java

public class shaderhelper { private static final string tag = "shaderhelper";  public static int compilevertexshader(string shadercode) {     return compileshader(gl_vertex_shader, shadercode); }  public static int compilefragmentshader(string shadercode) {     return compileshader(gl_fragment_shader, shadercode); }  private static int compileshader(int type, string shadercode) {      final int shaderobjectid = glcreateshader(type);      if (shaderobjectid == 0) {         log.w(tag, "could not create shader");          return 0;     }     // shader , associate shadercode     glshadersource(shaderobjectid, shadercode);     glcompileshader(shaderobjectid);      final int[] compilestatus = new int[1];     glgetshaderiv(shaderobjectid, gl_compile_status, compilestatus, 0);     log.v(tag, "results of compiling source:" + "\n" + shadercode + "\n:"             + glgetshaderinfolog(shaderobjectid));     if (compilestatus[0] == 0) { // if failed, delete shader object.                                     // gldeleteshader(shaderobjectid);         log.w(tag, "compilation of shader failed.");         return 0;     }      return shaderobjectid; }  public static int linkprogram(int vertexshaderid, int fragmentshaderid) {     final int programobjectid = glcreateprogram();      if (programobjectid == 0) {         log.w(tag, "couldn't craete prgram");         return 0;     }     glattachshader(programobjectid, vertexshaderid);     glattachshader(programobjectid, fragmentshaderid);      gllinkprogram(programobjectid);      final int[] linkstatus = new int[1];     glgetprogramiv(programobjectid, gl_link_status, linkstatus, 0);     log.v(tag, "results of linking program:\n"             + glgetprograminfolog(programobjectid));     if (linkstatus[0] == 0) { // if failed, delete program object.         gldeleteprogram(programobjectid);         log.w(tag, "linking of program failed.");         return 0;     }      return programobjectid; }  public static boolean validateprogram(int programobjectid) {     glvalidateprogram(programobjectid);      final int[] validatestatus = new int[1];     glgetprogramiv(programobjectid, gl_validate_status, validatestatus, 0);      log.v(tag, "results of validating program: " + validatestatus[0]             + "\nlog:" + glgetprograminfolog(programobjectid));     return validatestatus[0] != 0;  }  public static int buildprogram(string vertexshadersource,         string fragmentshadersource) {     int program;     // compile shaders.     int vertexshader = compilevertexshader(vertexshadersource);     int fragmentshader = compilefragmentshader(fragmentshadersource);     // link them shader program.     program = linkprogram(vertexshader, fragmentshader);     if (loggerconfig.on) {         validateprogram(program);     }     return program; }} 

vertexarray.java

public class vertexarray { private final floatbuffer floatbuffer;  public vertexarray(float[] vertexdata) {     floatbuffer = bytebuffer             .allocatedirect(vertexdata.length * constants.bytes_per_float)             .order(byteorder.nativeorder()).asfloatbuffer().put(vertexdata); }  public void setvertexattribpointer(int dataoffset, int attributelocation,         int componentcount, int stride) {     floatbuffer.position(dataoffset);     glvertexattribpointer(attributelocation, componentcount, gl_float,             false, stride, floatbuffer);     glenablevertexattribarray(attributelocation);     floatbuffer.position(0); }} 

i refering 2 websites link1 & link2. got black screen whenever run app. image in power of 2 (512*512) not able find problem in code. please if knows it.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -