Incorrect VBO for mesh: some triangles are connected and shouldn't [2D]

谁都会走 提交于 2020-01-14 18:47:07

问题


I'm generating my VBO with this code

    int SCREEN_WIDTH = 800;
    int SCREEN_HEIGHT = 480;
    int PIXEL_PER_VERTEX = 4;
    int CAVERN_TEXTURE_WIDTH = 1024;
    int CAVERN_TEXTURE_HEIGHT = 512;

    final int vertexCount = ((SCREEN_WIDTH / PIXEL_PER_VERTEX) +1 ) * 2;
    final float[] bufferDataLowerCave = new float[vertexCount * CavernBoundMesh.VERTEX_SIZE];

    for(int i=0; i < vertexCount; i += 2) {
        bufferDataLowerCave[i * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.VERTEX_INDEX_X] = PIXEL_PER_VERTEX * i / 2.f;
        bufferDataLowerCave[i * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.VERTEX_INDEX_Y] = 200;
        bufferDataLowerCave[i * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.TEXTURECOORDINATES_INDEX_U] = ( (float) PIXEL_PER_VERTEX * i / 2.f) / SCREEN_WIDTH;
        bufferDataLowerCave[i * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.TEXTURECOORDINATES_INDEX_V] = 0.f;

        bufferDataLowerCave[(i + 1 ) * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.VERTEX_INDEX_X] = PIXEL_PER_VERTEX * i / 2;
        bufferDataLowerCave[(i + 1 ) * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.VERTEX_INDEX_Y] = 0;
        bufferDataLowerCave[(i + 1 ) * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.TEXTURECOORDINATES_INDEX_U] = ( (float) PIXEL_PER_VERTEX * i / 2.f) / SCREEN_WIDTH;
        bufferDataLowerCave[(i + 1 ) * CavernBoundMesh.VERTEX_SIZE + CavernBoundMesh.TEXTURECOORDINATES_INDEX_V] = 0.5f;
    }

I'm drawing the mesh(centred in the origin) with TRIANGLE_STRIP.

Sorry, no openGL code since i'm using an engine(AndEngine). But if you really need, i can try to track it down..

As you can see in the image the last 2 vertices are connected with a vertex in the origin.I debugged the creation of the VBO and this is the first and the last vertices

//X       Y       U        V
[0.0,    200.0,  0.0,     0.0,
 0.0,    0.0,    0.0,     0.5,
 4.0,    200.0,  0.0050,  0.0,
 4.0,    0.0,    0.0050,  0.5,
 8.0,    200.0,  0.01,    0.0,
 8.0,    0.0,    0.01,    0.5,
 12.0,   200.0,  0.015,   0.0,
 12.0,   0.0,    0.015,   0.5,
 16.0,   200.0,  0.02,    0.0,
 16.0,   0.0,    0.02,    0.5
 [..]
 780.0,  200.0,  0.975,   0.0,
 780.0,  0.0,    0.975,   0.5,
 784.0,  200.0,  0.98,    0.0,
 784.0,  0.0,    0.98,    0.5,
 788.0,  200.0,  0.985,   0.0,
 788.0,  0.0,    0.985,   0.5,
 792.0,  200.0,  0.99,    0.0,
 792.0,  0.0,    0.99,    0.5,
 796.0,  200.0,  0.995,   0.0,
 796.0,  0.0,    0.995,   0.5]

This is the vertex shader that i used

uniform mat4 u_modelViewProjectionMatrix;
uniform float u_elapsedSeconds;

attribute vec4 a_position;
attribute vec2 a_textureCoordinates;

varying vec2 v_textureCoordinates;

void main(void)
{
    v_textureCoordinates = a_textureCoordinates;
    vec4 temp_position = u_modelViewProjectionMatrix * a_position;

    float variation = 0.3*sin((u_elapsedSeconds + 1.5 * temp_position.x))*(1.0 + temp_position.y);
    temp_position.y += variation;

    gl_Position = temp_position;
}

I'm very new to this stuff, so i do not know well the specifications and how it works, but i can't really understand what's going on to generate this strange effect. Can you help me out?

EDIT1:

i made some more tests.. Even with a standard vertex and fragment shader that just sets the position and the colour fetching it from a textures it gives weird results.

EDIT2: A few more images, this time just with the 2 standard vert and frag shader for position and texture colour (This images are taken from a Tablet, while the first is from a phone)


回答1:


I'm pretty sure that you are specifying the wrong number of primitives to your drawing code since the vertices themselves look alright to me. When you do a drawing call to openGL or any other 3D primitive drawing you do not specify the number of vertices to draw but the number of primitives. A primitive in you case is a triangle.

Since it's a triangle strip you're drawing it's always numberOfVertices - 2 primitives in your call. If it were a list of triangles it would have been numberOfVertices/3, and for a triangle fan it's numberOfVertices - 2 again.

Since you are probably specifying more triangles to be drawn than it has data to draw for, it's probably using some default (0,0,0,0) which explains that it is trying to draw two degenerate triangles in the bottom left corner that is also referencing the texture coordinate 0,0. This also makes sense since there isn't even a vertex that has the position 0,0 and the T/U 0,0 in your list, so the last vertex must be some default value.




回答2:


Ok, i solved it.

After trying libgdx (i don't really like it, it seams a lot of utility functions are missing, and i haven't really grasped the concept behind it, eg how it treats textures, how manage entities, etc..) and implemented a really small renderer in native language with jni and also setted up cocos-2d-x (but haven't tried it, since i first played a bit with the jni code and wrote the above mentioned renderer) i fell back to AndEngine.

I succesfully managed to implement a mesh with VBO and also a IBO, but finally i solved my problem by duplicating (adding 2 identical vertices) at the start and the end of the VBO, so that the additional triangles are degenerated and are not drawn. So, that's most a workaround, but as long as it works..



来源:https://stackoverflow.com/questions/12161633/incorrect-vbo-for-mesh-some-triangles-are-connected-and-shouldnt-2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!