I'm a little dumbfounded as to why this code for creating and displaying a VBO isn't working, I referenced (and in some areas copied exactly) code from the tutorial found here:
https://sites.google.com/site/justinscsstuff/rendering-methods/triangle-scene I start my code like this: //========================== glp = GLProfile.getDefault(); glp = GLProfile.getMaxProgrammable(false); caps = new GLCapabilities(glp); window = GLWindow.create(caps); window.setSize(WIDTH*SCALE, HEIGHT*SCALE); window.setVisible(true); window.setTitle("NEWT Window"); new WindowHandler(this); // create a new window handler, attached to this window window.addGLEventListener(this); // add this class as an event listener. //window.setFullscreen(true); // start loop animator = new Animator(window); animator.start(); // ------ (below are important lines of code from my init function, called after this startup but before render) GL2 gl = drawable.getGL().getGL2(); Triangle triangles[] = new Triangle[2]; for (int i=0; i < triangles.length; i++){ triangles[i] = new Triangle(); } screen = new Screen(gl, triangles); //========================== and call this in my render loop: //========================== GL2 gl = drawable.getGL().getGL2(); // double buffering on by default /** Clear screen */ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); // resets all translates and rotates // //gl.glOrtho(0, WIDTH, 0, HEIGHT, -1, 1); // left, right, bot, top, zNear, zFar screen.render(gl); //========================== here is my screen constructor (an exact copy from the tutorial): //========================== public Screen(GL2 gl, Triangle[] triangles){ totalNumVerts = triangles.length * 3; // create the VBO pointer / handle IntBuffer buf = Buffers.newDirectIntBuffer(1); gl.glGenBuffers(1, buf); vbo = buf.get(); // interleave vertex and col data FloatBuffer data = Buffers.newDirectFloatBuffer(triangles.length * 18); // not 100% sure why 18 is magic num. Cols * verts? for (int i=0; i < triangles.length; i++){ for (int j=0; j < 3; j++){ data.put(triangles[i].colors[j]); data.put(triangles[i].vertices[j]); } } data.rewind(); int bytesPerFloat = Float.SIZE / Byte.SIZE; // x-fer data to VBO int numBytes = data.capacity() * bytesPerFloat; gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo); gl.glBufferData(GL.GL_ARRAY_BUFFER, numBytes, data, GL.GL_STATIC_DRAW); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); vertexStride = 6 * bytesPerFloat; colorPointer = 0; vertexPointer = 3 * bytesPerFloat; } //========================== and here is my screen render (also an exact copy from the tutorial): //========================== gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo); // bind gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); gl.glEnableClientState(GL2.GL_COLOR_ARRAY); gl.glColorPointer(3, GL.GL_FLOAT, vertexStride, colorPointer); gl.glVertexPointer(3, GL.GL_FLOAT, vertexStride, vertexPointer); gl.glDrawArrays(GL.GL_TRIANGLES, 0, totalNumVerts); gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); gl.glDisableClientState(GL2.GL_COLOR_ARRAY); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); // unbind //========================== As you can see, I copied the tutorial's code exactly in terms of the rendering, and only veered away from it in the startup. So far I've tried adjusting vertexStride and the colorPointer, using only Int/Float buffers, moving my Ortho around, and drawing something other than triangles. Nothing has worked, I just get a black screen. If anyone can help me fix this code, or can point me to a tutorial/forum post that could help me fix my problem, I would appreciate it greatly. I'm really just looking for a direction to go in here, I need to identify my problem before I can work on fixing it, and right now I have no idea what's wrong! Thanks! =) |
Administrator
|
Hi
At first, please can you confirm that the unchanged code works as expected on your machine? Secondly, please provide a SSCCE instead of something partial and unusable. Thirdly, you should inject your changes gradually. Just make a small modification, check that the code still works and so on... That way, you'll probably be able to find alone what breaks your code and maybe we will be able to help you to fix it. Edit.: 18 = 3 * (3 + 3) 3 vertices, each vertex uses 3 floats for its coordinates and 3 floats for the RGB values (colors).
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |