Author Topic: little 3d cube with bumpmapping.  (Read 15293 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
little 3d cube with bumpmapping.
« on: May 18, 2013 »
hello guys.

ive mapped the bumpmapping stuff from the other thread onto a prespective correct cube. too see how it would look. ive kind of gotten myself a little stuck though.

the problem mainly comes from the fact that i keep my texture process seperate from the rasterizer so once it comes time too map it onto the cube if the texture gets shrunk of spun or stretched in diffrent direction from which it was processed the lighting gets inverted and skewed. i was trying some angular stuff too try and give myself a point of refrence too work off durring the texture lighting as can be seen with the blue circle but no joy.

if anyone has any ideas id love too hear cheers!

<edit for epiphany>actually thinking about it diffuse and specular terms should be an integral part of the rasterizer. its really the only way to do them correctly while moving and scaling the texture dynamically  :P. and all the info is already too hand i guess. x,y,z pos down too the pixel. some more thinking too do i guess!
« Last Edit: May 18, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #1 on: May 19, 2013 »
take two..,
ive striped it all back too make it easyer too understand.

ive integrated the diffuse term into the triangle rasterizer, it now does x,y,z on a pixel by pixel basis but i just cant get it too behave correctly you guys will see when you run it the diffuse is the same for each face which it shouldn't be im giving it diffrent values for each pixel so i really dont understand why it does this. im kind of stumped atm tried loads of stuff,

i think im maybe missing a crucial step out thats needed for this too work but cant think what that might be.

any advice is much apreciated.

edit < much much closer now but im still a little perplexed by a few things there is a few small cases where things go of.. oh and the diffuse term only works when the light moves. if i make the light static and just rotate the object the the light doesn't move over the surface its like its paused >
« Last Edit: May 19, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #2 on: May 20, 2013 »
To make this work properly you have bring all vectors into the same coordinate system.
The normal-map is stored in tangent space (which only works for an untransformed quad in the xy-plane, just like in the 2d version) where the normal is pointing along z aka. (0,0,1) or "blue".
But if you rotate your object you must rotate your normals, too.
You can construct a new coordinate system for each face from three vectors: a horizontal (x) and vertical (y) edge of a cube's face and its' face-normal (z).
Using this matrix you can transform every normal from the normal-map into world-space.
And using the inverse of this matrix you can transform the light- and view-direction from world-space back into tangent-space - which is much faster as you have to do this only once per polygon - or once per vertex and interpolate the result across your polygon.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #3 on: May 20, 2013 »
thank you hellfire!

so too rotate the normals you have too come up with an vertex x y z tangent conversion matrix that gets made up from the vertex data on each face and passed too the rasterizer then you rotate the tangent normals using this matrix.

i can now see how all the coordinates light cam etc will have too be in texture space also.

Quote
You can construct a new coordinate system for each face from three vectors: a horizontal (x) and vertical (y) edge of a cube's face and its' face-normal (z).

im not 100% on this part. i can get the normal of each Face and the x and y Vectors no probs but its just trying too envision how i would use these values too create the matrix and then how too apply it too the normals.

<edit> just done a quick an dirty test. i rendered a single quad and just rotated the normals in tangent space. by the same factor as the quad and it works!! 8). i realize though that itll only work on a single quad and very slowly until im able to build the tangent rotation matrix based on the face/vertex data

as for the light conversion does it just go like. i feed the LightPos Into the triangle routine taking away vertex x y z at point 1 2 3 too get 3 sets of direction vectors then interpolate these values the same as all the other values in the rasterizer. then at the end combine them with the matrix to get there tangent values?. does that sound about right.

thanks very much mate!
« Last Edit: May 20, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #4 on: May 20, 2013 »
Quote
You can construct a new coordinate system for each face from three vectors: a horizontal (x) and vertical (y) edge of a cube's face and its' face-normal (z).
im not 100% on this part. i can get the normal of each Face and the x and y Vectors no probs but its just trying too envision how i would use these values too create the matrix and then how too apply it too the normals.
You have to create a matrix which transforms the normals from the normal-map (which is defined in the xy-plane) onto your polygon.
If your mesh is using smoothed vertex normals, you've got a different tangent-space matrix for every vertex (because every vertex has a different normal vector):


In the case of a cube it's all a bit simpler because the tangent-plane is equal to the face of the cube and you don't have to interpolate the tangent-vectors across the polygon.
Since you're transforming direction vectors, the translation component of the matrix has no use and you just have to deal with a 3x3 matrix.
So you can simply build one matrix per quad, calculate light- and view-direction for each vertex of the quad, transform both vectors with the inverse matrix into normalmap-space and interpolate the two vectors across your quad.
Now all the math happens in normal-map space, just like in the 2d version.

Inverting the matrix is very simple btw because it's orthonormal (all three vectors are normalized and perpendicular), so you just have to transpose the values:
Code: [Select]
inverse[x][y] = matrix[y][x]
« Last Edit: May 20, 2013 by hellfire »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #5 on: May 21, 2013 »
thanks hellfire k+  :)

ahh i think i get it so the matrix were creating is a coordinate handle, of course now i think of it too use any coordinate system you would need some point of reference. to convert too and from systems i.e how much 1 unit in one system relates too 1 unit in the other. then when you apply the 3x3matrix too each normal it makes them relate too the world space position that they will be positioned.

ive done all the back ground stuff im pulling all the normals off the cube each frame. feeding the light position into the rasterizer and interpolating across so as soon as i figuare out the building of the matrix i should be good too go. im trying some different things atm.
« Last Edit: May 21, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #6 on: May 21, 2013 »
hey hellfire been bashing away at this all morning..

i think im close. theres still a few things off though like im having too invert x and y  matrix parts too get it too work like so.

Code: [Select]
No.X = (Temp.X*TangentMatrix(1).Y + Temp.Y*TangentMatrix(1).X + Temp.Z*TangentMatrix(1).Z)*1024
No.Y = (Temp.X*TangentMatrix(2).Y + Temp.Y*TangentMatrix(2).X + Temp.Z*TangentMatrix(2).Z)*1024
No.Z = (Temp.X*TangentMatrix(3).Y + Temp.Y*TangentMatrix(3).X + Temp.Z*TangentMatrix(3).Z)*1024

but it does rotate the normals in the correct direction.. my tangent matrix function looks like so atm.
Code: [Select]
Sub ComputeTangentMatrix( Vec1 As Vector4d, Vec2 As Vector4d, Vec3 As Vector4d, Uv1 As Vector2d, Uv2 As Vector2d, Uv3 As Vector2d _
    , Norm1 As Vector4d, Norm2 As Vector4d, Norm3 As Vector4d )
           
            Dim As Vector4d Tangent
            Dim As Vector4d Uv
           
            Dim As Double X1 = Vec2.X - Vec1.X
            Dim As Double X2 = Vec3.x - Vec1.X
            Dim As Double Y1 = Vec2.y - Vec1.Y
            Dim As Double Y2 = Vec3.y - Vec1.Y
            Dim As Double Z1 = Vec2.z - Vec1.Z
            Dim As Double Z2 = Vec3.z - Vec1.Z
       
            Dim As Double U1 = Uv2.x - Uv1.x
            Dim As Double U2 = Uv3.x - Uv1.x
            Dim As Double V1 = Uv2.y - Uv1.y
            Dim As Double V2 = Uv3.y - Uv1.y
           
            Uv.X = (V2 * X1 - V1 * X2) / (U1 * V2 - U2 * V1)
            Uv.Y = (V2 * Y1 - V1 * Y2) / (U1 * V2 - U2 * V1)
            Uv.Z = (V2 * Z1 - V1 * Z2) / (U1 * V2 - U2 * V1)
           
            Tangent.X = (U1 * X2 - U2 * X1) / (U1 * V2 - U2 * V1)
            Tangent.Y = (U1 * Y2 - U2 * Y1) / (U1 * V2 - U2 * V1)
            Tangent.Z = (U1 * Z2 - U2 * Z1) / (U1 * V2 - U2 * V1)

            TangentMatrix(3).X = Uv.X
            TangentMatrix(2).X = Tangent.X
            TangentMatrix(1).X = ( Tangent.X+Norm1.X * Tangent.X+Norm2.X * Tangent.X+Norm3.X )
           
            TangentMatrix(3).Y = Uv.Y
            TangentMatrix(2).Y = Tangent.Y
            TangentMatrix(1).Y = ( Tangent.Y+Norm1.Y * Tangent.Y+Norm2.Y * Tangent.Y+Norm3.Y )
           
            TangentMatrix(3).Z = Uv.Z
            TangentMatrix(2).Z = Tangent.Z
            TangentMatrix(1).Z = ( Tangent.Z+Norm1.Z * Tangent.Z+Norm2.Z * Tangent.Z+Norm3.Z )
            NormilizeVec( @TangentMatrix(1) )
            NormilizeVec( @TangentMatrix(2) )
            NormilizeVec( @TangentMatrix(3) )
           
End Sub

ive also added lots of other stuff in there like the normalization of the cube etc. im not yet converting the light but i am interpolating it. i figure i better get the basic matrix stuff nailed first and take it a step at a time

« Last Edit: May 21, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #7 on: May 22, 2013 »
sorry i realise that was very wrong  :).

im now creating a tangent binormal and normal for each vertex like this.
Code: [Select]
Sub ComputeTangentMatrix( Tangent As Vector4d, BiNorm As Vector4d, TangNorm As Vector4d, Vec1 As Vector4d, Vec2 As Vector4d, Vec3 As Vector4d _
    , Uv1 As Vector2d, Uv2 As Vector2d, Uv3 As Vector2d )
           
    Dim As Double v1x, v1y, v1z, v2x, v2y, v2z, u1x, u1y, u2x, u2y, Recip
 
    v1x = Vec2.x - Vec1.x
    v1y = Vec2.y - Vec1.y
    v1z = Vec2.z - Vec1.z
 
    v2x = Vec3.x - Vec1.x
    v2y = Vec3.y - Vec1.y
    v2z = Vec3.z - Vec1.z
 
    u1x = Uv2.x - Uv1.x
    u1y = Uv2.y - Uv1.y
 
    u2x = Uv3.x - Uv1.x
    u2y = Uv3.y - Uv1.y
 
    Recip = 1.0/(u1x * u2y - u2x * u1y)
 
    Tangent.X = (v1x * u2y - v2x * u1y) * Recip
    Tangent.Y = (v1y * u2y - v2y * u1y) * Recip
    Tangent.Z = (v1z * u2y - v2z * u1y) * Recip
 
    Binorm.X = (-v1x * u2x + v2x * u1x) * Recip
    Binorm.Y = (-v1y * u2x + v2y * u1x) * Recip
    Binorm.z = (-v1z * u2x + v2z * u1x) * Recip
 
    TangNorm.X = BiNorm.Y * tangent.Z - BiNorm.z * Tangent.Y
    TangNorm.Y = BiNorm.Z * tangent.X - BiNorm.x * Tangent.Z
    TangNorm.Z = BiNorm.X * tangent.Y - BiNorm.y * Tangent.X

    NormilizeVec( @Tangent )
    NormilizeVec( @binorm )
    NormilizeVec( @TangNorm )
   
End Sub

this just feeds the T B N Vectors back to the 3dobject struct and they sit alongside the vertex's. now that i have 1 T B N vector per vertex im not 100% sure how too use all three too rotate the normals. Maybe Feed them into the rasterizer and interpolate?.

im trying the lighting like so too
Code: [Select]
Sub SetUvLight1( Vertex As Vector4d, Light As Vector4d, T As Vector4d, B As Vector4d, N As Vector4d  )

    Ind0.Lx = (Light.X - Vertex.X)
    Ind0.Ly = (Light.Y - Vertex.Y)
    Ind0.Lz = (Light.Z - Vertex.Z)
   
    NormilizeVec( @Ind0 )
    'tx.bx.nx column major
    'ty.by.ny
    'tz.bz.nz
    'inv = row major
    'tx.ty.tz
    'bx.by.bz
    'nx.ny.nz
    'tried inverting the ind vectors also too see if it would work
    Ind0.Lx = (T.X*Ind0.Lx + T.Y*Ind0.Lx + T.Z*Ind0.Lx)
    Ind0.Ly = (B.X*Ind0.Ly + B.Y*Ind0.Ly + B.Z*Ind0.Ly)
    Ind0.Lz = (N.X*Ind0.Lz + N.Y*Ind0.Lz + N.Z*Ind0.Lz)

End Sub

each SetUvLight now gets its own T B N Vectors that correspond too the vertex. for some reason though the light is always offset too the left of the texture its like im missing another step. i tried a few things like translating and rotating the direction vector but with no luck. i think im definitly missing something but cant think what.

the reason im trying too do it on this way having a T B N Vector for each vertex is simply because all the examples out there are in glsl so im having too try and follow them but im not so good at shaders so its a bit sketchy for me.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #8 on: May 23, 2013 »
still going at this  :)

i went your way hellfire with one matrix made up of ( Tangent Binormal and Normal ) Vectors per quad its a much simpler way too work.. im just not getting proper values out the transformed vectors for some reason ie the light vector xyz after translation keeps creating very high values (positive and negative values all the way up too 700) .

and ive watched all the values in my un-normalised tbn matrix they all stay between 3 and -3 smoothly scrolling through the numbers in fractions, this is while everything is rotating. if i stop rotations the values in the matrix stay static which seems quite correct.

would these sort of values be in line with whats expected?

« Last Edit: May 23, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #9 on: May 24, 2013 »
I don't really know what you're doing but what you'd usually do is:
- calculate the normalized light direction vector for each vertex (vector from the light position to the vertex position)
- transform this vector to normalmap-space using the matrix constructed from normal, tangent & binormal
- interpolate the vectors across your polygon
- roughly re-normalize the vector at each pixel (or every few pixels)

And the same happens for the view direction (vector from the camera to the vertex position).
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #10 on: May 25, 2013 »
cheers hellfire,

yep thats exactly what im doing.

-create matrix ( 100% sure this is correct, cross refrenced with so many sources i know the exact building process now )
-calculate 3 light direction vectors, one for each vertex of the triangle
-use inv matrix on the 3 light vectors
-normalize results
-send into triangle routine each transformed light vector
-interpolate in  the exact same manner as uv co-ords between light vectors.
-then process the same as 2d version.

im only going for the diffuse element atm because i want too make sure everything works before tackling specular. so im not processing cam vector.
im not very sure whats up though ive been over and over my code effectlively treating everything like a check list watching all the values etc. it all looks good but i get lighting artifacts and the light moves in an unexpected manner during rotations.

the biggest problem is coming from the fact this is a first time for me doing this so im not sure what too look for when tracking values debugging etc.
« Last Edit: May 25, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #11 on: May 25, 2013 »
Are the vertex-positions and the light position in the same coordinate system when you calculate the light direction vector?
If the light position is defined in world space, the vertices must be transformed with the model matrix (without the camera orientation) and must not be 2d-projected.
When I'm not sure if my vectors make any sense I usually draw the interpolated xyz-components as rgb to the screen.
« Last Edit: May 25, 2013 by hellfire »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #12 on: May 25, 2013 »
excellent idea hellfire!!

both light and object are in the same projected and translated space.

exactly the sort of thing i needed too see what was going on. when i use color values on the x y interpolated vectore x= red y = green you can clearly see the problem im having when the cube is static i get predictable results when the direction is + i get a nice smooth increase in color and - fades too black,

however the minute i put any kind of rotation in i get serious artifacts. do you think i might have screwed the interpolation up?!

two exe's here one static and one rotation too show light as interpolation.
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #13 on: May 26, 2013 »
I haven't looked into your source code yet but I think the black areas are correct because the vector components get negative.
That's why I usually plot 128+x/2 (given that x is -128..+127).
So you get grey for (0,0,0) and more red where x increases, more green for y and blue for z (depending on your rgb/bgr order of course).
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #14 on: May 26, 2013 »
thanks hellfire,

ive made some real progress today i can see everything at work which has helped with lots of little things. i understand where my problem is coming from too,

when everything is unrotated it all behaves as i would expect i think it works properly, however as i put rotation in obviously +ve -ve for xyz starts too change and its exactly at the cross over point from +- -+ it creates a strong band of color. once it gets a little past 0 in either direction everthing returns too behaving as expected again.

so ive narrowed it down too where this artifact is coming from as much as i have tried though i cant get rid of it. currently installing my visual studio too cross refrence this with some glsl too try and get too the bottom of it.
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #15 on: May 26, 2013 »
I've made a quick test with glsl myself.

What I do per vertex is this:
Code: [Select]
uniform vec4 lightPosition;  // position of lightsource in world space (-1.0, 0.5, 1, 1.0)
uniform vec4 cameraPosition; // position of camera in world space (0.0, 0.0, 4,0)

// interpolated vertex attributes (passed to the fragment shader)
varying vec2 uv;             // texture coordinate
varying vec3 lightDir;       // light direction in normalmap space
varying vec3 viewDir;        // view direction in normalmap space

void main()
{
   // inverse model matrix (constant for all polys)
   mat4x4 invModel= inverse( gl_ModelViewMatrix );

   // transform light and camera into object space (untransformed vertices)
   // (constant for all polys)
   vec3 osLightPos = vec3( invModel * lightPosition );
   vec3 osCameraPos = vec3( invModel * cameraPosition );

   // pass interpolated uv to fragment shader
   uv = gl_MultiTexCoord0.xy;

   // tangent space matrix
   // transforms vectors from the normalmap into object space
   vec3 x= gl_MultiTexCoord1.xyz; // tangent
   vec3 y= gl_MultiTexCoord2.xyz; // binormal
   vec3 z= gl_Normal;

   // inverse tangent space transforms vectors from objects space into normalmap space
   mat3 inverseTangentSpaceMatrix = transpose( mat3(x, y, z) );

   // direction from light to vertex
   lightDir = normalize(gl_Vertex.xyz - osLightPos);

   // direction vector from camera to vertex
   viewDir= normalize(gl_Vertex.xyz - osCameraPos);

   // transform both vectors from object space into normalmap space
   lightDir= inverseTangentSpaceMatrix * lightDir;
   viewDir= inverseTangentSpaceMatrix * viewDir;

   // transform vertex to camera space
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

And per pixel:
Code: [Select]
// textures
uniform sampler2D texturemap;
uniform sampler2D normalmap;

// interpolated vertex attributes
varying vec2 uv;             // texture coordinate
varying vec3 lightDir;       // light direction in normalmap space
varying vec3 viewDir;        // view direction in normalmap space


void main()
{
    // initial color
    vec4 col = vec4(0.0, 0.0, 0.0, 0.0);

    // constant diffuse color
    vec4 diffuseColor= vec4(2.0, 1.2, 1.2, 1.0);

    // re-normalized light direction
    vec3 nLightDir= normalize( lightDir );

    // extract normal from unsigned rgb texture
    vec3 n= texture2D(normalmap, uv).xyz * 2.0 - 1.0;

    // re-normalize normal (better fix normalmap in advance)
    n= normalize( vec3(-n.x, n.y, -n.z) );

    // texture color
    vec4 texColor1= texture2D(texturemap, uv);

    // diffuse lighting
    float intensity = dot(n, nLightDir);
    if (intensity > 0.0)
    {
       col += texColor1 * diffuseColor * intensity;
    }

    // reflection vector
    vec3 refl = viewDir - n * 2.0 * dot(n, viewDir);

    // specular
    float specular = dot(refl, nLightDir);

    if (specular > 0.8)  // 0.8^30 = 0.0
    {
       float s = pow(specular, 30.0);

       col += s;
    }

    gl_FragColor = col;
}

And the vertex data I feed into it is the following:
Code: [Select]
//        vertexposition   normal     tangent    binormal   uv
/* 0 */  -0.5,-0.5,-0.5,   0, 0,-1,   1, 0, 0,   0,-1, 0,   0,1,  // front face (-z)
/* 1 */  -0.5, 0.5,-0.5,   0, 0,-1,   1, 0, 0,   0,-1, 0,   0,0,
/* 2 */   0.5, 0.5,-0.5,   0, 0,-1,   1, 0, 0,   0,-1, 0,   1,0,
/* 3 */   0.5,-0.5,-0.5,   0, 0,-1,   1, 0, 0,   0,-1, 0,   1,1,
     
/* 4 */   0.5,-0.5, 0.5,   0, 0, 1,  -1, 0, 0,   0,-1, 0,   0,1,  // back face (+z)
/* 5 */   0.5, 0.5, 0.5,   0, 0, 1,  -1, 0, 0,   0,-1, 0,   0,0,
/* 6 */  -0.5, 0.5, 0.5,   0, 0, 1,  -1, 0, 0,   0,-1, 0,   1,0,
/* 7 */  -0.5,-0.5, 0.5,   0, 0, 1,  -1, 0, 0,   0,-1, 0,   1,1,
     
/* 8 */   0.5,-0.5,-0.5,   1, 0, 0,   0, 0, 1,   0,-1, 0,   0,1,  // right face (+x)
/* 9 */   0.5, 0.5,-0.5,   1, 0, 0,   0, 0, 1,   0,-1, 0,   0,0,
/*10 */   0.5, 0.5, 0.5,   1, 0, 0,   0, 0, 1,   0,-1, 0,   1,0,
/*11 */   0.5,-0.5, 0.5,   1, 0, 0,   0, 0, 1,   0,-1, 0,   1,1,
     
/*12 */  -0.5,-0.5, 0.5,  -1, 0, 0,   0, 0,-1,   0,-1, 0,   0,1,  // left face (-x)
/*13 */  -0.5, 0.5, 0.5,  -1, 0, 0,   0, 0,-1,   0,-1, 0,   0,0,
/*14 */  -0.5, 0.5,-0.5,  -1, 0, 0,   0, 0,-1,   0,-1, 0,   1,0,
/*15 */  -0.5,-0.5,-0.5,  -1, 0, 0,   0, 0,-1,   0,-1, 0,   1,1,
     
/*16 */  -0.5,-0.5, 0.5,   0,-1, 0,   1, 0, 0,   0, 0, 1,   0,1,  // top face (-y)
/*17 */  -0.5,-0.5,-0.5,   0,-1, 0,   1, 0, 0,   0, 0, 1,   0,0,
/*18 */   0.5,-0.5,-0.5,   0,-1, 0,   1, 0, 0,   0, 0, 1,   1,0,
/*20 */   0.5,-0.5, 0.5,   0,-1, 0,   1, 0, 0,   0, 0, 1,   1,1,
     
/*20 */  -0.5, 0.5,-0.5,   0, 1, 0,   1, 0, 0,   0, 0,-1,   0,1,  // bottom face (+y)
/*21 */  -0.5, 0.5, 0.5,   0, 1, 0,   1, 0, 0,   0, 0,-1,   0,0,
/*22 */   0.5, 0.5, 0.5,   0, 1, 0,   1, 0, 0,   0, 0,-1,   1,0,
/*23 */   0.5, 0.5,-0.5,   0, 1, 0,   1, 0, 0,   0, 0,-1,   1,1
(6 quads of a handsome unit cube)

"gl_ModelViewMatrix" spins the cube around it's center,
"gl_ProjectionMatrix" moves the cube 4 units to the back
« Last Edit: May 26, 2013 by hellfire »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #16 on: May 26, 2013 »
hellfire thats awsome mate!! k++,

thats by far the most clear and intuative example around!

i was still trying too fake my way through without matrix's but i dont think its going too work without after looking at your code but on the plus side i should be able too get by for now with just a model matrix.
just one question really as the rest is very clear,
Code: [Select]
   // inverse model matrix (constant for all polys)
   mat4x4 invModel= inverse( gl_ModelViewMatrix );

to emulate this in software, would this part be just a matrix that relates too the cubes rotation. but in gl too get the same you have too inv the modelveiwmatrix?
also for refrence could i trouble you for the exe. if possible mate?
« Last Edit: May 26, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: little 3d cube with bumpmapping.
« Reply #17 on: May 26, 2013 »
just one question really as the rest is very clear,
Code: [Select]
   // inverse model matrix (constant for all polys)
   mat4x4 invModel= inverse( gl_ModelViewMatrix );
to emulate this in software, would this part be just a matrix that relates too the cubes rotation.
but in gl too get the same you have too inv the modelveiwmatrix?
The modelview matrix transforms the mesh from object space (that's the coordinate system of the untransformed vertices) to world space (that's the position/orientation of the mesh in the 3d world without the camera orientation).
In this case it only contains the rotation of the cube:
Code: [Select]
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glRotatef(time*12.0, 1,0,0);
   glRotatef(time*9.0, 0,1,0);
The inverse of the modelview transforms the other way round: from world space back into object space.
Doing the calculations in objects space saves a couple of additional transformation because the tangent space vectors stay fixed and just need be calculated once and can be stored for each vertex.

As the modelview only contains a rotation, the inverse can be done by a simple transpose. This doesn't work if the matrix contains a translation- or scale-component, though.

Quote
also for refrence could i trouble you for the exe. if possible mate?
Sure.
« Last Edit: May 26, 2013 by hellfire »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #18 on: May 26, 2013 »
that looks stunning!

i think youve just helped me hugely, ive been failing too realize that the inv modelveiw matrix transfered things into object space, everytime i looked at it for some reason it always looked like it was transforming the other way from obj too world.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: little 3d cube with bumpmapping.
« Reply #19 on: May 27, 2013 »
its definitly some fudgy stuff i was doing with my light and object co-ords, i was trying too do it all in world space before.

i now have two sets of vertex's for each object one set holds the original vertex data and the other set holds rotated but unprojected vertex data, i now do everthing except rendering on these rotated but unprojected points. 

its made a massive difference it seems too respond nicely with the lights position now and mostly just nearest faces get lighting.

could i trouble you when you get some free time. for an exe of your code that just does interpolated lightdir as rgb? im pretty sure mines is correct now but would like too check it against a known working version, i would do it myself but its going too take me a little while too get a good glsl framework setup. currently just getting a feel for modern shaders.
Challenge Trophies Won: