Take the 2-minute tour ×

I am new in with Java OpenGL (JOGL). I want to display a 3D cube in JOGL.

I have vertex array information with me.

double vertices[]= {
    0 0 0,
    0 0 1,
    0 1 0,
    0 1 1,
    1 0 0,
    1 0 1,
    1 1 0,
    1 1 1,

I want to use glDrawArrays() to display it. Since I have display a model having a huge amount of vertices.

The display method is given below.

public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();




I need to use GL_TRIANGLES or GL_TRIANGLE_STRIP only and not GL_QUADS.

If anyone knows how to use glDrawArrays(); in JOGL please help me. I want JOGL syntax and not opengl.

share|improve this question
What do you mean by "I want JOGL syntax and not opengl." ? –  Vallentin Sep 18 '13 at 12:21
add comment

1 Answer

If you want to use glDrawArrays(); then you have to create a VBO (Vertex Buffer Objects) or a VA (Vertex Arrays). If you're after speed, then choose VBO, though they require some extra lines of code to be made!

The best way I think I can explain how to create a VBO to you, is by giving you an example!

VBO Example

Here is a little example of a VBO storing Vertices and Colors for a Triangle and rendering it and also how to delete it!

Creating the VBO

This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.

int vertices = 3;

int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,

FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });

FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });

int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

You can of course add more Vertices and Colors to the vertex_data and color_data if you want to! But always remember that the amount of vertex data, need to match with the amount of color data and vice versa!

Important: Only create the VBO(s) once, and only update them when necessary! Don't create them for each frame, since them you will end up with a frame-rate worse than when using immediate mode for rendering!

Rendering the VBO

This is the code you need to call, to render the VBO.

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);


glDrawArrays(GL_TRIANGLES, 0, vertices);


Deleting the VBO

Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.

share|improve this answer
add comment

Your Answer


