PDA

View Full Version : Exception using VBOs



elect
02-03-2012, 07:36 AM
Hi guys,

I had my program working with VBOs, but then I had to change the general structure, create additional classes and make everything cleaner, ect..

But when I finished it was not working any more...

Here the basic:


public class Test extends javax.swing.JFrame {

private Viewer viewer=null;
ArrayList<Triangle> sectionPointsArray;
ArrayList<LightBulb> lightBulbs = new ArrayList<LightBulb>();
ArrayList<Plane> planes = new ArrayList<Plane>();

public Test() {
initComponents();

DataModelViewer.testclass = this;

this.setSize(800, 800);
viewer = new Viewer();
DataModelViewer.viewer = viewer;

this.getContentPane().add(viewer,BorderLayout.CENT ER);

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

fileChooser = new javax.swing.JFileChooser();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
button_load = new javax.swing.JMenuItem();
button_calc = new javax.swing.JMenuItem();
button_exit = new javax.swing.JMenuItem();

fileChooser.setDialogTitle("Choose a stl file..");
fileChooser.setFileFilter(new StlFilter());

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jMenu1.setText("Process");

button_load.setText("Load Model");
button_load.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_loadActionPerformed(evt);
}
});
jMenu1.add(button_load);

button_calc.setText("Calculate");
button_calc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_calcActionPerformed(evt);
}
});
jMenu1.add(button_calc);

button_exit.setText("Exit");
button_exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_exitActionPerformed(evt);
}
});
jMenu1.add(button_exit);

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

pack();
}// </editor-fold>

private void button_loadActionPerformed(java.awt.event.ActionEv ent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
System.out.println("Loading model.");
LoadSTL loadSTL = new LoadSTL(file, DataModelViewer.array);
System.out.println("Loading model finished.");
viewer.loadModel(DataModelViewer.gl);

}
else {
System.out.println("File access cancelled by user.");
}
}
}



public class Viewer extends JPanel implements GLEventListener, MouseListener,
MouseMotionListener, MouseWheelListener, KeyListener {
public Viewer () {

// this.frame=frame;
initDisplay();
initFlags();

//fPSAnimator = new FPSAnimator(gLCanvas, frameRate);

//JoglEventListener listener = new JoglEventListener(canvas);

}

private void initDisplay() {

glProfile = GLProfile.get(GLProfile.GL2);
final GLCapabilities glCapabilities = new GLCapabilities(glProfile);

// Initialize the GL component
GLCanvas glComponent = new GLCanvas(glCapabilities);
this.gLCanvas = glComponent;

this.setLayout(new BorderLayout());
gLCanvas.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
gLCanvas.setBackground(Color.RED);
this.add(gLCanvas, BorderLayout.CENTER);
gLCanvas.requestFocus();

//glComponent.addGLEventListener(this);
gLCanvas.addGLEventListener(this);
gLCanvas.addMouseListener(this);
gLCanvas.addMouseMotionListener(this);
gLCanvas.addMouseWheelListener(this);
gLCanvas.addKeyListener(this);
}

@Override
public void init(GLAutoDrawable gLAutoDrawable) {
System.out.println("Init");

currentRotation = identity();

// Create and start the animator
animator = new Animator(gLCanvas);
//animator.setRunAsFastAsPossible(true);
animator.start();

// Perform the default GL initialization
GL2 gl = gLAutoDrawable.getGL().getGL2();
DataModelViewer.gl = gl;

loadFloorTexture(gl);


gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
//gl.glClearColorIi(1, 10, 100, 1); // set background color
gl.glClearColor(0.17f, 0.65f, 0.92f, 0.0f); // light blue
// gl.glEnable(GL2.GL_DEPTH_TEST); // depth test for z-test
// gl.glEnable(GL2.GL_LIGHTING); // enable lighting
// gl.glEnable(GL2.GL_LIGHT0); // set light from viewer
//gl.glShadeModel(GL2.GL_SMOOTH); // use smooth shading
//gl.glMatrixMode(GL2.GL_MODELVIEW);

//gl.glEnable(GL2.GL_COLOR_MATERIAL);
//gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_DIFFUSE);
}

@Override
public void display(GLAutoDrawable gLAutoDrawable) {
//System.out.println("Display");

GL2 gl = gLAutoDrawable.getGL().getGL2();

if ( switchableFlags.get("lighting").getFlag() )
gl.glEnable(GL2.GL_LIGHTING);
else
gl.glDisable(GL2.GL_LIGHTING);

if ( switchableFlags.get("smoothing").getFlag() )
gl.glShadeModel(GL2.GL_SMOOTH);
else
gl.glShadeModel(GL2.GL_FLAT);

if ( switchableFlags.get("color").getFlag() )
gl.glEnable(GL2.GL_COLOR_MATERIAL);
else
gl.glDisable(GL2.GL_COLOR_MATERIAL);

// set modelview Matrix
gl.glMatrixMode(GL2.GL_MODELVIEW);

// clear color and depth buffers (faster than doing separately)
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

gl.glLoadIdentity();

//gl.glMultMatrixf(modelviewMatrix, 0);
gl.glMultMatrixf(currentRotation, 0);


gl.glTranslatef((float)translationX/20.0f, (float)translationY/20.0f, 0);
gl.glScalef(scale%10.0f, scale%10.0f, scale%10.0f);

gl.glMatrixMode(GL2.GL_PROJECTION);

if(isThereModel) {
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

// select buffer and specify its format
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
//gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexData);

// gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalPointBufferID);
// gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);

// gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
// gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);

gl.glColor3f(0.0f, 0.0f, 0.0f);
//

gl.glDrawArrays(GL2.GL_TRIANGLES, 0, triangleNumber*3);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
// gl.glDisable(GL2.GL_NORMAL_ARRAY);
}
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);

renderLightSource(gl);

drawFloor(gl);
}

private void initVertexArray(GL2 gl) {
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);

gl.glBufferData(GL2.GL_ARRAY_BUFFER, triangleNumber*3*3, vertexData, GL2.GL_STATIC_DRAW);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}



And this is the error I get:


Exception in thread "AWT-EventQueue-0-AWTAnimator-1" javax.media.opengl.GLException: javax.media.opengl.GLException: array vertex_buffer_object must be enabled to call this method
at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenG LThread(AWTThreadingPlugin.java:98)
at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(T hreadingImpl.java:197)
at javax.media.opengl.Threading.invokeOnOpenGLThread( Threading.java:164)
at javax.media.opengl.awt.GLCanvas.maybeDoSingleThrea dedWorkaround(GLCanvas.java:830)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.j ava:419)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWT AnimatorImpl.java:74)
at com.jogamp.opengl.util.AnimatorBase.display(Animat orBase.java:142)
at com.jogamp.opengl.util.Animator$MainLoop.run(Anima tor.java:176)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.media.opengl.GLException: array vertex_buffer_object must be enabled to call this method
at jogamp.opengl.gl4.GL4bcImpl.checkBufferObject(GL4b cImpl.java:32025)
at jogamp.opengl.gl4.GL4bcImpl.checkArrayVBOEnabled(G L4bcImpl.java:32052)
at jogamp.opengl.gl4.GL4bcImpl.glVertexPointer(GL4bcI mpl.java:31004)
at cuda.Viewer.display(Viewer.java:333)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawa bleHelper.java:182)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableH elper.java:170)
at javax.media.opengl.awt.GLCanvas$DisplayAction.run( GLCanvas.java:939)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawable Helper.java:398)
at javax.media.opengl.awt.GLCanvas$DisplayOnEventDisp atchThreadAction.run(GLCanvas.java:956)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:241)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:705)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 675)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)

carsten neumann
02-03-2012, 09:26 AM
Please use [ code]/[ /code] around source snippets so that they use a fixed width font. It also helps readability if you remove commented out parts ;)

I don't see a call to glGenBuffers().

elect
02-06-2012, 12:36 AM
Please use [ code]/[ /code] around source snippets so that they use a fixed width font. It also helps readability if you remove commented out parts ;)

I don't see a call to glGenBuffers().

Yeah sure, sorry but I was a little in a rush :p

Ok, I used the code tags, removed the commented parts out and added a glGebBuffers, but I still get an error :(


public class Test extends javax.swing.JFrame {

private Viewer viewer=null;
ArrayList<Triangle> sectionPointsArray;
ArrayList<LightBulb> lightBulbs = new ArrayList<LightBulb>();
ArrayList<Plane> planes = new ArrayList<Plane>();

public Test() {
initComponents();

DataModelViewer.testclass = this;

this.setSize(800, 800);
viewer = new Viewer();
DataModelViewer.viewer = viewer;

this.getContentPane().add(viewer,BorderLayout.CENT ER);

}

private void initComponents() {

fileChooser = new javax.swing.JFileChooser();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
button_load = new javax.swing.JMenuItem();
button_calc = new javax.swing.JMenuItem();
button_exit = new javax.swing.JMenuItem();

fileChooser.setDialogTitle("Choose a stl file..");
fileChooser.setFileFilter(new StlFilter());

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jMenu1.setText("Process");

button_load.setText("Load Model");
button_load.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_loadActionPerformed(evt);
}
});
jMenu1.add(button_load);

button_calc.setText("Calculate");
button_calc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_calcActionPerformed(evt);
}
});
jMenu1.add(button_calc);

button_exit.setText("Exit");
button_exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button_exitActionPerformed(evt);
}
});
jMenu1.add(button_exit);

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

pack();
}

private void button_loadActionPerformed(java.awt.event.ActionEv ent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
System.out.println("Loading model.");
LoadSTL loadSTL = new LoadSTL(file, DataModelViewer.array);
System.out.println("Loading model finished.");
viewer.loadModel(DataModelViewer.gl);

}
else {
System.out.println("File access cancelled by user.");
}
}
}



public class Viewer extends JPanel implements GLEventListener, MouseListener,
MouseMotionListener, MouseWheelListener, KeyListener {

int nbVBO = 1;
int[] VBO = new int[nbVBO];

public Viewer () {

initDisplay();
initFlags();
}

private void initDisplay() {

glProfile = GLProfile.get(GLProfile.GL2);
final GLCapabilities glCapabilities = new GLCapabilities(glProfile);

// Initialize the GL component
GLCanvas glComponent = new GLCanvas(glCapabilities);
this.gLCanvas = glComponent;

this.setLayout(new BorderLayout());
gLCanvas.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
gLCanvas.setBackground(Color.RED);
this.add(gLCanvas, BorderLayout.CENTER);
gLCanvas.requestFocus();

gLCanvas.addGLEventListener(this);
gLCanvas.addMouseListener(this);
gLCanvas.addMouseMotionListener(this);
gLCanvas.addMouseWheelListener(this);
gLCanvas.addKeyListener(this);
}

@Override
public void init(GLAutoDrawable gLAutoDrawable) {
System.out.println("Init");

currentRotation = identity();

// Create and start the animator
animator = new Animator(gLCanvas);
//animator.setRunAsFastAsPossible(true);
animator.start();

// Perform the default GL initialization
GL2 gl = gLAutoDrawable.getGL().getGL2();
DataModelViewer.gl = gl;

loadFloorTexture(gl);


gl.glClear(GL2.GL_COLOR_BUFFER_BIT);

gl.glClearColor(0.17f, 0.65f, 0.92f, 0.0f); // light blue
}

@Override
public void display(GLAutoDrawable gLAutoDrawable) {

GL2 gl = gLAutoDrawable.getGL().getGL2();

if ( switchableFlags.get("lighting").getFlag() )
gl.glEnable(GL2.GL_LIGHTING);
else
gl.glDisable(GL2.GL_LIGHTING);

if ( switchableFlags.get("smoothing").getFlag() )
gl.glShadeModel(GL2.GL_SMOOTH);
else
gl.glShadeModel(GL2.GL_FLAT);

if ( switchableFlags.get("color").getFlag() )
gl.glEnable(GL2.GL_COLOR_MATERIAL);
else
gl.glDisable(GL2.GL_COLOR_MATERIAL);

// set modelview Matrix
gl.glMatrixMode(GL2.GL_MODELVIEW);

// clear color and depth buffers (faster than doing separately)
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

gl.glLoadIdentity();

gl.glMultMatrixf(currentRotation, 0);

gl.glTranslatef((float)translationX/20.0f, (float)translationY/20.0f, 0);
gl.glScalef(scale%10.0f, scale%10.0f, scale%10.0f);

gl.glMatrixMode(GL2.GL_PROJECTION);

if(isThereModel) {
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

// select buffer and specify its format
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);

gl.glColor3f(0.0f, 0.0f, 0.0f);

gl.glDrawArrays(GL2.GL_TRIANGLES, 0, triangleNumber*3);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);

renderLightSource(gl);

drawFloor(gl);
}

private void initVertexArray(GL2 gl) {
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

gl.glGenBuffers(1, VBO, 0);

gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);

gl.glBufferData(GL2.GL_ARRAY_BUFFER, triangleNumber*3*3, vertexData, GL2.GL_STATIC_DRAW);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}



And this is still the error I get:


Exception in thread "AWT-EventQueue-0-AWTAnimator-1" javax.media.opengl.GLException: javax.media.opengl.GLException: array vertex_buffer_object must be enabled to call this method
at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenG LThread(AWTThreadingPlugin.java:98)
at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(T hreadingImpl.java:197)
at javax.media.opengl.Threading.invokeOnOpenGLThread( Threading.java:164)
at javax.media.opengl.awt.GLCanvas.maybeDoSingleThrea dedWorkaround(GLCanvas.java:830)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.j ava:419)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWT AnimatorImpl.java:74)
at com.jogamp.opengl.util.AnimatorBase.display(Animat orBase.java:142)
at com.jogamp.opengl.util.Animator$MainLoop.run(Anima tor.java:176)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.media.opengl.GLException: array vertex_buffer_object must be enabled to call this method
at jogamp.opengl.gl4.GL4bcImpl.checkBufferObject(GL4b cImpl.java:32025)
at jogamp.opengl.gl4.GL4bcImpl.checkArrayVBOEnabled(G L4bcImpl.java:32052)
at jogamp.opengl.gl4.GL4bcImpl.glVertexPointer(GL4bcI mpl.java:31004)
at cuda.Viewer.display(Viewer.java:333)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawa bleHelper.java:182)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableH elper.java:170)
at javax.media.opengl.awt.GLCanvas$DisplayAction.run( GLCanvas.java:939)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawable Helper.java:398)
at javax.media.opengl.awt.GLCanvas$DisplayOnEventDisp atchThreadAction.run(GLCanvas.java:956)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:241)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:705)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 675)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)

My specs are: Win7 64bit, 4gb, 560 Ti (285.62)
The code is JOGL in Netbeans. I am using OpenGL 2.0.

elect
02-06-2012, 02:13 AM
Can I safely declare the gl as public variable instead passing it to each function that needs it?

elect
02-06-2012, 03:36 AM
It is funny, look, I modified the following:


private void initVertexArray() {

vertexData = GLBuffers.newDirectFloatBuffer(9);
vertexData.put(new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f});
vertexData.flip();

gl.glGenBuffers(1, VBO, 0);

gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);

gl.glBufferData(GL2.GL_ARRAY_BUFFER, 36, vertexData, GL2.GL_STATIC_DRAW);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);

gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
}

If I call the initVertexArray from the init(), no problem, the triangle get rendered.

But if I call it when the user performs some action, then I get the exception...

O_o

elect
02-06-2012, 06:21 AM
I managed to make it work, I moved the initVertexArray() call into the display():


if(initialize) {
initVertexArray();
initialize = false;
}

In this way the call takes place within the same class

I ended up that the error was because I was calling


public void loadModel() {

System.out.println("BLA1");
initVertexArray();
System.out.println("BLA2");
//initNormalArray();

isThereModel = true;
}

from another class (but I am pretty sure always within the same thread