我的代码呈现黑屏

时间:2014-08-30 23:45:39

标签: opengl jogl

我尝试使用JOGL渲染一个简单的线框立方体。

然而,我会看到黑屏,任何比我更有经验的人都可以告诉我,我做错了吗?

着色器编译并且不会抛出任何错误。

private Mat4 projectionMatrix;
//reference to the uniform MPV matrix in the vertex shader
private int mpvUnif;

//Holds the references to GL objects
private final int[] glPointers = new int[3];
private static final int VBO = 0;
private static final int INDEXBUFFER = 1;
private static final int VAO = 2;

private int shaderProgram;

private FloatBuffer vertexDataBuffer;
private ShortBuffer indexDataBuffer;

private float[] testCube = new float[] {
        //Top
        -1, 1, 1,   //0
        1, 1, 1,    //1
        -1, -1, 1,  //2
        1, -1, 1,   //3
        //Bottom    
        -1, 1, -1,  //4
        1, 1, -1,   //5
        -1, -1, -1, //6
        1, -1, -1,  //7
};

private short[] testCubeIndexes = new short[] {
        0,1,2,3,
        4,5,6,7,
        0,1,4,5,
        2,3,6,7,
        0,2,4,6,
        1,3,5,7
};

public GLController() {
    this.vertexDataBuffer = FloatBuffer.wrap(testCube);
    this.indexDataBuffer = ShortBuffer.wrap(testCubeIndexes);
}

@Override
public void init(GLAutoDrawable drawable) {
    GL3 gl = drawable.getGL().getGL3();
    shaderProgram = gl.glCreateProgram();
    try {
        InitializeVertexBuffer(gl);
        InitializeVertexArrayObjects(gl);
        initShaders(gl);
    } catch (IOException e) {
        e.printStackTrace();
    }
    gl.glPolygonMode(GL3.GL_FRONT_AND_BACK, GL3.GL_LINE);
    gl.glFrontFace(GL3.GL_CW);
}

@Override
public void display(GLAutoDrawable drawable) {
    GL3 gl = drawable.getGL().getGL3();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glClear(GL3.GL_COLOR_BUFFER_BIT);

    gl.glUseProgram(shaderProgram);

    gl.glBindVertexArray(glPointers[VAO]);
    gl.glDrawElements(GL3.GL_QUADS,  indexDataBuffer.capacity(), GL3.GL_UNSIGNED_SHORT, 0);

    gl.glBindVertexArray(0);
    gl.glUseProgram(0);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL3 gl = drawable.getGL().getGL3();

    mpvUnif = gl.glGetUniformLocation(shaderProgram, "MPV");
    projectionMatrix = Matrices.perspective(60f, width/height, .1f, 100f);

    gl.glUseProgram(shaderProgram);
    gl.glUniformMatrix4fv(mpvUnif, 1, GLHelper.glBool(GL3.GL_FALSE), projectionMatrix.getBuffer());
    gl.glUseProgram(0);
}

/**
 * Called in GLEventListener.init()
 * @param gl
 */
private void InitializeVertexBuffer(GL3 gl) {
    gl.glGenBuffers(1, glPointers, VBO);
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, glPointers[VBO]);
    gl.glBufferData(GL3.GL_ARRAY_BUFFER, GLHelper.sizeof(vertexDataBuffer), vertexDataBuffer, GL3.GL_STATIC_DRAW);
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);

    gl.glGenBuffers(1, glPointers, INDEXBUFFER);
    gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, glPointers[INDEXBUFFER]);
    gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, GLHelper.sizeof(indexDataBuffer), indexDataBuffer, GL3.GL_STATIC_DRAW);
    gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 0);
}

/**
 * Called in GLEventListener.init()
 * @param gl
 */
private void InitializeVertexArrayObjects(GL3 gl) {
    gl.glGenVertexArrays(1, glPointers, VAO);
    gl.glBindVertexArray(glPointers[VAO]);

    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, glPointers[VBO]);
    gl.glEnableVertexAttribArray(0);
    gl.glVertexAttribPointer(0, 3, GL3.GL_FLOAT, GLHelper.glBool(GL3.GL_FALSE), 0, 0);
    gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, glPointers[INDEXBUFFER]);

    gl.glBindVertexArray(0);
}

我的顶点着色器:

#version 330

layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;

void main()
{
    vec4 v = vec4(vertexPosition_modelspace, 1);
    gl_Position = MVP * v;
}

此着色器确实在屏幕上绘制了一些内容:

#version 330

layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;//not used

void main()
{
    gl_Position = vec4(vertexPosition_modelspace, 1);
}

我的片段着色器:

#version 330

out vec4 outputColor;

void main()
{
    outputColor = vec4(1.0, 1.0, 1.0, 1.0);
}

1 个答案:

答案 0 :(得分:0)

我感觉很傻,主要的问题是着色器中的MVP被称为MPV!感谢gouessej试图提供帮助。