=== modified file 'model.cpp'
@@ -29,15 +29,15 @@
pMesh->mVertexQty = 3 * mPolygonQty;
pMesh->mPolygonQty = mPolygonQty;
pMesh->mMode = GL_TRIANGLES;
-
+
pMesh->mVertex = new Vertex[pMesh->mVertexQty];
-
+
for(unsigned i = 0; i < pMesh->mVertexQty; i += 3)
{
pMesh->mVertex[i + 0].v = mVertex[mPolygon[i / 3].mA].v;
pMesh->mVertex[i + 1].v = mVertex[mPolygon[i / 3].mB].v;
pMesh->mVertex[i + 2].v = mVertex[mPolygon[i / 3].mC].v;
-
+
pMesh->mVertex[i + 0].n = mVertex[mPolygon[i / 3].mA].n;
pMesh->mVertex[i + 1].n = mVertex[mPolygon[i / 3].mB].n;
pMesh->mVertex[i + 2].n = mVertex[mPolygon[i / 3].mC].n;
@@ -46,7 +46,7 @@
pMesh->mVertex[i + 1].t = mVertex[mPolygon[i / 3].mB].t;
pMesh->mVertex[i + 2].t = mVertex[mPolygon[i / 3].mC].t;
}
-
+
#ifdef _DEBUG
printf("[ Done ]\n");
#endif
@@ -79,7 +79,7 @@
{
Vector3f center;
Vector3f max = mVertex[0].v, min = mVertex[0].v;
-
+
for(unsigned i = 1; i < mVertexQty; i++)
{
if(mVertex[i].v.x > max.x) max.x = mVertex[i].v.x;
@@ -105,150 +105,143 @@
int Model::load_3ds(const char *pFileName)
{
+ int i;
+ FILE *l_file;
+ unsigned short l_chunk_id;
+ unsigned int l_chunk_length;
+ unsigned char l_char;
+ unsigned short l_qty;
+ size_t nread;
+
#ifdef _DEBUG
printf("Loading model from 3ds file... ");
#endif
- 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
-
- if ((l_file=fopen (pFileName, "rb"))== NULL)
- {
+
+ if ((l_file=fopen (pFileName, "rb"))== NULL) {
#ifdef _DEBUG
- printf("[ Fail ]\n");
+ printf("[ Fail ]\n");
#else
printf("Could not open 3ds file\n");
#endif
- return 0;
- }
-
- 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
- fread (&l_chunk_lenght, 4, 1, l_file); //Read the lenght of the chunk
-
- switch (l_chunk_id)
+ return 0;
+ }
+
+ // Loop to scan the whole file
+ while (ftell (l_file) < filelength (fileno (l_file))) {
+ // Read the chunk header
+ nread = fread (&l_chunk_id, 2, 1, l_file);
+ //Read the lenght of the chunk
+ nread = fread (&l_chunk_length, 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);
+ //----------------- 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 {
+ nread = fread (&l_char, 1, 1, l_file);
mName[i] = l_char;
- i++;
+ 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);
+ 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:
+ nread = fread (&l_qty, sizeof (unsigned short), 1, l_file);
mVertexQty = l_qty;
mVertex = new Vertex[mVertexQty];
- for (i=0; i<l_qty; i++)
- {
- fread (&mVertex[i].v.x, sizeof(float), 1, l_file);
- fread (&mVertex[i].v.y, sizeof(float), 1, l_file);
- fread (&mVertex[i].v.z, sizeof(float), 1, l_file);
- }
- break;
+ for (i = 0; i < l_qty; i++) {
+ nread = fread (&mVertex[i].v.x, sizeof(float), 1, l_file);
+ nread = fread (&mVertex[i].v.y, sizeof(float), 1, l_file);
+ nread = fread (&mVertex[i].v.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);
+ //--------------- 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:
+ nread = fread (&l_qty, sizeof (unsigned short), 1, l_file);
mPolygonQty = l_qty;
mPolygon = new Polygon[mPolygonQty];
- for (i=0; i<l_qty; i++)
- {
- fread (&mPolygon[i].mA, sizeof (unsigned short), 1, l_file);
- fread (&mPolygon[i].mB, sizeof (unsigned short), 1, l_file);
- fread (&mPolygon[i].mC, sizeof (unsigned short), 1, l_file);
- fread (&mPolygon[i].mFaceFlags, 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 (&mVertex[i].t.u, sizeof (float), 1, l_file);
- fread (&mVertex[i].t.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
+ for (i = 0; i < l_qty; i++) {
+ nread = fread (&mPolygon[i].mA, sizeof (unsigned short), 1, l_file);
+ nread = fread (&mPolygon[i].mB, sizeof (unsigned short), 1, l_file);
+ nread = fread (&mPolygon[i].mC, sizeof (unsigned short), 1, l_file);
+ nread = fread (&mPolygon[i].mFaceFlags, 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:
+ nread = fread (&l_qty, sizeof (unsigned short), 1, l_file);
+ for (i = 0; i < l_qty; i++) {
+ nread = fread (&mVertex[i].t.u, sizeof (float), 1, l_file);
+ nread = fread (&mVertex[i].t.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_length - 6, SEEK_CUR);
+ }
+ }
+ fclose(l_file); // Closes the file stream
#ifdef _DEBUG
printf("[ Success ]\n");
- printf(" Model Information\n");
- printf(" Name: %s\n", mName);
- printf(" Vertex count: %d\n", mVertexQty);
- printf(" Polygon count: %d\n", mPolygonQty);
-#endif
+ printf(" Model Information\n");
+ printf(" Name: %s\n", mName);
+ printf(" Vertex count: %d\n", mVertexQty);
+ printf(" Polygon count: %d\n", mPolygonQty);
+#endif
return 1;
}
=== modified file 'texture.cpp'
@@ -3,11 +3,10 @@
int load_texture(const char pFilename[], GLuint *pTexture)
{
SDL_Surface *surface;
- GLenum texture_format;
+ GLenum texture_format = GL_RGB;
GLint nOfColors;
- if ((surface = SDL_LoadBMP(pFilename)))
- {
+ if ((surface = SDL_LoadBMP(pFilename))) {
if ((surface->w & (surface->w - 1)) != 0)
printf("warning: image.bmp's width is not a power of 2\n");
@@ -15,24 +14,20 @@
printf("warning: image.bmp's height is not a power of 2\n");
nOfColors = surface->format->BytesPerPixel;
- if (nOfColors == 4)
- {
+ if (nOfColors == 4) {
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
- else
- {
- if (nOfColors == 3)
- {
+ else {
+ if (nOfColors == 3) {
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
- else
- {
+ else {
printf("warning: the image is not truecolor.. this will probably break\n");
}
}
@@ -40,49 +35,28 @@
glGenTextures(3, pTexture);
// Create Nearest Filtered Texture
- glBindTexture(GL_TEXTURE_2D, pTexture[0]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
-
- // Create Linear Filtered Texture
- glBindTexture(GL_TEXTURE_2D, pTexture[1]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
+ glBindTexture(GL_TEXTURE_2D, pTexture[0]);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
+ texture_format, GL_UNSIGNED_BYTE, surface->pixels);
+
+ // Create Linear Filtered Texture
+ glBindTexture(GL_TEXTURE_2D, pTexture[1]);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
+ texture_format, GL_UNSIGNED_BYTE, surface->pixels);
// Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, pTexture[2]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, surface->w, surface->h, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
-
-/*
- switch (filter)
- {
- case 0:
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
- break;
-
- case 1:
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
- break;
-
- case 2:
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
- break;
- }
-*/
+ glBindTexture(GL_TEXTURE_2D, pTexture[2]);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
+
+ gluBuild2DMipmaps(GL_TEXTURE_2D, 3, surface->w, surface->h,
+ texture_format, GL_UNSIGNED_BYTE, surface->pixels);
}
- else
- {
+ else {
fprintf(stderr, "SDL could not load image.bmp: %s\n", SDL_GetError());
return 0;
}