#include <GL/glew.h>
#include <glut.h>
#include "FreeImage.h"
#include <stdio.h>
#include <malloc.h>
#pragma comment(lib, "FreeImage.lib")
GLuint texture; // Storage For One Texture ( NEW )
/*void loadTexture()
{
FIBITMAP* img=FreeImage_Load(FIF_PNG, "asina.png");
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(img), FreeImage_GetHeight(img), 0, GL_RGBA, GL_BYTE, (const void*)FreeImage_GetBits(img));
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
FreeImage_Unload(img);
}*/
typedef struct
{
int w;
int h;
unsigned char *buf;
GLuint rgb_mode;
}GLBITMAP;
/*static GLuint S_png_tex = 0;
static GLuint S_jpg_tex = 0;
static GLuint S_bmp_tex = 0;
static GLuint S_tga_tex = 0;
static GLuint S_ico_tex = 0; */
GLBITMAP * FIBitmap2GLBitmap(FIBITMAP *fibmp)
{
int i, j, k;
int pitch = FreeImage_GetPitch(fibmp);
unsigned char *bits = FreeImage_GetBits(fibmp);
int bpp = FreeImage_GetBPP(fibmp);
GLBITMAP *glbmp = (GLBITMAP *)malloc(sizeof(GLBITMAP));
RGBQUAD *palette = NULL;
if ( !glbmp ) return NULL;
glbmp->w = FreeImage_GetWidth(fibmp);
glbmp->h = FreeImage_GetHeight(fibmp);
switch ( bpp ) {
case 8:
if ( !(palette = FreeImage_GetPalette(fibmp)) ) return NULL;
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*3)) ) return NULL;
glbmp->rgb_mode = GL_RGB;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
k = bits[i*pitch+j];
glbmp->buf[(i*glbmp->w+j)*3+0] = palette[k].rgbRed;
glbmp->buf[(i*glbmp->w+j)*3+1] = palette[k].rgbGreen;
glbmp->buf[(i*glbmp->w+j)*3+2] = palette[k].rgbBlue;
}
}
break;
case 24:
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*3)) ) return NULL;
glbmp->rgb_mode = GL_RGB;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
glbmp->buf[(i*glbmp->w+j)*3+0] = bits[i*pitch+j*3+2];
glbmp->buf[(i*glbmp->w+j)*3+1] = bits[i*pitch+j*3+1];
glbmp->buf[(i*glbmp->w+j)*3+2] = bits[i*pitch+j*3+0];
}
}
break;
case 32:
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*4)) ) return NULL;
glbmp->rgb_mode = GL_RGBA;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
glbmp->buf[(i*glbmp->w+j)*4+0] = bits[i*pitch+j*4+2];
glbmp->buf[(i*glbmp->w+j)*4+1] = bits[i*pitch+j*4+1];
glbmp->buf[(i*glbmp->w+j)*4+2] = bits[i*pitch+j*4+0];
glbmp->buf[(i*glbmp->w+j)*4+3] = bits[i*pitch+j*4+3];
}
}
break;
default: return NULL;
}
return glbmp;
}
void FreeGLBitmap(GLBITMAP *glbmp)
{
if ( glbmp ) {
if ( glbmp->buf ) free(glbmp->buf);
free(glbmp);
}
}
GLuint loadTexture(const char *filename)
{
GLuint tex = 0;
unsigned char *bits = NULL;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP *bitmap = NULL;
GLBITMAP *glbmp = NULL;
fif = FreeImage_GetFileType(filename, 0);
if ( FIF_UNKNOWN == fif ) {
fif = FreeImage_GetFIFFromFilename(filename);
if ( FIF_UNKNOWN == fif )
return 0;
}
if ( FreeImage_FIFSupportsReading(fif) )
bitmap = FreeImage_Load(fif, filename, 0);
if ( !bitmap )
return 0;
printf("bit: %d\n", FreeImage_GetBPP(bitmap));
glbmp = FIBitmap2GLBitmap(bitmap);
if ( !glbmp )
return 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
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, glbmp->rgb_mode, glbmp->w, glbmp->h, 0, glbmp->rgb_mode, GL_UNSIGNED_BYTE, glbmp->buf);
FreeGLBitmap(glbmp);
FreeImage_Unload(bitmap);
return tex;
}
void display()
{
glColor3f(1.0, 1.0, 1.0);
/*glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
gluOrtho2D(0.0, 800.0,600.0,0.0);
//glTranslatef(0.0f,0.0f,-5.0f);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBegin(GL_QUADS);
// Front Face
/*glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);*/
/*glTexCoord2f(0.0f, 0.0f); glVertex3f(-800.0f, -600.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 800.0f, -600.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 800.0f, 600.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-800.0f, 600.0f, 1.0f);*/
glTexCoord2f(0.0f, 0.0f); glVertex3f(10.0f, 0.0f, 0.4f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 74.0f, 0.0f, 0.4f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 74.0f, 64.0f, 0.4f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(10.0f, 64.0f, 0.4f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 64.0f, 0.0f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 64.0f, 64.0f, 0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 64.0f, 0.5f);
/*// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
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);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);*/
glEnd();
glFlush();
}
void init()
{
FreeImage_Initialise();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0,1.0,-1.0,1.0);
//glOrtho(0.0, 800.0, 0.0,600.0,-1.0,1.0);
gluOrtho2D(0.0, 800.0,600.0,0.0);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
texture=loadTexture("asina.png");
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("hello opengl");
init();
glutDisplayFunc(display);
glutMainLoop();
}
原文地址:http://www.cnblogs.com/marcher/p/openglpic.html