Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- tinyobj::attrib_t attrib;
- std::vector<tinyobj::shape_t> shapes;
- std::vector<tinyobj::material_t> materials;
- std::string err;
- std::vector<float> vertices;
- tinyobj::LoadObj(&attrib, &shapes, &materials, &err, a_strFileName.c_str());
- printf("# of vertices = %d\n", (int)(attrib.vertices.size()) / 3);
- printf("# of normals = %d\n", (int)(attrib.normals.size()) / 3);
- printf("# of texcoords = %d\n", (int)(attrib.texcoords.size()) / 2);
- printf("# of materials = %d\n", (int)materials.size());
- printf("# of shapes = %d\n", (int)shapes.size());
- for (size_t s = 0; s < shapes.size(); s++) {
- std::vector<float> buffer; // pos(3float), normal(3float), color(3float)
- for(size_t f = 0 ; f < shapes[s].mesh.indices.size() / 3 ; f++) {
- tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
- tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
- tinyobj::index_t idx2 = shapes[s].mesh.indices[3 * f + 2];
- float tc[3][2];
- if (attrib.texcoords.size() > 0) {
- if ((idx0.texcoord_index < 0) || (idx1.texcoord_index < 0) ||
- (idx2.texcoord_index < 0)) {
- // face does not contain valid uv index.
- tc[0][0] = 0.0f;
- tc[0][1] = 0.0f;
- tc[1][0] = 0.0f;
- tc[1][1] = 0.0f;
- tc[2][0] = 0.0f;
- tc[2][1] = 0.0f;
- }
- else {
- assert(attrib.texcoords.size() >
- size_t(2 * idx0.texcoord_index + 1));
- assert(attrib.texcoords.size() >
- size_t(2 * idx1.texcoord_index + 1));
- assert(attrib.texcoords.size() >
- size_t(2 * idx2.texcoord_index + 1));
- // Flip Y coord.
- tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
- tc[0][1] = 1.0f - attrib.texcoords[2 * idx0.texcoord_index + 1];
- tc[1][0] = attrib.texcoords[2 * idx1.texcoord_index];
- tc[1][1] = 1.0f - attrib.texcoords[2 * idx1.texcoord_index + 1];
- tc[2][0] = attrib.texcoords[2 * idx2.texcoord_index];
- tc[2][1] = 1.0f - attrib.texcoords[2 * idx2.texcoord_index + 1];
- }
- }
- else {
- tc[0][0] = 0.0f;
- tc[0][1] = 0.0f;
- tc[1][0] = 0.0f;
- tc[1][1] = 0.0f;
- tc[2][0] = 0.0f;
- tc[2][1] = 0.0f;
- }
- float v[3][3];
- for (int k = 0; k < 3; k++) {
- int f0 = idx0.vertex_index;
- int f1 = idx1.vertex_index;
- int f2 = idx2.vertex_index;
- assert(f0 >= 0);
- assert(f1 >= 0);
- assert(f2 >= 0);
- v[0][k] = attrib.vertices[3 * f0 + k];
- v[1][k] = attrib.vertices[3 * f1 + k];
- v[2][k] = attrib.vertices[3 * f2 + k];
- }
- for (int k = 0; k < 3; k++) {
- buffer.push_back(v[k][0]);
- buffer.push_back(v[k][1]);
- buffer.push_back(v[k][2]);
- buffer.push_back(tc[k][0]);
- buffer.push_back(tc[k][1]);
- }
- }
- m_Vertices.insert(m_Vertices.end(), buffer.begin(), buffer.end());
- }
- m_iNumVerts = m_Vertices.size() / (3 + 2);
- FileSystem* FH = new FileSystem();
- int width, height;
- char *image = 0;
- int imageNumber = Rand(5) + 1;
- image = FH->Load((char*)"../Resources/Textures/raptor.png", &width, &height);
- glGenTextures(1, &m_Texture);
- glBindTexture(GL_TEXTURE_2D, m_Texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement