📄 3ds.cpp
字号:
return vResult; // Return the resultant vector
}
// This divides a vector by a single number (scalar) and returns the result
CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler)
{
CVector3 vResult; // The variable to hold the resultant vector
vResult.x = vVector1.x / Scaler; // Divide Vector1's x value by the scaler
vResult.y = vVector1.y / Scaler; // Divide Vector1's y value by the scaler
vResult.z = vVector1.z / Scaler; // Divide Vector1's z value by the scaler
return vResult; // Return the resultant vector
}
// This returns the cross product between 2 vectors
CVector3 Cross(CVector3 vVector1, CVector3 vVector2)
{
CVector3 vCross; // The vector to hold the cross product
// Get the X value
vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));
// Get the Y value
vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));
// Get the Z value
vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x));
return vCross; // Return the cross product
}
// This returns the normal of a vector
CVector3 Normalize(CVector3 vNormal)
{
double Magnitude; // This holds the magitude
Magnitude = Mag(vNormal); // Get the magnitude
vNormal.x /= (float)Magnitude; // Divide the vector's X by the magnitude
vNormal.y /= (float)Magnitude; // Divide the vector's Y by the magnitude
vNormal.z /= (float)Magnitude; // Divide the vector's Z by the magnitude
return vNormal; // Return the normal
}
///////////////////////////////// COMPUTER NORMALS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function computes the normals and vertex normals of the objects
/////
///////////////////////////////// COMPUTER NORMALS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CLoad3DS::ComputeNormals(t3DModel *pModel)
{
CVector3 vVector1, vVector2, vNormal, vPoly[3];
// If there are no objects, we can skip this part
if(pModel->numOfObjects <= 0)
return;
// What are vertex normals? And how are they different from other normals?
// Well, if you find the normal to a triangle, you are finding a "Face Normal".
// If you give OpenGL a face normal for lighting, it will make your object look
// really flat and not very round. If we find the normal for each vertex, it makes
// the smooth lighting look. This also covers up blocky looking objects and they appear
// to have more polygons than they do. Basically, what you do is first
// calculate the face normals, then you take the average of all the normals around each
// vertex. It's just averaging. That way you get a better approximation for that vertex.
// Go through each of the objects to calculate their normals
for(int index = 0; index < pModel->numOfObjects; index++)
{
// Get the current object
t3DObject *pObject = &(pModel->pObject[index]);
// Here we allocate all the memory we need to calculate the normals
CVector3 *pNormals = new CVector3 [pObject->numOfFaces];
CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces];
pObject->pNormals = new CVector3 [pObject->numOfVerts];
// Go though all of the faces of this object
for(int i=0; i < pObject->numOfFaces; i++)
{
// To cut down LARGE code, we extract the 3 points of this face
vPoly[0] = pObject->pVerts[pObject->pFaces[i].vertIndex[0]];
vPoly[1] = pObject->pVerts[pObject->pFaces[i].vertIndex[1]];
vPoly[2] = pObject->pVerts[pObject->pFaces[i].vertIndex[2]];
// Now let's calculate the face normals (Get 2 vectors and find the cross product of those 2)
vVector1 = Vector(vPoly[0], vPoly[2]); // Get the vector of the polygon (we just need 2 sides for the normal)
vVector2 = Vector(vPoly[2], vPoly[1]); // Get a second vector of the polygon
vNormal = Cross(vVector1, vVector2); // Return the cross product of the 2 vectors (normalize vector, but not a unit vector)
pTempNormals[i] = vNormal; // Save the un-normalized normal for the vertex normals
vNormal = Normalize(vNormal); // Normalize the cross product to give us the polygons normal
pNormals[i] = vNormal; // Assign the normal to the list of normals
}
//////////////// Now Get The Vertex Normals /////////////////
CVector3 vSum = {0.0, 0.0, 0.0};
CVector3 vZero = vSum;
int shared=0;
for (i = 0; i < pObject->numOfVerts; i++) // Go through all of the vertices
{
for (int j = 0; j < pObject->numOfFaces; j++) // Go through all of the triangles
{ // Check if the vertex is shared by another face
if (pObject->pFaces[j].vertIndex[0] == i ||
pObject->pFaces[j].vertIndex[1] == i ||
pObject->pFaces[j].vertIndex[2] == i)
{
vSum = AddVector(vSum, pTempNormals[j]);// Add the un-normalized normal of the shared face
shared++; // Increase the number of shared triangles
}
}
// Get the normal by dividing the sum by the shared. We negate the shared so it has the normals pointing out.
pObject->pNormals[i] = DivideVectorByScaler(vSum, float(-shared));
// Normalize the normal for the final vertex normal
pObject->pNormals[i] = Normalize(pObject->pNormals[i]);
vSum = vZero; // Reset the sum
shared = 0; // Reset the shared
}
// Free our memory and start over on the next object
delete [] pTempNormals;
delete [] pNormals;
}
}
/////////////////////////////////////////////////////////////////////////////////
//
// * QUICK NOTES *
//
// This was a HUGE amount of knowledge and probably the largest tutorial yet!
// In the next tutorial we will show you how to load a text file format called .obj.
// This is the most common 3D file format that almost ANY 3D software will import.
//
// Once again I should point out that the coordinate system of OpenGL and 3DS Max are different.
// Since 3D Studio Max Models with the Z-Axis pointing up (strange and ugly I know! :),
// we need to flip the y values with the z values in our vertices. That way it
// will be normal, with Y pointing up. Also, because we swap the Y and Z we need to negate
// the Z to make it come out correctly. This is also explained and done in ReadVertices().
//
// CHUNKS: What is a chunk anyway?
//
// "The chunk ID is a unique code which identifies the type of data in this chunk
// and also may indicate the existence of subordinate chunks. The chunk length indicates
// the length of following data to be associated with this chunk. Note, this may
// contain more data than just this chunk. If the length of data is greater than that
// needed to fill in the information for the chunk, additional subordinate chunks are
// attached to this chunk immediately following any data needed for this chunk, and
// should be parsed out. These subordinate chunks may themselves contain subordinate chunks.
// Unfortunately, there is no indication of the length of data, which is owned by the current
// chunk, only the total length of data attached to the chunk, which means that the only way
// to parse out subordinate chunks is to know the exact format of the owning chunk. On the
// other hand, if a chunk is unknown, the parsing program can skip the entire chunk and
// subordinate chunks in one jump. " - Jeff Lewis (werewolf@worldgate.com)
//
// In a short amount of words, a chunk is defined this way:
// 2 bytes - Stores the chunk ID (OBJECT, MATERIAL, PRIMARY, etc...)
// 4 bytes - Stores the length of that chunk. That way you know when that
// chunk is done and there is a new chunk.
//
// So, to start reading the 3DS file, you read the first 2 bytes of it, then
// the length (using fread()). It should be the PRIMARY chunk, otherwise it isn't
// a .3DS file.
//
// Below is a list of the order that you will find the chunks and all the know chunks.
// If you go to www.wosit.org you can find a few documents on the 3DS file format.
// You can also take a look at the 3DS Format.rtf that is included with this tutorial.
//
//
//
// MAIN3DS (0x4D4D)
// |
// +--EDIT3DS (0x3D3D)
// | |
// | +--EDIT_MATERIAL (0xAFFF)
// | | |
// | | +--MAT_NAME01 (0xA000) (See mli Doc)
// | |
// | +--EDIT_CONFIG1 (0x0100)
// | +--EDIT_CONFIG2 (0x3E3D)
// | +--EDIT_VIEW_P1 (0x7012)
// | | |
// | | +--TOP (0x0001)
// | | +--BOTTOM (0x0002)
// | | +--LEFT (0x0003)
// | | +--RIGHT (0x0004)
// | | +--FRONT (0x0005)
// | | +--BACK (0x0006)
// | | +--USER (0x0007)
// | | +--CAMERA (0xFFFF)
// | | +--LIGHT (0x0009)
// | | +--DISABLED (0x0010)
// | | +--BOGUS (0x0011)
// | |
// | +--EDIT_VIEW_P2 (0x7011)
// | | |
// | | +--TOP (0x0001)
// | | +--BOTTOM (0x0002)
// | | +--LEFT (0x0003)
// | | +--RIGHT (0x0004)
// | | +--FRONT (0x0005)
// | | +--BACK (0x0006)
// | | +--USER (0x0007)
// | | +--CAMERA (0xFFFF)
// | | +--LIGHT (0x0009)
// | | +--DISABLED (0x0010)
// | | +--BOGUS (0x0011)
// | |
// | +--EDIT_VIEW_P3 (0x7020)
// | +--EDIT_VIEW1 (0x7001)
// | +--EDIT_BACKGR (0x1200)
// | +--EDIT_AMBIENT (0x2100)
// | +--EDIT_OBJECT (0x4000)
// | | |
// | | +--OBJ_TRIMESH (0x4100)
// | | | |
// | | | +--TRI_VERTEXL (0x4110)
// | | | +--TRI_VERTEXOPTIONS (0x4111)
// | | | +--TRI_MAPPINGCOORS (0x4140)
// | | | +--TRI_MAPPINGSTANDARD (0x4170)
// | | | +--TRI_FACEL1 (0x4120)
// | | | | |
// | | | | +--TRI_SMOOTH (0x4150)
// | | | | +--TRI_MATERIAL (0x4130)
// | | | |
// | | | +--TRI_LOCAL (0x4160)
// | | | +--TRI_VISIBLE (0x4165)
// | | |
// | | +--OBJ_LIGHT (0x4600)
// | | | |
// | | | +--LIT_OFF (0x4620)
// | | | +--LIT_SPOT (0x4610)
// | | | +--LIT_UNKNWN01 (0x465A)
// | | |
// | | +--OBJ_CAMERA (0x4700)
// | | | |
// | | | +--CAM_UNKNWN01 (0x4710)
// | | | +--CAM_UNKNWN02 (0x4720)
// | | |
// | | +--OBJ_UNKNWN01 (0x4710)
// | | +--OBJ_UNKNWN02 (0x4720)
// | |
// | +--EDIT_UNKNW01 (0x1100)
// | +--EDIT_UNKNW02 (0x1201)
// | +--EDIT_UNKNW03 (0x1300)
// | +--EDIT_UNKNW04 (0x1400)
// | +--EDIT_UNKNW05 (0x1420)
// | +--EDIT_UNKNW06 (0x1450)
// | +--EDIT_UNKNW07 (0x1500)
// | +--EDIT_UNKNW08 (0x2200)
// | +--EDIT_UNKNW09 (0x2201)
// | +--EDIT_UNKNW10 (0x2210)
// | +--EDIT_UNKNW11 (0x2300)
// | +--EDIT_UNKNW12 (0x2302)
// | +--EDIT_UNKNW13 (0x2000)
// | +--EDIT_UNKNW14 (0xAFFF)
// |
// +--KEYF3DS (0xB000)
// |
// +--KEYF_UNKNWN01 (0xB00A)
// +--............. (0x7001) ( viewport, same as editor )
// +--KEYF_FRAMES (0xB008)
// +--KEYF_UNKNWN02 (0xB009)
// +--KEYF_OBJDES (0xB002)
// |
// +--KEYF_OBJHIERARCH (0xB010)
// +--KEYF_OBJDUMMYNAME (0xB011)
// +--KEYF_OBJUNKNWN01 (0xB013)
// +--KEYF_OBJUNKNWN02 (0xB014)
// +--KEYF_OBJUNKNWN03 (0xB015)
// +--KEYF_OBJPIVOT (0xB020)
// +--KEYF_OBJUNKNWN04 (0xB021)
// +--KEYF_OBJUNKNWN05 (0xB022)
//
// Once you know how to read chunks, all you have to know is the ID you are looking for
// and what data is stored after that ID. You need to get the file format for that.
// I can give it to you if you want, or you can go to www.wosit.org for several versions.
// Because this is a proprietary format, it isn't a official document.
//
// I know there was a LOT of information blown over, but it is too much knowledge for
// one tutorial. In the animation tutorial that I eventually will get to, some of
// the things explained here will be explained in more detail. I do not claim that
// this is the best .3DS tutorial, or even a GOOD one :) But it is a good start, and there
// isn't much code out there that is simple when it comes to reading .3DS files.
// So far, this is the best I have seen. That is why I made it :)
//
// I would like to thank www.wosit.org and Terry Caton (tcaton@umr.edu) for his help on this.
//
// Let me know if this helps you out!
//
//
// Ben Humphrey (DigiBen)
// Game Programmer
// DigiBen@GameTutorials.com
// Co-Web Host of www.GameTutorials.com
//
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -