1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
class Model
{
private:
// Model Info
vector<Vertex3D> Vertices;
vector<Texture2D> Textures;
vector<Normal3D> Normals;
vector<Face> Faces;
public:
float ULX,ULY,LRX,LRY;
float X, Y, Z;
Model()
{
}
Model(float x, float y, float z)
{
this->X = x;
this->Y = y;
this->Z = z;
}
//LOAD OBJ
void Load(const char *OBJFile)
{
ifstream ifs(OBJFile);
cout << OBJFile << endl;
while (!ifs.eof()) {
char data[100000];
for (int Dataindx = 0; Dataindx < 100000; Dataindx++)
data[Dataindx] = '\0';
ifs >> data; //Extract 1 word from the file
if (strcmp(data, "v") == 0) {
float x, y, z; //Vertex data
//Extract x, y, z components
ifs >> x;
ifs >> y;
ifs >> z;
Vertex3D vert; //Create Vertex3D object
vert.x = x;
vert.y = y;
vert.z = z;
//Add the vertex into the vector
this->Vertices.push_back(vert);
}
else if (strcmp(data, "vt") == 0) {
float x, y; // Texture Data
// Extract x, y components
ifs >> x;
ifs >> y;
Texture2D text;
text.x = x;
text.y = y;
//cout << ": X: " << x << ", Y: " << y << endl;
// Add the texture into the vector
this->Textures.push_back(text);
}
else if (strcmp(data, "vn") == 0) {
float x, y, z; //Vertex normal
//Extract vertex normal
ifs >> x;
ifs >> y;
ifs >> z;
Normal3D norm; //Create Normal3D object
norm.x = x;
norm.y = y;
norm.z = z;
//Add this normal into the vector
this->Normals.push_back(norm);
}
else if (strcmp(data, "f") == 0) {
if (this->Faces.size() == 2)
int qwe = 0;
char faceData[100000]; //Face data
Face myFace;
while (true) {
ifs >> faceData; //Extract next data
if (ContainSlash(faceData, 100000)) { //Check if data is valid
char Vertexindx[15];
char Textureindx[15];
char Normalindx[15];
ExtractVertexIdx(faceData, Vertexindx);
ExtractTextureIdx(faceData, Textureindx);
ExtractNormalIdx(faceData, Normalindx);
int vIndex = atoi(Vertexindx);
//cout << "VIndex: " << atoi(Vertexindx) << endl;
int tIndex = atoi(Textureindx);
//cout << "TIndex: " << atoi(Textureindx) << endl;
int nIndex = atoi(Normalindx);
//cout << "NIndex: " << atoi(Normalindx) << endl << endl;
myFace.VertexIndex.push_back(vIndex);
myFace.TextureIndex.push_back(tIndex);
myFace.NormalIndex.push_back(nIndex);
}
else {
this->Faces.push_back(myFace); //INVALID face data
ifs.unget();
break;
}
}
}
}
}
//RENDERING OBJ FILE
void Render(GLuint _Texture)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPushMatrix();
glTranslatef(this->X, this->Y, this->Z);
glColor3f(1.0, 1.0, 1.0);
glLineWidth(1);
for (int i = 0; i < Faces.size(); i++)
{
glBegin(GL_POLYGON);
for (int j = 0; j < Faces[i].NormalIndex.size(); j++)
{
int Normalindx = Faces[i].NormalIndex[j] - 1; //Get the normal
float x, y, z;
x = Normals[Normalindx].x;
y = Normals[Normalindx].y;
z = Normals[Normalindx].z;
glNormal3f(x, y, z);
int Textureindx = this->Faces[i].TextureIndex[j] - 1;
x = this->Textures[Textureindx].x;
y = this->Textures[Textureindx].y;
glTexCoord2f(x, y);
//Get the vertex
int Vertexindx = Faces[i].VertexIndex[j] - 1;
x = Vertices[Vertexindx].x;
y = Vertices[Vertexindx].y;
z = Vertices[Vertexindx].z;
glVertex3f(x, y, z);
}
glEnd();
}
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
};
//DECLARE MODELS
Model model[6],Camera_Obj;
|