首页 > 代码库 > NeHe OpenGL lesson 7

NeHe OpenGL lesson 7

HeHe OpenGL 第七节,纹理部分,光照初步。由于Mac 上面没有Page键,所以用G 、 H键替换


// NeHe OpenGL lession 7
// Texture Filters, Lighting & keyboard Control
//

/**
 *	Linux
 *  #include <GL/glut.h>
 *	#include <GL/gl.h>
 *  #include <GL/glu.h>
 */

#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#include <stdio.h>	
#include <unistd.h>	  // Header file for Sleeping
#include <stdlib.h> 	// Header for malloc/free

/* ascii code for various special keys */
#define ESCAPE 			27
#define PAGE_UP			73
#define PAGE_DOWN		81
#define UP_ARROW		72
#define DOWN_ARROW	80
#define LEFT_ARROW  75
#define RIGHT_ARROW 77
#define KEY_G  	    103
#define KEY_H       104

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

/* lighting on/off (1 == on, 0 == off) */
int light;

/* L pressed (1 == yes, 0 == no) */
int lp;

/* F pressed (1 == yes, 0 == no) */
int fp;

GLfloat xrot; 	// x rotation
GLfloat yrot; 	// y rotation
GLfloat xspeed;	// x rotation speed
GLfloat yspeed; // y rotation speed

GLfloat z = -5.0f; // depth into the screen.

/* white ambient light at half intensity (rgba) */
GLfloat lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };

/* super bright, full intensity fiffuse light. */
GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };

/* position of light (x, y, z, (position of light) */
GLfloat lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };

GLuint filter; 			/* which filter to use (nearest/linear/mipmapped) */
GLuint texture[3]; 	/* storage for 3 texture. */

/* Image type - contains height, white, 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.
// if mesa ever gets glaux, let me know
// BMP
int imageLoad(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 planes in image (must be 1)
  unsigned short int bpp; 		// number of bits per pixel (must be 24)
	char temp; 									// used to convert bgr to rgb color

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

	// 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 with from %s.\n", filename);
//		return 1;
	}

	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 1;
	}

	if (image->sizeX > 256)
		image->sizeX = 256;
	if (image->sizeY > 256)
		image->sizeY = 256;

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

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

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

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

	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 lesson 7