opengl 使用soil读入bmp图像作为纹理

来源:互联网 发布:工程类 网络类 编辑:程序博客网 时间:2024/05/16 02:39

注意代码中要添加依赖库!!。soil还是非常好用的,下载地址网上有,注意版本,在不同的文件中有对应的vs的版本

转载自:http://blog.csdn.net/mutex86/article/details/8905813 copy

  1. // TextureTest.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <GL/glew.h>  
  8. #include <GL/glut.h>  
  9. #include <GL/SOIL.h>  
  10.   
  11. static GLuint texture;      
  12.   
  13. //装载一个bmp图像使之成为纹理,其中貌似包含了 glTexImage2D这个函数的功能  
  14. int LoadGLTextures(char *textureFilePath)  
  15. {  
  16.         texture = SOIL_load_OGL_texture(  
  17.         textureFilePath,  
  18.         SOIL_LOAD_AUTO,  
  19.         SOIL_CREATE_NEW_ID,  
  20.         SOIL_FLAG_INVERT_Y  
  21.         );  
  22.       
  23.     if(texture == 0)  
  24.         return -1;  
  25.   
  26.     glBindTexture(GL_TEXTURE_2D,texture);  
  27.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);  
  28.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);  
  29.   
  30.     return 0;  
  31. }  
  32.   
  33. void init()  
  34. {  
  35.     if ( !LoadGLTextures("texture2.bmp"))  
  36.         return;  
  37.     //glEnable( Gl_TEXTURE_2D);  
  38.     glShadeModel( GL_FLAT );  
  39.     glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );  
  40.     glEnable ( GL_DEPTH_TEST );  
  41. }  
  42.   
  43. void display( void )  
  44. {  
  45.     glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  46.   
  47.     glEnable( GL_TEXTURE_2D);               //激活纹理  
  48.   
  49.     glBindTexture(GL_TEXTURE_2D,texture);  
  50.     glBegin( GL_QUADS );       
  51.     glTexCoord2f( 0.0, 0.0);  glVertex2f( -1.0, -1.0);   //纹理坐标的设置  
  52.     glTexCoord2f( 0.0, 1.0 );  glVertex2f( -1.0, 1.0 );  
  53.     glTexCoord2f( 1.0, 1.0 );  glVertex2f( 1.0, 1.0 );  
  54.     glTexCoord2f( 1.0, 0.0 ); glVertex2f ( 1.0, -1.0 );  
  55.     glEnd( );  
  56.     glFlush();  
  57.   
  58.     glDisable( GL_TEXTURE_2D );             //终止纹理  
  59. }  
  60.   
  61.       
  62.   
  63.   
  64. int _tmain(int argc, char* argv[])  
  65. {  
  66.     glutInit( &argc, argv );  
  67.     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );  
  68.     glutInitWindowSize ( 400, 400 );  
  69.     glutInitWindowPosition( 100, 100 );  
  70.     glutCreateWindow(" 简单纹理贴图实验  ");  
  71.     init();  
  72.     glutDisplayFunc ( display );  
  73.     glutMainLoop();  
  74.     return 0;  
  75. }  

0 0
原创粉丝点击