lwjgl - OpenGL/JOGL throwing GL_INVALID_OPERATION -


i coding level editor game developing. use jogl , seem have problem. used lwjgl opengl calls , adjusting core opengl little confusing since lwjgl seem have simplified lot of stuff.

so problem created model holds vao id/name , vertex count , model loader creates model , renderer. renderer not batched @ moment. work on later. problem opengl throws gl_invalid_operation error. not sure causing it. else including basic triangle drew test environment works, there seems problem somewhere in loader or renderer.

here's code: model:

public class joglmodel { private int vaoid; private int vertexcount;  public joglmodel(int vertexcount, int vaoid) {     this.vertexcount = vertexcount;     this.vaoid = vaoid; }  public int getvertexcount() {     return vertexcount; }  public int getvaoid() {     return vaoid; } 

}

loader:

public class modelloader { private  gl2 gl; private list<int[]> vaos = new arraylist<int[]>(); private list<int[]> vbos = new arraylist<int[]>();  public modelloader(gl2 gl){     this.gl = gl; }  public joglmodel loadtovao(float[] positions){     int vaoid = createvao();     storedatainattributelist(0,positions);     unbind();       return new joglmodel(vaoid,positions.length/3); }  private int createvao(){     int[] vaoid = new int[1];     gl.glgenvertexarrays(vaoid.length, vaoid, 0);     vaos.add(vaoid);     gl.glbindvertexarray(vaoid[0]);     return vaoid[0]; }  private void storedatainattributelist(int attributenumber,float[] data){     int[] vboid = new int[1];     gl.glgenbuffers(vboid.length,vboid,0);     vbos.add(vboid);     gl.glbindbuffer(gl.gl_array_buffer,vboid[0]);     floatbuffer floatbuffer = createfloatbuffer(data);     gl.glbufferdata(gl.gl_array_buffer,floatbuffer.remaining(),floatbuffer,gl.gl_static_draw);     gl.glvertexattribpointer(attributenumber,3,gl.gl_float,false,0,0);     gl.glbindbuffer(gl.gl_array_buffer,0);  }  private floatbuffer createfloatbuffer(float[] data){     floatbuffer floatbuffer = floatbuffer.allocate(data.length);     floatbuffer.put(data);     floatbuffer.flip();     return floatbuffer; }  private void unbind(){} public void clear(){     for(int[] vao : vaos){         gl.gldeletevertexarrays(vao.length,vao,0);     }      for(int[] vbo: vbos){         gl.gldeletebuffers(vbo.length,vbo,0);     }     vaos.clear();     vbos.clear(); } 

}

renderer:

public class joglrenderer { 

private gl2 gl;

public joglrenderer(gl2 gl){     this.gl = gl; }  public void begin(){    gl.glclearcolor(1f,0f,0f,1f);    gl.glclear(gl.gl_clear_buffer);   }  public void render(joglmodel joglmodel){      gl.glbindvertexarray(joglmodel.getvaoid());     gl.glenablevertexattribarray(0);     gl.gldrawarrays(gl.gl_triangles,0,joglmodel.getvertexcount());     gl.gldisablevertexattribarray(0);     gl.glbindvertexarray(0);      /*        gl.glbegin(gl.gl_triangles);       gl.glcolor3f(1, 0, 0);        gl.glvertex2f(-1, -1);       gl.glcolor3f(0, 1, 0);       gl.glvertex2f(0, 1);       gl.glcolor3f(0, 0, 1);       gl.glvertex2f(1, -1);      gl.glend();     */ }  public void checkerror() {     string errorstring = "";     int error = gl.glgeterror();     if (error != gl.gl_no_error) {          switch (error) {             case gl.gl_invalid_enum:                 errorstring = "gl_invalid_enum";                 break;             case gl.gl_invalid_value:                 errorstring = "gl_invalid_value";                 break;             case gl.gl_invalid_operation:                 errorstring = "gl_invalid_operation";                 break;             case gl.gl_invalid_framebuffer_operation:                 errorstring = "gl_invalid_framebuffer_operation";                 break;             case gl.gl_out_of_memory:                 errorstring = "gl_out_of_memory";                 break;             default:                 errorstring = "unknown";                 break;         }      }     system.out.println(errorstring); } 

}

the commented out triangle part works fine. there seems error in clear screen method that's not concern right now. can 1 point out problem be?

thanks

(edit) figured out opengl error. accidentally passing vaoid vertex count , vice versa . fixed error gone. nothing being rendered. ideas?

i write here few considerations, since comments short that:

  • loadtovao lead wrong, don't load vao, vao useful remember vertices attributes arrays enabled, layout/format , vbo refer to, don't have call them every frame. can store bound element array. glenablevertexattribarray , gldisablevertexattribarray shouldn't go in render() function

  • the renderer should there default, i'd suggest have main , there initialize renderer (the gleventlistener)

  • i'd not bind vao in createvao

  • do not store gl element. keep transient (pass argument everytime) or glcontext. first option may increase complexity (since every gl call need have gl object class implementing gleventlistener) simplifies debugging (because know exactly in order gl calls executed).

  • if need 1 vao, avoid creating list that, same vbo.

  • i suggest use static final int variables hold vertices attribute indices. improves readability , avoid potential bugs.

  • unless not need direct buffers, use glbuffers allocate (direct) buffers.

  • what gl.gl_float? never saw that. use float.bytes or glbuffers.sizeof_float instead.

  • as @bdl said, @ glclear , call checkerror here, passing every time different string can find out problematic call if throw error.

  • jogl have gl_color_buffer_bit, write , call auto completition, ide should suggest right location or automatically insert right import if set properly

  • what looks missing (maybe didn't report it) glvertexattribpointer

  • if still not work, come basic test triangle, sure works , start building there. move outside renderer in own class, rich more geometry, use indexed drawing, ecc. each step control works, if doesn't, error lies in last modifications.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -