Home | API | File List | Examples | Download

cube.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 
00024 #include <lib3ds.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <math.h>
00029 
00030 #ifndef MPI
00031 #define M_PI 3.14159265358979323846
00032 #endif
00033 
00034 
00035 static float g_vertices[8][3] = {
00036     { -10.0, -10.0,  15.0 },
00037     {  10.0, -10.0,  15.0 },
00038     {  10.0,  10.0,  15.0 },
00039     { -10.0,  10.0,  15.0 },
00040     { -10.0, -10.0, -15.0 },
00041     {  10.0, -10.0, -15.0 },
00042     {  10.0,  10.0, -15.0 },
00043     { -10.0,  10.0, -15.0 }
00044 };
00045 
00046 
00047 // Texture coodinate origin (0,0) is in bottom-left corner!
00048 static float g_texcoords[8][3] = {
00049     {  0.00, 1.0 },
00050     {  0.25, 1.0 },
00051     {  0.50, 1.0 },
00052     {  0.75, 1.0 },
00053     {  0.00, 0.0 },
00054     {  0.25, 0.0 },
00055     {  0.50, 0.0 },
00056     {  0.75, 0.0 }
00057 };
00058 
00059 
00060 // CCW
00061 static unsigned short g_indices[12][3] = {
00062     { 0, 5, 1 },
00063     { 0, 4, 5 },
00064     { 1, 6, 2 },
00065     { 1, 5, 6 },
00066     { 2, 6, 7 },
00067     { 2, 7, 3 },
00068     { 0, 3, 7 },
00069     { 0, 7, 4 },
00070     { 0, 1, 2 },
00071     { 0, 2, 3 },
00072     { 4, 7, 6 },
00073     { 4, 6, 5 }
00074 };
00075 
00076 
00077 int main(int argc, char **argv) {
00078     Lib3dsFile *file = lib3ds_file_new();
00079     file->frames = 360;
00080     
00081     {
00082         Lib3dsMaterial *mat = lib3ds_material_new("c_tex");
00083         lib3ds_file_insert_material(file, mat, -1);
00084         strcpy(mat->texture1_map.name, "cube.tga");
00085         mat->texture1_map.percent = 1.0;
00086 
00087         mat = lib3ds_material_new("c_red");
00088         lib3ds_file_insert_material(file, mat, -1);
00089         mat->diffuse[0] = 1.0;
00090         mat->diffuse[1] = 0.0;
00091         mat->diffuse[2] = 0.0;
00092 
00093         mat = lib3ds_material_new("c_blue");
00094         lib3ds_file_insert_material(file, mat, -1);
00095         mat->diffuse[0] = 0.0;
00096         mat->diffuse[1] = 0.0;
00097         mat->diffuse[2] = 1.0;
00098     }
00099 
00100     {
00101         int i, j;
00102         Lib3dsMesh *mesh = lib3ds_mesh_new("cube");
00103         Lib3dsMeshInstanceNode *inst;        
00104         lib3ds_file_insert_mesh(file, mesh, -1);
00105 
00106         lib3ds_mesh_resize_vertices(mesh, 8, 1, 0);
00107         for (i = 0; i < 8; ++i) {
00108             lib3ds_vector_copy(mesh->vertices[i], g_vertices[i]);
00109             mesh->texcos[i][0] = g_texcoords[i][0];
00110             mesh->texcos[i][1] = g_texcoords[i][1];
00111         }
00112 
00113         lib3ds_mesh_resize_faces(mesh, 12);
00114         for (i = 0; i < 12; ++i) {
00115             for (j = 0; j < 3; ++j) {
00116                 mesh->faces[i].index[j] = g_indices[i][j];
00117             }
00118         }
00119 
00120         for (i = 0; i < 8; ++i) {
00121             mesh->faces[i].material = 0;
00122         }
00123         for (i = 0; i < 2; ++i) {
00124             mesh->faces[8+i].material = 1;
00125         }
00126         for (i = 0; i < 2; ++i) {
00127             mesh->faces[10+i].material = 2;
00128         }
00129 
00130         inst = lib3ds_node_new_mesh_instance(mesh, "01", NULL, NULL, NULL);
00131         lib3ds_file_append_node(file, (Lib3dsNode*)inst, NULL);
00132     }
00133 
00134     {
00135         Lib3dsCamera *camera;
00136         Lib3dsCameraNode *n;
00137         Lib3dsTargetNode *t;
00138         int i;
00139 
00140         camera = lib3ds_camera_new("camera01");
00141         lib3ds_file_insert_camera(file, camera, -1);
00142         lib3ds_vector_make(camera->position, 0.0, -100, 0.0);
00143         lib3ds_vector_make(camera->target, 0.0, 0.0, 0.0);
00144 
00145         n = lib3ds_node_new_camera(camera);
00146         t = lib3ds_node_new_camera_target(camera);
00147         lib3ds_file_append_node(file, (Lib3dsNode*)n, NULL);
00148         lib3ds_file_append_node(file, (Lib3dsNode*)t, NULL);
00149 
00150         lib3ds_track_resize(&n->pos_track, 37);
00151         for (i = 0; i <= 36; i++) {
00152             n->pos_track.keys[i].frame = 10 * i;
00153             lib3ds_vector_make(n->pos_track.keys[i].value, 
00154                 (float)(100.0 * cos(2 * M_PI * i / 36.0)), 
00155                 (float)(100.0 * sin(2 * M_PI * i / 36.0)), 
00156                 50.0
00157             );
00158         }
00159     }
00160 
00161     if (!lib3ds_file_save(file, "cube.3ds")) {
00162         fprintf(stderr, "ERROR: Saving 3ds file failed!\n");
00163     }
00164     lib3ds_file_free(file);
00165 }