Browse Source

Put everything into a class

master
Skia 3 years ago
parent
commit
cea6eead81
6 changed files with 312 additions and 64 deletions
  1. +0
    -17
      camera.h
  2. +4
    -26
      main.cpp
  3. +53
    -18
      scene.cpp
  4. +15
    -3
      scene.h
  5. +38
    -0
      types.h
  6. +202
    -0
      vectors.h

+ 0
- 17
camera.h View File

@@ -48,20 +48,3 @@ class Camera
}
};

class Light
{
public:
/* Light constructor */
Light()
{
/* Initialize basic light */
const GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
const GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
const GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
const GLfloat light_position[] = { 1.0, -5.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
};

+ 4
- 26
main.cpp View File

@@ -10,16 +10,11 @@

#include <scene.h>
#include <camera.h>
#include <vectors.h>

const struct aiScene* scene;
aiVector3D scene_min, scene_max, scene_center;

SceneHandler scene;

Camera c;

float angle = 0;

/* Initialize OpenGL */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
@@ -38,7 +33,6 @@ void initGL() {

void display()
{
float scale;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

@@ -47,17 +41,7 @@ void display()
glLoadIdentity();
c.updateCamera();

scale= scene_max.x-scene_min.x;
scale= max(scene_max.y - scene_min.y,scale);
scale= max(scene_max.z - scene_min.z,scale);
scale= 2.f / scale;
glScalef(scale, scale, scale);

glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );

angle += 0.9;
glRotatef(angle, 0, 1, 0);
recursive_render(scene, scene->mRootNode);
scene.render();

glutSwapBuffers();
}
@@ -81,16 +65,11 @@ void reshape(GLsizei width, GLsizei height) {
int main(int argc, char** argv)
{
c = Camera();
scene = SceneHandler();

scene = aiImportFile(argv[1],aiProcessPreset_TargetRealtime_MaxQuality);

if (!scene) {
if (scene.load_file(argv[1])) {
return 1;
}
get_bounding_box(scene, &scene_min,&scene_max);
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
@@ -102,7 +81,6 @@ int main(int argc, char** argv)
initGL();
glutTimerFunc(0, timer, 0);
glutMainLoop();
aiReleaseImport(scene);
return 0;
}


+ 53
- 18
scene.cpp View File

@@ -13,8 +13,45 @@

#include <scene.h>

SceneHandler::~SceneHandler() {
aiReleaseImport(scene);
}

bool SceneHandler::load_file(char *fName) {
scene = aiImportFile(fName,aiProcessPreset_TargetRealtime_MaxQuality);

if (!scene) {
std::cerr << "Error importing file" << std::endl;
return 1;
}
get_bounding_box();
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;

float tmp = 0;
tmp = scene_max.x-scene_min.x;
tmp = max(scene_max.y - scene_min.y,tmp);
tmp = max(scene_max.z - scene_min.z,tmp);
scale = 2.f / tmp;

angle = 0;

return 0;
}

void SceneHandler::render() {
glScalef(scale, scale, scale);

glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );

angle += 0.9;
glRotatef(angle, 0, 1, 0);
recursive_render(scene->mRootNode);

}

void recursive_render (const struct aiScene *sc, const struct aiNode* nd)
void SceneHandler::recursive_render (const struct aiNode* nd)
{
unsigned int i;
unsigned int n = 0, t;
@@ -27,7 +64,7 @@ void recursive_render (const struct aiScene *sc, const struct aiNode* nd)

// draw all meshes assigned to this node
for (; n < nd->mNumMeshes; ++n) {
const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];

// apply_material(sc->mMaterials[mesh->mMaterialIndex]);

@@ -66,15 +103,13 @@ void recursive_render (const struct aiScene *sc, const struct aiNode* nd)

// draw all children
for (n = 0; n < nd->mNumChildren; ++n) {
recursive_render(sc, nd->mChildren[n]);
recursive_render(nd->mChildren[n]);
}

glPopMatrix();
}

void get_bounding_box_for_node (const struct aiScene *sc, const aiNode* nd,
aiVector3D* min,
aiVector3D* max,
void SceneHandler::get_bounding_box_for_node (const aiNode* nd,
aiMatrix4x4* trafo
){
aiMatrix4x4 prev;
@@ -84,38 +119,38 @@ void get_bounding_box_for_node (const struct aiScene *sc, const aiNode* nd,
aiMultiplyMatrix4(trafo,&nd->mTransformation);

for (; n < nd->mNumMeshes; ++n) {
const aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
for (t = 0; t < mesh->mNumVertices; ++t) {

aiVector3D tmp = mesh->mVertices[t];
aiTransformVecByMatrix4(&tmp,trafo);

min->x = min(min->x,tmp.x);
min->y = min(min->y,tmp.y);
min->z = min(min->z,tmp.z);
scene_min.x = min(scene_min.x,tmp.x);
scene_min.y = min(scene_min.y,tmp.y);
scene_min.z = min(scene_min.z,tmp.z);

max->x = max(max->x,tmp.x);
max->y = max(max->y,tmp.y);
max->z = max(max->z,tmp.z);
scene_max.x = max(scene_max.x,tmp.x);
scene_max.y = max(scene_max.y,tmp.y);
scene_max.z = max(scene_max.z,tmp.z);
}
}

for (n = 0; n < nd->mNumChildren; ++n) {
get_bounding_box_for_node(sc, nd->mChildren[n],min,max,trafo);
get_bounding_box_for_node(nd->mChildren[n], trafo);
}
*trafo = prev;
}



void get_bounding_box (const struct aiScene *sc, aiVector3D* min, aiVector3D* max)
void SceneHandler::get_bounding_box ()
{
aiMatrix4x4 trafo;
aiIdentityMatrix4(&trafo);

min->x = min->y = min->z = 1e10f;
max->x = max->y = max->z = -1e10f;
get_bounding_box_for_node(sc, sc->mRootNode,min,max,&trafo);
scene_min.x = scene_min.y = scene_min.z = 1e10f;
scene_max.x = scene_max.y = scene_max.z = -1e10f;
get_bounding_box_for_node(scene->mRootNode,&trafo);
}



+ 15
- 3
scene.h View File

@@ -8,8 +8,20 @@
#define min(x,y) (x<y?x:y)
#define max(x,y) (y>x?y:x)

void recursive_render (const struct aiScene *sc, const struct aiNode* nd);
void get_bounding_box_for_node (const struct aiScene *sc, const aiNode* nd, aiVector3D* min, aiVector3D* max, aiMatrix4x4* trafo);
void get_bounding_box (const struct aiScene *sc, aiVector3D* min, aiVector3D* max);
class SceneHandler
{
public:
virtual ~SceneHandler();
bool load_file (char * fName);
void render();
void recursive_render (const struct aiNode* nd);
void get_bounding_box_for_node (const aiNode* nd, aiMatrix4x4* trafo);
void get_bounding_box ();

private:
const struct aiScene* scene;
aiVector3D scene_min, scene_max, scene_center;
float scale, angle;
};

#endif

+ 38
- 0
types.h View File

@@ -0,0 +1,38 @@
#ifndef __TYPES_H__
#define __TYPES_H__
typedef char int8;
typedef short int16;
typedef int int32;
typedef long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef float float32;
typedef double float64;
typedef long double float80;
#define INT8_MIN_VALUE -128
#define INT8_MAX_VALUE 127
#define UINT8_MAX_VALUE 256
#define INT16_MIN_VALUE -32768
#define INT16_MAX_VALUE 32767
#define UINT16_MAX_VALUE 65535
#define INT32_MIN_VALUE -2147483648
#define INT32_MAX_VALUE 2147483647
#define UINT32_MAX_VALUE 4294967295
#define FLOAT32_MIN_VALUE -3.4e+38f
#define FLOAT32_MAX_VALUE 3.4e+38f
#define FLOAT64_MIN_VALUE -1.7e+308
#define FLOAT64_MAX_VALUE 1.7e+308
#endif

+ 202
- 0
vectors.h View File

@@ -0,0 +1,202 @@
#ifndef __VECTORS_H__
#define __VECTORS_H__
#include <math.h>
#include <string.h>
#include <iostream>
#include <types.h>
template <class T>
class AbstractVec2
{
public:
AbstractVec2() {
memset( this, 0, sizeof( T ) * 2 );
}
AbstractVec2( T X, T Y ) : x( X ), y( Y ) {
}
friend std::ostream& operator<<(std::ostream& os, const AbstractVec2<T>& v) {
return os << "(" << v.x << ", " << v.y << ")";
}
bool operator==( const AbstractVec2 & v ) {
return (x == v.x && y == v.y);
}
AbstractVec2 operator+( T v ) const {
return AbstractVec2( x+v, y+v );
}
AbstractVec2 operator-( T v ) const {
return AbstractVec2( x-v, y-v );
}
AbstractVec2 operator*( T v ) const {
return AbstractVec2( x*v, y*v );
}
AbstractVec2 operator/( T v ) const {
return AbstractVec2( x/v, y/v );
}
AbstractVec2 & operator+=( T v ) {
x += v;
y += v;
return (*this);
}
public:
T x, y;
};
template <class T>
class AbstractVec3
{
public:
AbstractVec3() {
memset( this, 0, sizeof( T )*3 );
}
AbstractVec3( T X, T Y, T Z ) : x( X ), y( Y ), z( Z ) {
}
friend std::ostream& operator<<(std::ostream& os, const AbstractVec3<T>& v) {
return os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
}
bool operator==( const AbstractVec3 & v ) const {
return (x == v.x && y == v.y && z == v.z);
}
bool operator!=( const AbstractVec3 & v ) const {
return (x != v.x || y != v.y || z != v.z);
}
AbstractVec3 & operator+=( const AbstractVec3 & vVector ) {
x += vVector.x;
y += vVector.y;
z += vVector.z;
return (*this);
}
AbstractVec3 & operator*=( T factor ) {
x *= factor;
y *= factor;
z *= factor;
return (*this);
}
AbstractVec3 operator+( const AbstractVec3 & vVector ) const {
return AbstractVec3(vVector.x + x, vVector.y + y, vVector.z + z);
}
AbstractVec3 operator-( const AbstractVec3 & vVector ) const {
return AbstractVec3(x - vVector.x, y - vVector.y, z - vVector.z);
}
AbstractVec3 operator-() const {
return AbstractVec3(-x, -y, -z);
}
AbstractVec3 operator*( T num ) const {
return AbstractVec3(x * num, y * num, z * num);
}
AbstractVec3 operator/( T num ) const {
num = 1/num;
return AbstractVec3(x * num, y * num, z * num);
}
AbstractVec3 crossProduct( const AbstractVec3 & vVector ) const {
AbstractVec3 vNormal;
// Valeur X: (V1.y * V2.z) - (V1.z * V2.y)
vNormal.x = ((y * vVector.z) - (z * vVector.y));
// Valeur Y: (V1.z * V2.x) - (V1.x * V2.z)
vNormal.y = ((z * vVector.x) - (x * vVector.z));
// Valeur Z: (V1.x * V2.y) - (V1.y * V2.x)
vNormal.z = ((x * vVector.y) - (y * vVector.x));
return vNormal;
}
const T dotProduct( const AbstractVec3 & vVector ) const {
return x*vVector.x + y*vVector.y + z*vVector.z;
}
static T length( const AbstractVec3 & vNormal ) {
return (T) sqrt((vNormal.x * vNormal.x) + (vNormal.y * vNormal.y) + (vNormal.z * vNormal.z));
}
float getSquaredEuclideanDistance( const AbstractVec3 & v ) const {
return SQ3DDistance( x, y, z, v.x, v.y, v.z );
}
float getEuclideanDistance( const AbstractVec3 & v ) const {
return SR3DDistance( x, y, z, v.x, v.y, v.z );
}
T length() const {
return (T) sqrt((x * x) + (y * y) + (z * z));
}
static AbstractVec3 normalize( const AbstractVec3 & vVector ) {
T len = 1.0f / length(vVector);
if (len != 0.0) {
return vVector * len;
} else {
return vVector;
}
}
void normalize() {
T len = length();
if( len != 0.0 ) {
len = 1/len;
x = x * len;
y = y * len;
z = z * len;
}
}
public:
T x, y, z;
};
template <class T>
class AbstractVec4
{
public:
T x, y, z, w;
};
typedef AbstractVec2<uint32> Vec2ui;
typedef AbstractVec2<int32> Vec2i;
typedef AbstractVec2<float32> Vec2;
typedef AbstractVec3<uint32> Vec3ui;
typedef AbstractVec3<int32> Vec3i;
typedef AbstractVec3<float32> Vec3;
typedef AbstractVec4<uint32> Vec4ui;
typedef AbstractVec4<int32> Vec4i;
typedef AbstractVec4<float32> Vec4;
#endif

Loading…
Cancel
Save