Login  Register

Re: Can someone please help me complete this sample code?

Posted by s3a on Jul 29, 2011; 9:04pm
URL: https://forum.jogamp.org/Can-someone-please-help-me-complete-this-sample-code-tp3207414p3210751.html

Thanks for your answer. I ported some other C++ code I found online (because it looked more straightforward and like it would yield faster results) taking into account what you said as you will see in the code that will follow but I would again really appreciate to know if it's all been ported properly as well as what to put as the second parameter in the glBufferData method. I apologize if I ask anything obvious as I probably have in the past but I think I'm almost reaching a stage where I can "take the training wheels off" and be less dependent on the JOGL community for trivial things.

Here is the current code I am dealing with:

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;

public class MyClass implements GLEventListener
{

    public void init(GLAutoDrawable glad) {
        // code
    }

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

        // An array of 3 vectors which represents 3 vertices
        /*static final */float[] g_vertex_buffer_data = {
           -1.0f, -1.0f, 0.0f,
           1.0f, -1.0f, 0.0f,
           0.0f,  1.0f, 0.0f,
        };

        // This will identify our vertex buffer
        int[] vertexbuffer = new int[1];

        // Generate 1 buffer, put the resulting identifier in vertexbuffer
        gl.glGenBuffers(1, vertexbuffer, 0);

        // The following commands will talk about our 'vertexbuffer' buffer
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer[0]);

        // Give our vertices to OpenGL.
        gl.glBufferData(GL.GL_ARRAY_BUFFER, /*sizeof(g_vertex_buffer_data)*/, g_vertex_buffer_data, GL.GL_STATIC_DRAW); // WHAT DO I PUT AS THE SECOND PARAMETER?

        // 1rst attribute buffer : vertices
        gl.glEnableVertexAttribArray(0);
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer[0]);
        gl.glVertexAttribPointer(
           0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
           3,                  // size
           GL.GL_FLOAT,           // type
           false,           // normalized?
           0,                  // stride
           /*(void*)*/0            // array buffer offset
        );

        // Draw the triangle !
        gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        gl.glDisableVertexAttribArray(0);
    }

    public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) {
        // code
    }

    public void displayChanged(GLAutoDrawable glad, boolean bln, boolean bln1) {
        // code
    }

}

P.S.
Is there no way to elegantly place code in this forum? I tried checking "Message is in HTML Format" and then <code></code> but that didn't work.