天天看點

opengl 使用soil讀入bmp圖像作為紋理

注意代碼中要添加依賴庫!!。soil還是非常好用的,下載下傳位址網上有,注意版本,在不同的檔案中有對應的vs的版本

轉載自:http://blog.csdn.net/mutex86/article/details/8905813 copy

  1. // TextureTest.cpp : 定義控制台應用程式的入口點。  
  2. //  
  3. #include "stdafx.h"  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <GL/glew.h>  
  7. #include <GL/glut.h>  
  8. #include <GL/SOIL.h>  
  9. static GLuint texture;      
  10. //裝載一個bmp圖像使之成為紋理,其中貌似包含了 glTexImage2D這個函數的功能  
  11. int LoadGLTextures(char *textureFilePath)  
  12. {  
  13.         texture = SOIL_load_OGL_texture(  
  14.         textureFilePath,  
  15.         SOIL_LOAD_AUTO,  
  16.         SOIL_CREATE_NEW_ID,  
  17.         SOIL_FLAG_INVERT_Y  
  18.         );  
  19.     if(texture == 0)  
  20.         return -1;  
  21.     glBindTexture(GL_TEXTURE_2D,texture);  
  22.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);  
  23.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);  
  24.     return 0;  
  25. }  
  26. void init()  
  27. {  
  28.     if ( !LoadGLTextures("texture2.bmp"))  
  29.         return;  
  30.     //glEnable( Gl_TEXTURE_2D);  
  31.     glShadeModel( GL_FLAT );  
  32.     glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );  
  33.     glEnable ( GL_DEPTH_TEST );  
  34. }  
  35. void display( void )  
  36. {  
  37.     glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  38.     glEnable( GL_TEXTURE_2D);               //激活紋理  
  39.     glBindTexture(GL_TEXTURE_2D,texture);  
  40.     glBegin( GL_QUADS );       
  41.     glTexCoord2f( 0.0, 0.0);  glVertex2f( -1.0, -1.0);   //紋理坐标的設定  
  42.     glTexCoord2f( 0.0, 1.0 );  glVertex2f( -1.0, 1.0 );  
  43.     glTexCoord2f( 1.0, 1.0 );  glVertex2f( 1.0, 1.0 );  
  44.     glTexCoord2f( 1.0, 0.0 ); glVertex2f ( 1.0, -1.0 );  
  45.     glEnd( );  
  46.     glFlush();  
  47.     glDisable( GL_TEXTURE_2D );             //終止紋理  
  48. }  
  49. int _tmain(int argc, char* argv[])  
  50. {  
  51.     glutInit( &argc, argv );  
  52.     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );  
  53.     glutInitWindowSize ( 400, 400 );  
  54.     glutInitWindowPosition( 100, 100 );  
  55.     glutCreateWindow(" 簡單紋理貼圖實驗  ");  
  56.     init();  
  57.     glutDisplayFunc ( display );  
  58.     glutMainLoop();  
  59.     return 0;  
  60. }  
opengl 使用soil讀入bmp圖像作為紋理

繼續閱讀