加载纹理
// 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 = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data\n");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data form %s.\n", filename);
return 0;
}
for (i = 0; i < size; i += 3) { // reverse all of the colors. (bgr -> rgb)
temp = image->data[i];
image->data[i] = image->data[i + 2];
image->data[i+2] = temp;
}
fclose(file);
// we're done.
return 1;
}
// Load Bitmaps and convert to texture
void loadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture.
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image.\n");
exit(0);
}
// /Users/jabez/Developer/mac/9_7/Data/lesson6/NeHe.bmp
// ./Data/lession6/NeHe.bmp
if (!ImageLoader("Data/lesson6/NeHe.bmp", image1)) {
exit(1);
}
// Create texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
// scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// scale linearly when image smalled than texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// 2D texture, level of detail 0 (normal), 3 components (red, green, blue),
// x size from image, y size from image
// border 0 (normal), rgb color data, unsigned byte data, and finally the data
// itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX,
image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
}
/* A general OpenGL initialization function.
Sets all of the initial parameters. */
// We clal this right after our OpenGL window is created.
void initGL(int width, int height) {
loadGLTextures(); // Load the Texture(s)
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glClearColor(0.0f, 0.0f, 1.0f, 0.0f); // Clear the background color to blue
glClearDepth(1.0); // Enables Clearing of the depth buffer
glDepthFunc(GL_LESS); // The type of depth test to do.
glEnable(GL_DEPTH_TEST); // Enables depth testing
glShadeModel(GL_SMOOTH); // Enables smooth color shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset the projection matrix.
gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f); // Calculate the aspect ratio of the window.
glMatrixMode(GL_MODELVIEW);
}
/* The function called when our window is resized (which shouldn't happend,
because we're fullscreen) */
void resizeGLScene(int width, int height) {
if (height == 0)
height = 1; // Prevent a divide by zero if the window is too small
glViewport(0, 0, width, height); // reset teh current viewport and perspective transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void drawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units into the screen.
glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate on the x axis
glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate on the y axis
glRotatef(zrot, 0.0f, 0.0f, 1.0f); // Rotate on the z axis
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose teh texture to use.
glBegin(GL_QUADS); // begin drawing a cube
// Front Face (note the texture's corners have to match the quads's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // bottom right of the texture and quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // top left of the texture and quad
// Back face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // bottom left
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // top right
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // bottom left
// Left face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // top left
glEnd(); // done with the polygon.
xrot += 1.0f;
yrot += 1.0f;
zrot += 1.0f;
// since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers();
}
/* The function claled whever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this procedure */
usleep(100);
/* If escape is pressed, kill everything. */
if (key == ESCAPE)
{
/* shut down our window */
glutDestroyWindow(window);
exit(0); /* exit the program ... normal termination. */
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ..NeHe '99");
glutDisplayFunc(&drawGLScene);
// glutFullScreen()
glutIdleFunc(&drawGLScene);
glutReshapeFunc(&resizeGLScene);
glutKeyboardFunc(&keyPressed);
initGL(640, 480);
glutMainLoop(); /* Start Event Processing Engine. */
return 0;
}
原文:http://blog.csdn.net/jabezlee/article/details/39212443