简体   繁体   English

网格的VBO不正确:某些三角形已连接且不应[2D]

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

I'm generating my VBO with this code 我用这段代码生成我的VBO

    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. 我用TRIANGLE_STRIP绘制网格(以原点为中心)。

Sorry, no openGL code since i'm using an engine(AndEngine). 对不起,没有openGL代码,因为我使用的是引擎(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 正如您在图像中看到的那样,最后2个顶点与原点中的顶点连接。我调试了VBO的创建,这是第一个和最后一个顶点

//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: 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) EDIT2:还有一些图像,这次只有2个标准的vert和frag着色器用于位置和纹理颜色(这些图像来自平板电脑,而第一个来自手机)

错误的形象12°错误的图像3°错误的图像我最喜欢的错误形象

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 . 当您对openGL或任何其他3D基本绘图进行绘图调用时,不要指定要绘制的顶点数,而是指定基本体的数量。 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. 由于它是一个三角形条带,你在绘制它总是numberOfVertices - 2你的电话中有numberOfVertices - 2原语。 If it were a list of triangles it would have been numberOfVertices/3 , and for a triangle fan it's numberOfVertices - 2 again. 如果它是一个三角形列表,它将是numberOfVertices/3 ,对于三角形扇形,它的numberOfVertices - 2再次。

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 . 由于您可能指定要绘制的三角形比要绘制的数据多,所以它可能使用了一些默认值(0,0,0,0) ,这说明它试图在左下角绘制两个退化三角形,即也引用纹理坐标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. 这也是有道理的,因为甚至没有顶点在列表中具有位置0,0和T / U 0,0 ,因此最后一个顶点必须是某个默认值。

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. 在尝试了libgdx之后(我真的不喜欢它,它接缝很多实用功能都缺失了,我还没有真正掌握它背后的概念,例如它如何处理纹理,如何管理实体等等)并实施一个非常小的本地语言渲染器,使用jni并且还设置了cocos-2d-x(但是没有尝试过,因为我第一次使用jni代码并编写了上面提到的渲染器)我又回到了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. 我成功地设法用VBO和IBO实现网格,但最后我通过在VBO的开始和结束处复制(添加2个相同的顶点)来解决我的问题,因此附加的三角形被退化并且不被绘制。 So, that's most a workaround, but as long as it works.. 所以,这是最常见的解决方法,但只要它有效..

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

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