java - lwjgl glDrawArrays gives Invalid operation error -


when learning how use opengl 3.2 through lwjgl, followed tutorial @ here. keep getting invalid operation error on method call gldrawarrays. error occurs if copy source code tutorial.

my operating system mac os x 10.8.3 , after quick search not turn anything.

my code(adapted code tutorial):

render method:

public void render() {      gl11.glclear(gl11.gl_color_buffer_bit);                  // bind vao has information quad vertices      gl30.glbindvertexarray(vaoid);       gl20.glenablevertexattribarray(0);     this.exitonglerror("error before drawarrays");       // draw vertices         gl11.gldrawarrays(gl11.gl_triangles, 0, vertexcount);         this.exitonglerror("error in drawarrays");       // put default (deselect)         gl20.gldisablevertexattribarray(0);      gl30.glbindvertexarray(0);                   this.exitonglerror("error in render"); }    

rest of code:

import java.nio.floatbuffer; import org.lwjgl.bufferutils; import org.lwjgl.lwjglexception; import org.lwjgl.opengl.contextattribs; import org.lwjgl.opengl.display; import org.lwjgl.opengl.displaymode; import org.lwjgl.opengl.gl11; import org.lwjgl.opengl.gl15; import org.lwjgl.opengl.gl20; import org.lwjgl.opengl.gl30; import org.lwjgl.opengl.pixelformat; import org.lwjgl.util.glu.glu;  public class testopengl {  // entry point application   public static void main(string[] args) {         new testopengl();    }  // setup variables   private final string window_title = "the quad: gldrawarrays";    private final int width = 320;   private final int height = 240;  // quad variables    private int vaoid = 0;   private int vboid = 0;   private int vertexcount = 0;  public testopengl() {        // initialize opengl (display)           this.setupopengl();          this.setupquad();      while (!display.iscloserequested()) {                // single loop (logic/render)         render();         // force maximum fps of 60                   display.sync(60);                    // let cpu synchronize gpu if gpu tagging behind                     display.update();            }            // destroy opengl (display)          this.destroyopengl();        }  public void setupopengl() {          // setup opengl context api version 3.2          try {                pixelformat pixelformat = new pixelformat();                 contextattribs contextatrributes = new contextattribs(3, 2)                  .withforwardcompatible(true)                     .withprofilecore(true);          display.setdisplaymode(new displaymode(width, height));                  display.settitle(window_title);                  display.create(pixelformat, contextatrributes);          system.out.println(gl11.glgetstring(gl11.gl_version));               gl11.glviewport(0, 0, width, height);      } catch (lwjglexception e) {                 e.printstacktrace();                 system.exit(-1);         }      // setup xna background color            gl11.glclearcolor(0.4f, 0.6f, 0.9f, 0f);          // map internal opengl coordinate system entire screen            gl11.glviewport(0, 0, width, height);                    this.exitonglerror("error in setupopengl");  }  public void setupquad() {            // opengl expects vertices defined counter clockwise default            float[] vertices = {             // left bottom triangle              -0.5f, 0.5f, 0f,                 -0.5f, -0.5f, 0f,                0.5f, -0.5f, 0f,          // right top triangle                0.5f, -0.5f, 0f,             0.5f, 0.5f, 0f,          -0.5f, 0.5f, 0f          };      // sending data opengl requires usage of (flipped) byte buffers      floatbuffer verticesbuffer = bufferutils.createfloatbuffer(vertices.length);         verticesbuffer.put(vertices);            verticesbuffer.flip();             vertexcount = 6;               // create new vertex array object in memory , select (bind)           // vao can have 16 attributes (vbo's) assigned default     vaoid = gl30.glgenvertexarrays();     gl30.glbindvertexarray(vaoid);      // create new vertex buffer object in memory , select (bind)          // vbo collection of vectors in case resemble location of each vertex.             vboid = gl15.glgenbuffers();      gl15.glbindbuffer(gl15.gl_array_buffer, vboid);     gl15.glbufferdata(gl15.gl_array_buffer, verticesbuffer, gl15.gl_static_draw);     // put vbo in attributes list @ index 0     gl20.glvertexattribpointer(0, 3, gl11.gl_float, false, 0, 0);      // deselect (bind 0) vbo          gl15.glbindbuffer(gl15.gl_array_buffer, 0);      // deselect (bind 0) vao          gl30.glbindvertexarray(0);      this.exitonglerror("error in setupquad");  }  public void destroyopengl() {            // disable vbo index vao attributes list            gl20.gldisablevertexattribarray(0);      // delete vbo            gl15.glbindbuffer(gl15.gl_array_buffer, 0);     gl15.gldeletebuffers(vboid);           // delete vao        gl30.glbindvertexarray(0);           gl30.gldeletevertexarrays(vaoid);          display.destroy(); }  public void exitonglerror(string errormessage) {         int errorvalue = gl11.glgeterror();              if (errorvalue != gl11.gl_no_error) {            string errorstring = glu.gluerrorstring(errorvalue);             system.err.println("error - " + errormessage + ": " + errorstring);                  if (display.iscreated())             display.destroy();               system.exit(-1);             } } } 

the output is

3.2 intel-8.10.44 error - error in drawarrays: invalid operation 

you're code appears not using shaders entirely. requesting forward compatible opengl context there no support fixed function pipeline therefor shaders neccessary. have either use shaders proccess vertices or use compatibility context in order have opengl vertex , fragment processing.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -