2

I am building a parametric 3d modeler with obj export.

I am really puzzled.

I have changed my GPU last night and now, there are cracks between the vertices, I can see what is behind. My old card was an Nvidia GTX275 and the one, NVIDIA GTX960. I didn't change anything in the code, shader or otherwise. I use only flat colors (not textures).

On the image below, the black lines that cut the pentagon into triangle shouldn't be there.

enter image description here

It seems to be purely an OpenGL problem as when I export the model and look in Blender, the faces are contiguous and there is no duplicate vertices.

the shader code is quite simple :

_VERTEX2 = """
#version 330
#extension GL_ARB_explicit_uniform_location : enable
layout(location = 0 ) in vec3 position;
layout(location = 1 ) in vec4 color;
layout(location = 2 ) in vec3 normal;

varying vec4 baseColor;
// uniform mat4 proj;
layout(location = 0) uniform mat4 view;
layout(location = 4) uniform mat4 proj;

varying vec3 fragVertexEc;

void main(void) {
    gl_Position = proj * view * vec4(position, 1.0);
    fragVertexEc = (view * vec4(position, 1.0)).xyz;
    baseColor = color;
}
"""


_FRAGMENT2 = """
#version 330
#extension GL_OES_standard_derivatives : enable

varying vec3 fragVertexEc;
varying vec4 baseColor;

const vec3 lightPosEc = vec3(0,0,10);
const vec3 lightColor = vec3(1.0,1.0,1.0);

void main()
{
    vec3 X = dFdx(fragVertexEc);
    vec3 Y = dFdy(fragVertexEc);
    vec3 normal=normalize(cross(X,Y));

    vec3 lightDirection = normalize(lightPosEc - fragVertexEc);

    float light = max(0.0, dot(lightDirection, normal));


    gl_FragColor = vec4(normal, 1.0);
    gl_FragColor = vec4(baseColor.xyz * light, baseColor.w);
}
"""

the rendering code is also quite simple :

def draw(self, view_transform, proj, transform):
    self.shader.use()
    gl_wrap.glBindVertexArray(self.vao)
    try:
        self.vbo.bind()

        view_transform = view_transform * transform
        GL.glUniformMatrix4fv(0, 1, False, (ctypes.c_float*16)(*view_transform.toList()))
        GL.glUniformMatrix4fv(4, 1, False, (ctypes.c_float*16)(*proj.toList()))

        GL.glEnableVertexAttribArray(self.shader.attrib['position'])
        GL.glEnableVertexAttribArray(self.shader.attrib['color'])
        GL.glEnableVertexAttribArray(self.shader.attrib['normal'])

        STRIDE = 40
        GL.glVertexAttribPointer(
            self.shader.attrib['position'], len(Vector._fields), GL.GL_FLOAT,
            False, STRIDE, self.vbo)

        GL.glVertexAttribPointer(
            self.shader.attrib['color'], len(Color._fields), GL.GL_FLOAT,
            False, STRIDE, self.vbo+12)

        GL.glVertexAttribPointer(
            self.shader.attrib['normal'], len(Vector._fields), GL.GL_FLOAT,
            False, STRIDE, self.vbo+28)

        GL.glDrawElements(
            GL.GL_TRIANGLES,
            len(self.glindices),
            self.index_type,
            self.glindices)

    finally:
        self.vbo.unbind()
        gl_wrap.glBindVertexArray(0)
        self.shader.unuse()

A simple quad has the following data sent to OpenGL :

vertices ( flat array of pos+rgba+normal) :
[5.0, -3.061616997868383e-16, 5.0, 0.898, 0.0, 0.0, 1.0, 0.0, 1.0, 6.123233995736766e-17, 
-5.0, 3.061616997868383e-16, -5.0, 0.898, 0.0, 0.0, 1.0, 0.0, 1.0, 6.123233995736766e-17, 
-5.0, -3.061616997868383e-16, 5.0, 0.898, 0.0, 0.0, 1.0, 0.0, 1.0, 6.123233995736766e-17, 
5.0, 3.061616997868383e-16, -5.0, 0.898, 0.0, 0.0, 1.0, 0.0, 1.0, 6.123233995736766e-17]

indices  :: [0, 1, 2, 0, 3, 1]
LBarret
  • 1,113
  • 10
  • 23
  • Fun fact: Blender also uses OpenGL. – Jerem May 29 '15 at 07:12
  • 1
    I don't know what could be causing it, except slightly different vertex positions. Are you sure there is no duplicate vertex? You only have 5 vertices in your vertex buffer? – Jerem May 29 '15 at 07:17
  • @jerem : I have checked in Blender, the vertices seems ok, meaning shared. Moving the top vertex of a pyramid, move all the faces. But...the export code path doesn't do the triangulation, I'll check there. thks. – LBarret May 29 '15 at 07:29
  • @jerem : not it seems the triangulation seems ok, I asserted there is the right number of vertices – LBarret May 29 '15 at 07:46
  • 1
    Can you print the content of both your vertex buffer and your index buffer (the ones you use for rendering) and add it to your post? Maybe we'll see something there. – Jerem May 29 '15 at 07:55
  • 2
    Oh but first try to change your fragment shader to output a hard-coded color and see if the problem is still there. Just to be sure it's not a lighting code problem. – Jerem May 29 '15 at 07:58
  • added the vertices and indices for a quad at the bottom of the post – LBarret May 29 '15 at 08:22
  • changed the gl_fragcolor to a static color, still the same artifact. Even, the front place clipping generates it : http://i.imgur.com/mgkB2EB.png – LBarret May 29 '15 at 08:24

1 Answers1

3

ok, got it !

the strange display is due to glEnabled(GL_POLYGON_SMOOTH).

I had completely forgotten about it as my old card wasn't doing anything with this setting. It wasn't visible at least. But the new one has the correct behaviour : it tries to anti-alias each triangle independently, hence the black lines.

Many thanks to @Jerem who helped me cover the other possibilities.

LBarret
  • 1,113
  • 10
  • 23