0

我正在尝试在 Java (JOGL) 中移植一个简单​​的 C++ 示例。

但它不起作用,它一定是愚蠢的,但我看不到它..

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;
}
}

我试图尽可能地减少它(最初它使用更多的顶点和另一个 VAO)。glsl程序使用了一个我经常使用的外部类,应该没问题。好的也应该是来自另一个示例的顶点和片段着色器。如果错误看起来不在这里,我也会粘贴其余的。我没有收到任何错误,只是一个空白屏幕..如果我注释掉 display() 中的行,我可以看到三角形..

任何想法?

4

1 回答 1

0

发现,当我初始化 VAO 时,我首先启用顶点属性,然后禁用它们。

但我不应该禁用,因为这将存储在 VAO 状态......

于 2013-07-04T07:53:04.853 回答