Loading of a OFF ASCII file
Only supports triangles Not textured yet Supports scaling and rotation of the model
This commit is contained in:
parent
db0bd97580
commit
94bc1228f1
9 changed files with 31419 additions and 4 deletions
153
src/DataHandling/Model3D.cpp
Normal file
153
src/DataHandling/Model3D.cpp
Normal file
|
@ -0,0 +1,153 @@
|
|||
//
|
||||
// Created by trotfunky on 30/09/2019.
|
||||
//
|
||||
|
||||
#include "Model3D.h"
|
||||
|
||||
|
||||
Model3D::Model3D() : vertex_count(0), face_count(0), texture_count(0), is_textured(false)
|
||||
{}
|
||||
|
||||
Model3D::Model3D(const std::string& file_name) : Model3D()
|
||||
{
|
||||
load_ascii_off_file(file_name);
|
||||
draw_model();
|
||||
}
|
||||
|
||||
Model3D::~Model3D()
|
||||
{
|
||||
if (vertex_count > 0)
|
||||
{
|
||||
delete(vertices);
|
||||
}
|
||||
if (face_count > 0)
|
||||
{
|
||||
delete(faces);
|
||||
}
|
||||
|
||||
if (is_textured && texture_count > 0)
|
||||
{
|
||||
delete(texture_coordinates);
|
||||
delete(face_textures);
|
||||
delete(normals);
|
||||
}
|
||||
}
|
||||
|
||||
void Model3D::draw_model()
|
||||
{
|
||||
glPushMatrix();
|
||||
|
||||
glScalef(scaling.x,scaling.y,scaling.z);
|
||||
glRotatef(rotation_angle,rotation_axis.x,rotation_axis.y,rotation_axis.z);
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
|
||||
for (uint32_t i = 0;i<face_count;i++)
|
||||
{
|
||||
Vec3i face = faces[i];
|
||||
|
||||
// FIXME : Find a better way to draw the three vertices
|
||||
Vec3f vertex = vertices[face.x];
|
||||
if (is_textured)
|
||||
{
|
||||
// TODO : Draw texture
|
||||
}
|
||||
glVertex3f(vertex.x,vertex.y,vertex.z);
|
||||
|
||||
vertex = vertices[face.y];
|
||||
if (is_textured)
|
||||
{
|
||||
// TODO : Draw texture
|
||||
}
|
||||
glVertex3f(vertex.x,vertex.y,vertex.z);
|
||||
|
||||
vertex = vertices[face.z];
|
||||
if (is_textured)
|
||||
{
|
||||
// TODO : Draw texture
|
||||
}
|
||||
glVertex3f(vertex.x,vertex.y,vertex.z);
|
||||
}
|
||||
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
bool Model3D::load_ascii_off_file(const std::string& file_name)
|
||||
{
|
||||
std::fstream file_stream(file_name, std::ios_base::in);
|
||||
if (file_stream.fail())
|
||||
{
|
||||
std::cerr << "Error opening " << file_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string temp;
|
||||
// Discard the first line (OFF Header)
|
||||
std::getline(file_stream,temp);
|
||||
|
||||
// Ignore comments at the beginning of the file
|
||||
while (file_stream.peek() == '#')
|
||||
{
|
||||
if (!std::getline(file_stream,temp))
|
||||
{
|
||||
std::cerr << "Error while parsing OFF comments in " << file_name << std::endl;
|
||||
file_stream.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse count information (edge_count is can be safely ignored)
|
||||
int edge_count;
|
||||
file_stream >> vertex_count >> face_count >> edge_count;
|
||||
|
||||
vertices = new Vec3f[vertex_count];
|
||||
faces = new Vec3i[face_count];
|
||||
|
||||
for (uint32_t i = 0;i<vertex_count;i++)
|
||||
{
|
||||
file_stream >> vertices[i];
|
||||
}
|
||||
|
||||
for (uint32_t i = 0;i<face_count;i++)
|
||||
{
|
||||
// Check the the edge count of each face
|
||||
file_stream >> edge_count;
|
||||
if (edge_count != 3)
|
||||
{
|
||||
std::cerr << "Models must only have triangles !" << std::endl;
|
||||
file_stream.close();
|
||||
return false;
|
||||
}
|
||||
file_stream >> faces[i];
|
||||
}
|
||||
|
||||
if (!file_stream)
|
||||
{
|
||||
std::cerr << "Unexpected EOF while reading model data in " << file_name << std::endl;
|
||||
file_stream.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Model3D::assign_texture(Texture& new_texture)
|
||||
{
|
||||
texture = &new_texture;
|
||||
}
|
||||
|
||||
void Model3D::set_scaling(float x_scale, float y_scale, float z_scale)
|
||||
{
|
||||
scaling.x = x_scale;
|
||||
scaling.y = y_scale;
|
||||
scaling.z = z_scale;
|
||||
}
|
||||
|
||||
void Model3D::set_rotation(float angle, float x, float y, float z)
|
||||
{
|
||||
rotation_axis.x = x;
|
||||
rotation_axis.y = y;
|
||||
rotation_axis.z = z;
|
||||
rotation_angle = angle;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue