Определение состава и структуры автоматизированной системы визуализации технологических процессов, страница 5

 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

}


Приложение 2 (Загрузка файла текстуры)

#include <stdio.h>

#include <windows.h>

#include <GL/glut.h>

#include "texture.h"

/**********************************************************

 *

 * VARIABLES DECLARATION

 *

 *********************************************************/

int num_texture=-1; //Counter to keep track of the last loaded texture

/**********************************************************

 *

 * FUNCTION LoadBitmap(char *)

 *

 * This function loads a bitmap file and return the OpenGL reference ID to use that texture

 *

 *********************************************************/

int LoadBitmap(char *filename)

{

 int i, j=0; //Index variables

 FILE *l_file; //File pointer

 unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture

 // windows.h gives us these types to work with the Bitmap files

 BITMAPFILEHEADER fileheader;

 BITMAPINFOHEADER infoheader;

 RGBTRIPLE rgb;

 num_texture++; // The counter of the current texture is increased

 if( (l_file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading

 fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader

 fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader

 fread(&infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader

 // Now we need to allocate the memory for our image (width * height * color deep)

 l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);

 // And fill it with zeros

 memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

 // At this point we can read every pixel of the image

 for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)

 { 

 // We load an RGB value from the file

 fread(&rgb, sizeof(rgb), 1, l_file);

 // And store it

 l_texture[j+0] = rgb.rgbtRed; // Red component

 l_texture[j+1] = rgb.rgbtGreen; // Green component

 l_texture[j+2] = rgb.rgbtBlue; // Blue component

 l_texture[j+3] = 255; // Alpha value

 j += 4; // Go to the next position

 }

 fclose(l_file); // Closes the file stream

 glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

 // The next commands sets the texture parameters