Gå til innhold

.obj file loader.


Anbefalte innlegg

Skrevet

hei.

 

har hentet ned en .obj file loader fra gametutorials.com

og når jeg skal kompilere får jeg en del feil.

 

obj.h

#ifndef _OBJ_H
#define _OBJ_H

#include <iostream>
#include <stdlib.h>
#include "main.h"
#include "vector.h"


CVector vSum={0.0, 0.0, 0.0};

struct tFace
{
int vertIndex[3];  	// indicies for the verts that make up this triangle
int coordIndex[3];  	// indicies for the tex coords to texture this face
};

// This holds the information for a material.  It may be a texture map of a color.
// Some of these are not used, but I left them because you will want to eventually
// read in the UV tile ratio and the UV tile offset for some models.
struct tMaterialInfo
{
char  strName[255];  	// The texture name
char  strFile[255];  	// The texture file name (If this is set it's a texture map)
BYTE  color[3];    // The color of the object (R, G, B)
int   texureId;    // the texture ID
float uTile;    // u tiling of texture  (Currently not used)
float vTile;    // v tiling of texture	(Currently not used)
float uOffset;       // u offset of texture	(Currently not used)
float vOffset;    // v offset of texture	(Currently not used)
};

// This holds all the information for our model/scene. 
// You should eventually turn into a robust class that 
// has loading/drawing/querying functions like:
// LoadModel(...); DrawObject(...); DrawModel(...); DestroyModel(...);
struct t3DObject 
{
int  numOfVerts;  	// The number of verts in the model
int  numOfFaces;  	// The number of faces in the model
int  numTexVertex;  	// The number of texture coordinates
int  materialID;  	// The texture ID to use, which is the index into our texture array
bool bHasTexture;  	// This is TRUE if there is a texture map for this object
char strName[255];  	// The name of the object
CVector  *pVerts;  	// The object's vertices
CVector  *pNormals;  // The object's normals
CVector  *pTexVerts;  // The texture's UV coordinates
tFace *pFaces;    // The faces information of the object
};

// This holds our model information.  This should also turn into a robust class.
// We use STL's (Standard Template Library) vector class to ease our link list burdens. :)
struct t3DModel 
{
int numOfObjects;    	// The number of objects in the model
int numOfMaterials;    	// The number of materials for the model
vector<tMaterialInfo> pMaterials;	// The list of material information (Textures and colors)
vector<t3DObject> pObject;  	// The object list for our model
};

class Bulding {

public:

bool ImportObj(t3DModel *pModel, char *strFileName);

// This is the main loading loop that gets called in ImportObj()
void ReadObjFile(t3DModel *pModel);

// This is called in ReadObjFile() if we find a line starting with 'v'
void ReadVertexInfo();

// This is called in ReadObjFile() if we find a line starting with 'f'
void ReadFaceInfo();

// This is called when we are finished loading in the face information
void FillInObjectInfo(t3DModel *pModel);

// This isn't necessary for the loader, but it's nice to have vertex normals for lighting
void ComputeNormals(t3DModel *pModel);

// Since .obj files don't have texture/material names, we create a function to set them manually.
// The materialID is the index into the pMaterial array for our model.
void SetObjectMaterial(t3DModel *pModel, int whichObject, int materialID);

// To make it easier to assign a material to a .obj object we create a funtion to do so.
// We can pass in the model, the material name, the texture file name and the RGB colors.
// If we just want a color, pass in NULL for strFile.
void AddMaterial(t3DModel *pModel, char *strName, char *strFile, 
     int r = 255,      int g = 255,   int b = 255);

private:

// This holds our file pointer for reading in the file
FILE *m_FilePointer;

// This is an STL (Standard Template Library) vector that holds a list of vertices
vector<CVector>  m_pVertices;

// This is an STL (Standard Template Library) vector that holds a list of faces
vector<tFace> m_pFaces;

// This is an STL (Standard Template Library) vector that holds a list of UV Coordinates
vector<CVector>  m_pTextureCoords;

// This tells us if the current object has texture coordinates
bool m_bObjectHasUV;

// This tells us if we just read in face data so we can keep track of multiple objects
bool m_bJustReadAFace;
};


#endif

 

obj.cpp

 

#include "Obj.h"

#define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z))

// This calculates a vector between 2 points and returns the result
CVector Vector(CVector vPoint1, CVector vPoint2)
{
CVector vVector;      	// The variable to hold the resultant vector

vVector.x = vPoint1.x - vPoint2.x;  	// Subtract point1 and point2 x's
vVector.y = vPoint1.y - vPoint2.y;  	// Subtract point1 and point2 y's
vVector.z = vPoint1.z - vPoint2.z;  	// Subtract point1 and point2 z's

return vVector;        // Return the resultant vector
}


CVector Cross(CVector vVector1, CVector vVector2)
{
   CVector vCross;        // The vector to hold the cross product
       
   vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));
       
   vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));
    
    vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x));
    
    return vCross;        // Return the cross product
}

CVector Normalize(CVector vNormal)
{
double Magnitude;      	// This holds the magnitude  	

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
}

bool Bulding::ImportObj(t3DModel *pModel, char *strFileName)
{
char strMessage[255] = {0};    // This will be used for error messages

// Make sure we have a valid model and file name
if(!pModel || !strFileName) return false;

// Here we open the desired file for read only and return the file pointer
m_FilePointer = fopen(strFileName, "r");

// Check to make sure we have a valid file pointer
if(!m_FilePointer) {
 MessageBox(NULL, "could not open bulding file", "Error", MB_OK);
 return false;
}

// Now that we have a valid file and it's open, let's read in the info!
ReadObjFile(pModel);

// Now that we have the file read in, let's compute the vertex normals for lighting
ComputeNormals(pModel);

// Close the .obj file that we opened
fclose(m_FilePointer);

// Return a success!
return true;
}


///////////////////////////////// READ OBJ FILE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function is the main loop for reading in the .obj file
/////
///////////////////////////////// READ OBJ FILE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::ReadObjFile(t3DModel *pModel)
{
char strLine[255]  = {0};
char ch    	= 0;

while(!feof(m_FilePointer))
{
 float x = 0.0f, y = 0.0f, z = 0.0f;

 // Get the beginning character of the current line in the .obj file
 ch = fgetc(m_FilePointer);

 switch(ch)
 {
 case 'v':      // Check if we just read in a 'v' (Could be a vertice/normal/textureCoord)
 	
 	// If we just read in a face line, then we have gone to another object,
 	// so we need to save the last object's data before moving onto the next one.
 	if(m_bJustReadAFace) {
   // Save the last object's info into our model structure
   FillInObjectInfo(pModel);
 	}

 	// Decipher this line to see if it's a vertex ("v"), normal ("vn"), or UV coordinate ("vt")
 	ReadVertexInfo();
 	break;

 case 'f':      // Check if we just read in a face header ('f')
 	
 	// If we get here we then we need to read in the face information.
 	// The face line holds the vertex indices into our vertex array, or if
 	// the object has texture coordinates then it holds those too.
 	ReadFaceInfo();
 	break;

 case '\n':

 	// If we read in a newline character, we've encountered a blank line in
 	// the .obj file.  We don't want to do the default case and skip another
 	// line, so we just break and do nothing.
 	break;

 default:

 	// If we get here then we don't care about the line being read, so read past it.
 	fgets(strLine, 100, m_FilePointer);
 	break;
 }
}

// Now that we are done reading in the file, we have need to save the last object read.
FillInObjectInfo(pModel);
}


///////////////////////////////// READ VERTEX INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function reads in the vertex information ("v" vertex : "vt" UVCoord)
/////
///////////////////////////////// READ VERTEX INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::ReadVertexInfo()
{
CVector vNewVertex  = 0;
CVector vNewTexCoord	= 0;
char strLine[255]  = {0};
char ch=0;

// Read the next character in the file to see if it's a vertice/normal/UVCoord
ch = fgetc(m_FilePointer);

if(ch == ' ')    // If we get a space it must have been a vertex ("v")
{
 // Here we read in a vertice.  The format is "v x y z"
 fscanf(m_FilePointer, "%f %f %f", &vNewVertex.x, &vNewVertex.y, &vNewVertex.z);

 // Read the rest of the line so the file pointer returns to the next line.
 fgets(strLine, 100, m_FilePointer);

 // Add a new vertice to our list
 m_pVertices.push_back(vNewVertex);
}
else if(ch == 't')  	// If we get a 't' then it must be a texture coordinate ("vt")
{
 // Here we read in a texture coordinate.  The format is "vt u v"
 fscanf(m_FilePointer, "%f %f", &vNewTexCoord.x, &vNewTexCoord.y);

 // Read the rest of the line so the file pointer returns to the next line.
 fgets(strLine, 100, m_FilePointer);

 // Add a new texture coordinate to our list
 m_pTextureCoords.push_back(vNewTexCoord);

 // Set the flag that tells us this object has texture coordinates.
 // Now we know that the face information will list the vertice AND UV index.
 // For example, ("f 1 3 2" verses "f 1/1 2/2 3/3")
 m_bObjectHasUV = true;
}
else      // Otherwise it's probably a normal so we don't care ("vn")
{
 // We calculate our own normals at the end so we read past them.
 fgets(strLine, 100, m_FilePointer);
}
}


///////////////////////////////// READ FACE INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function reads in the face information ("f")
/////
///////////////////////////////// READ FACE INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::ReadFaceInfo()
{
tFace newFace  	= {0};
char strLine[255]  = {0};

// This function reads in the face information of the object.
// A face is a polygon (a triangle in this case) that has information about it.
// It has the 3D points that make up the polygon and may also have texture coordinates.
// When reading in an .obj, objects don't have to have UV texture coordinates so we
// need to read in the face information differently in that case.  If the object does have
// UV coordinates, then the format will look like this:
// "f vertexIndex1/coordIndex1 vertexIndex2/coordIndex2 vertexIndex3/coordIndex3"
// otherwise the format will look like this:"f vertexIndex1 vertexIndex2 vertexIndex3"
// The index values index into our vertice and texture coordinate arrays.  More explained in RenderScene().
// *Note* Make sure if you cut this code out for your own use you minus 1 from the indices.
// This is because arrays are zero based and the .obj indices start at 1.  Look at FillInObjectInfo().

// Check if this object has texture coordinates before reading in the values
if(m_bObjectHasUV )
{
 // Here we read in the object's vertex and texture coordinate indices.
 // Here is the format: "f vertexIndex1/coordIndex1 vertexIndex2/coordIndex2 vertexIndex3/coordIndex3"
 fscanf(m_FilePointer, "%d/%d %d/%d %d/%d", &newFace.vertIndex[0], &newFace.coordIndex[0],
              &newFace.vertIndex[1], &newFace.coordIndex[1],
              &newFace.vertIndex[2], &newFace.coordIndex[2]);    
}
else          // The object does NOT have texture coordinates
{
 // Here we read in just the object's vertex indices.
 // Here is the format: "f vertexIndex1 vertexIndex2 vertexIndex3"
 fscanf(m_FilePointer, "%d %d %d", &newFace.vertIndex[0],
           &newFace.vertIndex[1],
           &newFace.vertIndex[2]);    
}

// Read the rest of the line so the file pointer returns to the next line.
fgets(strLine, 100, m_FilePointer);
   
// Add the new face to our face list
m_pFaces.push_back(newFace);

// We set this flag to TRUE so we know that we just read in some face information.
// Since face information is the last thing we read in for an object we will check
// this flag when we read in a vertice.  If it's true, then we just finished reading
// in an object and we need to save off the information before going to the next object.
// Since there is no standard header for objects in a .obj file we do it this way.
m_bJustReadAFace = true;
}


///////////////////////////////// FILL IN OBJECT INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function is called after an object is read in to fill in the model structure
/////
///////////////////////////////// FILL IN OBJECT INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::FillInObjectInfo(t3DModel *pModel)
{
t3DObject newObject = {0};
int textureOffset = 0, vertexOffset = 0;
int i = 0;

// If we get here then we just finished reading in an object
// and need to increase the object count.
pModel->numOfObjects++;

// Add a new object to the list of objects in our model
pModel->pObject.push_back(newObject);

// Get a pointer to the current object so our code is easier to read
t3DObject *pObject = &(pModel->pObject[pModel->numOfObjects - 1]);

// Now that we have our list's full of information, we can get the size
// of these lists by calling size() from our vectors.  That is one of the
// wonderful things that the Standard Template Library offers us.  Now you
// never need to write a link list or constantly call malloc()/new.

// Here we get the number of faces, vertices and texture coordinates
pObject->numOfFaces   = m_pFaces.size();
pObject->numOfVerts   = m_pVertices.size();
pObject->numTexVertex = m_pTextureCoords.size();

// If we read in any faces for this object (required)
if(pObject->numOfFaces) {

 // Allocate enough memory to store all the faces in our object
 pObject->pFaces = new tFace [pObject->numOfFaces];
}

// If we read in any vertices for this object (required)
if(pObject->numOfVerts) {

 // Allocate enough memory to store all the vertices in our object
 pObject->pVerts = new CVector [pObject->numOfVerts];
}	

// If we read in any texture coordinates for this object (optional)
if(pObject->numTexVertex) {
 pObject->pTexVerts = new CVector [pObject->numTexVertex];
 pObject->bHasTexture = true;
}	

// Go through all of the faces in the object
for(i = 0; i < pObject->numOfFaces; i++)
{
 // Copy the current face from the temporary list to our Model list
 pObject->pFaces[i] = m_pFaces[i];

 // Because of the problem with .obj files not being very object friendly,
 // if a new object is found in the file, the face and texture indices start
 // from the last index that was used in the last object.  That means that if
 // the last one was 8, it would then go to 9 for the next object.  We need to
 // bring that back down to 1, so we just create an offset that we subtract from
 // the vertex and UV indices.

 // Check if this is the first face of the object
 if(i == 0) 
 {
 	// If the first index is NOT 1, then we must be past the first object
 	if(pObject->pFaces[0].vertIndex[0] != 1) {

   // To create the offset we take the current starting point and then minus 1.
   // Lets say the last object ended at 8.  Well we would then have 9 here.
   // We want to then subtract 8 from the 9 to get back to 1.
   vertexOffset = pObject->pFaces[0].vertIndex[0] - 1;

   // The same goes for texture coordinates, if we have them do the same
   if(pObject->numTexVertex > 0) {

   	// Take the current index and minus 1 from it
   	textureOffset = pObject->pFaces[0].coordIndex[0] - 1;
   }
 	}    	
 }

 // Because the face indices start at 1, we need to minus 1 from them due
 // to arrays being zero based.  This is VERY important!
 for(int j = 0; j < 3; j++)
 {
 	// For each index, minus 1 to conform with zero based arrays.
 	// We also need to add the vertex and texture offsets to subtract
 	// the total amount necessary for this to work.  The first object
 	// will have a offset of 0 for both since it starts at 1.
 	pObject->pFaces[i].vertIndex[j]  -= 1 + vertexOffset;
 	pObject->pFaces[i].coordIndex[j] -= 1 + textureOffset;
 }
}

// Go through all the vertices in the object
for(i = 0; i < pObject->numOfVerts; i++)
{
 // Copy the current vertice from the temporary list to our Model list
 pObject->pVerts[i] = m_pVertices[i];
}

// Go through all of the texture coordinates in the object (if any)
for(i = 0; i < pObject->numTexVertex; i++)
{
 // Copy the current UV coordinate from the temporary list to our Model list
 pObject->pTexVerts[i] = m_pTextureCoords[i];
}

// Since .OBJ files don't have materials, we set the material ID to -1.
// We need to manually give it a material using AddMaterial().
pObject->materialID = -1;

// Now that we have all the information from out list's, we need to clear them
// so we can be ready for the next object that we read in.
m_pVertices.clear();
m_pFaces.clear();
m_pTextureCoords.clear();

// Reset these booleans to be prepared for the next object
m_bObjectHasUV   = false;
m_bJustReadAFace = false;
}


///////////////////////////////// SET OBJECT MATERIAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function assigns a material to a specific object in our array of objects
/////
///////////////////////////////// SET OBJECT MATERIAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::SetObjectMaterial(t3DModel *pModel, int whichObject, int materialID)
{
// Make sure we have a valid model or else quit the function
if(!pModel) return;

// Make sure we don't index over the array of objects
if(whichObject >= pModel->numOfObjects) return;

// Here we set the material ID assigned to this object
pModel->pObject[whichObject].materialID = materialID;
}


///////////////////////////////// ADD MATERIAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function adds a material to our model manually since .obj has no such info
/////
///////////////////////////////// ADD MATERIAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void Bulding::AddMaterial(t3DModel *pModel, char *strName, char *strFile, 
       	int r,     int g,   int b)
{
tMaterialInfo newMaterial = {0};

// Set the RGB value for this material [0 - RED  1 - GREEN	2 - BLUE]
newMaterial.color[0] = r; newMaterial.color[1] = g; newMaterial.color[2] = b;

// If we have a file name passed in, let's copy it to our material structure
if(strFile) {
 strcpy(newMaterial.strFile, strFile);
}

// If we have a material name passed in, let's copy it to our material structure
if(strName) {
 strcpy(newMaterial.strName, strName);
}

// Now we add this material to model's list.  Once again we use the incredibly
// helpfull STL vector functions for allocating dynamic memory.
pModel->pMaterials.push_back(newMaterial);

// Increase the material count
pModel->numOfMaterials++;
}


void Bulding::ComputeNormals(t3DModel *pModel)
{
CVector vVector, vNormal, vPoly[3];

// If there are no objects, we can skip this part
if(pModel->numOfObjects <= 0)
 return;

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
 CVector *pNormals  = new CVector [pObject->numOfFaces];
 CVector *pTempNormals	= new CVector [pObject->numOfFaces];
 pObject->pNormals  = new CVector [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)

 	vVector = Vector(vPoly[0], vPoly[2]);  // Get the vector of the polygon (we just need 2 sides for the normal)
 	vVector = Vector(vPoly[2], vPoly[1]);  // Get a second vector of the polygon

 	vNormal  = Cross(vVector, vVector);  // 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 /////////////////
 CVector vZero = vSum;
 int shared=0;

 for (int 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 = 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] = 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;
}
}

 

ser dere noen feil her.

har rettet opp endel men fortsatt noen feil jeg ikke skjønner.

 

10 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.h `vSum' must be initialized by constructor, not by `{...}' 

57 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.h 'vector' is used as a type, but is not defined as a type. 

58 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.h 'vector' is used as a type, but is not defined as a type. 
.....

171 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.cpp `m_pTextureCoords' undeclared (first use this function) 

231 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.cpp `m_pFaces' undeclared (first use this function) 

259 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.cpp `struct t3DModel' has no member named `pObject' 

 

håper på svar.

 

mvh. Fredrik.

Videoannonse
Annonse
Skrevet

Prøv denne: http://www.gametutorials.com/download/Open...jLoader_OGL.zip

 

Er vel den samme som du bruker, men i litt mer oppegående form..?

 

Men feilene du får ser ganske enkle ut:

 

vector feilene du får i obj.h kommer ganske enkelt av at du ikke har inkludert <vector>. Legg med andre ord til:

 

#include <vector>

 

.. i starten av obj.h. I tillegg ligger vector i namespace std, så du må prefixe (supernorsk utrykk) vector med "std::", slik:

 

std::vector<CVector>

 

 

m_pTextureCoords og m_pFaces kommer sannynligvis som direkte følge av vector feilene. t3DModel-feilen ser også ut til å komme av mangel på "std::" prefix på vector i t3DModel-klassen.

 

Da er det vel bare den første feilen igjen. Den er jo ikke helt sikker på ut fra det du viser, men du kan jo prøve:

 

CVector vSum(0.0, 0.0, 0.0);

Skrevet (endret)

takk skal prøve dette. noe så enkelt a.

er det vector.h du mener for feilen forsvinner når jeg bruker det. men den første skjønner jeg ikke. hva skjer feil.

Endret av Fredrik90
Skrevet

hei.......... :)

 

nå er det noen som vet den første? den med konstruktor.

 

og nå har kompilatoren begynnt og kødde med dette her også.

struct tFace
{
int vertIndex[3];   // indicies for the verts that make up this triangle
int coordIndex[3];   // indicies for the tex coords to texture this face
};

har ikke rørt den delen siden sist og da var det ingen feil på den?

help I`m lost!!!!!!!! :(

her er feilmeldingen.

 

mvh. Fredrik :cool:

Skrevet (endret)
det er ganske greit å bare skrive "using namespace std;" med en gang også.

using namespace i global scope i header filer er fy fy. "using std::vector" ville vært bedre, men er likevel ikke veldig bra så lenge det ikke er i et eget namespace.

Endret av kjetil7
Skrevet
hei.......... :)

 

nå er det noen som vet den første? den med konstruktor.

 

og nå har kompilatoren begynnt og kødde med dette her også.

struct tFace
{
int vertIndex[3];   // indicies for the verts that make up this triangle
int coordIndex[3];   // indicies for the tex coords to texture this face
};

har ikke rørt den delen siden sist og da var det ingen feil på den?

help I`m lost!!!!!!!! :(

her er feilmeldingen.

 

mvh. Fredrik :cool:

Ja, hva er feilmeldingen?....

 

Kan du liste koden til CVector kanskje?

Skrevet

her er CVector koden. men den kompilerer fint og blir brukt i andre deler av programet uten problemer.

#ifndef __VECTOR_H
#define __VECTOR_H

#include <math.h>

#define PI  (3.14159265359f)
#define DEG2RAD(a)	(PI/180*(a))
#define RAD2DEG(a)	(180/PI*(a))

typedef float scalar_t;

class CVector
{
public:
union
{
 struct
 {	
 	scalar_t x;
 	scalar_t y;
 	scalar_t z;    // x,y,z coordinates
 };
 scalar_t v[3];
};

public:
    CVector(scalar_t a = 0, scalar_t b = 0, scalar_t c = 0) : x(a), y(b), z(c) {}
    CVector(const CVector &vec) : x(vec.x), y(vec.y), z(vec.z) {}

// vector index
scalar_t &operator[](const long idx)
{
 return *((&x)+idx);
}

    // vector assignment
    const CVector &operator=(const CVector &vec)
    {
         x = vec.x;
         y = vec.y;
         z = vec.z;

         return *this;
    }

    // vecector equality
    const bool operator==(const CVector &vec) const
    {
         return ((x == vec.x) && (y == vec.y) && (z == vec.z));
    }

    // vecector inequality
    const bool operator!=(const CVector &vec) const
    {
         return !(*this == vec);
    }

    // vector add
    const CVector operator+(const CVector &vec) const
    {
         return CVector(x + vec.x, y + vec.y, z + vec.z);
    }

    // vector add (opposite of negation)
    const CVector operator+() const
    {    
         return CVector(*this);
    }

    // vector increment
    const CVector& operator+=(const CVector& vec)
    {    x += vec.x;
         y += vec.y;
         z += vec.z;
         return *this;
    }

    // vector subtraction
    const CVector operator-(const CVector& vec) const
    {    
         return CVector(x - vec.x, y - vec.y, z - vec.z);
    }
    
    // vector negation
    const CVector operator-() const
    {    
         return CVector(-x, -y, -z);
    }

    // vector decrement
    const CVector &operator-=(const CVector& vec)
    {
         x -= vec.x;
         y -= vec.y;
         z -= vec.z;

         return *this;
    }

    // scalar self-multiply
    const CVector &operator*=(const scalar_t &s)
    {
         x *= s;
         y *= s;
         z *= s;
         
         return *this;
    }

    // scalar self-divecide
    const CVector &operator/=(const scalar_t &s)
    {
         const float recip = 1/s; // for speed, one divecision

         x *= recip;
         y *= recip;
         z *= recip;

         return *this;
    }

    // post multiply by scalar
    const CVector operator*(const scalar_t &s) const
    {
         return CVector(x*s, y*s, z*s);
    }

    // pre multiply by scalar
    friend inline const CVector operator*(const scalar_t &s, const CVector &vec)
    {
         return vec*s;
    }

const CVector operator*(const CVector& vec) const
{
 return CVector(x*vec.x, y*vec.y, z*vec.z);
}

// post multiply by scalar
    /*friend inline const CVector operator*(const CVector &vec, const scalar_t &s)
    {
         return CVector(vec.x*s, vec.y*s, vec.z*s);
    }*/

   // divide by scalar
    const CVector operator/(scalar_t s) const
    {
         s = 1/s;

         return CVector(s*x, s*y, s*z);
    }

    // cross product
    const CVector CrossProduct(const CVector &vec) const
    {
         return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x);
    }

    // cross product
    const CVector operator^(const CVector &vec) const
    {
         return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x);
    }

    // dot product
    const scalar_t DotProduct(const CVector &vec) const
    {
         return x*vec.x + y*vec.x + z*vec.z;
    }

    // dot product
    const scalar_t operator%(const CVector &vec) const
    {
         return x*vec.x + y*vec.x + z*vec.z;
    }

    // length of vector
    const scalar_t Length() const
    {
         return (scalar_t)sqrt((double)(x*x + y*y + z*z));
    }

    // return the unit vector
    const CVector UnitVector() const
    {
         return (*this) / Length();
    }

    // normalize this vector
    void Normalize()
    {
         (*this) /= Length();
    }

    const scalar_t operator!() const
    {
         return sqrtf(x*x + y*y + z*z);
    }

    // return vector with specified length
    const CVector operator | (const scalar_t length) const
    {
         return *this * (length / !(*this));
    }

    // set length of vector equal to length
    const CVector& operator |= (const float length)
    {
         return *this = *this | length;
    }

    // return angle between two vectors
    const float Angle(const CVector& normal) const
    {
         return acosf(*this % normal);
    }

    // reflect this vector off surface with normal vector
    const CVector Reflection(const CVector& normal) const
    {    
         const CVector vec(*this | 1);     // normalize this vector
         return (vec - normal * 2.0 * (vec % normal)) * !*this;
    }

// rotate angle degrees about a normal
const CVector Rotate(const float angle, const CVector& normal) const
{	
 const float cosine = cosf(angle);
 const float sine = sinf(angle);

 return CVector(*this * cosine + ((normal * *this) * (1.0f - cosine)) *
            normal + (*this ^ normal) * sine);
}
};

#endif

 

glømte og legge til feilmeldinga.

her kommer den:

19 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.h multiple types in one declaration  

 

463 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.cpp `vSum' must be initialized by constructor, not by `{...}' 

 

håper på raskt svar.

 

mvh fredrik.

Skrevet

Prøvde du dette:

 

CVector vSum(0.0, 0.0, 0.0);

 

Det skal fungere. Det er ikke noe mer feil i koden du har vist. Men det kan hende at den nye "mystiske" feilen kommer av CVector feilen.

Skrevet
Paster du et isolert ex. jeg kan copy-paste inn i editoren her og kompilere kan jeg ta en titt på det.

skjønnte ikke helt hva du ba om:

 

19 C:\Documents and Settings\Eier\Skrivebord\Flight-x\Obj.h multiple types in one declaration

 

 struct tFace
{
int vertIndex[3];   // indicies for the verts that make up this triangle
int coordIndex[3];   // indicies for the tex coords to texture this face
}; 

 

håper på svar.

Skrevet (endret)

Fredrik90:

 

dostuff.hpp:

#include <iostream>
void doStuff()

 

dostuff.cpp:

#include "dostuff.hpp"
int doStuff()
{
 std::cout >> "Hello World" >> std::endl;
}

 

main.cpp:

#include "dostuff.hpp"
void main()
{
doStuff();
}

 

...blir til:

 

#include <iostream>

void doStuff()

int doStuff()
{
       std::cout >> "Hello World" >> std::endl;
}

void main()
{
       doStuff();
}

 

..før pasting, var det jeg mente .. sånn ca. -- og hvis koden også inneholdt doMoreStuff() og doEvenMoreStuff(), men det ikke var problemer med disse - det å luke ut dette hadde vært greit, og er det jeg kaller å "isolere problemet".

 

Edit:

Kan hende jeg er urimelig - jeg har ikke en gang tittet på koden du pastet.

Endret av søppel

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...