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
|
@ -11,7 +11,7 @@ if(NOT ${OPENGL_GLU_FOUND})
|
|||
endif()
|
||||
|
||||
add_executable(tests_opengl
|
||||
src/main.cpp src/displayers.h src/displayers.cpp src/Texture.cpp src/Texture.h)
|
||||
src/main.cpp src/displayers.h src/displayers.cpp src/DataHandling/Texture.cpp src/DataHandling/Texture.h src/DataHandling/Model3D.cpp src/DataHandling/Model3D.h src/types.h)
|
||||
|
||||
target_link_libraries(tests_opengl
|
||||
${OPENGL_LIBRARIES}
|
||||
|
|
31149
resources/RAPTOR.off
Normal file
31149
resources/RAPTOR.off
Normal file
File diff suppressed because it is too large
Load diff
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;
|
||||
}
|
57
src/DataHandling/Model3D.h
Normal file
57
src/DataHandling/Model3D.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
//
|
||||
// Created by trotfunky on 30/09/2019.
|
||||
//
|
||||
|
||||
#ifndef TESTS_OPENGL_MODEL3D_H
|
||||
#define TESTS_OPENGL_MODEL3D_H
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "GL/glut.h"
|
||||
|
||||
#include "../types.h"
|
||||
#include "Texture.h"
|
||||
|
||||
class Model3D {
|
||||
public:
|
||||
Model3D();
|
||||
explicit Model3D(const std::string& file_name);
|
||||
~Model3D();
|
||||
|
||||
/// 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);
|
||||
|
||||
void set_scaling(float x_scale, float y_scale, float z_scale);
|
||||
void set_rotation(float angle, float x, float y, float z);
|
||||
|
||||
void draw_model();
|
||||
|
||||
bool is_textured;
|
||||
private:
|
||||
uint32_t vertex_count;
|
||||
uint32_t face_count;
|
||||
|
||||
/// Coordinates of the vertices
|
||||
Vec3f* vertices{};
|
||||
/// Indices of the vertices making up each face
|
||||
Vec3i* faces{};
|
||||
/// Normals of each face
|
||||
Vec3f* normals{};
|
||||
|
||||
uint32_t texture_count;
|
||||
/// U,V coordinates inside the texture
|
||||
Vec2f* texture_coordinates{};
|
||||
/// Indices of the texture coordinates of each point on a face
|
||||
Vec3i* face_textures{};
|
||||
|
||||
Texture* texture{};
|
||||
|
||||
Vec3f scaling{};
|
||||
Vec3f rotation_axis{};
|
||||
float rotation_angle{};
|
||||
};
|
||||
|
||||
|
||||
#endif //TESTS_OPENGL_MODEL3D_H
|
|
@ -5,10 +5,10 @@
|
|||
#ifndef TESTS_OPENGL_TEXTURE_H
|
||||
#define TESTS_OPENGL_TEXTURE_H
|
||||
|
||||
#include <GL/glut.h>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <GL/glut.h>
|
||||
|
||||
class Texture {
|
||||
public:
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include <GL/glut.h>
|
||||
|
||||
#include "Texture.h"
|
||||
#include "DataHandling/Texture.h"
|
||||
|
||||
/// Displays a square based pyramid from a base position, height and side length
|
||||
/// \param x X coordinate of the center
|
||||
|
|
11
src/main.cpp
11
src/main.cpp
|
@ -2,11 +2,13 @@
|
|||
#include "GL/gl.h"
|
||||
|
||||
#include "displayers.h"
|
||||
#include "Texture.h"
|
||||
#include "DataHandling/Texture.h"
|
||||
#include "DataHandling/Model3D.h"
|
||||
|
||||
|
||||
volatile unsigned long long int timer_ticks = 0;
|
||||
static Texture tree_texture;
|
||||
static Model3D raptor;
|
||||
|
||||
void display()
|
||||
{
|
||||
|
@ -31,6 +33,8 @@ void display()
|
|||
|
||||
display_tree(0,-5,0,3,5,tree_texture);
|
||||
|
||||
raptor.draw_model();
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
|
@ -67,9 +71,14 @@ int main(int argc, char** argv)
|
|||
// Load and generate tree texture
|
||||
tree_texture.load_rgba_tga("resources/arbre.tga","resources/arbre_masque.tga");
|
||||
glGenTextures(1,tree_texture.opengl_id);
|
||||
// TODO : Put in the Texture class
|
||||
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);
|
||||
|
||||
raptor.load_ascii_off_file("resources/RAPTOR.off");
|
||||
raptor.set_scaling(0.01,0.01,0.01);
|
||||
raptor.set_rotation(-90,1,0,0);
|
||||
|
||||
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
|
||||
|
||||
glutIdleFunc(display);
|
||||
|
|
47
src/types.h
Normal file
47
src/types.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// Created by trotfunky on 30/09/2019.
|
||||
//
|
||||
|
||||
#ifndef TESTS_OPENGL_TYPES_H
|
||||
#define TESTS_OPENGL_TYPES_H
|
||||
|
||||
#include <fstream>
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct Vec2
|
||||
{
|
||||
T x;
|
||||
T y;
|
||||
|
||||
friend std::fstream& operator>>(std::fstream& stream, Vec2& vec2)
|
||||
{
|
||||
stream >> vec2.x >> vec2.y;
|
||||
return stream;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Vec3
|
||||
{
|
||||
T x;
|
||||
T y;
|
||||
T z;
|
||||
|
||||
friend std::fstream& operator>>(std::fstream& stream, Vec3& vec3)
|
||||
{
|
||||
stream >> vec3.x >> vec3.y >> vec3.z;
|
||||
return stream;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Define aliases for common types
|
||||
typedef Vec2<int> Vec2i;
|
||||
typedef Vec2<float> Vec2f;
|
||||
|
||||
typedef Vec3<int> Vec3i;
|
||||
typedef Vec3<float> Vec3f;
|
||||
|
||||
|
||||
#endif //TESTS_OPENGL_TYPES_H
|
Loading…
Add table
Reference in a new issue