# Calculating normals in compute shaders results in strange lines appearing on surface

Hello,

I am working on a new project to make a super fast dynamic mesh with compute shaders.
I am using compute shaders to modify vertices positions and colours successfully so far, but I have a problem when it comes to calculating the normals.

I need the normals so that I can use them in frag/vert shaders for a nice dynamic point lighting. I’m using a ofVBO which I set with ofBufferObject (one for positions, colours etc…) so that I can bind those buffers onto the GPU to access them on the Compute Shaders.

Here’s how I set up the mesh originally:

``````	for (int y = 0; y < myHeight; y += skip) {
for (int x = 0; x < myWidth; x += skip) {

myVertices.push_back((float)x);
myVertices.push_back((float)y);
myVertices.push_back((float)0);
myVertices.push_back((float)1);
myNormals.push_back((float)0);
myNormals.push_back((float)0);
myNormals.push_back((float)0);
myNormals.push_back((float)1);
myVertColours.push_back(originalColor);

if (x < myWidth - 1 && y < myHeight - 1) {

myIndices[iteratorIndices] = x + y * myWidth;
myIndices[iteratorIndices + 1] = (x + 1) + y * myWidth;
myIndices[iteratorIndices + 2] = x + (y + 1) * myWidth;

myIndices[iteratorIndices + 3] = (x + 1) + y * myWidth;
myIndices[iteratorIndices + 4] = (x + 1) + (y + 1) * myWidth;
myIndices[iteratorIndices + 5] = x + (y + 1) * myWidth;

iteratorIndices += 6;
}
}
}
``````

I then bind the buffers to the GPU using:

``````	vertexBuffer.allocate(myVertices, GL_DYNAMIC_DRAW);
colorBuffer.allocate(myVertColours, GL_DYNAMIC_DRAW);
//indicesBuffer.allocate(myIndices, GL_DYNAMIC_DRAW);
normalBuffer.allocate(myNormals, GL_DYNAMIC_DRAW);

myVBO.setVertexBuffer(vertexBuffer, 4, 4 * sizeof(float));
myVBO.setColorBuffer(colorBuffer, sizeof(ofFloatColor));
myVBO.setNormalBuffer(normalBuffer, 4);
myVBO.setIndexData(myIndices, myWidth * myHeight * 6, GL_STATIC_DRAW);

``````

Calculating normals is done with a function on the Compute Shaders:

``````calcNormals(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
``````

Then the function looks like:

``````            vec3 d0 = v[x+(y-1)*W].xyz - v[x+y*W].xyz;
vec3 d1 = v[x+1+y*W].xyz - v[x+y*W].xyz;
vec3 w = -cross(d1, d0);
n[x+y*W].xyz += w;//vec4(w, 0);
n[x+(y-1)*W].xyz += w;//vec4(w, 0);
n[x+1+y*W].xyz += w;//vec4(w, 0);

d0 = v[x+1+y*W].xyz - v[x+y*W].xyz;
d1 = v[x+(y+1)*W].xyz - v[x+y*W].xyz;
w = -cross(d1, d0);
n[x+y*W].xyz += w;//vec4(w, 0);
n[x+1+y*W].xyz += w;//vec4(w, 0);
n[x+(y+1)*W].xyz += w;//vec4(w, 0);

n[x+y*W].xyz = normalize(n[x+y*W].xyz);
n[x+1+y*W].xyz = normalize(n[x+1+y*W].xyz);
n[x+(y+1)*W].xyz = normalize(n[x+(y+1)*W].xyz);
n[x+1+y*W].xyz += normalize(n[x+1+y*W].xyz);
n[x+(y+1)*W].xyz += normalize(n[x+(y+1)*W].xyz);
n[(x+1)+(y+1)*W].xyz += normalize(n[(x+1)+(y+1)*W].xyz);
``````

With v[ ] being the vertices array, and n[ ] the normals array which I bind earlier.

Now everything seems to work fine, the lighting is happening with a point light and I can see it affecting the vertices brightness and colours except that I can’t get ridd of the lines/strides that you see on the screenshot. I am not sure why it’s happening instead of having a smooth surface.

Any idea why this is occuring?

Thanks a lot for your help
Romain