Quantcast

simple example with shaders, VAO and index array

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

simple example with shaders, VAO and index array

elect
I am trying to port a simple C++ example in Java (JOGL).

But it doesn't work and it must be something stupid, but I cannot see it..

public class OverlapNoDepth implements GLEventListener {

private int imageWidth = 800;
private int imageHeight = 600;
private GLCanvas canvas;
private GLSLProgramObject programObject;
private int[] vertexBufferObject = new int[1];
private int[] indexBufferObject = new int[1];
private int[] VAOs = new int[1];
private float[] vertexData = new float[]{
    0.25f, 0.25f, -1.25f, 1.0f,
    0.25f, -0.25f, -1.25f, 1.0f,
    -0.25f, 0.25f, -1.25f, 1.0f,
    0.0f, 1.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f, 1.0f
};
private int[] indexData = new int[]{
    0, 2, 1
};
private String shadersFilepath = "/tut05/shaders/";
float[] perspectiveMatrix = new float[16];
float fFrustumScale = 1.0f;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    OverlapNoDepth overlapNoDepth = new OverlapNoDepth();

    Frame frame = new Frame("Tutorial 05 - Overlap No Depth");

    frame.add(overlapNoDepth.getCanvas());

    frame.setSize(overlapNoDepth.getCanvas().getWidth(), overlapNoDepth.getCanvas().getHeight());

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
        }
    });

    frame.setVisible(true);
}

public OverlapNoDepth() {
    initGL();
}

private void initGL() {
    GLProfile profile = GLProfile.getDefault();

    GLCapabilities capabilities = new GLCapabilities(profile);

    canvas = new GLCanvas(capabilities);

    canvas.setSize(imageWidth, imageHeight);

    canvas.addGLEventListener(this);
}

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

    canvas.setAutoSwapBufferMode(false);

    GL3 gl3 = glad.getGL().getGL3();

    initializeProgram(gl3);

    initializeVertexBuffer(gl3);

    initializeVertexArrayObjects(gl3);

    gl3.glEnable(GL3.GL_CULL_FACE);
    gl3.glCullFace(GL3.GL_BACK);
    gl3.glFrontFace(GL3.GL_CW);
}

@Override
public void dispose(GLAutoDrawable glad) {
    System.out.println("dispose");
}

@Override
public void display(GLAutoDrawable glad) {
    System.out.println("display");

    GL3 gl3 = glad.getGL().getGL3();

    gl3.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl3.glClear(GL3.GL_COLOR_BUFFER_BIT);

    programObject.bind(gl3);
    {
        gl3.glBindVertexArray(VAOs[0]);
        {
            programObject.setUniform(gl3, "offset", new float[]{0.0f, 0.0f, 0.0f}, 3);
            gl3.glDrawElements(GL3.GL_TRIANGLES, indexData.length, GL3.GL_UNSIGNED_INT, 0);
        }
        gl3.glBindVertexArray(0);
    }
    programObject.unbind(gl3);

//        programObject.bind(gl3);
//        {
//            gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
//            {
//                gl3.glEnableVertexAttribArray(0);
//                gl3.glEnableVertexAttribArray(1);
//                {
//                    programObject.setUniform(gl3, "offset", new float[]{0.0f, 0.0f, 0.0f}, 3);
//                    gl3.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
//                    gl3.glVertexAttribPointer(1, 4, GL3.GL_FLOAT, false, 0, 3 * 4 * 4);
//
//                    gl3.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
//                }
//                gl3.glDisableVertexAttribArray(0);
//                gl3.glDisableVertexAttribArray(1);
//            }
//        }
//        programObject.unbind(gl3);
    glad.swapBuffers();
}

@Override
public void reshape(GLAutoDrawable glad, int x, int y, int w, int h) {
    System.out.println("reshape() x: " + x + " y: " + y + " width: " + w + " height: " + h);

    GL3 gl3 = glad.getGL().getGL3();

    perspectiveMatrix[0] = fFrustumScale / (w / (float) h);
    perspectiveMatrix[5] = fFrustumScale;

    programObject.bind(gl3);
    {
        int perspectiveMatrixLocation = gl3.glGetUniformLocation(programObject.getProgId(), "perspectiveMatrix");
        gl3.glUniformMatrix4fv(perspectiveMatrixLocation, 1, false, perspectiveMatrix, 0);
    }
    programObject.unbind(gl3);

    gl3.glViewport(x, y, w, h);
}

private void buildShaders(GL3 gl3) {
    System.out.print("Building shaders...");

    programObject = new GLSLProgramObject(gl3);
    programObject.attachVertexShader(gl3, shadersFilepath + "MatrixPerspective_VS.glsl");
    programObject.attachFragmentShader(gl3, shadersFilepath + "StandardColor_FS.glsl");
    programObject.initializeProgram(gl3, true);

    System.out.println("ok");
}

private void initializeVertexBuffer(GL3 gl3) {
    gl3.glGenBuffers(1, IntBuffer.wrap(vertexBufferObject));

    gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
    {
        gl3.glBufferData(GL3.GL_ARRAY_BUFFER, vertexData.length * 4, GLBuffers.newDirectFloatBuffer(vertexData), GL3.GL_STATIC_DRAW);
    }
    gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);


    gl3.glGenBuffers(1, IntBuffer.wrap(indexBufferObject));

    gl3.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, indexBufferObject[0]);
    {
        gl3.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, indexData.length * 4, GLBuffers.newDirectIntBuffer(indexData), GL3.GL_STATIC_DRAW);
    }
    gl3.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 0);
}

private void initializeProgram(GL3 gl3) {
    buildShaders(gl3);

    programObject.bind(gl3);
    {
        float fzNear = 0.5f;
        float fzFar = 3.0f;

        perspectiveMatrix[0] = fFrustumScale;
        perspectiveMatrix[5] = fFrustumScale;
        perspectiveMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
        perspectiveMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
        perspectiveMatrix[11] = -1.0f;

        int matrixLocation = gl3.glGetUniformLocation(programObject.getProgId(), "perspectiveMatrix");
        gl3.glUniformMatrix4fv(matrixLocation, 1, false, perspectiveMatrix, 0);
    }
    programObject.unbind(gl3);
}

private void initializeVertexArrayObjects(GL3 gl3) {
    int colorDataOffset;
    int positionDataOffset;



    gl3.glGenVertexArrays(1, IntBuffer.wrap(VAOs));
    gl3.glBindVertexArray(VAOs[0]);
    gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
    {
//        2 objects * 18 vertices * 3 coordinate * 4 Bytes/Float
//            colorDataOffset = 2 * 18 * 3 * 4;
        colorDataOffset = 3 * 4 * 4;

        gl3.glEnableVertexAttribArray(0);
        gl3.glEnableVertexAttribArray(1);
        {
            gl3.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
            gl3.glVertexAttribPointer(1, 4, GL3.GL_FLOAT, false, 0, colorDataOffset);
        }
        gl3.glDisableVertexAttribArray(0);
        gl3.glDisableVertexAttribArray(1);

        gl3.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, indexBufferObject[0]);
    }
    gl3.glBindVertexArray(0);
}

public GLCanvas getCanvas() {
    return canvas;
}

public void setCanvas(GLCanvas canvas) {
    this.canvas = canvas;
}
}




I tried to reduced it as much as possible (originally it uses many more vertices and another VAO).

The glsl program uses an external class that I uses a lot, it should be ok. Ok should be also the vertex and fragment shaders, coming from the another example. If the error does not look to be here I will paste also the rest. I do not get any error, just a blank screen.. If I comment out the lines in the display() I can see the triangle..

Any idea?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple example with shaders, VAO and index array

elect
Found, when I initialize the VAO I fisrt enable the vertex attributes and then disable them..

But I shouldn't disable then since this will be stored in the VAO state...


God, it took me some times..
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple example with shaders, VAO and index array

Sven Gothel
Administrator
On 07/04/2013 09:53 AM, elect [via jogamp] wrote:
> Found, when I initialize the VAO I fisrt enable the vertex attributes and then
> disable them..
>
> But I shouldn't disable then since this will be stored in the VAO state...
>

This should have been fixed for a while, at least when using a GL3
context. <https://jogamp.org/git/?p=jogl.git;a=commit;h=b414c4b1be05249590138e73558ada82bd170f15>

Have you tested w/ RC12 ?

If it is [still] a bug, pls create a new bug report depending/hinting at Bug 692.

>
> God, it took me some times..

Sorry for that.

~Sven



signature.asc (911 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple example with shaders, VAO and index array

elect
Sven Gothel wrote
Have you tested w/ RC12 ?
No, I am using one of the last stables
Loading...