Added texture loading and displaying for 3D models

This commit is contained in:
trotFunky 2019-09-30 19:20:10 +02:00
parent 01ad1a2317
commit 4050410039
4 changed files with 89 additions and 8 deletions

View file

@ -35,6 +35,16 @@ Model3D::~Model3D()
void Model3D::draw_model()
{
if (is_textured)
{
glEnable(GL_ALPHA_TEST);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBindTexture(GL_TEXTURE_2D,texture->opengl_id[0]);
glColor4f(1.0f, 1.0f, 1.0f,1.00);
}
glPushMatrix();
glScalef(scaling.x,scaling.y,scaling.z);
@ -46,31 +56,45 @@ void Model3D::draw_model()
{
Vec3i face = faces[i];
Vec3i face_texture;
if (is_textured)
{
face_texture = face_textures[i];
}
// FIXME : Find a better way to draw the three vertices
Vec3f vertex = vertices[face.x];
if (is_textured)
{
// TODO : Draw texture
Vec2f vertex_texture = texture_coordinates[face_texture.x];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
}
glVertex3f(vertex.x,vertex.y,vertex.z);
vertex = vertices[face.y];
if (is_textured)
{
// TODO : Draw texture
Vec2f vertex_texture = texture_coordinates[face_texture.y];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
}
glVertex3f(vertex.x,vertex.y,vertex.z);
vertex = vertices[face.z];
if (is_textured)
{
// TODO : Draw texture
Vec2f vertex_texture = texture_coordinates[face_texture.z];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
}
glVertex3f(vertex.x,vertex.y,vertex.z);
}
glEnd();
glPopMatrix();
if (is_textured)
{
glDisable(GL_ALPHA_TEST);
}
}
bool Model3D::load_ascii_off_file(const std::string& file_name)
@ -129,12 +153,56 @@ bool Model3D::load_ascii_off_file(const std::string& file_name)
return false;
}
while(std::getline(file_stream,temp) && temp.find("EXT"));
file_stream >> texture_count;
if (texture_count > 0)
{
is_textured = true;
texture_coordinates = new Vec2f[texture_count];
face_textures = new Vec3i[face_count];
normals = new Vec3f[face_count];
// Parse U,V coordinates
for (uint32_t i = 0;i<texture_count;i++)
{
file_stream >> texture_coordinates[i];
}
// Assign U,V coordinates to vertices
for (uint32_t i = 0;i<face_count;i++)
{
file_stream >> face_textures[i];
}
for (uint32_t i=0;i<vertex_count;i++)
{
file_stream >> normals[i];
}
if (!file_stream)
{
std::cerr << "Error while parsing texture data in " << file_name << std::endl;
file_stream.close();
return false;
}
}
return true;
}
void Model3D::assign_texture(Texture& new_texture)
bool Model3D::assign_texture(Texture& new_texture)
{
texture = &new_texture;
if (is_textured)
{
texture = &new_texture;
return true;
}
else
{
return false;
}
}
void Model3D::set_scaling(float x_scale, float y_scale, float z_scale)