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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include "Model.h"
Vertex Mesh::CalculateNormal(float *p1, float *p2, float *p3)
{
float a[3], b[3], result[3];
float length;
a[0] = p1[0] - p2[0];
a[1] = p1[1] - p2[1];
a[2] = p1[2] - p2[2];
b[0] = p1[0] - p3[0];
b[1] = p1[1] - p3[1];
b[2] = p1[2] - p3[2];
result[0] = a[1] * b[2] - b[1] * a[2];
result[1] = b[0] * a[2] - a[0] * b[2];
result[2] = a[0] * b[1] - b[0] * a[1];
// calculate the length of the normal
length = (float)sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]);
Vertex nrml= {result[0]/length, result[1]/length, result[2]/length};
return nrml;
// normalize and specify the normal
//glNormal3f(result[0]/length, result[1]/length, result[2]/length);
}
int Mesh::LoadOBJ(const char* location)
{
// First open fstream and load all the strings into memory
std::ifstream file(location);
std::vector<std::string> fileContents;
std::vector<Vertex> verts;
std::vector<Vertex> nrmls;
numVertices = 0;
numTris = 0;
if(file)
{
std::string buffer;
while(std::getline(file, buffer))
{
// Add the buffer contents to our fileContents vector if it's not a comment
// (Doing the check now reduces memory usage :D
if(buffer[0] != '#' || buffer[0] != ' ')
{
fileContents.push_back(buffer);
}
// We really don't need an if statement...
}
if(fileContents.size() != 0)
{
// If the whole file wasn't all comments...
for(unsigned int n = 0; n < fileContents.size(); n++)
{
if(fileContents[n].c_str()[0] == 'v' && fileContents[n].c_str()[1] == ' ')
{
std::cout << "Vertex\n";
float tmpx, tmpy, tmpz;
numVertices++; // Tell the compiler there is another Vertex
sscanf(fileContents[n].c_str(), "v %f %f %f" ,&tmpx,&tmpy,&tmpz);
Vertex tmpVert = {tmpx, tmpy, tmpz};
verts.push_back(tmpVert);
}
/*else if(fileContents[n].c_str()[0] == 'v' && fileContents[n].c_str()[1] == 'n' && fileContents[n].c_str()[2] == ' ')
{
std::cout << "Normal\n";
float tmpx, tmpy, tmpz;
numNormals++; // Tell the program there is another normal
sscanf(fileContents[n].c_str(), "vn %f %f %f" ,&tmpx,&tmpy,&tmpz);
Vertex tmpVert = {tmpx, tmpy, tmpz};
nrmls.push_back(tmpVert);
}*/
/*else if((*cm_coordinates[i])[0] == 'f')
{
int a,b,c,d,e;
if(count(cm_coordinates[i]->begin(),cm_coordinates[i]->end(), ' ') == 3)
{
sscanf(cm_coordinates[i]->c_str(), "f %d//%d %d//%d %d//%d" ,&a, &b, &c, &b, &d, &b);
cm_faces.push_back(new face(b, a, c, d));
}
else
{
sscanf(cm_coordinates[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d" ,&a, &b, &c, &b, &d, &b, &e, &b);
cm_faces.push_back(new face(b, a, c, d, e));
}
}*/
}
// Take the contents out of memory, we don't need them any more
/*for(unsigned int s = 0; s < fileContents.size(); s++)
{
fileContents.pop_back();
}*/
// Now that we got it all loaded, lets put the data together
// If the number of Vertices is divisible by 3, it's a valid format as it's
// a triangle. If not, it's a quad based (Assuming), and the modeller will
// be forced to convert it to a triangle based model. (Easy with blender)
if((numVertices % 3) == 0)
{
std::cout << "Triangular! :D\n";
numTris = (numVertices / 3); // Set the number of triangles
std::cout << numTris << "\n";
mesh = new Triangle[numTris]; // Allocate memory for the triangles
// Put all vertices inside the newly allocated triangles
for(unsigned int n = 0; n < numVertices;)
{
for(unsigned int m = 0; m < numTris; m++)
{
mesh[m].points[0] = verts[n];
n++;
mesh[m].points[1] = verts[n];
n++;
mesh[m].points[2] = verts[n];
n++;
}
}
for(unsigned int t = 0; t < numTris; t++)
{
// Get arrays to calculate normal
float p1[] = {mesh[t].points[0].x, mesh[t].points[0].y, mesh[t].points[0].z};
float p2[] = {mesh[t].points[1].x, mesh[t].points[1].y, mesh[t].points[1].z};
float p3[] = {mesh[t].points[2].x, mesh[t].points[2].y, mesh[t].points[2].z};
// Calculate the normal
mesh[t].normal = CalculateNormal(p1, p2, p3);
}
// Now all calculations are done, the model should be loaded
std::cout << "Model successfully loaded.\n";
}
else
{
// (I don't like it when people have quad based models :P)
std::cout << "Failure to load model at \"" << location << "\". Not triangle based.\n";
return 3;
}
}
else
{
std::cout << "File \"" << location << "\" Was empty... Failure to load\n";
return 2;
}
}
else
{
std::cout << "Failure opening file at \"" << location <<"\".";
return 1;
}
}
void Mesh::Render(float x, float y, float z)
{
glLoadIdentity();
glTranslatef(x, y, z);
for(Uint32 n = 0; n < numTris; n++)
{
glBegin(GL_TRIANGLES);
// Normals
glNormal3f(mesh[n].normal.x, mesh[n].normal.y, mesh[n].normal.z);
std::cout << "=======================================================\n";
std::cout << "Normal - " << mesh[n].normal.x << mesh[n].normal.y << mesh[n].normal.z << "\n";
std::cout << "Triangle(" << n << ")\n";
std::cout << mesh[n].points[0].x << " " << mesh[n].points[0].y << " " << mesh[n].points[0].z << "\n"
<< mesh[n].points[1].x << " " << mesh[n].points[1].y << " " << mesh[n].points[1].z << "\n"
<< mesh[n].points[2].x << " " << mesh[n].points[2].y << " " << mesh[n].points[2].z << "\n";
//std::cout << "====================\n";
// Triangular points
glVertex3f(mesh[n].points[0].x, mesh[n].points[0].y, mesh[n].points[0].z);
glVertex3f(mesh[n].points[1].x, mesh[n].points[1].y, mesh[n].points[1].z);
glVertex3f(mesh[n].points[2].x, mesh[n].points[2].y, mesh[n].points[2].z);
glEnd();
}
}
|