 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Wed Jun 07, 2006 9:10 pm Post subject: scaling meshes - keeping aspect ratio |
|
|
hey!
i'm trying to scale my models (with a program on my pc)
what do i do?
i grab all my data... find out MIN and MAX values of ALL coordinates
i calculate height with and depth of my meshes
take the maximum extension and keep it
i normalize the dimensions of the mesh to 1 and then scale it up to what was passed to my program...
so this is my code... but somehow it does not scale to eg... 20
some values are larger in my result...
| Code: |
minX = lmesh.verticesV3[0].x; // init...
minY = lmesh.verticesV3[0].y;
minZ = lmesh.verticesV3[0].z;
maxX = lmesh.verticesV3[0].x;
maxY = lmesh.verticesV3[0].y;
maxZ = lmesh.verticesV3[0].z;
for( unsigned long idx = 1; idx<lmesh.vertexCount; idx++ )
{
minX = min(minX, lmesh.verticesV3[idx].x);
minY = min(minY, lmesh.verticesV3[idx].y);
minZ = min(minZ, lmesh.verticesV3[idx].z);
maxX = max(maxX, lmesh.verticesV3[idx].x);
maxY = max(maxY, lmesh.verticesV3[idx].y);
maxZ = max(maxZ, lmesh.verticesV3[idx].z);
}
// resize values
sizeX = (maxX - minX)/2;
sizeY = (maxY - minY)/2;
sizeZ = (maxZ - minZ)/2;
float fSizemax=max(max(sizeX,sizeY),sizeZ);
printf("fSizemax = %f\n" ,fSizemax);
printf("sizeX = %f\n" ,sizeX);
printf("sizeY = %f\n" ,sizeY);
printf("sizeZ = %f\n" ,sizeZ);
// write new values
for (unsigned int i=0;i<lmesh.vertexCount;i++)
{
lmesh.verticesV3[i].x /= fSizemax;
lmesh.verticesV3[i].x *= scale;
lmesh.verticesV3[i].y /= fSizemax;
lmesh.verticesV3[i].y *= scale;
lmesh.verticesV3[i].z /= fSizemax;
lmesh.verticesV3[i].z *= scale;
}
for( unsigned long i = 0; i<lmesh.vertexCount; i++ )
{
printf("%f %f %f 0x%X %f %f %f\n", lmesh.verticesV3[i].nx, lmesh.verticesV3[i].ny, lmesh.verticesV3[i].nz,
lmesh.verticesV3[i].color, lmesh.verticesV3[i].x, lmesh.verticesV3[i].y, lmesh.verticesV3[i].z);
}
break; |
would be awesome if someone finds my fault
this seems to be one of my last 2 bugs! (centering works on 9/10 meshes)
thanks in advance
lumo
PS: on 13th June i have to publish my work in university, do an presentation and so on; when this is done sourcecode will go public! so guys, possibly one of you will use my code soon ;) _________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Wed Jun 07, 2006 10:25 pm Post subject: |
|
|
Why are you dividing by 2? That doesn't seem right...
| Code: |
// resize values
sizeX = (maxX - minX)/2;
sizeY = (maxY - minY)/2;
sizeZ = (maxZ - minZ)/2;
|
if minX is 0 and maxX is 50, then you'll get sizeX = 25. If minX = -50 and maxX is 50 then you'll get sizeX = 50, but it's really 100. |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Wed Jun 07, 2006 10:29 pm Post subject: |
|
|
So... what you want to do is to use the 16bit coords, right? There are several ways you could do it, but I thinkg the easiest is not to use the same pivot as in the original mesh. If you want to get the most precision out of the 16bits, you have to translate the mesh too.
Something like this should work fine:
| Code: | for(each vertex)
{
meshMin = min(meshMin, v[i]);
meshMax = max(meshMax, v[i]);
}
vec3 scale;
scale.x = max(abs(meshMin.x), meshMax.x);
scale.y = max(abs(meshMin.y), meshMax.y);
scale.z = max(abs(meshMin.z), meshMax.z);
for(each vertex)
{
p.x = v[i].x / scale.x;
p.y = v[i].y / scale.y;
p.z = v[i].z / scale.z;
out[i] = p;
}
|
That code will fit the whole mesh into unit cube, that is the vertices will be in range -1..1. IIRC the 16bit coords have that range. You need to store the scale along with the mesh and then create scale matrix later.
If the nonuniform scaling fucks up the lighting--as I assume--you can use the max scale value instead.
If you wan to use the full range of the values, then you need to also offset. The followin bit of code should do that. It assumes you know the min/max already.
| Code: | center = (meshMin + meshMax)/2;
extend = (meshMax - meshMin)/2;
scale = max(max(extend.x, extend.y), extend.z);
for(each vertex)
out[i] = (v[i] - center) / scale;
|
In thing case you store the center and scale. To convert back you should use:
| Code: | | p = v[i] * scale + center; |
Hope it helps. |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Wed Jun 07, 2006 11:35 pm Post subject: |
|
|
@memon
you are absolutely right!
what i should have noticed is that i center the mesh before i scale it!
so its already done before i scale it
@SSpeare good point :) will test without div 2
EDIT: div2 is cause... + and - values!
so i finally think the point is here:
| Code: | | float fSizemax=max(max(sizeX,sizeY),sizeZ)*2; |
_________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Thu Jun 08, 2006 11:43 pm Post subject: |
|
|
When you say center, you mean that you use the center of the same bounding box as you calculate in the provided code? If not, then your scale should be like this:
| Code: | vec3 ext;
ext.x = max(abs(meshMin.x), meshMax.x);
ext.y = max(abs(meshMin.y), meshMax.y);
ext.z = max(abs(meshMin.z), meshMax.z);
float scale = max(ext.x, ext.y, ext.z); |
If your center is excatly at the center of the bounding box, then meshMin and meshMax should be symmetrical (meshMin = -meshMax) and you can use the mesh max of all components of mesh max.
One thing to note is that there are two common ways to define bounding boxes. One is to specify the min and max of that bbox, and another is to specify the center and extend of the box. The mapping between the two is like this:
min = center - extend
max = center + extend
and vice versa
center = (max + min)/2
extend = (max - min)/2
So if you want to map any point in a bounding box to "normalized" -1..1 range, then the formula is:
v' = (v - center) / extend
or if you have the min/max bounding box
v' = -1 + 2 * (v - min) / (max - min) |
|
| Back to top |
|
 |
white rabbit
Joined: 06 Jul 2005 Posts: 60
|
Posted: Sat Jun 10, 2006 4:36 am Post subject: |
|
|
Hmm, I hadn't thought about scaling the model to make it fit the boundaries better...
Has anybody tried - what are the issues with the normals?
... How can you deal with this? .. capture the normals pre-scale I guess, and somehow stick them back in with the new position data... sounds hard... |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Sat Jun 10, 2006 4:46 am Post subject: |
|
|
i had no problems with my normals...
i calculated em when converting the mesh...
note: no probs with normals when keeping aspect ratio nor when you discard it. _________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|