Новичок
Профиль
Группа: Участник
Сообщений: 2
Регистрация: 23.11.2007
Репутация: нет Всего: нет
|
VC++ 7.1 ----------------------- Функция загрузки 3ds main.h ----------------------------------------------------------------------------- | Код | #define MAX_VERTICES 65535 // Max number of vertices (for each object) #define MAX_POLYGONS 65535 // Max number of polygons (for each object)
// Our vertex type typedef struct{ float x,y,z; }vertex_type;
// The polygon (triangle), 3 numbers that aim 3 vertices typedef struct{ int a,b,c; }polygon_type;
// The mapcoord type, 2 texture coordinates for each vertex typedef struct{ float u,v; }mapcoord_type;
// The object type typedef struct { char name[20]; int vertices_qty; int polygons_qty;
vertex_type vertex[MAX_VERTICES]; polygon_type polygon[MAX_POLYGONS]; mapcoord_type mapcoord[MAX_VERTICES]; int id_texture; } obj_type, *obj_type_ptr;
extern char Load3DS (obj_type_ptr ogg, char *filename);
-------------------------------------------- function code -------------------------------------------- main.cpp ---------------------------------------------
char Load3DS (obj_type_ptr p_object, char *p_filename) { int i; FILE *l_file; unsigned short l_chunk_id; unsigned int l_chunk_lenght; unsigned char l_char; unsigned short l_qty; unsigned short l_face_flags; if ((l_file=fopen (p_filename, "rb"))== NULL) return 0; while (ftell (l_file) < filelength (fileno (l_file))) { fread (&l_chunk_id, 2, 1, l_file); fread (&l_chunk_lenght, 4, 1, l_file); 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; for (i=0; i<l_qty; i++) { fread (&p_object->vertex[i].x, sizeof(float), 1, l_file); fread (&p_object->vertex[i].y, sizeof(float), 1, l_file); fread (&p_object->vertex[i].z, sizeof(float), 1, l_file); } 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; for (i=0; i<l_qty; i++) { fread (&p_object->polygon[i].a, sizeof (unsigned short), 1, l_file); fread (&p_object->polygon[i].b, sizeof (unsigned short), 1, l_file); fread (&p_object->polygon[i].c, sizeof (unsigned short), 1, l_file); fread (&l_face_flags, sizeof (unsigned short), 1, l_file); } 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); fread (&p_object->mapcoord[i].v, sizeof (float), 1, l_file); } 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 }
|
------------------------------------------------------ Затем В проге объяви object для 3D объекта obj_type object; --------------------------------------------- Ну и на этапе инициализации (у меня в функции InitScene() ) пиши | Код | Load3DS (&object,"sun.3ds"); Грузим солнце KOOF=0.7; кооф сжатия (чтоб не ебатца :) ) SUN=glGenLists(1); Делаем лист glNewList(SUN, GL_COMPILE); начинаем
|
Расчет нормалей!! | Код | int l_index; for (l_index=0;l_index<object.polygons_qty;l_index++) { float vx1,vy1,vz1,nx,ny,nz; float vx2,vy2,vz2,wrki; vx1 = object.vertex[ object.polygon[l_index].a ].x- object.vertex[ object.polygon[l_index].b ].x; vy1 = object.vertex[ object.polygon[l_index].a ].y- object.vertex[ object.polygon[l_index].b ].y; vz1 = object.vertex[ object.polygon[l_index].a ].z- object.vertex[ object.polygon[l_index].b ].z;
vx2 = object.vertex[ object.polygon[l_index].b ].x- object.vertex[ object.polygon[l_index].c ].x; vy2 = object.vertex[ object.polygon[l_index].b ].y- object.vertex[ object.polygon[l_index].c ].y; vz2 = object.vertex[ object.polygon[l_index].b ].z- object.vertex[ object.polygon[l_index].c ].z; // вектор перпендикулярен центру треугольника nx = vy1 * vz2 - vz1 * vy2; ny = vz1 * vx2 - vx1 * vz2; nz = vx1 * vy2 - vy1 * vx2; // получаем унитарный вектор единичной длины wrki = sqrt (nx * nx + ny * ny + nz * nz); if (wrki==0) wrki=1; // для предотвращения деления на ноль vx1 = nx / wrki; vy1 = ny / wrki; vz1 = nz / wrki; // вот они нормали! :)) glNormal3f(vx1,vy1,vz1); //--------------------------------------------------- glBegin(GL_TRIANGLES); //----------------- FIRST VERTEX ----------------- // Texture glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u, object.mapcoord[ object.polygon[l_index].a ].v); // Coordinates glVertex3f( object.vertex[ object.polygon[l_index].a ].x*KOOF, object.vertex[ object.polygon[l_index].a ].y*KOOF, object.vertex[ object.polygon[l_index].a ].z*KOOF); //----------------- SECOND VERTEX ----------------- // Texture glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u, object.mapcoord[ object.polygon[l_index].b ].v); // Coordinates glVertex3f( object.vertex[ object.polygon[l_index].b ].x*KOOF, object.vertex[ object.polygon[l_index].b ].y*KOOF, object.vertex[ object.polygon[l_index].b ].z*KOOF); //----------------- THIRD VERTEX ----------------- // Texture glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u, object.mapcoord[ object.polygon[l_index].c ].v); // Coordinates glVertex3f( object.vertex[ object.polygon[l_index].c ].x*KOOF, object.vertex[ object.polygon[l_index].c ].y*KOOF, object.vertex[ object.polygon[l_index].c ].z*KOOF); glEnd(); } glEndList(); заканчиваем
|
Это сообщение отредактировал(а) Alexeis - 1.4.2009, 11:03
|