I found out I was scaling them wrong and my projection was wrong.
I change the code I posted above.
Anyway...
to scale the trees..
Get the length of rows 0 1 and 2. These are the scales
"^" means power or you can do "(M(0) * m(0))" for example.
sx = Sqrt((m(0) ^ 2) + (m(1) ^ 2) + (m(3) ^ 2))
sy = Sqrt((m(4) ^ 2) + (m(5) ^ 2) + (m(6) ^ 2))
sz = Sqrt((m(8) ^ 2) + (m(9) ^ 2) + (m(10) ^ 2))
And average them.. Multiplying by 1/3 (its faster then division)
tree_scale = (sx + sy + sz) * 0.3333
There are 6 vertices in each leaf. These create 2 triangles.
I look up the corners using the 2 arrays:
Dim vx() As Single = {-0.5!, 0.5!, 0.5!, -0.5!}
Dim vy() As Single = {0.5!, 0.5!, -0.5!, -0.5!}
...
Here is my code. I'm still trying to get the lighting correct.
Code: Select all
Dim m = Trees.matrix(tree).matrix
Dim sx = Sqrt((m(0) ^ 2) + (m(1) ^ 2) + (m(3) ^ 2))
Dim sy = Sqrt((m(4) ^ 2) + (m(5) ^ 2) + (m(6) ^ 2))
Dim sz = Sqrt((m(8) ^ 2) + (m(9) ^ 2) + (m(10) ^ 2))
Dim tree_scale As Single = (sx + sy + sz) * 0.3333
Dim vx() As Single = {-0.5!, 0.5!, 0.5!, -0.5!}
Dim vy() As Single = {0.5!, 0.5!, -0.5!, -0.5!}
For i = 0 To tree_data.l_indices.Length - 3
'struct LeafVertex
'
'00 00 Vec3 position;
'12 03 Vec3 normal;
'24 06 Single texCoords(2)
'32 08 Single windInfo(2)
'40 10 Single rotInfo(2)
'48 12 Single geomInfo(2)
'56 14 Single extraInfo(3)
'68 17 Single pivotInfo(2)
'76 19 Vec3 Tangent; ' 3 floats
p.x = -tree_data.l_vert((tree_data.l_indices(i) * 22) + 0)
p.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 1)
p.z = tree_data.l_vert((tree_data.l_indices(i) * 22) + 2)
norm.x = tree_data.l_vert((tree_data.l_indices(i) * 22) + 3)
norm.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 4)
norm.z = tree_data.l_vert((tree_data.l_indices(i) * 22) + 5)
uv.x = tree_data.l_vert((tree_data.l_indices(i) * 22) + 6)
uv.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 7)
rot.x = tree_data.l_vert((tree_data.l_indices(i) * 22) + 17)
rot.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 18)
vn = tree_data.l_vert((tree_data.l_indices(i) * 22) + 15) ' corner index
vn2 = tree_data.l_vert((tree_data.l_indices(i) * 22) + 16)
If vn2 <> 1.0 Then
Stop ' has NEVER been hit. It is always 1.0
End If
sizey = tree_data.l_vert((tree_data.l_indices(i) * 22) + 12)
sizex = tree_data.l_vert((tree_data.l_indices(i) * 22) + 13)
pivot.x = tree_data.l_vert((tree_data.l_indices(i) * 22) + 10)
pivot.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 11)
'not sure 100% sure.. This might also be the BiNormal
tangent.x = tree_data.l_vert((tree_data.l_indices(i) * 22) + 19)
tangent.y = tree_data.l_vert((tree_data.l_indices(i) * 22) + 20)
tangent.z = tree_data.l_vert((tree_data.l_indices(i) * 22) + 21)
'debug string
sb.Append(rot.x.ToString("0.000000") + " " + rot.y.ToString("0.000000") + vbCrLf)
If vn < 4 Then 'less than 4 means this is the special case
If cnt > 5 Then
cnt = 0
'Stop ' debug stop at end of each set of 6
End If
idx.x = vx(vn)
idx.y = vy(vn)
Dim corner As vect3
corner.x = vx(vn) '- pivot.x
corner.y = vy(vn) '+ pivot.y
corner.x *= tree_scale
corner.y *= tree_scale
corner.x *= sizex
corner.y *= sizex
Gl.glMultiTexCoord3f(0, -uv.x, uv.y, 0.0)
Gl.glMultiTexCoord3f(1, corner.x, corner.y, 0.0)
Gl.glMultiTexCoord3f(2, tangent.x, tangent.y, tangent.z)
Gl.glNormal3f(norm.x, norm.y, norm.z)
Gl.glVertex3f(p.x, p.y, p.z)
cnt += 1
Else
' just use these directly.
lx = p.x
ly = p.y
lz = p.z
Gl.glMultiTexCoord3f(0, -uv.x, uv.y, 0.0)
Gl.glMultiTexCoord3f(1, 0.0, 0.0, 0.0)
Gl.glMultiTexCoord3f(2, tangent.x, tangent.y, tangent.z)
Gl.glNormal3f(norm.x, norm.y, norm.z)
Gl.glVertex3f(p.x, p.y, p.z)
End If
Next
I posted this code as part of that zip but this section is updated.
I'm not sure about the pivot... I don't know how to use it.
I send the center point as the vertex and the corner as a UV coordinate.
In my shader program I do this at the start of the vertex stage.
Code: Select all
texCoord.xy = gl_MultiTexCoord0.st;
vec4 p = gl_ModelViewMatrix * gl_Vertex;
p.xyz += gl_MultiTexCoord1.xyz;
p = inverse(gl_ModelViewMatrix) * p;
gl_Position = gl_ModelViewProjectionMatrix * p;
This gets my corner on the screen at the correct location.
Im not sure it you can add custom shaders to your GL wrapper.
If so.. I might be able to help create one once I get my shader lighting correctly.
I am really not sure about the tangent.. It could be a BiNormal.