简体   繁体   English

使用VAO在JOGL中纹理为黑色

[英]Textures are black in JOGL using VAO

So far, I used the deprecated immediate mode rendering, and I'm trying to switch to VAO then VBO. 到目前为止,我已使用了不推荐使用的即时模式渲染,并且尝试切换到VAO和VBO。 2D quads are normally rendered using VAO, but when I'm trying to attach a texture, it remains black. 通常使用VAO渲染2D四边形,但是当我尝试附加纹理时,它保持黑色。 I would really appreciate if somebody could take a look at my code, and point out the part where I'm wrong. 如果有人可以看一下我的代码,并指出我做错的部分,我将不胜感激。

public class CSpriteVAO {
/*VAO*/
private FloatBuffer vertices;
private ShortBuffer indices;
private FloatBuffer textures;
private int VAOVertices;
private int VAOIndices;
private int VAOTextures;

/*SPRITE*/
private String mTexture;
private CPoint mPosition;
private CPoint mDimension;
private CPreferences mPreferences;

public CSpriteVAO(GL2 gl, CPreferences preferences, String spriteID, CRectangle dimensions){
    mPreferences = preferences;
    mTexture = spriteID;
    mDimension = new CPoint(dimensions.width, dimensions.height);
    mPosition = new CPoint(dimensions.x, dimensions.y);

    CCreateVAO(gl);
}

public void onDraw(GL2 gl){
    gl.glLoadIdentity();
    CBindTexture(gl);
    CDraw(gl);
}

private void CDraw(GL2 gl){
    //gl.glCullFace(GL2.GL_CW);
    gl.glTranslatef(mPosition.x, mPosition.y, 0);
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOVertices);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);

        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOTextures);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, VAOTextures);

        gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOIndices);
        gl.glDrawElements(GL2.GL_TRIANGLES, indices.capacity(), GL2.GL_UNSIGNED_SHORT, 0);

    gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}

private void CCreateVAO(GL2 gl){
    //float[] textureArray = {0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f};
    float[] textureArray = {0f, 0f, 1f, 0f, 1f, 1f, 0f, 1f};
    textures = Buffers.newDirectFloatBuffer(textureArray.length);
    textures.put(textureArray);
    textures.flip();

    float[] vertexArray = {0,            mDimension.y, 0,
                           mDimension.x, mDimension.y, 0,
                           mDimension.x, 0,            0,
                           0,            0,            0};
    vertices = Buffers.newDirectFloatBuffer(vertexArray.length);
    vertices.put(vertexArray);
    vertices.flip();

    short[] indexArray = {0, 1, 2, 0, 2, 3};
    indices = Buffers.newDirectShortBuffer(indexArray.length);
    indices.put(indexArray);
    indices.flip();

    int[] temp = new int[3];
    gl.glGenBuffers(3, temp, 0);

    VAOTextures = temp[0];
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOTextures);
    gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, textures.capacity() * Buffers.SIZEOF_FLOAT, textures, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);

    VAOVertices = temp[1];
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOVertices);
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.capacity() * Buffers.SIZEOF_FLOAT, vertices, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);

    VAOIndices = temp[2];
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOIndices);
    gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * Buffers.SIZEOF_SHORT,  indices, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
}

protected void CBindTexture(GL2 gl){
    if (mTexture != CUtils.CurrentTexture){
        if (mTexture != null){
            CAssets.CWGGetTexture(mTexture).enable(gl);
            CAssets.CWGGetTexture(mTexture).bind(gl);
        }
        CUtils.CurrentTexture = mTexture;
    }
}
}

For the record: my vcard reports to have OpenGl 4.3.0 with (obviously) VAO support. 作为记录:我的vcard报告具有OpenGl 4.3.0,(显然)具有VAO支持。 Immediate rendering with textures is working fine. 立即使用纹理渲染效果很好。

I would really appreciate any kind of help. 我真的很感谢任何帮助。 Many thanks in advance. 提前谢谢了。

Check your texture coordinates. 检查纹理坐标。

If that does not work, check that you have set vertex color to white (so it will display colors as they show in the texture). 如果这不起作用,请检查您是否将顶点颜色设置为白色(这样它将显示纹理中显示的颜色)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM