Commit 0c509ece authored by Romain Pacanowski's avatar Romain Pacanowski

Not storing a mrf::geom::mesh but vector directly

parent 02d59e21
......@@ -15,7 +15,7 @@ Viewer::Viewer()
}
Viewer::Viewer(std::string const &full_path_to_mesh)
: _winWidth(0), _winHeight(0), _theta(0), _rotate(true), _meshIsInitialized(false)
: _winWidth(0), _winHeight(0), _theta(0), _rotate(false), _meshIsInitialized(false)
{
std::string ext = full_path_to_mesh.substr(full_path_to_mesh.size() - 3, 3);
......@@ -24,7 +24,71 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
throw std::runtime_error(" OTHER THAN PLY FILE IS NOT SUPPORTED");
}
mrf::io::loadMeshFromPLY( full_path_to_mesh, _mesh );
//mrf::io::loadMeshFromPLY( full_path_to_mesh, _mesh );
_vboCPU.clear();
try
{
mrf::io::loadPLY<mrf::math::Vec3d>(full_path_to_mesh, _vertices, _normals, _uvs, _color_per_vertex, _index_faces);
if( _normals.size() != _vertices.size() )
{
mrf::geom::_Mesh<mrf::math::Vec3d>::computeNormals(_vertices, _index_faces, _normals );
}
mrf::geom::_Mesh<mrf::math::Vec3d>::computeTangents(_vertices, _normals, _tangents);
std::cout << _index_faces[0] << " " << _index_faces[1] << " " << _index_faces[2] << std::endl;
//Construct the linear VBO in CPU MEMORY
for (unsigned int i = 0; i < _vertices.size(); i++)
{
_vboCPU.push_back( _vertices[i] );
_vboCPU.push_back( _normals[i] );
_vboCPU.push_back(_tangents[i]);
if( i ==0 )
{
std::cout << " VBO CPU at 0 = " << _vboCPU[0] << " Normals = " << _vboCPU[1] << " Tangents " << _vboCPU[2]
<< _vboCPU[3] << " " << _vboCPU[4] << " " << _vboCPU[5] << std::endl;
std::cout << " Vertices[0] = " << _vertices[0] << " Normals[0] = " << _normals[0] << " Tangents[0]=" << _tangents[0] << std::endl;
}
if( _color_per_vertex.size() == _vertices.size() )
{
_vboCPU.push_back(_color_per_vertex[i] );
}
if( _uvs.size() == _vertices.size() )
{
_vboCPU.push_back( _uvs[i]);
}
}
_nb_bytes_between_two_vertex = 3*sizeof(mrf::math::Vec3f);
if( _color_per_vertex.size() == _vertices.size() )
{
std::cout << " color per vertex present " << std::endl;
_nb_bytes_between_two_vertex += sizeof(mrf::math::Vec3f);
}
if(_uvs.size() == _vertices.size() )
{
std::cout << " uvs present " << std::endl;
_nb_bytes_between_two_vertex += sizeof(mrf::math::Vec3f);
}
std::cout << " Linear indexes computed with " << _vboCPU.size() << " elements " << std::endl;
std::cout << " _nb Bytes between two vertices = " << _nb_bytes_between_two_vertex << std::endl;
}
catch (const std::exception &e)
{
std::cerr << " Problem while loading PLY file. Aborting" << std::endl;
}
}
......@@ -84,9 +148,9 @@ void Viewer::drawScene()
Matrix3f matN = matLocal2Cam.topLeftCorner<3,3>().inverse().transpose();
glUniformMatrix3fv(_shader.getUniformLocation("normal_mat"),1,GL_FALSE,matN.data());
Vector3f lightDir = Vector3f(1,0,1).normalized();
lightDir = (matLocal2Cam.topLeftCorner<3,3>() * lightDir).normalized();
glUniform3fv(_shader.getUniformLocation("lightDir"),1,lightDir.data());
// Vector3f lightDir = Vector3f(1,0,1).normalized();
// lightDir = (matLocal2Cam.topLeftCorner<3,3>() * lightDir).normalized();
// glUniform3fv(_shader.getUniformLocation("lightDir"),1,lightDir.data());
draw(_shader);
......@@ -213,25 +277,31 @@ void Viewer::initVBA()
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferId);
// copy the data from host's RAM to GPU's video memory:
std::vector<mrf::math::Vec3f> const & vertices = _mesh->vertices();
glBufferData(GL_ARRAY_BUFFER, sizeof(mrf::math::Vec3f) * _mesh->numVertices(), vertices[0].ptr(), GL_STATIC_DRAW);
// std::vector<mrf::math::Vec3f> const & vertices = _mesh->vertices();
glBufferData(GL_ARRAY_BUFFER, sizeof(mrf::math::Vec3f) * _vboCPU.size(), _vboCPU[0].ptr(), GL_STATIC_DRAW);
glGenBuffers(1, &mIndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
std::vector<mrf::uint> const & faces = _mesh->faces();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3 * _mesh->numFaces(), faces.data(), GL_STATIC_DRAW);
// std::vector<mrf::uint> const & faces = _mesh->faces();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * _index_faces.size(), _index_faces.data(), GL_STATIC_DRAW);
glGenVertexArrays(1, &mVertexArrayId);
_meshIsInitialized = true;
std::cout << " INITI VBA COMPLETED" << std::endl;
checkError();
}
void
void
Viewer::draw(Shader const & shd)
{
//std::cout << " STRARTING DRAW " << std::endl;
checkError();
// Activate the VBO of the current mesh:
glBindVertexArray(mVertexArrayId);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferId);
......@@ -242,47 +312,78 @@ Viewer::draw(Shader const & shd)
// 1 - get id of the attribute "vtx_position" as declared as "in vec3 vtx_position" in the vertex shader
int vertex_loc = shd.getAttribLocation("vtx_position");
// 2 - tells OpenGL where to find the x, y, and z coefficients:
glVertexAttribPointer(vertex_loc, // id of the attribute
3, // number of coefficients (here 3 for x, y, z)
GL_FLOAT, // type of the coefficients (here float)
GL_FALSE, // for fixed-point number types only
sizeof(mrf::math::Vec3f), // number of bytes between the x coefficient of two vertices
// (e.g. number of bytes between x_0 and x_1)
0); // number of bytes to get x_0
glVertexAttribPointer(vertex_loc, // id of the attribute
3, // number of coefficients (here 3 for x, y, z)
GL_FLOAT, // type of the coefficients (here float)
GL_FALSE, // for fixed-point number types only
_nb_bytes_between_two_vertex, // number of bytes between the x coefficient of two vertices
// (e.g. number of bytes between x_0 and x_1)
0); // number of bytes to get x_0
// 3 - activate this stream of vertex attribute
glEnableVertexAttribArray(vertex_loc);
int normal_loc = shd.getAttribLocation("vtx_normal");
if(normal_loc>=0)
{
checkError();
glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void*)sizeof(mrf::math::Vec3f));
glEnableVertexAttribArray(normal_loc);
}
int color_loc = shd.getAttribLocation("vtx_color");
if(color_loc>=0)
int tangent_loc = shd.getAttribLocation("vtx_tangent");
if (tangent_loc >= 0)
{
glVertexAttribPointer(tangent_loc, 3, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void *)(2*sizeof(mrf::math::Vec3f)));
glEnableVertexAttribArray(tangent_loc);
}
int color_loc = -1;
if (_color_per_vertex.size() == _vertices.size())
{
glVertexAttribPointer(color_loc, 3, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void*)(2*sizeof(mrf::math::Vec3f)));
glEnableVertexAttribArray(color_loc);
color_loc = shd.getAttribLocation("vtx_color");
if (color_loc >= 0)
{
glVertexAttribPointer(color_loc, 3, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void *)(3 * sizeof(mrf::math::Vec3f)));
glEnableVertexAttribArray(color_loc);
}
}
int texcoord_loc = shd.getAttribLocation("vtx_texcoord");
if(texcoord_loc>=0)
int texcoord_loc = -1;
if (_uvs.size() == _vertices.size())
{
glVertexAttribPointer(texcoord_loc, 2, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void*)(2*sizeof(mrf::math::Vec3f)+sizeof(mrf::math::Vec4f)));
glEnableVertexAttribArray(texcoord_loc);
texcoord_loc = shd.getAttribLocation("vtx_texcoord");
if (texcoord_loc >= 0)
{
glVertexAttribPointer(texcoord_loc, 2, GL_FLOAT, GL_FALSE, sizeof(mrf::math::Vec3f), (void *)(4 * sizeof(mrf::math::Vec3f)));
glEnableVertexAttribArray(texcoord_loc);
}
}
// send the geometry
// glDrawElements(GL_TRIANGLES, 3*mFaces.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, _mesh->numFaces()*3, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, _index_faces.size(), GL_UNSIGNED_INT, 0);
// at this point the mesh has been drawn and raserized into the framebuffer!
glDisableVertexAttribArray(vertex_loc);
if(normal_loc>=0) glDisableVertexAttribArray(normal_loc);
if(color_loc>=0) glDisableVertexAttribArray(color_loc);
if(texcoord_loc>=0) glDisableVertexAttribArray(texcoord_loc);
checkError();
if (tangent_loc >= 0) glDisableVertexAttribArray(tangent_loc);
if (_color_per_vertex.size() == _vertices.size())
{
if (color_loc >= 0)
glDisableVertexAttribArray(color_loc);
}
if (_uvs.size() == _vertices.size())
{
if (texcoord_loc >= 0)
glDisableVertexAttribArray(texcoord_loc);
}
checkError();
}
\ No newline at end of file
......@@ -46,7 +46,23 @@ private:
//MRF Mesh
std::shared_ptr<mrf::geom::Mesh> _mesh;
//std::shared_ptr<mrf::geom::Mesh> _mesh;
//CPU Data of the PLY File
std::vector<mrf::math::Vec3d> _vertices;
std::vector<mrf::math::Vec3d> _normals;
std::vector<mrf::math::Vec3d> _tangents;
std::vector<mrf::math::Vec3d> _uvs;
std::vector<mrf::math::Vec3d> _color_per_vertex;
std::vector<unsigned int> _index_faces;
unsigned int _nb_bytes_between_two_vertex;
std::vector< mrf::math::Vec3f > _vboCPU;
GLuint mVertexBufferId;
GLuint mIndexBufferId;
GLuint mVertexArrayId;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment