Tangent Computation for Bump Mapping doesn't Work Well

Hi everybody,
I made an adaptation from this code:


to OF; just the application code, not the shaders.
For the shaders part I used the Orange Book ones, version 1.2 of GLSL.
The problem is that I get a lot of artifacts in the light reproduction. Seems like I got something wrong in the adaptation.

Here is my application code:

ofPlanePrimitive plane;
ofSpherePrimitive sphere;
ofEasyCam cam;
ofColor backColor1, backColor2;

GLfloat* T;

//--------------------------------------------------------------
void ofApp::setup(){
    //ofDisableArbTex();
    ofEnableDepthTest();       //rendering happens in order of z-depth rather than draw order
    ofDisableAlphaBlending();
    //ofEnableNormalizedTexCoords(); //not really useful here
    
    shader.load( "shaderVert.vs", "shaderFrag.fs");
    
    plane.setWidth(ofGetWidth()-200);
    plane.setHeight(ofGetHeight()-200);
    
    sphere.setRadius(200);
    sphere.setResolution(100);
  
    backColor2.set(0,0,0);
    backColor1.set(100,20,20);
    
    T = computeTangentSpace(plane.getMesh());
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackgroundGradient(backColor1, backColor2, OF_GRADIENT_CIRCULAR);
    
    shader.begin();
    shader.setUniform3f("LightPosition", 0, 0, 500);
    shader.setAttribute3fv("Tangent", T);
    
    cam.begin();
    cam.setVFlip(true);
    plane.setPosition(ofVec3f(0,0,0));
    plane.draw();
    sphere.setPosition(ofVec3f(0,0,0));
    //sphere.draw();
    cam.end();
    
    shader.end();

    ofDrawBitmapString("fps: " + ofToString(ofGetFrameRate()), 20, 20);
}

//----Tangent Computation-------

GLfloat* ofApp::computeTangentSpace(ofMesh &meshData) {
    GLfloat * tangents = new GLfloat[meshData.getVertices().size()];
    GLfloat * binormals = new GLfloat[meshData.getVertices().size()];
    
    vector<ofVec3f> tangent;
    vector<ofVec3f> binormal;
    
    for(size_t i = 0; i < meshData.getIndices().size(); i += 3) {
        
        ofVec3f vertex0 = meshData.getVertex(i);
        ofVec3f vertex1 = meshData.getVertex(i+1);
        ofVec3f vertex2 = meshData.getVertex(i+2);
        
        ofVec3f delta10 = vertex1-vertex0;
        ofVec3f delta20 = vertex2-vertex0;
        
        ofVec3f normal = delta10.cross(delta20);
        
        ofVec3f deltaPos;
        if(vertex0 == vertex1)
        deltaPos = delta20;
        else
        deltaPos = delta10;
        
        ofVec2f uv0 = meshData.getTexCoord(i);
        ofVec2f uv1 = meshData.getTexCoord(i+1);
        ofVec2f uv2 = meshData.getTexCoord(i+2);
        
        ofVec2f deltaUV1 = uv1 - uv0;
        ofVec2f deltaUV2 = uv2 - uv0;
        
        ofVec3f tan;
        ofVec3f bin;
        
        if(deltaUV1.x != 0)
        tan = deltaPos / deltaUV1.x;
        else
        tan = deltaPos / 1.0f;
        
        tan = (tan - (normal.dot(tan)*normal));
        tan.normalize();
        
        bin = tan.cross(normal);
        bin.normalize();
        
        tangents[meshData.getIndex(i)] = tan.x;
        tangents[meshData.getIndex(i)+1] = tan.y;
        tangents[meshData.getIndex(i)+2] = tan.z;
        
        tangents[meshData.getIndex(i+1)] = tan.x;
        tangents[meshData.getIndex(i+1)+1] = tan.y;
        tangents[meshData.getIndex(i+1)+2] = tan.z;
        
        tangents[meshData.getIndex(i+2)] = tan.x;
        tangents[meshData.getIndex(i+2)+1] = tan.y;
        tangents[meshData.getIndex(i+2)+1] = tan.z;
        
        binormals[meshData.getIndex(i)] = bin.x;
        binormals[meshData.getIndex(i)+1] = bin.y;
        binormals[meshData.getIndex(i)+2] = bin.z;
        
        binormals[meshData.getIndex(i+1)] = bin.x;
        binormals[meshData.getIndex(i+1)+1] = bin.y;
        binormals[meshData.getIndex(i+1)+2] = bin.z;
        
        binormals[meshData.getIndex(i+2)] = bin.x;
        binormals[meshData.getIndex(i+2)+1] = bin.y;
        binormals[meshData.getIndex(i+2)+2] = bin.z;
    }
    
    return tangents;
}

This is what I get:

I don’t know if I can post the code for the shaders, because it’s from the Orange Book and I don’t know about the copyright. Anyway I guess the error is here in the host code.

Any help will be really appreciated!

Thank you

Just a guess but it could be because the default mode of plane primitive is OF_PRIMITIVE_TRIANGLE_STRIP, so the vertex calculations arent using the right vertices. Try using OF_PRIMITIVE_TRIANGLES when you create the plane mesh.

Otherwise I think just post the shader code or a link to it.