VBOs and Shaders in JOGL

Mm yeah I tried what I think you suggested and it still didn’t work, I think I’m going to try looking at the code here: http://www.spacesimulator.net/wiki/index.php/Tutorials:OpenGL_and_Glut_(OpenGL3.3) and porting the matrix related things over.
On a side note, how do you specify the link text for a link on this forum?

Alright I figured out what was going wrong with your code. There were multiple issues.

  1. In your vertex shader, you assign vec4(vertex, 1.0) to gl_Position. However, you’re responsible for projecting and transforming the vertex into camera space before you write it to gl_Position. This gives you incorrect results, because you have specified z = -2, which is outside the viewable region after projection (which is what OpenGL assumes you have done).

To fix this part, you will need to multiply the vertex by the projection and modelview matrices. This is easiest when using older versions of OpenGL if you don’t have the 4x4 matrices lying around in memory. To do it that way, use the GL2 profile and the built-in uniforms gl_ProjectionMatrix and gl_ModelViewMatrix and change the vertex shader to (note besides switching from GL3 to GL2, I’ve switched shader versions to 1.2):


#version 120
attribute vec3 vertex;
void main() {
   gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(vertex, 1.0);
}

If you want to do the same thing with shader 1.5 and OpenGL 3.0, you’ll have to declare some uniform mat4 variables for the projection and modelview, compute what the matrices should be manually (instead of using gluPerspective, for example) and assign the uniform values before rendering with the shader. This is a lot of trouble, so I’d suggest stepping down to an older version of OpenGL until you get used to things more.

  1. You were not enabling the vertex attribute index that you wanted to use to send values to the ‘vertex’ in variable. You need to add a call:

gl.glEnableVertexAttribArray(gl.glGetAttribLocation(shader.p, "vertex"));

before you call glVertexAttribPointer.

  1. In your glVertexAttribPointer, you should not set the stride to 20, the way you’ve built your vertex data, it has a stride of 0.

  2. When building your vertex VBO, you are getting an OpenGL error generated by your call to glBufferSubData because your vertex nio buffer’s capacity is 112 (or 7 * 4 * 4) and you created the VBO to have a size of 48. You can fix this by creating the nio buffer with a capacity of vertexData.length * 4.

  3. Your call to glDrawRangeElements has parameters that don’t make sense with your indices. You’re still passing in 4 indices even though you’re using triangle strips and you have indices that range from 0 to 3. Currently you’re claiming that your indices range from 0 to 2 and that you pass in 3 indices. So change the draw call to:


glDrawRangeElements(GL2.GL_TRIANGLE_STRIP, 0, 3, 4, GL2.GL_UNSIGNED_INT, 0);

I think that was everything that was buggy that I had to change to make it work. I don’t have OpenGL 3.0 on my laptop so I had to rewrite your shaders to use version 1.20 and switch every GL3 to a GL2. This means that I went with the gl_ProjectionMatrix and gl_ModelViewMatrix solution to getting your vertices to display correctly. It also means that you’ll still have to set the matrix mode and load identity matrices as I mentioned before.

I found most of these bugs (except for the triangle strip and projection/modelview bugs) by adding DebugGL wrappers to the GL instances you use in your RealMain class, since you had only added them to your ShaderProgram class. My advice would be to always remember that tool and make sure you’re using it rigorously when you’re encountering unexpected errors with OpenGL.

Ok yeah I’d fixed some of the nonsensical values for drawRangeElements (or so I though I had) in the latest paste of the code, but I really had no idea what to do to convert the coordinates to screen coordinates. And on a side note it works :slight_smile: Thank you very much everyone for your help. And ihkbob do you have any recommendation for future reference for where to learn how to deal with openGL matrices?

Here is a link to a forum post I wrote about matrices: http://www.java-gaming.org/index.php/topic,24545.msg206908.html#msg206908. I would google around for linear algebra tutorials or choose math classes that cover linear algebra if you’re in college. The Redbook from OpenGL has a good coverage of how they use matrices and I found it really helpful in understanding their approach and camera model.

Okay thank you, I plan on getting the red book at some point; maybe I should make some point now.

I think you can get old versions online at opengl.org for free.