简体   繁体   中英

lwjgl glDrawElements not draw

I want to draw a polygon using glDrawElements. Using glDrawArrays everything is fine, but nothing is drawn glDrawElements.

Here is my code:

float[] polys = {
    0f,0f,0f,
    0f,0f,-1f,
    -1f,0f,-1f,
    -1f,0f,0f
};

int[] indexs = {
        0,1,2,
        3,4,5,
        6,7,8,
        9,10,11
    };

float[] texs = {
        0f,0f,
        0f,1f,
        1f,1f,
        1f,0f
    };


FloatBuffer buff_polys = new FloatBuffer(polys.length).put(polys).normalize();  
IntegerBuffer buff_indexs = new IntegerBuffer(indexs.length).put(indexs).normalize();
FloatBuffer buff_texs = new FloatBuffer(texs.length).put(texs).normalize();

GL11.glVertexPointer(3, 0, buff_polys.getBuffer()); 
GL11.glTexCoordPointer(2, 0, buff_texs.getBuffer());

//GL11.glDrawArrays(GL11.GL_QUADS, 0, buff_polys.size()/3);
GL11.glDrawElements(GL11.GL_QUADS, buff_indexs.getBuffer());

Help! In what could be the problem?

An index refers to a vertex (x, y, AND z), not a vertex component (x, y OR z).

float[] polys = {
    0f,0f,0f,    // Vertex 0
    0f,0f,-1f,   // Vertex 1
    -1f,0f,-1f,  // Vertex 2
    -1f,0f,0f    // Vertex 3
};

Try defining your indices like this:

int[] indexs = { 0, 1, 2, 3 };

A vertex has three values, so you have four vertices in your polys float array: 0, 1, 2, and 3. The index array must refer to one of these. If you enter a non-existent index (4 and above) there will almost certainly be problems (depending on the implementation of OpenGL you're using).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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