LWJGL不渲染我的网格,只有glClear颜色

时间:2014-08-13 02:24:41

标签: java opengl rendering lwjgl

我有一个用lwjgl编​​写的程序,它意味着渲染一个立方体,每个顶点的颜色不同。我遇到的问题是它根本不会渲染立方体。什么都没有出现,为什么?我使用了2个VBO,其中一个是连续的位置和颜色数据,另一个是索引,它们被绑定到一个VAO中,假定被绘制。< / p>

private void initGLdata(String[] shaders, String[] uniforms) {
    //vertex buffer initialization
    verBufId = glGenBuffers(); //create a vertex buffer
    glBindBuffer(GL_ARRAY_BUFFER, verBufId); //bind the buffer for loading
    FloatBuffer vBuf = BufferUtils.createFloatBuffer(verts.length*7);
    //load positions in first
    for(Vec v : verts)
        vBuf.put(v.x).put(v.y).put(v.z);//XYZ(openGL assigns W to 1)
    //then load colors in
    for(int i = 0; i < verts.length; i++)
        vBuf.put(color[i].x).put(color[i].y).put(color[i].z).put(color[i].w);//RGBA
    vBuf.rewind();//needs to be rewound before sending
    glBufferData(GL_ARRAY_BUFFER, vBuf, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //index buffer initialization
    iBufId = glGenBuffers();//create buffer, as above
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iBufId);//bind, this time to element array
    IntBuffer iBuf = BufferUtils.createIntBuffer(faces.length*3);//3 verts per face
    for(Face f : faces) iBuf.put(f.ind[0]).put(f.ind[1]).put(f.ind[2]);//123
    iBuf.rewind();//again, rewind first
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBuf, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    //initialize VAO with VBOs, similar to drawing
    verArrId = glGenVertexArrays();
    glBindVertexArray(verArrId);

    glBindBuffer(GL_ARRAY_BUFFER, verBufId);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
    glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, verts.length*3);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iBufId);

    glBindVertexArray(0);

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CW);
    if(shaders != null){
        shadHandles = new int[shaders.length];
        for(int i = 0; i < shaders.length; i++)
            shadHandles[i] = ShaderLib.createShader(shaders[i]);
        shadProg = ShaderLib.createProgram(shadHandles);
        if(uniforms != null){
            this.unif = ShaderLib.getUniforms(shadProg, uniforms);
            glUseProgram(shadProg);
            glUniformMatrix4(unif.get("camToClip").intValue(), false,
                world.camToClip.asBuf());
            glUseProgram(0);
        }
    }
}

public void draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(shadProg);

    glBindVertexArray(verArrId);
    glDrawElements(GL_TRIANGLES, faces.length*3, GL_UNSIGNED_INT, 0);
    /* diagnostic. to see the final vertex locations in OpenGL
    for(int i = 0; i < 8; i++)
        System.out.println(world.camToClip.mult(verts[i]).toString());
    */
    glBindVertexArray(0);
    glUseProgram(0);
}

public boolean init(){
    glClearColor(0.5f, 0.5f, 0.5f, 0f); // I prefer grey
    //Depth stuff
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_DEPTH_CLAMP);
    glClearDepth(1); //clear the depth buffer as the back of the screen
    glDepthMask(true);
    glDepthFunc(GL_LESS);
    glDepthRange(0, 1);
    return true;
}

这些是分配给顶点着色器的gl_Position变量的值。

<-0.4, -0.6, -2.8>
<0.6, -0.6, -2.8>
<0.6, 0.4, -2.8>
<-0.4, 0.4, -2.8>
<-0.4, -0.6, 2.2>
<0.6, -0.6, 2.2>
<0.6, 0.4, 2.2>
<-0.4, 0.4, 2.2>

为什么不渲染任何内容?

0 个答案:

没有答案