void Mesh::construct() { if (!m_material || !m_geometry) { return; } destruct(); glGenVertexArrays(1, &m_vertexArray); glBindVertexArray(m_vertexArray); // Fill vertex position data glGenBuffers(1, &m_vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); VertexList vertices = m_geometry->vertices(); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); glVertexAttribPointer((GLint)ShaderAttribute::VertexPosition, 3, GL_FLOAT, GL_FALSE, 0, bufferOffset(0)); glEnableVertexAttribArray((GLint)ShaderAttribute::VertexPosition); // Fill UV vertex data if (m_geometry->uvCount()) { glGenBuffers(1, &m_textureCoordinatesBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_textureCoordinatesBuffer); UVList uvs = m_geometry->uvs(); glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), uvs.data(), GL_STATIC_DRAW); glVertexAttribPointer((GLint)ShaderAttribute::VertexTextureCoordinates, 2, GL_FLOAT, GL_FALSE, 0, bufferOffset(0)); glEnableVertexAttribArray((GLint)ShaderAttribute::VertexTextureCoordinates); } // Fill vertex normal data if (m_geometry->normalCount()) { glGenBuffers(1, &m_vertexNormalsBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_vertexNormalsBuffer); VertexList normals = m_geometry->normals(); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW); glVertexAttribPointer((GLint)ShaderAttribute::VertexNormals, 3, GL_FLOAT, GL_FALSE, 0, bufferOffset(0)); glEnableVertexAttribArray((GLint)ShaderAttribute::VertexNormals); } // Fill index buffer glGenBuffers(1, &m_indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); IndexList indices = m_geometry->indices(); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), indices.data(), GL_STATIC_DRAW); glBindVertexArray(0); m_dirty = false; }
void GeometryExportFile::writeSprite( Object *o, ofstream &out ) { // out << "OBJECT poly"<<endl; out << " WORLD (" << endl; out << " TEXTURES (" << endl; out << " MAX_TEXTURES (10)" << endl; out << " TEXTURE 'abstract_a032.gif' ()" << endl; out << " TEXTURE 'andrew_wood.gif' ()" << endl; out << " TEXTURE 'sydney.gif' ()" << endl; out << " TEXTURE 'bla.png' ()" << endl; out << " TEXTURE 'bla2.gif' ()" << endl; out << " )" << endl; // Don't know how to get the name. out << "SPRITE 'sydney'(" << endl; // Add the texture. // We also need to add the texture to the top of the world file. TextureMaterial *tm = o->getTextureMaterial(); if(tm != 0 && tm->texture != 0) { // Need to get the file name that the image will be in CS's VFS. // out << "TEXNR('"<<tm->texture->getFilename()->ascii()\ // << "')" <<endl; out << "TEXNR('bla2.gif')" << endl; } // Write the frames. err... "frame". We done needs verts, and // uv coordinates. // We are going to waste some vertices here. // // We are going to add an extra set of vertices // // for each face: // add to the new vertex list, the vertices in the face. // add to the new uv coords list, the uvs for the face. // // The triangle list will be made by consecutively using three vertices // from the list of vertices. VertexList *vlist = o->getVerts(); UVList *uvlist = o->getUVs(); vector<Vector4> new_vert_list; vector<Vector4> new_uv_list; int num_verts = ( int ) vlist->size(); int num_uvs = ( int ) uvlist->size(); //DEBUG // out << "num_verts is:" << num_verts << " num_uvs is:" << num_uvs <<endl; int num_faces = ( int ) o->numFaces(); int tris = -1; for(int i = 0; i < num_faces; i++) { Face *face = o->getFace( i ); vector<int> *face_vlist = face->getVerts(); vector<int> *face_uvlist = face->getUVs(); int v_size = ( int ) face_vlist->size(); int uv_size = ( int ) face_uvlist->size(); for(int ii = 0; ii < uv_size; ii++) { new_uv_list.push_back( face->getParentObject() ->getUVCoord( ( *face_uvlist ) [ ii ] ) ->getPosition() ); } for(int ii = 0; ii < v_size; ii++) { new_vert_list.push_back( face->getParentObject() ->getVertex( ( *face_vlist ) [ ii ] ) ->getPosition() ); } } // write out the frames... er just one frame for now. // DEBUG // out << "new_vert_list size:" << new_vert_list.size() << endl; // out << "new_uv_list size:" << new_uv_list.size() << endl; // Just place the name of the frame for now. out << "FRAME 'stand1' ("; for(uint i = 0; i < new_vert_list.size(); i++) { out << "V(" << new_vert_list[ i ].x << "," << new_vert_list[ i ].y << ","; // The z axis are oposite for I3D and CS. out << new_vert_list[ i ].z * -1 << ":"; // Have to convert systems, as cs uses x = 0, y = 0 at top left. // Inovation 3d uses bottom left. out << new_uv_list[ i ].x << "," << ( 1.0 - new_uv_list[ i ].y ) << ")"; out << endl; } out << ")" << endl; // write out the list of triangles. for(int i = 0; i < new_vert_list.size(); i += 3) { out << "TRIANGLE(" << i + 2 << "," << i + 1 << "," << i << ")" << endl; } }