首页 > 代码库 > neHe OpenGL lession 6

neHe OpenGL lession 6

加载纹理

// lession6
// Mac OS X / Linux
// linux <GL/glut.h> <GL/gl.h> <GL/glu.h>

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* ascii code for the escape key */
#define ESCAPE 27

/* the number of our GLUT window */
int window;

/* float for x rotation, y rotation, z rotation */
float xrot, yrot, zrot;

/* storage for one texture */
GLuint texture[1];

/* Imaget type - contains height, width, and data */
struct Image {
	unsigned long sizeX;
	unsigned long sizeY;
	char *data;
};

typedef struct Image Image;

// quick and dirty bitmap loader... for 24 bit bitmaps with 1 plane only.
// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
int ImageLoader(char *filename, Image *image) {
	FILE *file; 
	unsigned long size; 			// size of the image in bytes
	unsigned long i;				// standard counter.
	unsigned short int planes; 		// number of plances in image (must be 1)
	unsigned short int bpp; 		// number of bits per pixel (must be 24)
	char temp;						// temporary color storage for bgr-rgb conversion.

	// make sure the file is there.
	if ((file = fopen(filename, "rb")) == NULL) {
		printf("File not found : %s\n", filename);
		return 0;
	}

	// seek through the bmp header, up to the width/height:
    fseek(file, 18, SEEK_CUR);

    // read the width
    if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
		printf("Error reading width from %s.\n", filename);
		return 0;
    }
    printf("Width of %s: %lu\n", filename, image->sizeX);
    
    // read the height 
    if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
	printf("Error reading height from %s.\n", filename);
	// return 0;
    }
    printf("Height of %s: %lu\n", filename, image->sizeY);

    // image->sizeX = image->sizeY = 256;

    // if (image->sizeX != 256 || )

	// calculate the size (assuming 24 bits or 3 bytes per pixel).
	size = image->sizeX * image->sizeY * 3;

	// read the plances
	if ((fread(&planes, 2, 1, file)) != 1) {
		printf("Error reading planes from %s.\n", filename);
		return 0;
	}

	if (planes != 1) {
		printf("Plances from %s is not 1: %u\n", filename, planes);
		return 0;
	}

	// read the bpp
	if ((i == fread(&bpp, 2, 1, file)) != 1) {
		printf("Error reading bpp from %s.\n", filename);
		return 0;
	}

	if (bpp != 24) {
		printf("Bpp from %s is not 24: %u\n", filename, bpp);
		return 0;
	}

	// Seek past the rest of the bitmap header.
	fseek(file, 24, SEEK_CUR);

	// read the data.
	image->data = http://www.mamicode.com/(char *) malloc(size);>























neHe OpenGL lession 6