Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gilberto.vollpe@gmail.com committed May 30, 2013
1 parent 7a2a3b8 commit 6dce8ae
Show file tree
Hide file tree
Showing 31 changed files with 2,106 additions and 0 deletions.
165 changes: 165 additions & 0 deletions cg/Trabalho2CG/3dsloader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <stdio.h>
#include <stdlib.h>
#include "Types3D.h"
#include "3dsloader.h"

#include <sys/stat.h>

/**********************************************************
*
* FUNCTION Load3DS (obj_type_ptr, char *)
*
* This function loads a mesh from a 3ds file.
* Please note that we are loading only the vertices, polygons and mapping lists.
* If you need to load meshes with advanced features as for example:
* multi objects, materials, lights and so on, you must insert other chunk parsers.
*
*********************************************************/


long filelength(int f) {
struct stat buf;

fstat(f, &buf);

return (buf.st_size);
}

char Load3DS(obj_type_ptr p_object, char *p_filename) {
int i; //Index variable

FILE *l_file; //File pointer

unsigned short l_chunk_id; //Chunk identifier
unsigned int l_chunk_lenght; //Chunk lenght

unsigned char l_char; //Char variable
unsigned short l_qty; //Number of elements in each chunk

unsigned short l_face_flags; //Flag that stores some face information

if ((l_file = fopen(p_filename, "rb")) == NULL) return 0; //Open the file

while (ftell(l_file) < filelength(fileno(l_file))) //Loop to scan the whole file
//while(!EOF)
{
//getch(); //Insert this command for debug (to wait for keypress for each chuck reading)

fread(&l_chunk_id, 2, 1, l_file); //Read the chunk header
// printf("ChunkID: %x\n", l_chunk_id);
fread(&l_chunk_lenght, 4, 1, l_file); //Read the lenght of the chunk
// printf("ChunkLenght: %x\n", l_chunk_lenght);

switch (l_chunk_id) {
//----------------- MAIN3DS -----------------
// Description: Main chunk, contains all the other chunks
// Chunk ID: 4d4d
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x4d4d:
break;

//----------------- EDIT3DS -----------------
// Description: 3D Editor chunk, objects layout info
// Chunk ID: 3d3d (hex)
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x3d3d:
break;

//--------------- EDIT_OBJECT ---------------
// Description: Object block, info for each object
// Chunk ID: 4000 (hex)
// Chunk Lenght: len(object name) + sub chunks
//-------------------------------------------
case 0x4000:
i = 0;
do {
fread(&l_char, 1, 1, l_file);
p_object->name[i] = l_char;
i++;
} while (l_char != '\0' && i < 20);
break;

//--------------- OBJ_TRIMESH ---------------
// Description: Triangular mesh, contains chunks for 3d mesh info
// Chunk ID: 4100 (hex)
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x4100:
break;

//--------------- TRI_VERTEXL ---------------
// Description: Vertices list
// Chunk ID: 4110 (hex)
// Chunk Lenght: 1 x unsigned short (number of vertices)
// + 3 x float (vertex coordinates) x (number of vertices)
// + sub chunks
//-------------------------------------------
case 0x4110:
fread(&l_qty, sizeof (unsigned short), 1, l_file);
p_object->vertices_qty = l_qty;
// printf("Number of vertices: %d\n", l_qty);
for (i = 0; i < l_qty; i++) {
fread(&p_object->vertex[i].x, sizeof (float), 1, l_file);
// printf("Vertices list x: %f\n", p_object->vertex[i].x);
fread(&p_object->vertex[i].y, sizeof (float), 1, l_file);
// printf("Vertices list y: %f\n", p_object->vertex[i].y);
fread(&p_object->vertex[i].z, sizeof (float), 1, l_file);
// printf("Vertices list z: %f\n", p_object->vertex[i].z);
}
break;

//--------------- TRI_FACEL1 ----------------
// Description: Polygons (faces) list
// Chunk ID: 4120 (hex)
// Chunk Lenght: 1 x unsigned short (number of polygons)
// + 3 x unsigned short (polygon points) x (number of polygons)
// + sub chunks
//-------------------------------------------
case 0x4120:
fread(&l_qty, sizeof (unsigned short), 1, l_file);
p_object->polygons_qty = l_qty;
// printf("Number of polygons: %d\n", l_qty);
for (i = 0; i < l_qty; i++) {
fread(&p_object->polygon[i].a, sizeof (unsigned short), 1, l_file);
// printf("Polygon point a: %d\n", p_object->polygon[i].a);
fread(&p_object->polygon[i].b, sizeof (unsigned short), 1, l_file);
// printf("Polygon point b: %d\n", p_object->polygon[i].b);
fread(&p_object->polygon[i].c, sizeof (unsigned short), 1, l_file);
// printf("Polygon point c: %d\n", p_object->polygon[i].c);
fread(&l_face_flags, sizeof (unsigned short), 1, l_file);
// printf("Face flags: %x\n", l_face_flags);
}
break;

//------------- TRI_MAPPINGCOORS ------------
// Description: Vertices list
// Chunk ID: 4140 (hex)
// Chunk Lenght: 1 x unsigned short (number of mapping points)
// + 2 x float (mapping coordinates) x (number of mapping points)
// + sub chunks
//-------------------------------------------
case 0x4140:
fread(&l_qty, sizeof (unsigned short), 1, l_file);
for (i = 0; i < l_qty; i++) {
fread(&p_object->mapcoord[i].u, sizeof (float), 1, l_file);
// printf("Mapping list u: %f\n", p_object->mapcoord[i].u);
fread(&p_object->mapcoord[i].v, sizeof (float), 1, l_file);
// printf("Mapping list v: %f\n", p_object->mapcoord[i].v);
}
break;

//----------- Skip unknow chunks ------------
//We need to skip all the chunks that currently we don't use
//We use the chunk lenght information to set the file pointer
//to the same level next chunk
//-------------------------------------------
default:
fseek(l_file, l_chunk_lenght - 6, SEEK_CUR);
}
}

fclose(l_file); // Closes the file stream
return (1); // Returns ok
}
19 changes: 19 additions & 0 deletions cg/Trabalho2CG/3dsloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef THREEDSLOADER_H

#define THREEDSLOADER_H

/**********************************************************
*
* FUNCTION Load3DS (obj_type_ptr, char *)
*
* This function loads a mesh from a 3ds file.
* Please note that we are loading only the vertices, polygons and mapping lists.
* If you need to load meshes with advanced features as for example:
* multi objects, materials, lights and so on, you must insert other chunk parsers.
*
*********************************************************/

extern char Load3DS(obj_type_ptr ogg, char *filename);

#endif

97 changes: 97 additions & 0 deletions cg/Trabalho2CG/BasicFramework.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* File: BasicFramework.cpp
* Author: gilvolpe
*
* Created on March 29, 2013, 6:28 PM
*/

#include "BasicFramework.h"

BasicFramework *BasicFramework::instance = 0;

void BasicFramework::setInstance(BasicFramework* basicFramework) {
instance = basicFramework;
}

void BasicFramework::handleKeyPressWrapper(unsigned char key, int x, int y) {
instance->handleKeyPress(key, x, y);
}

void BasicFramework::handleKeyPressWrapperNumber(int key, int x, int y) {
instance->handleKeyPressNumber(key, x, y);
}

void BasicFramework::displayWrapper() {
instance->display();
}

void BasicFramework::timerWrapper(GLint unused) {
instance->timer(unused);
}

void BasicFramework::handleMouseWrapper(GLint state, GLint button, GLint x, GLint y) {
instance->handleMouse(state, button, x, y);
}

void BasicFramework::handleMouseMotionWrapper(GLint x, GLint y) {
instance->handleMouseMotion(x, y);
}

void BasicFramework::doSomethingInTimerWrapper() {
instance->doSomethingInTime();
}

void BasicFramework::timer(GLint unused) {
doSomethingInTimerWrapper();
glutPostRedisplay();
glutTimerFunc(TIME_UPDATE_MS, timerWrapper, unused);
}

/*
* Metodo para deixar o programa em loop
*/
void BasicFramework::start(int argc, char *argv[]) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(sizeX, sizeY); // definindo o tamanho da tela
glutCreateWindow("Segundo Trabalho de Computacao Grafica");
glutInitWindowPosition(0,0);

//Inicializa as funções necessárias ao openGL
glutDisplayFunc(displayWrapper); // metodo que faz os desenhos
glutIdleFunc(displayWrapper);
glutKeyboardFunc(handleKeyPressWrapper);
glutSpecialFunc(handleKeyPressWrapperNumber);
glutMouseFunc(handleMouseWrapper);
glutMotionFunc(handleMouseMotionWrapper);
glutTimerFunc(TIME_UPDATE_MS, timerWrapper, 0);

int screen_width=SIDE_SIZE; // We obtain the new screen width values and store it
int screen_height=SIDE_SIZE; // Height value

glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);

glViewport(0,0,screen_width,screen_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f);

glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_TEXTURE_2D);

glutMainLoop();
}

BasicFramework::BasicFramework() {
this->sizeX = SIDE_SIZE;
this->sizeY = SIDE_SIZE;
}

BasicFramework::~BasicFramework() {

}
48 changes: 48 additions & 0 deletions cg/Trabalho2CG/BasicFramework.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* File: BasicFramework.h
* Author: gilvolpe
*
* Created on March 29, 2013, 6:28 PM
*/

#ifndef BASICFRAMEWORK_H
#define BASICFRAMEWORK_H

#include "basicInformation.h"

class BasicFramework {
protected:
static BasicFramework *instance;

private:
void setColor(GLint color);
GLint sizeX, sizeY;

public:

BasicFramework();
virtual ~BasicFramework();

static void setInstance(BasicFramework *basicFramework);
static void handleKeyPressWrapper(unsigned char key, int x, int y);
static void handleKeyPressWrapperNumber(int key, int x, int y);
static void handleMouseWrapper(GLint state,GLint button,GLint x,GLint y);
static void handleMouseMotionWrapper(GLint x,GLint y);
static void displayWrapper();
static void timerWrapper(GLint unused);
static void doSomethingInTimerWrapper();

virtual void display() = 0;
virtual void handleKeyPress(unsigned char key, GLint x, GLint y) = 0;
virtual void handleKeyPressNumber(int key, GLint x, GLint y) = 0;
virtual void handleMouse(GLint button,GLint state,GLint x,GLint y) = 0;
virtual void handleMouseMotion(GLint x,GLint y) = 0;
virtual void drawInterest() = 0;
virtual void doSomethingInTime() = 0;

void timer(GLint unused);
void start(int argc, char *argv[]);
};

#endif /* BASICFRAMEWORK_H */

Loading

0 comments on commit 6dce8ae

Please sign in to comment.