Added texture loading and displaying for 3D models
This commit is contained in:
parent
01ad1a2317
commit
4050410039
4 changed files with 89 additions and 8 deletions
BIN
resources/RAPTOR.tga
Normal file
BIN
resources/RAPTOR.tga
Normal file
Binary file not shown.
|
@ -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)
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
|
||||
/// Loads an ASCII OFF file. Detects if there is texture data. Only triangles are supported.
|
||||
bool load_ascii_off_file(const std::string& file_name);
|
||||
void assign_texture(Texture& new_texture);
|
||||
bool assign_texture(Texture& new_texture);
|
||||
|
||||
void set_scaling(float x_scale, float y_scale, float z_scale);
|
||||
void set_rotation(float angle, float x, float y, float z);
|
||||
|
@ -37,7 +37,7 @@ private:
|
|||
Vec3f* vertices{};
|
||||
/// Indices of the vertices making up each face
|
||||
Vec3i* faces{};
|
||||
/// Normals of each face
|
||||
/// Normals of each vertex
|
||||
Vec3f* normals{};
|
||||
|
||||
uint32_t texture_count;
|
||||
|
|
15
src/main.cpp
15
src/main.cpp
|
@ -9,6 +9,7 @@
|
|||
volatile unsigned long long int timer_ticks = 0;
|
||||
static Texture tree_texture;
|
||||
static Model3D raptor;
|
||||
static Texture raptor_texture;
|
||||
|
||||
void display()
|
||||
{
|
||||
|
@ -22,7 +23,7 @@ void display()
|
|||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(10, 10, 10,
|
||||
gluLookAt(7.5, 7.5, 7.5,
|
||||
0, 0, 1,
|
||||
0, 1, 0);
|
||||
|
||||
|
@ -33,7 +34,10 @@ void display()
|
|||
|
||||
display_tree(0,-5,0,3,5,tree_texture);
|
||||
|
||||
glPushMatrix();
|
||||
glRotatef(angleY,0,-1,0);
|
||||
raptor.draw_model();
|
||||
glPopMatrix();
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
@ -75,7 +79,16 @@ int main(int argc, char** argv)
|
|||
glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]);
|
||||
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,tree_texture.width,tree_texture.height,GL_RGBA,GL_UNSIGNED_BYTE,tree_texture.image_data);
|
||||
|
||||
// Load and generate raptor texture
|
||||
raptor_texture.load_rgb_tga("resources/RAPTOR.tga");
|
||||
glGenTextures(1, raptor_texture.opengl_id);
|
||||
// TODO : Put in the Texture class
|
||||
glBindTexture(GL_TEXTURE_2D, raptor_texture.opengl_id[0]);
|
||||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, raptor_texture.width, raptor_texture.height, GL_RGB, GL_UNSIGNED_BYTE, raptor_texture.image_data);
|
||||
|
||||
|
||||
raptor.load_ascii_off_file("resources/RAPTOR.off");
|
||||
raptor.assign_texture(raptor_texture);
|
||||
raptor.set_scaling(0.01,0.01,0.01);
|
||||
raptor.set_rotation(-90,1,0,0);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue