yoale

05-07-2003, 10:56 AM

Hello again coders! Alright, heres the lowdown. I thought I came up with this perfect per-vertex bump-mapping algorithm, but it just won't work! You can see some screen shots at http://yoale.tripod.com/help.html (a page in my site).

Can any one help? I've commented each main piece of code as best as I could, so hopefully the code explains itself. Any insight would be most appreciated. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

void Mesh::GenST( Decimal *ptrM, Decimal *notused, Decimal *lpES, Decimal *notused2, float lumens)

{

float *tri = &this->strip[0]; // Stores vertices

float *array = &this->nmapVert[0]; // Stores texture coordinates for normal map

float *nrmlOut = &this->nMap[0]; // Normal to vertex in object space

float mag;

float lvES[4]; // Light position in object space

float lvTS[3]; // Light vector in Tangent Space

float lvOS[3]; // Light vector in Object Space

float inverted[3];

// Variabes used in rotating nrml vector to object space

float COS, SIN, axis[3], vmat[16];

unsigned long max = this->endt * 2 * 3;

FLOAT t1;

FLOAT tx;

FLOAT ty;

FLOAT txx;

FLOAT tyy;

FLOAT txy;

FLOAT sx;

FLOAT sy;

//Vertex in eye space

FLOAT test[3];

//Distance from light to plane in eye space

float dstOut;

float negd;

float rsq;

float nES[3];

//*

for (this->t = this->begt; this->t < max; ++t, array += 2, tri += 3, nrmlOut += 3)

{

// Transform vertex from object space to eye space

test[0] = ptrM[0] * tri[0] + ptrM[4] * tri[1] + ptrM[8] * tri[2] + ptrM[12];

test[1] = ptrM[1] * tri[0] + ptrM[5] * tri[1] + ptrM[9] * tri[2] + ptrM[13];

test[2] = ptrM[2] * tri[0] + ptrM[6] * tri[1] + ptrM[10] * tri[2] + ptrM[14];

// Transform normal out to eyespace

nES[0] = ptrM[0] * nrmlOut[0] + ptrM[4] * nrmlOut[1] + ptrM[8] * nrmlOut[2];

nES[1] = ptrM[1] * nrmlOut[0] + ptrM[5] * nrmlOut[1] + ptrM[9] * nrmlOut[2];

nES[2] = ptrM[2] * nrmlOut[0] + ptrM[6] * nrmlOut[1] + ptrM[10] * nrmlOut[2];

negd = nES[0] * test[0] +

nES[1] * test[1] +

nES[2] * test[2];

dstOut = nES[0] * lpES[0] +

nES[1] * lpES[1] +

nES[2] * lpES[2] - negd;

rsq = nES[0] * nES[0] +

nES[1] * nES[1] +

nES[2] * nES[2];

if (rsq)

{

rsq = sqrt(rsq);

}

//*/

// Find inverted translate vector

InvertedTranslate(inverted, ptrM);

// Find light vector in eye space (vector from light to vertex)

lvES[0] = lpES[0] - test[0];

lvES[1] = lpES[1] - test[1];

lvES[2] = lpES[2] - test[2];

// Transform lvES into object space

lvOS[0] = ptrM[0] * lvES[0] + ptrM[1] * lvES[1] + ptrM[2] * lvES[2] + inverted[0];

lvOS[1] = ptrM[4] * lvES[0] + ptrM[5] * lvES[1] + ptrM[6] * lvES[2] + inverted[1];

lvOS[2] = ptrM[8] * lvES[0] + ptrM[9] * lvES[1] + ptrM[10] * lvES[2] + inverted[2];

//I assume that if I rotate lvOS around vector a x b / |a x b|

//(where b is <0, 0, 1> ) by arcsin(|a x b|) that the lvOS

//vector would be in tangent space, is this right?

// No longer need these, provides for optimization since they should always equal zero

//FLOAT tzz = t * axis[2] * axis[2];

//FLOAT txz = tx * axis[2];

//FLOAT tyz = ty * axis[2];

//FLOAT sz = SIN * axis[2];

COS = nrmlOut[2];

SIN = sqrt(nrmlOut[0] * nrmlOut[0] + nrmlOut[1] * nrmlOut[1]);

axis[0] = nrmlOut[1];

axis[1] = -nrmlOut[0];

axis[2] = 0;

if (SIN)

{

axis[0] /= SIN;

axis[1] /= SIN;

}

t1 = 1.0f - COS;

tx = t1 * axis[0];

ty = t1 * axis[1];

txx = tx * axis[0];

tyy = ty * axis[1];

txy = tx * axis[1];

sx = SIN * axis[0];

sy = SIN * axis[1];

vmat[0] = txx + COS; vmat[1] = txy; vmat[2] = -sy;

vmat[4] = txy; vmat[5] = tyy + COS; vmat[6] = sx;

vmat[8] = sy; vmat[9] = -sx; vmat[10] = COS;

lvTS[0] = lvOS[0] * vmat[0] +

lvOS[1] * vmat[4] +

lvOS[2] * vmat[8];

lvTS[1] = lvOS[0] * vmat[1] +

lvOS[1] * vmat[5] +

lvOS[2] * vmat[9];

lvTS[2] = lvOS[0] * vmat[2] +

lvOS[1] * vmat[6] +

lvOS[2] * vmat[10];

mag = lvTS[0] * lvTS[0] +

lvTS[1] * lvTS[1] +

lvTS[2] * lvTS[2];

if (mag)

{

mag = 1 / sqrt(mag);

lvTS[0] *= mag;

lvTS[1] *= mag;

lvTS[2] *= mag;

}

array[0] = lvTS[0] * rsq / (dstOut * lumens) + 0.5f;

array[1] = lvTS[1] * rsq / (dstOut * lumens) + 0.5f;

}

[This message has been edited by yoale (edited 05-07-2003).]

[This message has been edited by yoale (edited 05-07-2003).]

Can any one help? I've commented each main piece of code as best as I could, so hopefully the code explains itself. Any insight would be most appreciated. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

void Mesh::GenST( Decimal *ptrM, Decimal *notused, Decimal *lpES, Decimal *notused2, float lumens)

{

float *tri = &this->strip[0]; // Stores vertices

float *array = &this->nmapVert[0]; // Stores texture coordinates for normal map

float *nrmlOut = &this->nMap[0]; // Normal to vertex in object space

float mag;

float lvES[4]; // Light position in object space

float lvTS[3]; // Light vector in Tangent Space

float lvOS[3]; // Light vector in Object Space

float inverted[3];

// Variabes used in rotating nrml vector to object space

float COS, SIN, axis[3], vmat[16];

unsigned long max = this->endt * 2 * 3;

FLOAT t1;

FLOAT tx;

FLOAT ty;

FLOAT txx;

FLOAT tyy;

FLOAT txy;

FLOAT sx;

FLOAT sy;

//Vertex in eye space

FLOAT test[3];

//Distance from light to plane in eye space

float dstOut;

float negd;

float rsq;

float nES[3];

//*

for (this->t = this->begt; this->t < max; ++t, array += 2, tri += 3, nrmlOut += 3)

{

// Transform vertex from object space to eye space

test[0] = ptrM[0] * tri[0] + ptrM[4] * tri[1] + ptrM[8] * tri[2] + ptrM[12];

test[1] = ptrM[1] * tri[0] + ptrM[5] * tri[1] + ptrM[9] * tri[2] + ptrM[13];

test[2] = ptrM[2] * tri[0] + ptrM[6] * tri[1] + ptrM[10] * tri[2] + ptrM[14];

// Transform normal out to eyespace

nES[0] = ptrM[0] * nrmlOut[0] + ptrM[4] * nrmlOut[1] + ptrM[8] * nrmlOut[2];

nES[1] = ptrM[1] * nrmlOut[0] + ptrM[5] * nrmlOut[1] + ptrM[9] * nrmlOut[2];

nES[2] = ptrM[2] * nrmlOut[0] + ptrM[6] * nrmlOut[1] + ptrM[10] * nrmlOut[2];

negd = nES[0] * test[0] +

nES[1] * test[1] +

nES[2] * test[2];

dstOut = nES[0] * lpES[0] +

nES[1] * lpES[1] +

nES[2] * lpES[2] - negd;

rsq = nES[0] * nES[0] +

nES[1] * nES[1] +

nES[2] * nES[2];

if (rsq)

{

rsq = sqrt(rsq);

}

//*/

// Find inverted translate vector

InvertedTranslate(inverted, ptrM);

// Find light vector in eye space (vector from light to vertex)

lvES[0] = lpES[0] - test[0];

lvES[1] = lpES[1] - test[1];

lvES[2] = lpES[2] - test[2];

// Transform lvES into object space

lvOS[0] = ptrM[0] * lvES[0] + ptrM[1] * lvES[1] + ptrM[2] * lvES[2] + inverted[0];

lvOS[1] = ptrM[4] * lvES[0] + ptrM[5] * lvES[1] + ptrM[6] * lvES[2] + inverted[1];

lvOS[2] = ptrM[8] * lvES[0] + ptrM[9] * lvES[1] + ptrM[10] * lvES[2] + inverted[2];

//I assume that if I rotate lvOS around vector a x b / |a x b|

//(where b is <0, 0, 1> ) by arcsin(|a x b|) that the lvOS

//vector would be in tangent space, is this right?

// No longer need these, provides for optimization since they should always equal zero

//FLOAT tzz = t * axis[2] * axis[2];

//FLOAT txz = tx * axis[2];

//FLOAT tyz = ty * axis[2];

//FLOAT sz = SIN * axis[2];

COS = nrmlOut[2];

SIN = sqrt(nrmlOut[0] * nrmlOut[0] + nrmlOut[1] * nrmlOut[1]);

axis[0] = nrmlOut[1];

axis[1] = -nrmlOut[0];

axis[2] = 0;

if (SIN)

{

axis[0] /= SIN;

axis[1] /= SIN;

}

t1 = 1.0f - COS;

tx = t1 * axis[0];

ty = t1 * axis[1];

txx = tx * axis[0];

tyy = ty * axis[1];

txy = tx * axis[1];

sx = SIN * axis[0];

sy = SIN * axis[1];

vmat[0] = txx + COS; vmat[1] = txy; vmat[2] = -sy;

vmat[4] = txy; vmat[5] = tyy + COS; vmat[6] = sx;

vmat[8] = sy; vmat[9] = -sx; vmat[10] = COS;

lvTS[0] = lvOS[0] * vmat[0] +

lvOS[1] * vmat[4] +

lvOS[2] * vmat[8];

lvTS[1] = lvOS[0] * vmat[1] +

lvOS[1] * vmat[5] +

lvOS[2] * vmat[9];

lvTS[2] = lvOS[0] * vmat[2] +

lvOS[1] * vmat[6] +

lvOS[2] * vmat[10];

mag = lvTS[0] * lvTS[0] +

lvTS[1] * lvTS[1] +

lvTS[2] * lvTS[2];

if (mag)

{

mag = 1 / sqrt(mag);

lvTS[0] *= mag;

lvTS[1] *= mag;

lvTS[2] *= mag;

}

array[0] = lvTS[0] * rsq / (dstOut * lumens) + 0.5f;

array[1] = lvTS[1] * rsq / (dstOut * lumens) + 0.5f;

}

[This message has been edited by yoale (edited 05-07-2003).]

[This message has been edited by yoale (edited 05-07-2003).]